-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmax.java
More file actions
41 lines (35 loc) · 946 Bytes
/
max.java
File metadata and controls
41 lines (35 loc) · 946 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
38
39
40
41
package com.kunal;
public class Max {
public static void main(String[] args) {
int[] arr = {1, 3, 2, 9, 18};
System.out.println(maxRange(arr, 1, 3));
}
// work on edge cases here, like array being null
static int maxRange(int[] arr, int start, int end) {
if (start > end) {
return -1;
}
if (arr == null) {
return -1;
}
int maxVal = arr[start];
for (int i = start; i <= end; i++) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}
return maxVal;
}
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;
}
}