GATE Computer ScienceHardGATE CS (PYQ pattern)
int ds(int n) { return n < 10 ? n : ds(n / 10) + n % 10; } int steps(int n) { int c = 0; while (n >= 10) { n = ds(n); c++; } return c; } What is the sum of steps(n) for n = 1 to 999?
- A. 1815
- B. 1765
- C. 1935
- D. 999
Correct answer: A. 1815
Each step replaces n by its digit sum until n < 10; a number below 10 takes 0 steps, most others 1 or 2 (99 and 999 style cases take 2). Summing steps(n) over 1..999 by program gives 1815.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- The keys 3, 9, 6, 12, 1, 15, 8 are inserted one after another into an initially empty max-heap (each insertion places the key at the end…
- int fib(int n) { if (n < 2) return n; return fib(n-1) + fib(n-2); } Including the initial call, how many times is fib executed in total…
- A hash table of size 13 uses double hashing with h1(k) = k mod 13, h2(k) = 1 + (k mod 11) and probe sequence (h1 + i·h2) mod 13. The keys…
- A dynamic array starts with capacity 1. Whenever an append finds it full, a new array of double the capacity is allocated and all current…
- In an AVL tree a single node has height 0. What is the minimum number of nodes in an AVL tree of height 6?
- int f(int n) { static int c = 0; c += n; if (n > 0) f(n - 1); return c; } int main() { int x = f(4); int y = f(2); printf("%d", x + y); }…