-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecondMaxArray.java
More file actions
31 lines (25 loc) · 828 Bytes
/
Copy pathSecondMaxArray.java
File metadata and controls
31 lines (25 loc) · 828 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
/*
* Author: Hasnain Memon
* Date: 29/10/2024
*/
// Task : Find second maximum value in an array
import java.util.Arrays;
public class SecondMaxArray {
private static int findSecondMax(int[] arr, int n) {
int max = Integer.MIN_VALUE, secondMax = Integer.MIN_VALUE;
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
secondMax = max;
max = arr[i];
} else if (arr[i] > secondMax && arr[i] != max) {
secondMax = arr[i];
}
}
return secondMax;
}
public static void main(String[] args) {
int[] arr = {2, 3, 4, 6, 9, 0, 11};
System.out.println(Arrays.toString(arr));
System.out.println("SecondMax = " + findSecondMax(arr, arr.length));
}
}