Second Largest Element in an Array
Problem Statement:β
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.
-
Example:
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.
β Solution: Brute Force β Sorting β One-Pass Linear Scanβ
β Solution 1: Brute Forceβ
int findSecondLargest(int n, vector<int> &arr) {
int maxEle = INT_MIN;
// First, find the largest element
for (int i = 0; i < n; i++) {
maxEle = max(maxEle, arr[i]);
}
int secondLargest = INT_MIN;
// Now, find the largest element that is not equal to maxEle
for (int i = 0; i < n; i++) {
if (arr[i] != maxEle) {
secondLargest = max(secondLargest, arr[i]);
}
}
return secondLargest == INT_MIN ? -1 : secondLargest;
}
β Solution 2: Sorting Approachβ
int findSecondLargest(int n, vector<int> &arr) {
sort(arr.begin(), arr.end()); // Sort ascending
int largest = arr[n - 1];
// Traverse from second last and return first smaller element
for (int i = n - 2; i >= 0; i--) {
if (arr[i] != largest) {
return arr[i];
}
}
return -1; // All elements are equal
}
β Solution 3: One-Pass Optimal (Best Approach)β
int findSecondLargest(int n, vector<int> &arr) {
int Largest = INT_MIN;
int secondLargest = INT_MIN;
for (int i = 0; i < n; i++) {
if (arr[i] > Largest) {
secondLargest = Largest;
Largest = arr[i];
}
else if (arr[i] > secondLargest && arr[i] != Largest) {
secondLargest = arr[i];
}
}
return secondLargest == INT_MIN ? -1 : secondLargest;
}
π How It Worksβ
- Brute Force: Find the max first, then find the best candidate not equal to max.
- Sorting: Sort the array and look for the first number less than the largest.
- Optimal Approach: Traverse once while tracking both the largest and second largest values.
π§© Key Formulaβ
- For every element
x:- if
x > Largest, thensecondLargest = Largest,Largest = x - else if
x > secondLargest && x != Largest, updatesecondLargest
- if
β±οΈ Time & Space Complexityβ
| Approach | Time | Space |
|---|---|---|
| Brute Force | O(2N) | O(1) |
| Sorting | O(N log N) | O(1) or O(N) |
| Optimal β | O(N) | O(1) |
β οΈ Edge Casesβ
- All elements are the same β return
1 - Only one element β return
1 - Negative values β Works fine
- Duplicates allowed β Second largest must be strictly smaller than largest
π‘ Other Approachesβ
| Approach | Time | Notes |
|---|---|---|
| Heap (min-heap) | O(N log K) | Overkill for just 2 largest elems |
π Related Problemsβ
- GFG: Second Largest Element in Array β
- LeetCode 1980: Find Second Highest Salary (SQL)
- GFG: Second Smallest Element
- LeetCode: Third Maximum Number
π οΈ Other Notesβ
- β Prefer the one-pass linear solution for interviews.
- β οΈ Avoid sorting for such questions unless asked explicitly.
- β You can easily extend the logic to find third largest, k-th largest, etc.
π¬