Data Structures & AlgorithmsVery hardGATE CS · TCS NQT

What is the minimum number of operations needed to reduce 191 to 0, where each operation subtracts any single power of two that is at most the current value?

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

Correct answer: B. 7

Each subtraction of a power of two can clear at most one 1-bit, and clearing one bit at a time works, so the answer is the popcount: 191 = 10111111₂ has 7 ones. The bit length is 8, 1 counts the zero bits, and 2 imagines 192 − 1, which needs an addition.

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

Exams that ask this topic