Data Structures & AlgorithmsHardGATE CS · DSA
In the binary array [0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1] you may flip at most 2 zeros to ones. What is the length of the longest run of consecutive ones achievable? (Sliding window with at most 2 zeros inside.)
- A. 4
- B. 5
- C. 6
- D. 2
Correct answer: B. 5
Keep a window containing at most 2 zeros; when a third zero enters, move the left edge past the earliest zero. The widest such window has length 5. Simply adding 2 to the longest existing run (2) ignores that flips can join runs.
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
- Delete duplicates from a SORTED linked list. Version A keeps one copy of each value; version B removes every value that appears more than…
- Trains occupy a platform from arrival to departure INCLUSIVE, so a train arriving at the same minute another departs needs a separate…
- Counting sort on [1, 1, 2, 1, 0, 3, 3, 0, 2, 0] (values 0–4) builds count[], turns it into cumulative counts, then scans the input from…
- What is the length of the shortest contiguous subarray of [1, 3, 1, 8, 7, 3, 4, 7, 1, 2] whose sum is at least 20? (All values are…
- Count zero-sum contiguous subarrays with a prefix-sum frequency map. What is printed? python from collections import defaultdict a = [0…
- Rotate a square matrix 90° clockwise in place by transposing and then reversing each row. What is printed? python m = [[1, 2, 3], [4, 5…