# Recursive Programs to find Minimum and Maximum elements of array

Given an array of integers, we need to find the minimum element of that array using recursion.

Examples :

```Input : A = {1, 4, 3, -5, -4, 8, 6};
Output : -5

Input : A = {1, 4, 45, 6, 10, -8}
Output : -8
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Program to find largest element in an array

Recursive Minimum

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

## C++

 `// Recursive C++ program to find minimum ` `#include ` `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; ` `    ``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); ` `    ``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;  ` `     `  `    ``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

 ` `

Output:

```-50
```

Recursive Maximum

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

## C++

 `// Recursive C++ program to find maximum ` `#include ` `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; ` `    ``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); ` `    ``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 `

## Python 3

 `# 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;  ` `     `  `    ``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

 ` `

Output:

```45
```

This article is contributed by Pratik Chhajer. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.