Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

A binary string "1011001010" should become non-decreasing (all 0s before all 1s). In one operation you flip a single character. What is the minimum number of flips needed?

  1. A. 7
  2. B. 5
  3. C. 3
  4. D. 6

Correct answer: B. 5

Choose the split point k: cost = (ones before k) + (zeros after k). Scanning the split point left to right with a running count of ones and a precomputed suffix count of zeros gives O(n). Counting all ones (flipping every 1 to 0) ignores the option of keeping some 1s.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic