Skip to content
Related Articles

Related Articles

GATE | GATE-CS-2006 | Question 54
  • Difficulty Level : Hard
  • Last Updated : 28 Aug, 2014
GeeksforGeeks - Summer Carnival Banner

Given two arrays of numbers a1, a2, a3,…an and b1, b2, .. bn where each number is 0 or 1, the fastest algorithm to find the largest span(i, j) such that ai + ai+1, ….aj = bi + bi+1, .. bj. or report that there is not such span,
(A) Takes O(n3) and Ω(2n) time if hashing is permitted
(B) Takes O(n3) and Ω(n2.5) time in the key comparison model
(C) Takes θ(n) time and space
(D) Takes O(√n) time only if the sum of the 2n elements is an even number

Answer: (C)

Explanation: The problem can be solved in  Takes θ(n) time and space.

The idea is based on below observations.

  1. Since there are total n elements, maximum sum is n for both arrays.
  2. Difference between two sums varies from -n to n. So there are total 2n + 1 possible values of difference.
  3. If differences between prefix sums of two arrays become same at two points, then subarrays between these two points have same sum.

Below is Complete Algorithm.

  1. Create an auxiliary array of size 2n+1 to store starting points of all possible values of differences (Note that possible values of differences vary from -n to n, i.e., there are total 2n+1 possible values)
  2. Initialize starting points of all differences as -1.
  3. Initialize maxLen as 0 and prefix sums of both arrays as 0, preSum1 = 0, preSum2 = 0
  4. Travers both arrays from i = 0 to n-1.
    1. Update prefix sums: preSum1 += arr1[i], preSum2 += arr2[i]
    2. Compute difference of current prefix sums: curr_diff = preSum1 – preSum2
    3. Find index in diff array: diffIndex = n + curr_diff // curr_diff can be negative and can go till -n
    4. If curr_diff is 0, then i+1 is maxLen so far
    5. Else If curr_diff is seen first time, i.e., starting point of current diff is -1, then update starting point as i
    6. Else (curr_diff is NOT seen first time), then consider i as ending point and find length of current same sum span. If this length is more, then update maxLen
  5. Return maxLen

See Longest Span with same Sum in two Binary arrays for complete running code

Quiz of this Question


My Personal Notes arrow_drop_up
Recommended Articles
Page :