-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_Array_Element.java
More file actions
35 lines (31 loc) · 825 Bytes
/
Copy pathmax_Array_Element.java
File metadata and controls
35 lines (31 loc) · 825 Bytes
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
package Easy;
import java.util.Scanner;
public class max_Array_Element {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0;i<n;i++){
arr[i] = sc.nextInt();
}
int max = maxArray(arr, n -1);
System.out.println(max);
}
//Method 1
private static int maxArray(int[] arr, int index, int max) {
if(index == arr.length){
return max;
}
if(arr[index]>max){
max = arr[index];
}
return maxArray(arr, index+1, max);
}
//Method 2
private static int maxArray(int[] arr, int n){
if(n==0){
return arr[0];
}
return Math.max(arr[n-1], maxArray(arr, n-1));
}
}