A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/dsa/recursive-programs-to-find-minimum-and-maximum-elements-of-array/ below:

Recursive Programs to find Minimum and Maximum elements of array

Recursive Programs to find Minimum and Maximum elements of array

Last Updated : 05 Jul, 2025

Given an array of integers arr[], the task is to find the minimum and maximum elements in the array using recursion only.

Examples:

Input: arr[] = [1, 4, 3, -5, -4, 8, 6]
Output: min = -5, max = 8

Input: arr[] = [1, 4, 45, 6, 10, -8]
Output: min = -8, max = 45

Input: arr[] = [12, 3, 15, 7, 9]
Output: min = 3, max = 15

Approach:  

The idea is to recursively reduce the problem size in each call by one, until only one element is left (which is trivially the min or max). This forms a natural divide into smaller subproblems where each call trusts the previous recursive result and compares it with the current element.

Base Case:

If n == 1, return arr[0] since a single element is both the min and max in its range.

Recursive Call:

If the base case is not met, then call the function by passing the array of one size less from the end

Return Statement:

At each recursive call (except for the base case), return the minimum of the last element of the current array (i.e. arr[n-1]) and the element returned from the previous recursive call. 

Steps to implement the above idea:

C++
// C++ code using recursion to find min 
// and max in an array
#include <iostream>
#include <vector>
using namespace std;

int getMinRec(vector<int> &arr, int n) {

    // Base case: only one element
    if (n == 1) {
        return arr[0];
    }

    // Recursive case: find min in rest of the array
    int minInRest = getMinRec(arr, n - 1);

    // Return the smaller value between 
    // last element and recursive min
    if (arr[n - 1] < minInRest) {
        return arr[n - 1];
    } else {
        return minInRest;
    }
}

int getMaxRec(vector<int> &arr, int n) {

    // Base case: only one element
    if (n == 1) {
        return arr[0];
    }

    // Recursive case: find max in rest of the array
    int maxInRest = getMaxRec(arr, n - 1);

    // Return the larger value between last
    // element and recursive max
    if (arr[n - 1] > maxInRest) {
        return arr[n - 1];
    } else {
        return maxInRest;
    }
}

// Main function to find min and max
vector<int> findMinMax(vector<int> &arr) {

    int n = arr.size();

    int minValue = getMinRec(arr, n);
    int maxValue = getMaxRec(arr, n);

    return {minValue, maxValue};
}

int main() {

    vector<int> arr = {1, 4, 3, -5, -4, 8, 6};

    vector<int> res = findMinMax(arr);

    cout << "min = " << res[0] << ", max = " << res[1];

    return 0;
}
Java
// Java code using recursion to find min 
// and max in an array
class GfG {

    static int getMinRec(int[] arr, int n) {

        // Base case: only one element
        if (n == 1) {
            return arr[0];
        }

        // Recursive case: find min in rest of the array
        int minInRest = getMinRec(arr, n - 1);

        // Return the smaller value between 
        // last element and recursive min
        if (arr[n - 1] < minInRest) {
            return arr[n - 1];
        } else {
            return minInRest;
        }
    }

    static int getMaxRec(int[] arr, int n) {

        // Base case: only one element
        if (n == 1) {
            return arr[0];
        }

        // Recursive case: find max in rest of the array
        int maxInRest = getMaxRec(arr, n - 1);

        // Return the larger value between last
        // element and recursive max
        if (arr[n - 1] > maxInRest) {
            return arr[n - 1];
        } else {
            return maxInRest;
        }
    }

    static int[] findMinMax(int[] arr) {

        int n = arr.length;

        int minValue = getMinRec(arr, n);
        int maxValue = getMaxRec(arr, n);

        return new int[] {minValue, maxValue};
    }

    public static void main(String[] args) {

        int[] arr = {1, 4, 3, -5, -4, 8, 6};

        int[] res = findMinMax(arr);

        System.out.println("min = " + res[0] + ", max = " + res[1]);
    }
}
Python
# Python code using recursion to find min 
# and max in an array

