-
Notifications
You must be signed in to change notification settings - Fork 17
/
SecondLargestElement.java
41 lines (34 loc) · 1.08 KB
/
SecondLargestElement.java
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 by.andd3dfx.numeric;
import java.util.Arrays;
/**
* Find the second-largest element in array of numbers
*
* @see <a href="https://youtu.be/TYU9OsImhP8">Video solution</a>
*/
public class SecondLargestElement {
public static int find_NlogN(int[] array) {
if (array.length < 2) {
throw new IllegalArgumentException("Array size should be at least 2!");
}
Arrays.sort(array);
return array[array.length - 2];
}
public static int find_N(int[] array) {
if (array.length < 2) {
throw new IllegalArgumentException("Array size should be at least 2!");
}
int max_1 = Math.max(array[0], array[1]);
int max_2 = Math.min(array[0], array[1]);
for (int i = 2; i < array.length; i++) {
if (array[i] >= max_1) {
max_2 = max_1;
max_1 = array[i];
continue;
}
if (array[i] >= max_2) {
max_2 = array[i];
}
}
return max_2;
}
}