Recursive Approach to find the maximum of Array

The idea is similar to the iterative approach. Here the traversal of the array is done recursively instead of an iterative loop. 

  • Set an integer i = 0 to denote the current index being searched.
  • Check if i is the last index, return arr[i].
  • Increment i and call the recursive function for the new value of i.
  • Compare the maximum value returned from the recursion function with arr[i].
  • Return the max between these two from the current recursion call.

Below is the implementation of the above approach:

C++




// C++ program to find maximum in arr[] of size n
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the largest element
int largest(int arr[], int n, int i)
{
    // Last index return the element
    if (i == n - 1) {
        return arr[i];
    }
 
    // Find the maximum from rest of the array
    int recMax = largest(arr, n, i + 1);
 
    // Compare with i-th element and return
    return max(recMax, arr[i]);
}
 
// Driver Code
int main()
{
    int arr[] = { 10, 324, 45, 90, 9808 };
    int n = sizeof(arr) / sizeof(arr[0]);
   
    // Function call
    cout << "Largest in given array is "
         << largest(arr, n, 0);
    return 0;
}
 
// This Code is contributed by Rajdeep Mallick


Java




// Java program to find maximum in arr[] of size n
import java.io.*;
 
class GFG {
 
    // Function to find the largest element
    static int largest(int arr[], int n, int i)
    {
        // Last index return the element
        if (i == n - 1) {
            return arr[i];
        }
 
        // Find the maximum from rest of the array
        int recMax = largest(arr, n, i + 1);
 
        // Compare with i-th element and return
        return Math.max(recMax, arr[i]);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 10, 324, 45, 90, 9808 };
        int n = arr.length;
       
        // Function call
        System.out.println("Largest in given array is "
                           + largest(arr, n, 0));
    }
}
 
// this code is contributed by rajdeep999


Python3




# Python program to find maximum in arr[] of size n
 
import math
 
 
# Function to find the largest element
def largest(arr,  n,  i):
    # Last index return the element
    if (i == n - 1):
        return arr[i]
 
    # Find the maximum from rest of the array
    recMax = largest(arr, n, i + 1)
 
    # Compare with i-th element and return
    return max(recMax, arr[i])
 
 
# Driver Code
if __name__ == '__main__':
    arr = [10, 324, 45, 90, 9808]
    n = len(arr)
 
    # Function call
    print("Largest in given array is " + str(largest(arr, n, 0)))
 
 
# This code is contributed by aadityaburujwale.


C#




// C# program to find maximum in arr[] of size n
using System;
 
public class GFG {
   
    // Function to find the largest element
    public static int largest(int[] arr, int n, int i)
    {
        // Last index return the element
        if (i == n - 1) {
            return arr[i];
        }
 
        // Find the maximum from rest of the array
        var recMax = GFG.largest(arr, n, i + 1);
 
        // Compare with i-th element and return
        return Math.Max(recMax, arr[i]);
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int[] arr = { 10, 324, 45, 90, 9808 };
        var n = arr.Length;
 
        // Function call
        Console.WriteLine(
            "Largest in given array is "
            + GFG.largest(arr, n, 0).ToString());
    }
}
 
// This code is contributed by aadityaburujwale.


Javascript




// JS program to find maximum
// in arr[] of size n
function largest(arr, n, i)
{
    // last index
    // return the element
    if (i == n - 1) {
        return arr[i];
    }
 
    // find the maximum from rest of the array
    let recMax = largest(arr, n, i + 1);
 
    // compare with i-th element and return
    return Math.max(recMax, arr[i]);
}
 
// Driver Code
let arr = [ 10, 324, 45, 90, 9808 ];
let n = arr.length;
console.log("Largest in given array is", largest(arr, n, 0));
 
// This Code is contributed by akashish__


PHP




<?php
// PHP program to find maximum in arr[] of size n
 
// Returns maximum in arr[] of size n
function largest($arr, $n, $i)
{
    // last index
    // return the element
    if ($i == $n - 1) {
        return $arr[$i];
    }
  
    // find the maximum from rest of the array
    $recMax = largest($arr, $n, $i + 1);
  
    // compare with i-th element and return
    return max($recMax, $arr[$i]);
}
  
// Driver Code
$arr = [ 10, 324, 45, 90, 9808 ];
$n = count($arr);
echo "Largest in given array is " , largest($arr, $n, 0) ;
// This code is contributed by ameyabavkar02.
?>


Output

Largest in given array is 9808

Time Complexity: O(N), where N is the size of the given array. 
Auxiliary Space: O(N), for recursive calls

Program to find largest element in an Array

Given an array arr[] of size N, the task is to find the largest element in the given array. 

Examples: 

Input: arr[] = {10, 20, 4}
Output: 20
Explanation: Among 10, 20 and 4, 20 is the largest. 

Input : arr[] = {20, 10, 20, 4, 100}
Output : 100

Recommended Practice

Similar Reads

Iterative Approach to find the largest element of Array:

The simplest approach is to solve this problem is to traverse the whole list and find the maximum among them....

Recursive Approach to find the maximum of Array:

...

Find the maximum of Array using Library Function:

...