Minimum and Maximum elements Using Recursion
Last Updated :
24 Sep, 2025
Given an array of integers arr[], find the minimum and maximum elements in the array using recursion only. The first element of the output represents the minimum value, and the second element represents the maximum value in the array.
Examples:
Input: arr[] = [1, 4, 3, -5, -4, 8, 6]
Output: [-5, 8]
Explanation: -5 is the minimum and 8 is the maximum element in the array
Input: arr[] = [12, 3, 15, 7, 9]
Output: [3, 15]
Explanation: 3 is the minimum and 15 is the maximum element in the array
Approach:
The main idea is to use recursion to process one element of the array at a time. In the base case, if the array has only one element, that element is both the minimum and maximum. In the recursive step, we first find the minimum and maximum of the first n-1 elements, then compare the n-th element to update the minimum or maximum if needed.
C++
#include <iostream>
#include <vector>
using namespace std;
// Helper recursive function
vector<int> findMinMaxRec(vector<int>& arr, int index) {
// Base case: only one element
if (index == 0) {
return {arr[0], arr[0]};
}
// Recursive call
vector<int> result = findMinMaxRec(arr, index - 1);
// Update min
if (arr[index] < result[0]) result[0] = arr[index];
// Update max
if (arr[index] > result[1]) result[1] = arr[index];
return result;
}
vector<int> findMinMax(vector<int>& arr) {
return findMinMaxRec(arr, arr.size() - 1);
}
int main() {
vector<int> arr = {1, 4, 3, -5, -4, 8, 6};
vector<int> res = findMinMax(arr);
cout << res[0] << " " << res[1];
return 0;
}
Java
import java.util.ArrayList;
class GFG {
// Helper recursive function
public static ArrayList<Integer> findMinMaxRec(int[] arr, int index) {
// Base case: only one element
if (index == 0) {
ArrayList<Integer> base = new ArrayList<>();
base.add(arr[0]);
base.add(arr[0]);
return base;
}
// Recursive call for first index elements
ArrayList<Integer> result = findMinMaxRec(arr, index - 1);
// Update min
if (arr[index] < result.get(0)) result.set(0, arr[index]);
// Update max
if (arr[index] > result.get(1)) result.set(1, arr[index]);
return result;
}
public static ArrayList<Integer> findMinMax(int[] arr) {
return findMinMaxRec(arr, arr.length - 1);
}
public static void main(String[] args) {
int[] arr = {1, 4, 3, -5, -4, 8, 6};
ArrayList<Integer> res = findMinMax(arr);
System.out.println(res.get(0) + " " + res.get(1));
}
}
Python
# Helper recursive function
def findMinMaxRec(arr, index):
# Base case: only one element
if index == 0:
return [arr[0], arr[0]]
# Recursive call for first index elements
result = findMinMaxRec(arr, index - 1)
# Update min
if arr[index] < result[0]:
result[0] = arr[index]
# Update max
if arr[index] > result[1]:
result[1] = arr[index]
return result
def findMinMax(arr):
return findMinMaxRec(arr, len(arr) - 1)
if __name__ == '__main__':
arr = [1, 4, 3, -5, -4, 8, 6]
res = findMinMax(arr)
print(res[0], res[1])
C#
using System;
using System.Collections.Generic;
class GFG {
// Helper recursive function
static List<int> findMinMaxRec(int[] arr, int index) {
// Base case: only one element
if (index == 0) {
return new List<int> { arr[0], arr[0] };
}
// Recursive call for first index elements
List<int> result = findMinMaxRec(arr, index - 1);
// Update min
if (arr[index] < result[0]) result[0] = arr[index];
// Update max
if (arr[index] > result[1]) result[1] = arr[index];
return result;
}
static List<int> findMinMax(int[] arr) {
return findMinMaxRec(arr, arr.Length - 1);
}
static void Main() {
int[] arr = {1, 4, 3, -5, -4, 8, 6};
List<int> res = findMinMax(arr);
Console.WriteLine($"{res[0]} {res[1]}");
}
}
JavaScript
// Helper recursive function
function findMinMaxRec(arr, index) {
// Base case: only one element
if (index === 0) {
return [arr[0], arr[0]];
}
// Recursive call for first index elements
let result = findMinMaxRec(arr, index - 1);
// Update min
if (arr[index] < result[0]) result[0] = arr[index];
// Update max
if (arr[index] > result[1]) result[1] = arr[index];
return result;
}
function findMinMax(arr) {
return findMinMaxRec(arr, arr.length - 1);
}
// Driver Code
let arr = [1, 4, 3, -5, -4, 8, 6];
let res = findMinMax(arr);
console.log(res[0], res[1]);
Time Complexity: O(n)
Auxiliary Space: O(n), due to recursion stack space
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem