Contiguous Array
Keep a running balance from index 0. If the balance at index i equals the balance at an earlier index j, then everything in nums[j+1..i] added up to 0, so it is balanced. Store the first index where each balance appeared (with balance 0 at index −1). At each i, the longest balanced subarray ending at i has length i − first[balance].
nums[j+1..i] has equal 0s and 1s exactly when the running balance at j equals the running balance at i.
Problem
You are given an array nums that holds only 0s and 1s.
Find the longest contiguous subarray that has the same number of 0s and 1s, and return its length. If there is no such subarray, return 0.
A binary array nums (every value is 0 or 1).
The length of the longest subarray with equally many 0s and 1s.
The whole array has one 0 and one 1.
[0, 1] or [1, 0] both work. The whole array has two 0s and one 1, so it does not.
[1, 1, 1, 0, 0, 0] (indices 3..8) has three of each. No longer subarray is balanced.
- • 1 ≤ nums.length ≤ 10^5
- • nums[i] is either 0 or 1
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
What single number could tell me "this part has as many 0s as 1s"?
- 2.
If I count a 1 as +1 and a 0 as −1, what is the sum of a balanced subarray?
- 3.
If the running total is the same at two different indices, what happened in between?
- 4.
I want the longest answer. For a fixed end, which earlier index should I pair it with?
- 5.
How do I handle a balanced subarray that starts right at index 0?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Fix each start, grow the end with a running balance, check balance == 0 | O(N²) | O(1) |
| Optimal | Same balance seen before ⇒ the part in between is balanced; store the first index of each balance | O(N) | O(N) |
Why does this belong to this pattern?
- “contiguous subarray”A range [l..r] → think window or prefix sums.
- “same number of 0s and 1s”Two counts that must cancel → map 0 → −1, 1 → +1 and look for sum 0.
- “longest”Pair each end with the **earliest** matching prefix → store the first index, not a count.
This is the anchor of the Prefix state / balance sub-pattern. The new move compared with Exact sum + hash map is the transform: turn the array into +1/−1 so that "balanced" becomes "sum 0", which becomes "same prefix value seen before". And because we want the longest subarray (not a count), the map stores the first index of each balance instead of how many times it appeared. The next questions keep this idea but change the state: a bitmask of parities (vowels in even counts), or a balance that must be greater than 0 (well-performing interval).
Don't make this mistake
This is a pattern-confusion trap. A window needs a rule like "if it is broken, shrinking can fix it". Here there is none: a window with too many 1s might become balanced by adding more 0s on the right, not by removing from the left. And removing a 0 from the left makes it worse. So you cannot tell when to shrink. "Equal counts" is an exact-sum condition (sum = 0 after the ±1 transform), which is a prefix-sum job.
If you update the map every time, it keeps the last index of each balance. Then i − first[balance] pairs the end with the closest start instead of the farthest, and you get shorter answers. On [0, 1, 0, 1] it returns 2 instead of 4.
Without it, a balanced subarray that starts at index 0 is never found. On [0, 1] the balance goes −1, 0 and 0 was never stored, so the answer comes out 0 instead of 2.
Length is i − first[balance]. The empty prefix ends before index 0, i.e. at −1. Seeding 0 makes every subarray that starts at index 0 one too short.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Prefix Sum translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.