Practice Recursion Medium (original) (raw)

Last Updated : 11 Apr, 2026

**Question 1 : Predict the output of the following program. What does the following fun2() do in general?

C++ `

using namespace std; #define LIMIT 1000 void fun2(int n) { if (n <= 0) return; if (n > LIMIT) return; cout << n << " "; fun2(2 * n); cout << n << " "; }

// Driver code int main() { fun2(100); }

C

#define LIMIT 1000 void fun2(int n) { if (n <= 0) return; if (n > LIMIT) return; printf("%d ", n); fun2(2 * n); printf("%d ", n); }

// Driver code int main() { fun2(100); }

Java

class GFG { static int LIMIT = 1000; static void fun2(int n) { if (n <= 0) return; if (n > LIMIT) return;

    System.out.print(n + " ");
    fun2(2 * n);
    System.out.print(n + " ");
}

// Driver code
public static void main(String[] args) { fun2(100); }

}

Python

LIMIT = 1000

def fun2(n): if (n <= 0): return if (n > LIMIT): return print(n, end=" ") fun2(2 * n) print(n, end=" ")

Driver code

fun2(100)

C#

using System; class GFG {

static int LIMIT = 1000;
static void fun2(int n)
{
    if (n <= 0)
        return;
    if (n > LIMIT)
        return;
    Console.Write(n + " ");
    fun2(2 * n);
    Console.Write(n + " ");
}

// Driver code
static void Main(string[] args) { fun2(100); }

}

JavaScript

let LIMIT = 1000; function fun2(n) { if (n <= 0) return; if (n > LIMIT) return;

console.log(n + " ");
fun2(2 * n);
console.log(n + " ");

}

// Driver code fun2(100)

`

Answer: For a positive n, fun2(n) prints the values of n, 2n, 4n, 8n ... while the value is smaller than LIMIT. After printing values in increasing order, it prints same numbers again in reverse order. For example fun2(100) prints 100, 200, 400, 800, 800, 400, 200, 100.
If n is negative, the function is returned immediately.

**Question 2 : Predict the output of the following program. What does the following fun() do in general?

C++ `

using namespace std;

int fun(int a[], int n) { int x; if (n == 1) return a[0]; else x = fun(a, n - 1); if (x > a[n - 1]) return x; else return a[n - 1]; }

// Driver code int main() { int arr[] = {12, 10, 30, 50, 100}; cout << " " << fun(arr, 5) << " "; getchar(); return 0; }

C

#include<stdio.h> int fun(int a[],int n) { int x; if(n == 1) return a[0]; else x = fun(a, n-1); if(x > a[n-1]) return x; else return a[n-1]; }

// Driver code int main() { int arr[] = {12, 10, 30, 50, 100}; printf(" %d ", fun(arr, 5)); getchar(); return 0; }

Java

class GFG {

static int fun(int a[],int n)
{
    int x;
    if(n == 1)
        return a[0];
    else
        x = fun(a, n - 1);
    if(x > a[n - 1])
        return x;
    else
        return a[n - 1];
}

// Driver code
public static void main (String[] args)
{
    int arr[] = {12, 10, 30, 50, 100};
    System.out.println(" "+fun(arr, 5)+" ");
}

}

Python

def fun( a, n): if(n == 1): return a[0] else: x = fun(a, n - 1) if(x > a[n - 1]): return x else: return a[n - 1]

Driver code

arr = [12, 10, 30, 50, 100] print(fun(arr, 5))

C#

using System;

public class GFG{ static int fun(int[] a,int n) { int x; if(n == 1) return a[0]; else x = fun(a, n - 1); if(x > a[n - 1]) return x; else return a[n - 1]; }

// Driver code
static public void Main ()
{
    int[] arr = {12, 10, 30, 50, 100};
    Console.Write(" "+fun(arr, 5)+" ");
}

}

JavaScript

function fun(a, n) { var x; if(n == 1) return a[0]; else x = fun(a, n - 1); if(x > a[n - 1]) return x; else return a[n - 1]; }

// Driver code var arr = [12, 10, 30, 50, 100]; console.log(fun(arr, 5));

`

Answer: fun() returns the maximum value in the input array a[] of size n.

**Question 3 : Predict the output of the following program. What does the following fun() do in general?

