Data Structures & AlgorithmsVery hardGATE CS · DSA
For the binary array [0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1], replace 0 by −1 and use prefix sums with a first-occurrence map. What is the length of the longest subarray with equal numbers of 0s and 1s, and how many such subarrays exist?
- A. 8 and 19
- B. 8 and 21
- C. 8 and 18
- D. 6 and 19
Correct answer: A. 8 and 19
Equal prefix sums (after 0 → −1) bound a balanced subarray; the longest uses each sum's first occurrence (8), and the count is Σ C(f, 2) over prefix-sum frequencies (19). 2·min(#0, #1) is only an upper bound.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Which application does NOT naturally use a stack?
- Two numbers are stored as linked lists with digits in REVERSE order (units first). What does this addition print? python def add(a, b): d…
- How many contiguous subarrays of [5, 4, 4, 3, 2, 5, 2, 2] have a sum between 7 and 11 inclusive? (All values positive: count atMost(11) −…
- KMP failure function (lps[i] = length of the longest proper prefix of p[0..i] that is also a suffix). What is printed? python def lps(p)…
- Two numbers are stored as linked lists with digits in REVERSE order (units first). What does this addition print? python def add(a, b): d…
- A monotonic deque reports the maximum of every window of size 3 in [14, 5, −3, 14, 5, 14, −5, 16, 4]. What is the SUM of all reported…