-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMax.java
More file actions
37 lines (35 loc) · 921 Bytes
/
Max.java
File metadata and controls
37 lines (35 loc) · 921 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
36
37
public class Max {
public static void main(String[] args) {
int [] arr = {1, 3, 23, 9, 18};
System.out.println(maxRange(arr, 1,3));
}
// work on edge cases here, lik array being null
static int maxRange(int[]arr, int start, int end){
if(end>start){
return -1;
}
if (arr==null){
return -1;
}
int maxVal = arr[start];
for (int i = 1; i<=end;i++){
if(arr[i]>maxVal){
maxVal=arr[i];
}
}
return maxVal;
}
// imagine that arr is not empty
static int max(int[]arr){
if (arr.length==0){
return -1;
}
int maxVal = arr[0];
for (int i = 1; i<=arr.length;i++){
if(arr[i]>maxVal){
maxVal=arr[i];
}
}
return maxVal;
}
}