GATE CSE 2018 Q55: Number of prints by recursive Count(1024,1024)
- Programming and Data Structures
- 2 marks
- NAT
- No negative marking
Syllabus topic: Recursion
Question
Consider the following program written in pseudo-code. Assume that x and y are integers.
Count(x,y) {
if (y != 1){
if (x != 1) {
print("*");
Count(x/2, y);
}
else {
y = y-1;
Count(1024, y);
}
}
}The number of times that the print statement is executed by the call Count(1024,1024) is _____.
Try it: numerical answer: type the value
Show answer and explanation
Answer: 10230 (official GATE 2018 final answer key)
Concept used: Count prints once per halving of x from 1024 down to 1, and each time x reaches 1 it decrements y and restarts x at 1024, until y becomes 1.
First step: Count how many prints happen while x goes from 1024 to 1 for a fixed y, then count how many values of y (from 1024 down to 2) get such a round.
Full step-by-step solution
Loading…
Answer from the official final answer key. Found a mistake? Use Report issue and we will fix it.
More questions on Recursion
Every PYQ on this topic →Share this question
Source: official GATE 2018 CS question paper and final answer key, organised by IIT Guwahati. Question paper · Answer key
More Programming and Data Structures PYQs
- 2026 Session 1 · Q23Non-leaf indices in a 1-indexed binary heap with n odd
- 2026 Session 1 · Q24Empty slots after linear-probing inserts with h(x) = (x+7) mod 11
- 2026 Session 1 · Q33Maximum height of a full binary tree with 23 nodes
- 2026 Session 1 · Q34Output of a C function with a shadowed block variable
- 2026 Session 1 · Q39Recursive length of a non-empty singly linked list
- 2026 Session 1 · Q40Third insertion that can never yield a complete BST of 1..15