-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecondMinApp.java
More file actions
39 lines (32 loc) · 1.1 KB
/
SecondMinApp.java
File metadata and controls
39 lines (32 loc) · 1.1 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
package gr.aueb.cf.ch10.projects;
public class SecondMinApp {
public static void main(String[] args) {
int[] arr = {0, 3, 7, 12, 20};
int secMin;
secMin = getSecondMinPosition(arr);
if (secMin == -1) System.exit(1);
System.out.printf("SecMinPos: %d, SecMinValue: %d", secMin + 1, arr[secMin]);
}
public static int getSecondMinPosition(int[] arr) {
int min= 0;
int secondMin = 1;
int minValue = 0;
int secondMinValue = 0;
if (arr == null) return -1;
if (arr.length < 2) return -1;
minValue = Integer.MAX_VALUE;
secondMinValue = Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++) {
if ((arr[i] < minValue) && (arr[i] < secondMinValue)) {
secondMin = min;
secondMinValue = minValue;
min = i;
minValue = arr[i];
} else if ((arr[i] > minValue) && (arr[i] < secondMinValue)) {
secondMin = i;
secondMinValue = arr[i];
}
}
return secondMin;
}
}