Longest Common Span with Same Sum in Binary Arrays

HardGFG
⭐⭐⭐⭐

Longest Common Span with Same Sum in Binary Arrays

Pattern:

Idea:

Variations :


πŸ’» Code

Time complexity - O(n)

Aux. Space complexity - O(n)


Longest Common Span with Same Sum in Binary Arrays

Tags: #Arrays #PrefixSum #Hashing #HashMap #BinaryArray #DifferenceArray #Interview-Pattern #FAANG

Problem Statement

Given two binary arrays A and B of the same length, find the length of the longest span (contiguous subarray) such that the sum of elements in both spans is equal.

Example

  • A = [0,1,0,0,0,0]

  • B = [1,0,1,0,0,1]

Output: 4

A span means the same index range in both arrays.


Key Idea

Reduce the problem to Longest Subarray with Sum = 0.

Instead of comparing sums separately, build a difference array:

  • diff[i] = A[i] - B[i]

Possible values:

  • 0 β†’ both equal

  • 1 β†’ (1,0)

  • -1 β†’ (0,1)

For any span (l...r):

Sum(A) = Sum(B)

is equivalent to:

Sum(A) βˆ’ Sum(B) = 0

which becomes:

Sum(diff) = 0

Now the problem is identical to finding the longest zero-sum subarray.


Intuition (The WHY)

Consider:

A0100
B1010
Diff-11-10

If a span has equal sums:

Sum(A) = Sum(B)

Subtract both sides:

Sum(A βˆ’ B) = 0

Rather than tracking two prefix sums, one transformed array captures everything.

This is the reusable interview trick:

When two arrays are compared element-wise, think Difference Array + Prefix Sum.


Optimal Approach β€” Difference Array + Prefix HashMap

Algorithm

  1. Traverse both arrays simultaneously.

  2. Compute the running prefix of A[i] - B[i].

  3. Store the first occurrence of every prefix.

  4. If the same prefix appears again, the span between them has sum 0.

  5. Track the maximum length.

Python Solution

def longestCommonSum(A, B):
    first = {0: -1}
    prefix = 0
    ans = 0

    for i in range(len(A)):
        prefix += A[i] - B[i]

        if prefix in first:
            ans = max(ans, i - first[prefix])
        else:
            first[prefix] = i

    return ans

Dry Run

A = [0,1,0,0,0,0]

B = [1,0,1,0,0,1]

Difference array:

IndexABDiffPrefix
-1β€”β€”β€”0
001-1-1
110+10
201-1-1
3000-1
4000-1
501-1-2

Repeated prefixes:

  • Prefix 0 at indices -1 and 1 β†’ Length = 2

  • Prefix -1 at indices 0 and 4 β†’ Length = 4

Answer = 4

The longest common span is:

Indices: 1 β†’ 4

A = [1,0,0,0]
B = [0,1,0,0]

Sum = 1 in both arrays

Why Does Repeated Prefix Mean Equal Sums?

Let the prefix of the difference array be P.

If:

P[i] = P[j]

then:

P[j] βˆ’ P[i] = 0

which means the sum of the difference array between those indices is zero:

Sum(A) βˆ’ Sum(B) = 0

Therefore:

Sum(A) = Sum(B)

Exactly the condition we need.


Complexity

MetricValue
TimeO(n)
Auxiliary SpaceO(n)

Space is due to the prefix hashmap.


Relationship to Previous Problems

ProblemTransformation
Equal 0s & 1s0 β†’ -1
Longest Sum = KPrefix Sum
Count Sum = KPrefix + Frequency
Longest Common SpanA[i] - B[i]

Notice that all four problems reduce to the same underlying pattern:

Transform β†’ Prefix Sum β†’ HashMap

Only the transformation changes.


Common Mistakes

  • Computing two separate prefix sums instead of using a difference array.

  • Forgetting to initialize {0: -1}.

  • Overwriting the first occurrence of a prefix.

  • Assuming the arrays must contain only 0 and 1β€”the same approach actually works for any integers.


Key Takeaways / Pattern Recognition

  • When comparing two arrays over the same interval, build a difference array.

  • Equal sums immediately become a zero-sum subarray problem.

  • Repeated prefix sums imply the intervening span has sum 0.

  • This is the natural extension of Longest Subarray with Equal 0s and 1s: instead of transforming one array, we transform two arrays into one.

Local Graph View

Start typing to search
Try: two sum or #Arrays or #Amazon