Data Structure Questions and Answers – Largest and Smallest Number in an Array using Recursion

This set of Data Structure online test focuses on “Largest and Smallest Number in an Array using Recursion”.

1. Which of the following methods can be used to find the largest and smallest element in an array?
a) Recursion
b) Iteration
c) Both recursion and iteration
d) No method is suitable
View Answer

Answer: c
Explanation: Both recursion and iteration can be used to find the largest and smallest element in an array.

2. Consider the following iterative code snippet to find the largest element:

int get_max_element(int *arr,int n)
{
      int i, max_element = arr[0];
      for(i = 1; i < n; i++)
          if(________)
          max_element = arr[i];
      return max_element;
}

Which of the following lines should be inserted to complete the above code?
a) arr[i] > max_element
b) arr[i] < max_element
c) arr[i] == max_element
d) arr[i] != max_element
View Answer

Answer: a
Explanation: The line “arr[i] > max_element” should be inserted to complete the above code snippet.
advertisement
advertisement

3. Consider the following code snippet to find the smallest element in an array:

int get_min_element(int *arr, int n)
{
      int i, min_element = arr[0];
      for(i = 1; i < n; i++)
        if(_______)
          min_element = arr[i];
      return min_element;
}

Which of the following lines should be inserted to complete the above code?
a) arr[i] > min_element
b) arr[i] < min_element
c) arr[i] == min_element
d) arr[i] != min_element
View Answer

Answer: b
Explanation: The line “arr[i] < min_element” should be inserted to complete the above code.
advertisement

4. What is the output of the following code?

#include<stdio.h>
int get_max_element(int *arr,int n)
{
      int i, max_element = arr[0];
      for(i = 1; i < n; i++)
        if(arr[i] > max_element)
          max_element = arr[i];
      return max_element;
}
int get_min_element(int *arr, int n)
{
      int i, min_element = arr[0];
      for(i = 1; i < n; i++)
        if(arr[i] < min_element)
          min_element = arr[i];
      return min_element;
}
int main()
{
     int n = 7, arr[7] = {5,2,4,7,8,1,3};
     int max_element = get_max_element(arr,n);
     int min_element = get_min_element(arr,n);
     printf("%d %d",max_element,min_element);
     return 0;
}

a) 5 3
b) 3 5
c) 8 1
d) 1 8
View Answer

Answer: c
Explanation: The program prints the values of the largest and the smallest elements in the array, which are 8 and 1 respectively.
advertisement

5. What is the output of the following code?

#include<stdio.h>
int get_max_element(int *arr,int n)
{
      int i, max_element = arr[0];
      for(i = 1; i < n; i++)
        if(arr[i] > max_element)
          max_element = arr[i];
      return max_element;
}
int get_min_element(int *arr, int n)
{
      int i, min_element;
      for(i = 1; i < n; i++)
        if(arr[i] < min_element)
          min_element = arr[i];
      return min_element;
}
int main()
{
     int n = 7, arr[7] = {1,1,1,0,-1,-1,-1};
     int max_element = get_max_element(arr,n);
     int min_element = get_min_element(arr,n);
     printf("%d %d",max_element,min_element);
     return 0;
}

a) 1 -1
b) -1 1
c) 1 Garbage value
d) Depends on the compiler
View Answer

Answer: c
Explanation: Since the min_element variable is not initialised, some compilers will auto initialise as 0 which produces -1 as output whereas some compilers won’t initialise automatically. In that case, the result will be a garbage value hence the output depends on the compiler.

6. What is the time complexity of the following iterative implementation used to find the largest and smallest element in an array?

#include<stdio.h>
int get_max_element(int *arr,int n)
{
      int i, max_element = arr[0];
      for(i = 1; i < n; i++)
        if(arr[i] > max_element)
          max_element = arr[i];
      return max_element;
}
int get_min_element(int *arr, int n)
{
      int i, min_element;
      for(i = 1; i < n; i++)
        if(arr[i] < min_element)
          min_element = arr[i];
      return min_element;
}
int main()
{
     int n = 7, arr[7] = {1,1,1,0,-1,-1,-1};
     int max_element = get_max_element(arr,n);
     int min_element = get_min_element(arr,n);
     printf("%d %d",max_element,min_element);
     return 0;
}

a) O(1)
b) O(n)
c) O(n2)
d) O(n/2)
View Answer

Answer: b
Explanation: The time complexity of the above iterative implementation used to find the largest and the smallest element in an array is O(n).

7. Consider the following recursive implementation to find the largest element in an array.

int max_of_two(int a, int b)
{
      if(a > b)
        return a;
      return b;
}
int recursive_max_element(int *arr, int len, int idx)
{
      if(idx == len - 1)
      return arr[idx];
      return _______;
}

