Java Program to Find Maximum Value in Array by Using Recursion

Prerequisite: Recursion in Java

In the previous article, we have discussed about Java Program to Check if a Number is a Palindrome by Using Recursion

In this program we are going to find maximum value in array by using recursion in Java programming language.

Java Program to Find Maximum Value in Array by Using Recursion

If any array element is greater than all the array elements present in the array, then that element is the maximum element in the array.

For example-

If an array a[]={5, 8, 2, 9, 3}
Then the maximum element in array = 9

Now let’s see different ways to find maximum value in array by using recursion.

Method-1: Java Program to Find Maximum Value in Array By Using Static Input and Recursion

Approach:

  • Declare and initiate one array with some integer values.
  • Declare another integer variable in which the length of the array will be stored.
  • Define a user defined method find_Max() and pass arr[] and len as parameter.
  • Inside the user defined method find maximum number by traversing the total array by calling the same method recursively.
  • Print the result.

Program:

import java.util.*;
class Main 
{
    public static void main(String args[])
    {
        //Declare and initiate an array with some integer values
        int arr[] = {1,23,21,-8,45};
        int len = arr.length;
        //call the method
        System.out.println("Maximum value present in the array is: "+find_Max(arr, len));
    }
    
    public static int find_Max(int arr[], int len)
    {
        // if size = 0 means whole array has been traversed
        if(len == 1)
            return arr[0];
        return Math.max(arr[len-1], find_Max(arr, len-1));
    }
}
Output:

Maximum value present in the array is: 45

Method-2: Java Program to Find Maximum Value in Array By Using User Input and Recursion

Approach:

  • Declare an array with some integer values.
  • Declare another integer variable in which the length of the array will be stored.
  • Prompt the user to take input of the length value of array and array elements.
  • Define a user defined method find_Max() and pass arr[] and len as parameter.
  • Inside the user defined method find maximum number by traversing the total array by calling the same method recursively.
  • Print the result.

Program:

import java.lang.Math;
import java.util.Scanner;
class Main 
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the number of elements you want to store: ");
        int n=sc.nextInt();
        // Input array
        int[] arr = new int[100];
        System.out.print("Enter the elements of array: ");
        for(int i=0;i<n;i++)
        {
            arr[i]=sc.nextInt();
        }
        int len = arr.length;
        //call the method
        System.out.println("Minimum value present in the array is: "+find_Max(arr, len));
    }
    
    public static int find_Max(int arr[], int len)
    {
        // if size = 0 means whole array has been traversed
        if(len == 1)
            return arr[0];  
 
        return Math.max(arr[len-1], find_Max(arr, len-1));
    }
}
Output:

Enter the number of elements you want to store: 7
Enter the elements of array: 1 2 34 56 -23 -6 41
Minimum value present in the array is: 56

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output.

Related Java Programs: