Question: Find the Second Largest Number in an Array
Write a function secondLargest(arr) that takes an array of numbers and returns the second largest unique number in the array.
Requirements
- The array must contain at least two numbers.
- If the array contains all equal elements or only one unique element, return:
"No second largest found" - If the array has less than two elements, return:
"Array should have at least two numbers"
Constraints
- Time Complexity:
O(n)(Single pass through the array) - Space Complexity:
O(1)
Examples
Input: [0, 3, 5, 2, 7, 9]
Output: 7
Input: [4, 4, 4, 4]
Output: "No second largest found"
Input: [5]
Output: "Array should have at least two numbers"
Input: [10, 20]
Output: 10
Approach
- Check array length. If fewer than 2 elements, return appropriate message.
- Initialize
firstLargestandsecondLargestto smallest possible values. - Iterate through array and update values accordingly:
- If current number >
firstLargest: updatesecondLargestthenfirstLargest. - Else if current number >
secondLargestand !=firstLargest: updatesecondLargest. - After loop, check if
secondLargestwas updated. If not, return message.
Time & Space Complexity
- Time Complexity:
O(n)– Single pass through the array. - Space Complexity:
O(1)– Constant space used for two variables.
function secondLargest(arr) {
if (arr.length < 2) return "Array should have at least two numbers";
let first = -Infinity, second = -Infinity;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > first) {
second = first;
first = arr[i];
} else if (arr[i] > second && arr[i] !== first) {
second = arr[i];
}
}
return second === -Infinity ? "No second largest found" : second;
}
console.log(secondLargest([0, 3, 5, 2, 7, 9])); // 7
#include <stdio.h>
#include <limits.h>
const char* secondLargest(int arr[], int n, int* second) {
if (n < 2) return "Array should have at least two numbers";
int first = INT_MIN;
*second = INT_MIN;
for (int i = 0; i < n; i++) {
if (arr[i] > first) {
*second = first;
first = arr[i];
} else if (arr[i] > *second && arr[i] != first) {
*second = arr[i];
}
}
return (*second == INT_MIN) ? "No second largest found" : NULL;
}
int main() {
int arr[] = {0, 3, 5, 2, 7, 9};
int second;
const char* msg = secondLargest(arr, 6, &second);
if (msg) printf("%s\n", msg);
else printf("%d\n", second);
return 0;
}
#include <iostream>
#include <climits>
using namespace std;
string secondLargest(int arr[], int n) {
if (n < 2) return "Array should have at least two numbers";
int first = INT_MIN, second = INT_MIN;
for (int i = 0; i < n; i++) {
if (arr[i] > first) {
second = first;
first = arr[i];
} else if (arr[i] > second && arr[i] != first) {
second = arr[i];
}
}
return (second == INT_MIN) ? "No second largest found" : to_string(second);
}
int main() {
int arr[] = {0, 3, 5, 2, 7, 9};
cout << secondLargest(arr, 6) << endl;
return 0;
}
public class Main {
public static String secondLargest(int[] arr) {
if (arr.length < 2) return "Array should have at least two numbers";
int first = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;
for (int num : arr) {
if (num > first) {
second = first;
first = num;
} else if (num > second && num != first) {
second = num;
}
}
return (second == Integer.MIN_VALUE) ? "No second largest found" : String.valueOf(second);
}
public static void main(String[] args) {
int[] arr = {0, 3, 5, 2, 7, 9};
System.out.println(secondLargest(arr));
}
}
def second_largest(arr):
if len(arr) < 2:
return "Array should have at least two numbers"
first = float('-inf')
second = float('-inf')
for num in arr:
if num > first:
second = first
first = num
elif num > second and num != first:
second = num
return second if second != float('-inf') else "No second largest found"
print(second_largest([0, 3, 5, 2, 7, 9])) # 7
