Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Maximize minimum element of an Array using operations
Next article icon

Recursive Programs to find Minimum and Maximum elements of array

Last Updated : 19 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array of integers arr, the task is to find the minimum and maximum element of that array using recursion.

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

Recursive approach to find the Minimum element in the array

Approach:  

  • Get the array for which the minimum is to be found
  • Recursively find the minimum according to the following: 
    • Recursively traverse the array from the end
    • Base case: If the remaining array is of length 1, return the only present element i.e. arr[0] 
if(n == 1)     return arr[0];
  • Recursive call: If the base case is not met, then call the function by passing the array of one size less from the end, i.e. from arr[0] to arr[n-1].
  • 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. 
return min(arr[n-1], recursive_function(arr, n-1));
  • Print the returned element from the recursive function as the minimum element

Pseudocode for Recursive function: 

If there is single element, return it.  Else return minimum of following.      a) Last Element      b) Value returned by recursive call         for n-1 elements.

Below is the implementation of the above approach:

C++




// Recursive C++ program to find minimum
  
#include <iostream>
using namespace std;
  
// function to print Minimum element using recursion
int findMinRec(int A[], int n)
{
    // if size = 0 means whole array has been traversed
    if (n == 1)
        return A[0];
    return min(A[n-1], findMinRec(A, n-1));
}
  
// driver code to test above function
int main()
{
    int A[] = {1, 4, 45, 6, -50, 10, 2};
    int n = sizeof(A)/sizeof(A[0]);
    cout <<  findMinRec(A, n);
    return 0;
}
 
 

Java




// Recursive Java program to find minimum
import java.util.*;
  
class GFG {
  
    // function to return minimum element using recursion
    public static int findMinRec(int A[], int n)
    {
      // if size = 0 means whole array
      // has been traversed
      if(n == 1)
        return A[0];
          
        return Math.min(A[n-1], findMinRec(A, n-1));
    }
      
    // Driver code
    public static void main(String args[])
    {
        int A[] = {1, 4, 45, 6, -50, 10, 2};
        int n = A.length;
          
        // Function calling
        System.out.println(findMinRec(A, n));
    }
}
  
//This code is contributed by Niraj_Pandey
 
 

Python3




# Recursive python 3 program to 
# find minimum
  
# function to print Minimum element 
# using recursion
def findMinRec(A, n):
      
    # if size = 0 means whole array
    # has been traversed
    if (n == 1):
        return A[0]
    return min(A[n - 1], findMinRec(A, n - 1))
  
# Driver Code
if __name__ == '__main__':
    A = [1, 4, 45, 6, -50, 10, 2]
    n = len(A)
    print(findMinRec(A, n))
  
# This code is contributed by
# Shashank_Sharma
 
 

C#




// Recursive C# program to find minimum 
using System;
  
class GFG
{
      
// function to return minimum 
// element using recursion 
public static int findMinRec(int []A,
                             int n) 
{ 
// if size = 0 means whole array 
// has been traversed 
if(n == 1) 
    return A[0]; 
      
    return Math.Min(A[n - 1],
                    findMinRec(A, n - 1)); 
} 
  
// Driver code 
static public void Main ()
{
    int []A = {1, 4, 45, 6, -50, 10, 2}; 
    int n = A.Length; 
      
    // Function calling 
    Console.WriteLine(findMinRec(A, n)); 
} 
} 
  
// This code is contributed by Sachin
 
 

PHP




<?php
// Recursive PHP program to find minimum
  
// function to print Minimum
// element using recursion
function findMinRec($A, $n)
{
      
    // if size = 0 means whole 
    // array has been traversed
    if ($n == 1)
        return $A[0];
    return min($A[$n - 1], findMinRec($A, $n - 1));
}
  
    // Driver Code
    $A = array (1, 4, 45, 6, -50, 10, 2);
    $n = sizeof($A);
    echo findMinRec($A, $n);
  
// This code is contributed by akt
?>
 
 

Javascript




<script>
  
// Javascript program to find minimum
  
// Function to print Minimum
// element using recursion
function findMinRec(A, n)
{
      
    // If size = 0 means whole
    // array has been traversed
    if (n == 1)
        return A[0];
          
    return Math.min(A[n - 1], 
        findMinRec(A, n - 1));
}
  
// Driver Code
let A = [ 1, 4, 45, 6, -50, 10, 2 ];
let n = A.length;
  
document.write( findMinRec(A, n));
  
// This code is contributed by sravan kumar G
  
</script>
 
 
Output
-50

Recursive approach to find the Maximum element in the array

Approach:  

  • Get the array for which the maximum is to be found
  • Recursively find the maximum according to the following: 
    • Recursively traverse the array from the end
    • Base case: If the remaining array is of length 1, return the only present element i.e. arr[0] 
if(n == 1)     return arr[0];
  • Recursive call: If the base case is not met, then call the function by passing the array of one size less from the end, i.e. from arr[0] to arr[n-1].
  • Return statement: At each recursive call (except for the base case), return the maximum of the last element of the current array (i.e. arr[n-1]) and the element returned from the previous recursive call. 
return max(arr[n-1], recursive_function(arr, n-1));
  • Print the returned element from the recursive function as the maximum element

Pseudocode for Recursive function:  

If there is single element, return it.  Else return maximum of following.      a) Last Element      b) Value returned by recursive call         for n-1 elements.

Below is the implementation of the above approach:

C++




// Recursive C++ program to find maximum
#include <iostream>
using namespace std;
  
// function to return maximum element using recursion
int findMaxRec(int A[], int n)
{
    // if n = 0 means whole array has been traversed
    if (n == 1)
        return A[0];
    return max(A[n-1], findMaxRec(A, n-1));
}
  
// driver code to test above function
int main()
{
    int A[] = {1, 4, 45, 6, -50, 10, 2};
    int n = sizeof(A)/sizeof(A[0]);
    cout <<  findMaxRec(A, n);
    return 0;
}
 
 

Java




// Recursive Java program to find maximum
import java.util.*;
  
class GFG {
      
    // function to return maximum element using recursion
    public static int findMaxRec(int A[], int n)
    {
      // if size = 0 means whole array
      // has been traversed
      if(n == 1)
        return A[0];
          
        return Math.max(A[n-1], findMaxRec(A, n-1));
    }
  
    // Driver code
    public static void main(String args[])
    {
        int A[] = {1, 4, 45, 6, -50, 10, 2};
        int n = A.length;
          
        // Function calling
        System.out.println(findMaxRec(A, n));
    }
}
  
//This code is contributed by Niraj_Pandey
 
 

Python3




# Recursive Python 3 program to 
# find maximum
  
# function to return maximum element
# using recursion
def findMaxRec(A, n):
  
    # if n = 0 means whole array 
    # has been traversed
    if (n == 1):
        return A[0]
    return max(A[n - 1], findMaxRec(A, n - 1))
  
# Driver Code
if __name__ == "__main__":
      
    A = [1, 4, 45, 6, -50, 10, 2]
    n = len(A)
    print(findMaxRec(A, n))
  
# This code is contributed by ita_c
 
 

C#




// Recursive C# program to find maximum 
using System;
  
class GFG
{
// function to return maximum 
// element using recursion 
public static int findMaxRec(int []A, 
                             int n) 
{ 
// if size = 0 means whole array 
// has been traversed 
if(n == 1) 
    return A[0]; 
      
    return Math.Max(A[n - 1], 
                    findMaxRec(A, n - 1)); 
} 
  
// Driver code 
static public void Main ()
{
    int []A = {1, 4, 45, 6, -50, 10, 2}; 
    int n = A.Length; 
      
    // Function calling 
    Console.WriteLine(findMaxRec(A, n)); 
}
}
  
// This code is contributed by Sach_Code
 
 

PHP




<?php
// Recursive PHP program to find maximum
  
// function to return maximum
// element using recursion
function findMaxRec($A, $n)
{
    // if n = 0 means whole array 
    // has been traversed
    if ($n == 1)
        return $A[0];
    return max($A[$n - 1], 
        findMaxRec($A, $n - 1));
}
  
// Driver Code
$A = array(1, 4, 45, 6, -50, 10, 2);
$n = sizeof($A);
echo findMaxRec($A, $n);
  
// This code is contributed
// by Akanksha Rai
?>
 
 

Javascript




<script>
  
// Recursive Java program to find maximum
  
// Function to return maximum element 
// using recursion
function findMaxRec(A, n)
{
      
    // If size = 0 means whole array
    // has been traversed
    if (n == 1)
        return A[0];
      
    return Math.max(A[n - 1], 
        findMaxRec(A, n - 1));
}
  