C++ `

using namespace std;

int fun(int i) { if (i % 2) return (i++); else return fun(fun(i - 1)); }

// Driver code int main() { cout << " " << fun(200) << " "; getchar(); return 0; }

C

int fun(int i) { if ( i%2 ) return (i++); else return fun(fun( i - 1 )); }

// Driver code int main() { printf(" %d ", fun(200)); getchar(); return 0; }

Java

class GFG { static int fun(int i) { if (i % 2 == 1) return (i++); else return fun(fun(i - 1)); }

// Driver code
public static void main (String[] args) {
    System.out.println(" " + fun(200) + " ");
}

}

Python

def fun(i) :

if (i % 2 == 1) :
    i += 1
    return (i - 1)
else :
    return fun(fun(i - 1))

Driver code

print(fun(200))

C#

using System;

class GFG {

static int fun(int i)
{
    if (i % 2 == 1)
        return (i++);
    else
        return fun(fun(i - 1));
}

// Driver code
static public void Main()
{
    Console.WriteLine(fun(200));
}

}

JavaScript

function fun(i) { if (i % 2 == 1) return (i++); else return fun(fun(i - 1)); }

// Driver code console.log(fun(200));

`

Answer: If n is odd, then return n, else returns (n-1). Eg., for n = 12, you get 11 and for n = 11 you get 11. The statement _"return i++;" returns the value of i only as it is a post-increment.

**Question 4 : Predict the output of the following program. What does the following fun() do in general?

C++ `

using namespace std;

int fun(int n, int* fp) { int t, f;

if (n <= 2) {
    *fp = 1;
    return 1;
}
t = fun(n - 1, fp);
f = t + *fp;
*fp = t;
return f;

}

// Driver code int main() { int x = 15; cout << fun(5, &x) << endl; return 0; }

C

int fun(int n, int* fp) { int t, f;

if (n <= 2) {
    *fp = 1;
    return 1;
}
t = fun(n - 1, fp);
f = t + *fp;
*fp = t;
return f;

}

// Driver code int main() { int x = 15; printf("%d\n", fun(5, &x));

return 0;

}

Java

class GFG { static int fp = 15; static int fun(int n) { int t, f;

    if (n <= 2) {
        fp = 1;
        return 1;
    }
    t = fun(n - 1);
    f = t + fp;
    fp = t;
    return f;
}
public static void main(String[] args)
{
    System.out.println(fun(5));
}

}

Python

fp = 15

def fun(n): global fp if (n <= 2): fp = 1 return 1

t = fun(n - 1)
f = t + fp
fp = t
return f

Driver code

print(fun(5))

C#

using System;

class GFG { static int fp = 15; static int fun(int n) { int t, f;

    if (n <= 2) {
        fp = 1;
        return 1;
    }
    t = fun(n - 1);
    f = t + fp;
    fp = t;
    return f;
}

// Driver code
static public void Main() { Console.Write(fun(5)); }

}

JavaScript

var fp = 15; function fun( n ) { var t, f;

if ( n <= 2 )
{
    fp = 1;
    return 1;
}
t = fun ( n - 1 );
f = t + fp;
fp = t;
return f;

}

// Driver code console.log(fun(5))

`

The program calculates n-th Fibonacci Number. The statement t = fun ( n-1, fp ) gives the (n-1)th Fibonacci number and *fp is used to store the (n-2)th Fibonacci Number. The initial value of *fp (which is 15 in the above program) doesn't matter. The following recursion tree shows all steps from 1 to 10, for the execution of fun(5, &x).

                           (1) fun(5, fp)  
                          /           \  
                     (2) fun(4, fp)   (8) t = 3, f = 5, *fp = 3  
                     /          \  
               (3) fun(3, fp)    (7) t = 2, f = 3, *fp = 2  
              /            \  
          (4) fun(2, fp)   (6) t = 1, f = 2, *fp = 1  
         /     
  (5) *fp = 1  

**Question 5 : Predict the output of the following program.

