Find maximum and minimum elements in array using recursion

Problem statement:- Program to Find maximum and minimum elements in the array using recursion.

Data requirement:-

   Input Data:- n, arr

  Output Data:-
FindMax(arr,n), FindMin(arr,n)

Program in C

Here is the source code of the C program to find maximum and minimum elements in an array using recursion.

Code:

#include<stdio.h>
#include<limits.h>
int FindMax(int arr[],int n)
{
    static int i=0,max=INT_MIN;
    if(i<n)
    {
        if(arr[i]>=max)
            max=arr[i];
            i++;
            FindMax(arr,n);
    }
    return max;
}
int FindMin(int arr[],int n)
{
    static int i=0,min=INT_MAX;
    if(i<n)
    {
        if(arr[i]<=min)
            min=arr[i];
            i++;
            FindMin(arr,n);
    }
    return min;
}
int main()
{
    int n,i;
    printf("Enter the array size:");
    scanf("%d",&n);
    int arr[n];
    printf("Enter the array element:");
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
     printf("Maximum Element of the array is: %d\n",FindMax(arr,n));
     printf("Minimum Element of the array is: %d",FindMin(arr,n));
}

Input/Output:
Enter the array size:5
Enter the array element:4
6
7
3
5
Maximum Element of the array is: 7
Minimum Element of the array is: 3

Program in C++

Here is the source code of the C++ Program to Find maximum and minimum elements in an array using recursion.

Code:

#include<iostream>
#include<climits>
using namespace std;
int FindMax(int arr[],int n)
{
    static int i=0,max=INT_MIN;
    if(i<n)
    {
        if(arr[i]>=max)
            max=arr[i];
            i++;
            FindMax(arr,n);
    }
    return max;
}
int FindMin(int arr[],int n)
{
    static int i=0,min=INT_MAX;
    if(i<n)
    {
        if(arr[i]<=min)
            min=arr[i];
            i++;
            FindMin(arr,n);
    }
    return min;
}
int main()
{
    int n,i;
    cout<<"Enter the array size:";
    cin>>n;
    int arr[n];
    cout<<"Enter the array element:";
    for(i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    cout<<"Maximum Element of the array is: "<<FindMax(arr,n)<<endl;
    cout<<"Minimum Element of the array is: "<<FindMin(arr,n)<<endl;
}

Input/Output:
Enter the array size:6
Enter the array element:11 8 13 32 9 12
Maximum Element of the array is: 32
Minimum Element of the array is: 8

Program in Java

Here is the source code of the Java program to find maximum and minimum elements in an array using recursion.

Code:

import java.util.Scanner;
public class FindMaximumMinimumElement {
static int FindMax(int arr[],int n)
{
if(n==1)
return arr[0];
    return Math.max(arr[n-1], FindMax(arr, n-1));
}
static int FindMin(int arr[],int n)
{
if(n==1)
return arr[0];
    return Math.min(arr[n-1], FindMin(arr, n-1));
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
        int n,i;
    System.out.print("Enter the array size:");
    n=cs.nextInt();
    int arr[]=new int[n];
    System.out.print("Enter the array element:");
    for(i=0;i<n;i++)
    {
       arr[i]=cs.nextInt();
    }
   System.out.println("Maximum Element of the array is: "+FindMax(arr,n));
    System.out.println("Minimum Element of the array is: "+FindMin(arr,n));
        cs.close();
}
}

Input/Output:
Enter the array size:4
Enter the array element:78 7 30 25
Maximum Element of the array is: 78
Minimum Element of the array is: 7

Program in Python

Here is the source code of the Python program to Find maximum and minimum elements in array using recursion.

Code:

import sys
def FindMax(arr,n):
    if n == 1:
        return arr[0]
    return max(arr[n - 1], FindMax(arr, n - 1))
def FindMin(arr,n):
    if n==1:
        return arr[0]
    return min(arr[n-1], FindMin(arr, n-1))

arr=[]
n = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,n):
    num = int(input())
    arr.append(num)

print("Maximum Element of the array is: ",FindMax(arr,len(arr)))
print("Minimum Element of the array is: ",FindMin(arr,len(arr)))

Input/Output:

Post a Comment

0 Comments