-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecondLargest.java
39 lines (33 loc) · 1.19 KB
/
secondLargest.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
// Given an array of positive integers arr[], return the second largest element from the array. If the second largest element doesn't exist then return -1.
// Note: The second largest element should not be equal to the largest element.
// Examples:
// Input: arr[] = [12, 35, 1, 10, 34, 1]
// Output: 34
// Explanation: The largest element of the array is 35 and the second largest element is 34.
// Input: arr[] = [10, 5, 10]
// Output: 5
// Explanation: The largest element of the array is 10 and the second largest element is 5.
// Input: arr[] = [10, 10, 10]
// Output: -1
// Explanation: The largest element of the array is 10 and the second largest element does not exist.
// Constraints:
// 2 ≤ arr.size() ≤ 105
// 1 ≤ arr[i] ≤ 105
// Solution:
class Solution {
public int getSecondLargest(int[] arr) {
// Code Here
int n = arr.length;
int max = -1, secondMax = -1;
for (int i = 0; i < n; i++) {
if (arr[i] > max) {
secondMax = max;
max = arr[i];
}
else if (arr[i] < max && arr[i] > secondMax) {
secondMax = arr[i];
}
}
return secondMax;
}
}