Data Structures & AlgorithmsModerateCodeforces Div2 C · TCS NQT
In the binary array [1, 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 1s you can make?
- A. 4
- B. 5
- C. 7
- D. 2
Correct answer: B. 5
Sliding window with at most k zeros inside: once the window holds more than k zeros, shrink from the left. The window length after the flips is the answer. Counting only maximal runs of ones misses the bridges that flips create.
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
- Positions 0 to 9 form a line. Cells marked 1 are blocked, the rest are free. From a cell you may jump forward by at most 3 positions (to…
- Positions [15, 3, 22, 9, 40, 27] are given in arbitrary order. What is the largest difference between two consecutive positions when the…
- How many subsets (chosen by position, the empty subset included) of [6, 1, 3, 5, 2] have a bitwise XOR equal to 4?
- What is the length of the shortest contiguous subarray of [2, -1, 2, 3, -4, 5, 1] whose sum is at least 6? Negative numbers are present…
- A sorted array of distinct values was rotated some number of times. Which technique finds the minimum element in O(log n) time, and why…
- In one operation you may increase any single element by 1. What is the minimum total number of operations needed to make the array [1, 1…