Data Structures & AlgorithmsVery hardGATE CS · TCS NQT
In the binary array [1, 1, 0, 1, 1, 1, 0, 1, 1], you may flip exactly one 0 to 1 (or flip none). What is the length of the longest run of consecutive 1s after the best choice?
- A. 7
- B. 6
- C. 3
- D. 9
Correct answer: B. 6
A window with at most one zero: flipping index 6 joins 1 1 1 and 1 1 into 1 1 1 1 1 1 (indices 3–8) = 6. Counting all the ones (7) ignores adjacency, 3 is the best run without a flip, and 9 would need both zeros flipped.
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
- How many index pairs i < j in [4, 9, 1, 7, 3, 8, 2] have a sum s with 6 <= s <= 10?
- How many contiguous subarrays of [10, 5, 2, 6] have a product strictly less than 100?
- Greedy removal: scanning "aabaacaab" left to right, you delete any 'a' that immediately follows another 'a' in the current output. How…
- Remove exactly 2 digits from "7419" (keeping order) to make the largest possible number. What is the result?
- Using the Z-function on "ab#abababcab", how many occurrences of the pattern "ab" are found in the text "abababcab"?
- For "abacabad", how many indices i (0-indexed) have prefix-function value pi[i] equal to 0?