Bytes
rocket

Your Success, Our Mission!

6000+ Careers Transformed.

Subarray Problems

Last Updated: 3rd September, 2026

A coffee shop owner tracks daily earnings:

[100, -50, 200, -30, 150, -20, 300]

Some days are great, some are bad.

But the owner isn’t interested in just one day.
He wants to know:

“Which continuous stretch of days gave me the highest profit?”

Not random days.
Not scattered choices.
Only continuous days.

That’s exactly what a subarray is.

kumud lion (1).png

What is a Subarray?

subarray is a continuous part of an array.

Array:    [1, 2, 3, 4]

Subarrays:
[1][2][3][4]
[1,2][2,3][3,4]
[1,2,3][2,3,4]
[1,2,3,4]

Important:

  • Must be continuous
  • Order matters

Brute Force Approach (Checking Every Possibility)

You try every possible subarray and calculate the sum.

def max_subarray_brute(arr):
  max_sum = float('-inf')
  
  forin range(len(arr)):
      forin range(i, len(arr)):
          current_sum = sum(arr[i:j+1])
          max_sum = max(max_sum, current_sum)
          
  return max_sum

Problem:

  • Time Complexity = O(n³) (very slow)

Kadane’s Algorithm (The Smart Insight)

Instead of checking everything, we think smart:

“If the current sum becomes negative, drop it!”

Because a negative sum will only reduce future profit.

Concept - Maximum Subarray Sum

def kadane(arr):
  current_sum = arr[0]
  max_sum = arr[0]
  
  for num in arr[1:]:
      current_sum = max(num, current_sum + num)
      max_sum = max(max_sum, current_sum)
      
  return max_sum

Technical Example:

Input:

[-2, 1, -3, 4, -1, 2, 1, -5, 4]

Output:

6  → subarray [4, -1, 2, 1]

Real-Life Insight:

  • Ignore bad streaks (loss days)
  • Focus on profitable continuous period

This is optimization thinking

Sliding Window (Fixed Size Subarrays)

Sometimes interviewer asks:

“Find maximum sum of subarray of size k”

Example:

[215132], k = 3

Concept - Window Moves Forward

def max_sum_k(arr, k):
  window_sum = sum(arr[:k])
  max_sum = window_sum
  
  forin range(k, len(arr)):
      window_sum += arr[i] - arr[i-k]
      max_sum = max(max_sum, window_sum)
      
  return max_sum

Time Complexity = O(n)

Real-Life Example:

Finding the best 3-day sales period without recalculating everything.

Interviewers don’t say “subarray problem”.

They say things like:

  • “Continuous segment”
  • “Maximum sum”
  • “Longest length”
  • “Fixed size window”

All are hints toward subarray techniques

kumud ssc (1).png

Module 3: Subarray ProblemsSubarray Problems

Top Tutorials

Logo

Top 10 Machine Learning Projects with Source Code (Beginner to Advanced)

Explore the Top 10 Machine Learning projects with source code, from beginner to advanced. Learn real-world ML applications, build portfolio-ready projects, and master hands-on skills with step-by-step tutorials from AlmaBetter.

3 Modules10 Lessons1151 Learners
Start Learning
Logo
Careers in Tech

Technologies to Learn in 2026: Building the Future of Innovation

Explore the top technologies to learn in 2026 including Generative AI, Cloud, Cybersecurity, Web3, Data Science, AR/VR, Quantum, RPA, and Green Tech.

00 Lessons909 Learners
Start Learning
Logo
Careers in Tech

aws

This tutorial presents a structured, beginner-focused yet industry-aligned guide to Amazon Web Services, designed specifically for 2026 learning and career requirements

00 Lessons42 Learners
Start Learning
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • Follow Us
  • facebook
    instagram
    linkedin
    twitter
    youtube
    telegram

© 2026 AlmaBetter