Last Updated: March 21, 2026
We have an array of only 0s and 1s, and we need to find the longest contiguous subarray where the count of 0s equals the count of 1s. So for a subarray of length 6 to qualify, it needs exactly 3 zeros and 3 ones. The subarray must be contiguous, meaning the elements have to be consecutive in the original array.
The brute force instinct is to check every possible subarray and count 0s and 1s. But with arrays up to 100,000 elements long, we need something smarter.
Here's the key insight: if we treat every 0 as -1, then a subarray with equal 0s and 1s has a sum of exactly 0. Why? Because each 0 contributes -1 and each 1 contributes +1. Equal counts means they cancel out perfectly. This transforms our problem into finding the longest subarray with sum 0, which is a well-known prefix sum problem.
nums.length <= 10^5 → We need O(n) or O(n log n). An O(n^2) brute force with 10^10 operations is too slow.nums[i] is 0 or 1 → Binary values simplify counting. The 0-to-(-1) transformation is clean since there are only two possible values.The most straightforward idea: try every possible subarray and check if it has equal 0s and 1s. For each starting index, extend the subarray one element at a time, keeping a running count of 0s and 1s. Whenever the counts match, update the maximum length.
This is what anyone would try first. It's correct and easy to reason about, just slow.
maxLen to 0i from 0 to n-1:j from i to n-1:nums[j]maxLen with j - i + 1maxLenFor every starting index, we scan forward counting 0s and 1s from scratch, even though adjacent subarrays share most of their counts. What if we could compute a running total once and use it to instantly determine the count balance for any subarray?
Here's the transformation that makes everything click: replace every 0 in the array with -1. Now every 0 subtracts 1 from the running sum, and every 1 adds 1. A subarray with equal 0s and 1s has an equal number of +1s and -1s, so its sum is exactly 0.
Finding the longest subarray with sum 0 is a classic prefix sum problem. The prefix sum at index i is the sum of all elements from index 0 to i. If the prefix sum at index j equals the prefix sum at index i, that means the subarray from i+1 to j has a sum of 0. Why? Because everything added between those two indices cancelled out.
So the algorithm becomes: compute the running prefix sum, and use a hash map to record the first time each sum value appears. When you see a sum you've already recorded, the distance from that first occurrence to the current index is a candidate for the longest subarray.
One subtle detail: we initialize the map with sum 0 at index -1. This handles the case where the subarray starting from the very beginning has sum 0. Without this initialization, we'd miss subarrays like [0, 1] where the prefix sum returns to 0.
The prefix sum at any index represents the "balance" between 1s and 0s seen so far. If the balance at index j is the same as at index i, it means between indices i+1 and j, the number of 1s added equals the number of 0s (treated as -1s) added. They perfectly cancelled out, which means equal counts of 0s and 1s in that subarray.
By storing only the first occurrence of each prefix sum, we guarantee we're always computing the longest possible subarray for that balance value. If a sum appears at indices 2, 5, and 9, the longest subarray comes from pairing index 2 with index 9, not index 5 with index 9.
prefixSum to 0 and maxLen to 0i in the array:nums[i] is 1, or -1 if nums[i] is 0, to prefixSumprefixSum exists in the map, update maxLen with i - map[prefixSum]prefixSum with index i in the mapmaxLen