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?
- A. 1498
- B. 1500
- C. 1994
- 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
- A dynamic array starts with capacity 2. When it is full and an element is appended, the capacity becomes ⌈1.5 × capacity⌉ and all current…
- Consider t(n): if n ≤ 2 return 1; else return t(n−1) + t(n−2) + t(n−3). How many calls to t (including the initial call) are made when…
- How many times is x++ executed? for (i = 2; i <= 100; i++) for (j = ii; j <= 100; j += i) x++;
- How many times is x++ executed? for (i = 1; i <= 1000; i = 2) for (j = i; j >= 1; j /= 3) x++; (integer division)
- Let f(n) = (log₂ n)^(log₂ n), g(n) = n^(log₂ log₂ n) and h(n) = (log₂ log₂ n)!. Which of the following is correct? I. f(n) = Θ(g(n)). II…
- Let f₁ = n^(1/log₂n), f₂ = (log₂ n)³, f₃ = (log₂ n)^(log₂ log₂ n), f₄ = 2^√(log₂ n), f₅ = n^0.001. Which lists them in increasing order of…