GATE / CS ARCHIVE

GATE CSE 2006 / QUESTION 54

Longest equal-sum span of binary arrays

Algorithms · Hashing & Randomized Algorithms · Full GATE CSE 2006 paper

GATE 2006, Q54 2 marks · MCQ
Report issue

Hashing & Randomized Algorithms

Question 54, GATE 2006: Given two length-n binary arrays a,b, find the longest interval [i,j] on which Σₖ₌ᵢʲaₖ=Σₖ₌ᵢʲbₖ, or report none. Which complexity describes the fastest method?
Options:
(A) O(3^n) and Ω(2^n) time if hashing is allowed
(B) O(n³) and Ω(n^2.5) time in the key-comparison model
(C) Θ(n) time and space
(D) O(√n) time only if the sum of all 2n entries is even
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. Set prefix differences D[0]=0 and D[k]=sum(a[1..k])−sum(b[1..k]). A span (i,j) has equal sums iff D[i−1]=D[j].

2. Every D[k] lies in [−n,n], so an array of 2n+1 entries can store the earliest index of each difference. Scan once and maximize the distance to its first occurrence.

3. Time and space are Θ(n), option C; hashing is unnecessary. Reading the inputs already takes Ω(n) time.

4. Teaching check: Store the earliest occurrence of each prefix difference because that maximizes the length to a later equal difference. A binary-array prefix difference stays between −n and n, so direct indexing gives worst-case linear performance rather than relying on average-case hash-table behavior.

Original paper, page 8