Skip to content
Official PYQ · 2021

GATE CS 2021 Set 2 Q23 — Asymptotic Worst Case Time and Space Complexity

Explain with AI

Consider the following ANSI C\text{ANSI C} function:

int SomeFunction (int x, int y)
{
    if ((x==1) || (y==1)) return 1;
    if (x==y) return x;
    if (x > y) return SomeFunction(x-y, y);
    if (y > x) return SomeFunction (x, y-x);

}

The value returned by SomeFunction(15, 255)\textrm{SomeFunction(15, 255)} is __________

View worked solution

Answer: 15 : 15

The given C function implements the subtraction-based Euclidean algorithm to find the Greatest Common Divisor (GCD) of two positive integers xx and yy, with an additional base case for when either number is 11 (which also correctly returns 11, since gcd⁡(1,k)=1\gcd(1, k) = 1).

Let's trace the execution of SomeFunction(15, 255):

  • Since 255>15255 > 15, the function executes SomeFunction(x, y-x), which translates to SomeFunction(15, 240).
  • This subtraction step continues repeatedly because y>xy > x at each step: SomeFunction(15,255)→SomeFunction(15,240)→SomeFunction(15,225)→⋯→SomeFunction(15,15)\text{SomeFunction}(15, 255) \rightarrow \text{SomeFunction}(15, 240) \rightarrow \text{SomeFunction}(15, 225) \rightarrow \dots \rightarrow \text{SomeFunction}(15, 15)
  • When the arguments reach SomeFunction(15, 15), the condition if (x == y) is satisfied, and the function returns x, which is 1515.

Mathematically, the function computes: gcd⁡(15,255)=15\gcd(15, 255) = 15

Therefore, the value returned by SomeFunction(15, 255) is 15.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Asymptotic Worst Case Time and Space Complexity questions