-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay1.java
More file actions
33 lines (30 loc) · 1.08 KB
/
Copy pathDay1.java
File metadata and controls
33 lines (30 loc) · 1.08 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
/*Second Largest
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. */
/*class Solution {
public int getSecondLargest(int[] arr) {
// code here
int largest =-1;
int secondLargest =-1;
for(int num:arr){
if(num>largest){
secondLargest=largest;
largest=num;
}
else if(num>secondLargest&& num !=largest){
secondLargest=num;
}
}
return secondLargest;
}
}*/