Data Structures & AlgorithmsModerateTCS NQT · GATE CS

What is the smallest integer x ≥ 100 whose binary representation contains exactly 4 ones?

  1. A. 101
  2. B. 102
  3. C. 100
  4. D. 104

Correct answer: A. 101

Scanning upward from N and testing the number of one-bits (popcount) finds the first match, which is exact. Adding k to N, or only looking at N's own bit count, gives values that need not have exactly k ones.

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

Exams that ask this topic