-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP26_SecondLargestElement.java
More file actions
48 lines (40 loc) · 1.51 KB
/
Copy pathP26_SecondLargestElement.java
File metadata and controls
48 lines (40 loc) · 1.51 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
40
41
42
43
44
45
46
47
48
package programs;
import java.util.Arrays;
/**
* ============================================================
* PROGRAM 26: Find Second Largest Element in Array
* ============================================================
* Problem: WAP to find the Second Largest element in an array
* in a SINGLE pass (O(n) time and O(1) space) without sorting.
* ============================================================
*/
public class P26_SecondLargestElement {
public static int findSecondLargest(int[] arr) {
if (arr == null || arr.length < 2) return Integer.MIN_VALUE;
int largest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
for (int num : arr) {
if (num > largest) {
secondLargest = largest;
largest = num;
} else if (num > secondLargest && num != largest) {
secondLargest = num;
}
}
return secondLargest;
}
public static void main(String[] args) {
int[][] testArrays = {
{12, 35, 1, 10, 34, 1},
{10, 10, 10},
{5, 20, 15, 30, 25},
{-10, -40, -20, -5}
};
System.out.println("=== SECOND LARGEST ELEMENT TESTS ===");
for (int[] arr : testArrays) {
int second = findSecondLargest(arr);
System.out.printf(" Array: %-25s -> Second Largest: %s%n",
Arrays.toString(arr), (second == Integer.MIN_VALUE ? "None (all duplicates)" : second));
}
}
}