Data Structures & AlgorithmsVery hardCodeforces Div2 C · GATE CS

A circular binary string is "1101001010". A swap exchanges any two characters. What is the minimum number of swaps needed so that all 1s become contiguous on the circle (wrapping around the end)?

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

Correct answer: B. 2

Slide a window of length equal to the number of 1s around the circle; the best window holding the most 1s gives the fewest swaps, since each 0 inside the window needs one swap. Counting zeros in the whole string overcounts.

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

Exams that ask this topic