def getMinRec(arr, n):

    # Base case: only one element
    if n == 1:
        return arr[0]

    # Recursive case: find min in rest of the array
    minInRest = getMinRec(arr, n - 1)

    # Return the smaller value between 
    # last element and recursive min
    if arr[n - 1] < minInRest:
        return arr[n - 1]
    else:
        return minInRest

def getMaxRec(arr, n):

    # Base case: only one element
    if n == 1:
        return arr[0]

    # Recursive case: find max in rest of the array
    maxInRest = getMaxRec(arr, n - 1)

    # Return the larger value between last
    # element and recursive max
    if arr[n - 1] > maxInRest:
        return arr[n - 1]
    else:
        return maxInRest

def findMinMax(arr):

    n = len(arr)

    minValue = getMinRec(arr, n)
    maxValue = getMaxRec(arr, n)

    return [minValue, maxValue]

if __name__ == "__main__":

    arr = [1, 4, 3, -5, -4, 8, 6]

    res = findMinMax(arr)

    print("min = {}, max = {}".format(res[0], res[1]))
C#
// C# code using recursion to find min 
// and max in an array
using System;

class GfG {

    static int getMinRec(int[] arr, int n) {

        // Base case: only one element
        if (n == 1) {
            return arr[0];
        }

        // Recursive case: find min in rest of the array
        int minInRest = getMinRec(arr, n - 1);

        // Return the smaller value between 
        // last element and recursive min
        if (arr[n - 1] < minInRest) {
            return arr[n - 1];
        } else {
            return minInRest;
        }
    }

    static int getMaxRec(int[] arr, int n) {

        // Base case: only one element
        if (n == 1) {
            return arr[0];
        }

        // Recursive case: find max in rest of the array
        int maxInRest = getMaxRec(arr, n - 1);

        // Return the larger value between last
        // element and recursive max
        if (arr[n - 1] > maxInRest) {
            return arr[n - 1];
        } else {
            return maxInRest;
        }
    }

    static int[] findMinMax(int[] arr) {

        int n = arr.Length;

        int minValue = getMinRec(arr, n);
        int maxValue = getMaxRec(arr, n);

        return new int[] {minValue, maxValue};
    }

    static void Main() {

        int[] arr = {1, 4, 3, -5, -4, 8, 6};

        int[] res = findMinMax(arr);

        Console.WriteLine("min = " + res[0] + ", max = " + res[1]);
    }
}
JavaScript
// JavaScript code using recursion to find min 
// and max in an array

function getMinRec(arr, n) {

    // Base case: only one element
    if (n === 1) {
        return arr[0];
    }

    // Recursive case: find min in rest of the array
    let minInRest = getMinRec(arr, n - 1);

    // Return the smaller value between 
    // last element and recursive min
    if (arr[n - 1] < minInRest) {
        return arr[n - 1];
    } else {
        return minInRest;
    }
}

function getMaxRec(arr, n) {

    // Base case: only one element
    if (n === 1) {
        return arr[0];
    }

    // Recursive case: find max in rest of the array
    let maxInRest = getMaxRec(arr, n - 1);

    // Return the larger value between last
    // element and recursive max
    if (arr[n - 1] > maxInRest) {
        return arr[n - 1];
    } else {
        return maxInRest;
    }
}

function findMinMax(arr) {

    let n = arr.length;

    let minValue = getMinRec(arr, n);
    let maxValue = getMaxRec(arr, n);

    return [minValue, maxValue];
}

// Driver Code
let arr = [1, 4, 3, -5, -4, 8, 6];

let res = findMinMax(arr);

console.log("min = " + res[0] + ", max = " + res[1]);

Time Complexity: O(n), each element is visited once in both recursive functions independently to compare values.
Space Complexity: O(n), due to recursion stack space as each recursive call adds a frame to the stack.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4