GATE Computer ScienceModerateGATE CS (PYQ pattern)

A base-3 (ternary) counter starts at 0 and is incremented 1000 times. The cost of an increment is the number of digits that change (trailing 2s become 0 and the next digit increases by 1; the counter has enough digits). What is the total cost of the 1000 increments?

  1. A. 1498
  2. B. 1500
  3. C. 1994
  4. D. 2000

Correct answer: A. 1498

Digit j (weight 3^j) changes exactly ⌊1000/3^j⌋ times, so the total is 1000 + 333 + 111 + 37 + 12 + 4 + 1 = 1498. The amortized bound 1.5 per increment gives the near-miss 1500; 1994 = 2·1000 − popcount(1000) is the binary-counter answer.

More Algorithms questions

Exams that ask this topic