Data Structures & AlgorithmsVery hardProduct placement · Amazon/Microsoft

What is the minimum number of swaps (of any two elements) needed to bring all the 1s of [1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1] together into one contiguous block? (Slide a window whose size equals the number of 1s.)

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

Correct answer: D. 2

There are 6 ones, so the final block has length 6. The window of that length containing the most 1s needs one swap per 0 inside it: 2.

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

Exams that ask this topic