// Driver code
let A = [ 1, 4, 45, 6, -50, 10, 2 ];
let n = A.length;
  
// Function calling
document.write(findMaxRec(A, n));
  
// This code is contributed by sravan kumar G
  
</script>
 
 
Output
45

Related article: 
Program to find largest element in an array

 



Next Article
Maximize minimum element of an Array using operations

P

Pratik Chhajer
Improve
Article Tags :
  • Arrays
  • DSA
  • Recursion
  • Searching
Practice Tags :
  • Arrays
  • Recursion
  • Searching

Similar Reads

  • Program to find the minimum (or maximum) element of an array
    Given an array, write functions to find the minimum and maximum elements in it. Examples: Input: arr[] = [1, 423, 6, 46, 34, 23, 13, 53, 4]Output: Minimum element of array: 1 Maximum element of array: 423 Input: arr[] = [2, 4, 6, 7, 9, 8, 3, 11]Output: Minimum element of array: 2 Maximum element of
    14 min read
  • Find the minimum and maximum sum of N-1 elements of the array
    Given an unsorted array A of size N, the task is to find the minimum and maximum values that can be calculated by adding exactly N-1 elements. Examples: Input: a[] = {13, 5, 11, 9, 7} Output: 32 40 Explanation: Minimum sum is 5 + 7 + 9 + 11 = 32 and maximum sum is 7 + 9 + 11 + 13 = 40.Input: a[] = {
    10 min read
  • Maximize minimum element of an Array using operations
    Given an array A[] of N integers and two integers X and Y (X ≤ Y), the task is to find the maximum possible value of the minimum element in an array A[] of N integers by adding X to one element and subtracting Y from another element any number of times, where X ≤ Y. Examples: Input: N= 3, A[] = {1,
    9 min read
  • Maximum of minimums of every window size in a given array
    Given an integer array arr[] of size n, the task is to find the maximum of the minimums for every window size in the given array, where the window size ranges from 1 to n. Example: Input: arr[] = [10, 20, 30]Output: [30, 20, 10]Explanation: First element in output indicates maximum of minimums of al
    14 min read
  • Sum and Product of minimum and maximum element of an Array
    Given an array. The task is to find the sum and product of the maximum and minimum elements of the given array. Examples: Input : arr[] = {12, 1234, 45, 67, 1} Output : Sum = 1235 Product = 1234 Input : arr[] = {5, 3, 6, 8, 4, 1, 2, 9} Output : Sum = 10 Product = 9 Take two variables min and max to
    13 min read
  • Leftmost and rightmost indices of the maximum and the minimum element of an array
    Given an array arr[], the task is to find the leftmost and the rightmost indices of the minimum and the maximum element from the array where arr[] consists of non-distinct elements.Examples: Input: arr[] = {2, 1, 1, 2, 1, 5, 6, 5} Output: Minimum left : 1 Minimum right : 4 Maximum left : 6 Maximum r
    15+ min read
  • Find the Array element left after alternate removal of minimum and maximum
    Given an array Arr of length N, it is reduced by 1 element at each step. Maximum and Minimum elements will be removed in alternating order from the remaining array until a single element is remaining in the array. The task is to find the remaining element in the given array. Examples: Input: arr[] =
    4 min read
  • Find the number of operations required to make all array elements Equal
    Given an array of N integers, the task is to find the number of operations required to make all elements in the array equal. In one operation we can distribute equal weights from the maximum element to the rest of the array elements. If it is not possible to make the array elements equal after perfo
    6 min read
  • Minimum index of element with maximum multiples in Array
    Given an array arr[] of length n, the task is to calculate the min index of the element which has maximum elements present in the form of {2 * arr[i], 3 * arr[i], 4 * arr[i], 5 * arr[i]}. Examples: Input: n = 4. arr[] = {2, 1, 3, 6}Output: 1Explanation: For 2, {2*2, 3*2, 4*2, 5*2} => {4, 6, 8, 10
    5 min read
  • Maximum sum of minimums of pairs in an array
    Given an array arr[] of N integers where N is even, the task is to group the array elements in the pairs (X1, Y1), (X2, Y2), (X3, Y3), ... such that the sum min(X1, Y1) + min(X2, Y2) + min(X3, Y3) + ... is maximum.Examples: Input: arr[] = {1, 5, 3, 2} Output: 4 (1, 5) and (3, 2) -> 1 + 2 = 3 (1,
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences