Skip to content

Commit e713edf

Browse files
committed
MinMaxArray
1 parent 8d3c55c commit e713edf

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Recursion/MinMaxRecursion.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*Write a program to find Minimum and Maximum of an Array.
2+
Eg :
3+
Input :
4+
5
5+
7 8 9 4 0
6+
Output :
7+
[0,9]
8+
*/
9+
import java.util.*;
10+
class MinMaxRecursion {
11+
public static void main(String[] args) {
12+
Scanner input = new Scanner(System.in);
13+
int n = input.nextInt(); //size of array
14+
int[] arr = new int[n];
15+
for (int i = 0; i < n; i++) {
16+
arr[i] = input.nextInt(); //Input array
17+
}
18+
19+
int[] array = new int[]{MinArrRec(arr, n), MaxArrRec(arr, n)}; //Array of [min,max]
20+
System.out.println(Arrays.toString(array));
21+
input.close();
22+
}
23+
public static int MinArrRec(int[] A, int n)
24+
{
25+
26+
if(n == 1) //If size of array is 1, then same element is min and max
27+
return A[0];
28+
29+
return Math.min(A[n-1], MinArrRec(A, n-1));//Recursively finding minimum
30+
31+
}
32+
public static int MaxArrRec(int[] A, int n)
33+
{
34+
if(n == 1) //If size of array is 1, then same element is min and max
35+
return A[0];
36+
37+
return Math.max(A[n-1], MaxArrRec(A, n-1)); //Recursively finding maximum
38+
39+
}
40+
41+
42+
}

0 commit comments

Comments
 (0)