GATE Computer ScienceHardGATE CS (PYQ pattern)
int g(int a, int b) { return b == 0 ? a : g(b, a % b); } How many calls to g (including the first) does g(610, 987) make, and how many does g(1000, 987) make?
- A. 15 and 5
- B. 16 and 6
- C. 14 and 4
- D. 16 and 5
Correct answer: D. 16 and 5
With a < b the first call only swaps (610 % 987 = 610), costing one extra call before the Fibonacci chain 987, 610, 377, ... , 1, 0, which takes 15 calls from g(987, 610). So g(610, 987) makes 16. g(1000, 987) -> g(987, 13) -> g(13, 12) -> g(12, 1) -> g(1, 0): 5 calls.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- int w(int n) { if (n < 0) return 0; if (n == 0) return 1; return w(n - 1) + w(n - 3) + w(n - 4); } What does w(10) return, and how many…
- int x = 0; void q(int n) { if (n > 0) { x += n; q(n - 1); printf("%d ", x); if (n > 2) q(n - 2); } } What does q(4) print?
- Integers 1, 2, 3, 4, 5 are pushed on an initially empty stack in this order; pops may be interleaved with pushes at any time. How many of…
- The integers 1 to 6 are pushed in this order onto a stack that can hold at most 2 elements at any moment; pops may be interleaved with…
- A circular queue stores items in an array of 8 slots (indices 0 to 7) and keeps two fields, front (index of the first item) and count…
- The infix expression a + b (c ^ d - e) ^ (f + g h) - i (with ^ right-associative and higher than and +) is converted to postfix with the…