Skip to content
GATEiit

GATE CSE 2018 Q55: Number of prints by recursive Count(1024,1024)

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

Numerical answer. No negative marking.
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.

Concept notes

Loading notes…

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

WhatsAppDownload image

Source: official GATE 2018 CS question paper and final answer key, organised by IIT Guwahati. Question paper · Answer key