-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathLargestNumber.java
More file actions
32 lines (27 loc) · 840 Bytes
/
LargestNumber.java
File metadata and controls
32 lines (27 loc) · 840 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
public class LargestNumber {
static int largestNum(int arr[]) {
if (arr.length < 3)
return -1;
int firstMax = 0;
int secondMax = 0;
int thirdMax = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > firstMax) {
thirdMax = secondMax;
secondMax = firstMax;
firstMax = arr[i];
} else if (arr[i] > secondMax) {
thirdMax = secondMax;
secondMax = arr[i];
} else if (arr[i] > thirdMax) {
thirdMax = arr[i];
}
}
return thirdMax;
}
public static void main(String[] args) {
// int arr[] = { 5, 9, 7, 4, 8, 6, 3, 2, 1, 10 };
int arr[] = { 2, 1, 3 };
System.out.println(largestNum(arr));
}
}