Practice Questions for Recursion | Set 1

21 Jun, 2022
Explain the functionality of the following functions. 
Question 1 
C++
C
Java
Python3
C#
Javascript
int fun1(int x, int y){    if (x == 0)        return y;    else        return fun1(x - 1, x + y);}
int fun1(int x, int y){    if (x == 0)        return y;    else        return fun1(x - 1, x + y);}
static int fun1(int x, int y){    if (x == 0)        return y;    else        return fun1(x - 1, x + y);}
def fun1(x, y) :     if (x == 0) :        return y    else :        return fun1(x - 1, x + y) # This code is contributed by divyesh072019
static int fun1(int x, int y){    if (x == 0)        return y;    else        return fun1(x - 1, x + y);} // This code is contributed by divyesh072019
<script> function fun1(x, y){     if (x == 0)        return y    else        return fun1(x - 1, x + y)} // This code is contributed by gottumukkalabobby </script>
Answer: The function fun() calculates and returns ((1 + 2 ... + x-1 + x) +y), which is x(x+1)/2 + y. For example, if x is 5 and y is 2, then fun should return 15 + 2 = 17.
Question 2 
C++
C
Java
Python3
C#
Javascript
// minimum index finder 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    swap(arr[start_index], arr[min_index]);     fun2(arr, start_index + 1, end_index);} // This code is contributed by nishant_0073
// minimum index finder 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);     temp = arr[start_index];    arr[start_index] = arr[min_index];    arr[min_index] = temp;     fun2(arr, start_index + 1, end_index);}
// minimum index finderstatic int minIndex(int arr[], int s, int e){    int sml = Integer.MAX_VALUE;    int mindex = ;    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);} // This code is contributed by nishant_0073
# Minimum index finderdef 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) # This code is contributed by rag2127
// minimum index finderstatic int minIndex(int[] arr, int s, int e){    int sml = Int32.MaxValue;    int mindex;    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);} // This code is contributed by avanitrachhadiya2155
<script>//Javascript Implementation// minimum index finder  function minIndex(arr, s, e){    var sml = Number.MAX_SAFE_INTEGER;    var mindex;    for (int 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);}// This code is contributed by shubhamsingh10</script>
Answer: The function fun2() is a recursive implementation of Selection Sort.
Time complexity: O(n)
Auxiliary Space: O(1)
Please write comments if you find any of the answers/codes incorrect, or you want to share more information about the topics discussed above. 
NikSta
nishant0073
divyesh072019
divyeshrabadiya07
rag2127
avanitrachhadiya2155
gottumukkalabobby
SHUBHAMSINGH10
geekygirl2001
Misc
Recursion
Misc
Recursion
Misc
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to write Regular Expressions?
OOPs | Object Oriented Design
Association Rule
Software Engineering | Prototyping Model
std::unique in C++
Write a program to print all permutations of a given string
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Program for Tower of Hanoi
Backtracking | Introduction
Print all subsequences of a string