Which of the following lines should be inserted to complete the above code?
a) max_of_two(arr[idx], recursive_max_element(arr, len, idx))
b) recursive_max_element(arr, len, idx)
c) max_of_two(arr[idx], recursive_max_element(arr, len, idx + 1))
d) recursive_max_element(arr, len, idx + 1)
View Answer

Answer: c
Explanation: The line “max_of_two(arr[idx], recursive_max_element(arr, len, idx + 1))” should be inserted to complete the above code.

8. What is the output of the following code?

#include<stdio.h>
int max_of_two(int a, int b)
{
      if(a > b)
        return a;
      return b;
}
int min_of_two(int a, int b)
{
      if(a < b)
        return a;
      return b;
}
int recursive_max_element(int *arr, int len, int idx)
{
      if(idx == len - 1)
      return arr[idx];
      return max_of_two(arr[idx], recursive_max_element(arr, len, idx + 1));
}
int recursive_min_element(int *arr, int len, int idx)
{
      if(idx == len - 1)
      return arr[idx];
      return min_of_two(arr[idx], recursive_min_element(arr, len, idx + 1));
}
int main()
{
    int n = 10, idx = 0, arr[] = {5,2,6,7,8,9,3,-1,1,10};
    int max_element = recursive_max_element(arr,n,idx);
    int min_element = recursive_min_element(arr,n,idx);
    printf("%d %d",max_element,min_element);
    return 0;
}

a) -1 10
b) 10 -1
c) 1 10
d) 10 1
View Answer

Answer: b
Explanation: The program prints the values of the largest and the smallest element in the array, which are 10 and -1 respectively.

9. What is the time complexity of the following recursive implementation used to find the largest and the smallest element in an array?

#include<stdio.h>
int max_of_two(int a, int b)
{
      if(a > b)
        return a;
      return b;
}
int min_of_two(int a, int b)
{
      if(a < b)
        return a;
      return b;
}
int recursive_max_element(int *arr, int len, int idx)
{
      if(idx == len - 1)
      return arr[idx];
      return max_of_two(arr[idx], recursive_max_element(arr, len, idx + 1));
}
int recursive_min_element(int *arr, int len, int idx)
{
      if(idx == len - 1)
      return arr[idx];
      return min_of_two(arr[idx], recursive_min_element(arr, len, idx + 1));
}
int main()
{
    int n = 10, idx = 0, arr[] = {5,2,6,7,8,9,3,-1,1,10};
    int max_element = recursive_max_element(arr,n,idx);
    int min_element = recursive_min_element(arr,n,idx);
    printf("%d %d",max_element,min_element);
    return 0;
}

a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
View Answer

Answer: b
Explanation: The time complexity of the above recursive implementation used to find the largest and smallest element in an array is O(n).

10. How many times is the function recursive_min_element() called when the following code is executed?

int min_of_two(int a, int b)
{
      if(a < b)
        return a;
      return b;
}
int recursive_min_element(int *arr, int len, int idx)
{
      if(idx == len - 1)
      return arr[idx];
      return min_of_two(arr[idx], recursive_min_element(arr, len, idx + 1));
}
int main()
{
     int n = 10, idx = 0, arr[] = {5,2,6,7,8,9,3,-1,1,10};
     int min_element = recursive_min_element(arr,n,idx);
     printf("%d",min_element);
     return 0;
}

a) 9
b) 10
c) 11
d) 12
View Answer

Answer: b
Explanation: The function recursive_min_element() is called 10 times when the above code is executed.

11. What is the output of the following code?

#include<stdio.h>
int max_of_two(int a, int b)
{
      if(a > b)
        return a;
      return b;
}
int min_of_two(int a, int b)
{
      if(a < b)
        return a;
      return b;
}
int recursive_max_element(int *arr, int len, int idx)
{
      if(idx == len - 1)
      return arr[idx];
      return max_of_two(arr[idx], recursive_max_element(arr, len, idx + 1));
}
int recursive_min_element(int *arr, int len, int idx)
{
      if(idx == len - 1)
      return arr[idx];
      return min_of_two(arr[idx], recursive_min_element(arr, len, idx + 1));
}
int main()
{
     int n = 5, idx = 0, arr[] = {1,1,1,1,1};
     int max_element = recursive_max_element(arr,n,idx);
     int min_element = recursive_min_element(arr,n,idx);
     printf("%d %d",max_element,min_element);
     return 0;
}

a) 1 1
b) 0 0
c) compile time error
d) runtime error
View Answer

Answer: a
Explanation: The program prints the values of the largest and the smallest element in the array, which are 1 and 1.

Sanfoundry Global Education & Learning Series – Data Structures & Algorithms.

To practice all areas of Data Structures & Algorithms, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.