-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxPositionApp.java
More file actions
32 lines (26 loc) · 843 Bytes
/
MaxPositionApp.java
File metadata and controls
32 lines (26 loc) · 843 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
package gr.aueb.cf.ch10.projects;
public class MaxPositionApp {
public static void main(String[] args) {
int[] arr = {1, 2, 7, 9, 5};
int maxPos;
maxPos = getMaxPosition(arr, 0, arr.length -1);
if (maxPos == -1) System.exit(1);
System.out.println(arr[maxPos]);
}
public static int getMaxPosition(int[] arr, int low, int high) {
int maxPosition = 0;
int maxValue = 0;
if ((arr == null) || (arr.length < 1)) return -1;
if ((low < 0) || (high > arr.length - 1) || (low > high)) {
return -1;
}
maxValue = arr[low];
for (int i = low + 1; i <= high ; i++) {
if (arr[i] > maxValue) {
maxValue = arr[i];
maxPosition = i;
}
}
return maxPosition;
}
}