C++ `

using namespace std;

int minIndex(int arr[], int s, int e) { int sml = INT32_MAX; int mindex; for (int i = s; i < e; i++) { if (sml > arr[i]) { sml = arr[i]; mindex = i; } } return mindex; }

void fun2(int arr[], int start_index, int end_index) { if (start_index >= end_index) return; int min_index; int temp;

// minIndex() returns index of minimum value in
// array arr[start_index...end_index] 
min_index = minIndex(arr, start_index, end_index);

// swap the element at start_index and min_index
temp = arr[start_index];
arr[start_index] = arr[min_index];
arr[min_index] = temp;

fun2(arr, start_index + 1, end_index);

}

// Driver code

int main() { int arr[] = {64, 25, 12, 22, 11}; int n = sizeof(arr) / sizeof(arr[0]);

fun2(arr, 0, n);

for (int i = 0; i < n; i++)
    cout << arr[i] << " ";
cout << endl;


return 0;

}

C

// minimum index finder #include <stdio.h> #include <limits.h> int minIndex(int arr[], int s, int e) { int sml = INT_MAX; int mindex; for (int i = s; i < e; i++) { if (sml > arr[i]) { sml = arr[i]; mindex = i; } } return mindex; }

void fun2(int arr[], int start_index, int end_index) { if (start_index >= end_index) return; int min_index; int temp;

// minIndex() returns index of minimum value in
// array arr[start_index...end_index] 
min_index = minIndex(arr, start_index, end_index);

temp = arr[start_index];
arr[start_index] = arr[min_index];
arr[min_index] = temp;

fun2(arr, start_index + 1, end_index);

}

// Driver code

int main() { int arr[] = {64, 25, 12, 22, 11}; int n = sizeof(arr) / sizeof(arr[0]); fun2(arr, 0, n); for (int i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n");

return 0;

}

Java

static int minIndex(int arr[], int s, int e) { int sml = Integer.MAX_VALUE; int mindex = s; for (int i = s; i < e; i++) { if (sml > arr[i]) { sml = arr[i]; mindex = i; } } return mindex; }

static void fun2(int arr[], int start_index, int end_index) { if (start_index >= end_index) return; int min_index; int temp;

// minIndex() returns index of minimum value in
//   array arr[start_index...end_index]
min_index = minIndex(arr, start_index, end_index);

temp = arr[start_index];
arr[start_index] = arr[min_index];
arr[min_index] = temp;

fun2(arr, start_index + 1, end_index);

}

// Driver code

public static void main(String[] args) { int arr[] = {64, 25, 12, 22, 11}; int n = arr.length; fun2(arr, 0, n); for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); System.out.println(); }

Python

Minimum index finder

def minIndex(arr, s, e):

sml = sys.maxsize
mindex = 0

for i in range(s, e):
    if (sml > arr[i]):
        sml = arr[i]
        mindex = i
        
return mindex

def fun2(arr, start_index, end_index):

if (start_index >= end_index):
    return
    
# minIndex() returns index of minimum value in
# array arr[start_index...end_index]
min_index = minIndex(arr, start_index, end_index)
arr[start_index], arr[min_index] = arr[min_index], arr[start_index]
fun2(arr, start_index + 1, end_index)

Driver code

arr = [64, 25, 12, 22, 11] n = len(arr) fun2(arr, 0, n) print(*arr)

C#

using System; class Recursion{ static int minIndex(int[] arr, int s, int e) { int sml = Int32.MaxValue; int mindex = s; for(int i = s; i < e; i++) { if(sml > arr[i]) { sml = arr[i]; mindex = i; } } return mindex;
} static void fun2(int[] arr, int start_index, int end_index)
{ if(start_index >= end_index) { return; } int min_index; int temp;

// minIndex() returns index of minimum value in
// array arr[start_index...end_index]
min_index = minIndex(arr, start_index, end_index);
temp = arr[start_index];
arr[start_index] = arr[min_index];
arr[min_index] = temp;

fun2(arr, start_index + 1, end_index);

}

// Driver code

static void Main(string[] args) { int[] arr = {64, 25, 12, 22, 11}; int n = arr.Length; fun2(arr, 0, n); for (int i = 0; i < n; i++) Console.Write(arr[i] + " "); Console.WriteLine(); } }

JavaScript

function minIndex(arr, s, e) { var sml = Number.MAX_SAFE_INTEGER; var mindex; for (let i = s; i < e; i++) { if (sml > arr[i]) { sml = arr[i]; mindex = i; } } return mindex; }

function fun2(arr, start_index, end_index) { if (start_index >= end_index) return; var min_index; var temp;

// minIndex() returns index of minimum value in
// array arr[start_index...end_index]
min_index = minIndex(arr, start_index, end_index);

// swap the element at start_index and min_index
temp = arr[start_index];
arr[start_index] = arr[min_index];
arr[min_index] = temp;

fun2(arr, start_index + 1, end_index);

}

// Driver code

let arr = [64, 25, 12, 22, 11]; let n = arr.length; fun2(arr, 0, n); console.log(arr.join(" "));

`

**Answer: The function fun2() is a recursive implementation of Selection Sort.

Please write comments if you find any of the answers/codes incorrect, or you want to share more information about the topics discussed above.