-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathMinMaxRecursion.java
More file actions
42 lines (34 loc) · 1.07 KB
/
MinMaxRecursion.java
File metadata and controls
42 lines (34 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*Write a program to find Minimum and Maximum of an Array.
Eg :
Input :
5
7 8 9 4 0
Output :
[0,9]
*/
import java.util.*;
class MinMaxRecursion {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt(); //size of array
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = input.nextInt(); //Input array
}
int[] array = new int[]{MinArrRec(arr, n), MaxArrRec(arr, n)}; //Array of [min,max]
System.out.println(Arrays.toString(array));
input.close();
}
public static int MinArrRec(int[] A, int n)
{
if(n == 1) //If size of array is 1, then same element is min and max
return A[0];
return Math.min(A[n-1], MinArrRec(A, n-1));//Recursively finding minimum
}
public static int MaxArrRec(int[] A, int n)
{
if(n == 1) //If size of array is 1, then same element is min and max
return A[0];
return Math.max(A[n-1], MaxArrRec(A, n-1)); //Recursively finding maximum
}
}