Problem Statement
Write a function secondLargest(arr) that takes an array of numbers and returns the second largest unique number in the array.
Approach
- Check array length:
- If the array contains fewer than 2 elements, return “Array should have at least two numbers”.
- Initialize two variables:
- firstLargest as the smallest possible value (e.g., -∞ or Integer.MIN_VALUE).
- secondLargest as the smallest possible value.
- Iterate through the array:
- For each element
num:- If
numis greater thanfirstLargest:- Update
secondLargesttofirstLargest. - Update
firstLargesttonum.
- Update
- Else if
numis greater thansecondLargestand not equal tofirstLargest:- Update
secondLargesttonum.
- Update
- If
- For each element
- Result:
- Return
secondLargest(even if it is-Infinity).
- Return
Constraints
Examples
- Input: [0, 3, 5, 2, 7, 9]
Output: 7 - Input: [5]
Output: Array should have at least two numbers
function secondLargest(arr) {
if (arr.length < 2) {
return "Array should have at least two numbers";
}
let firstLargest = -Infinity;
let secondLargest = -Infinity;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > firstLargest) {
secondLargest = firstLargest;
firstLargest = arr[i];
} else if (arr[i] > secondLargest && arr[i] != firstLargest) {
secondLargest = arr[i];
}
}
return secondLargest;
}
let arr = [0, 3, 5, 2, 7, 9];
console.log(secondLargest(arr));
#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 firstLargest = INT_MIN, secondLargest = INT_MIN;
for (int i = 0; i < n; i++) {
if (arr[i] > firstLargest) {
secondLargest = firstLargest;
firstLargest = arr[i];
} else if (arr[i] > secondLargest && arr[i] != firstLargest) {
secondLargest = arr[i];
}
}
if (secondLargest == INT_MIN) return "No second largest found";
return to_string(secondLargest);
}
int main() {
int arr[] = {0, 3, 5, 2, 7, 9};
int n = sizeof(arr) / sizeof(arr[0]);
cout << secondLargest(arr, n) << endl;
return 0;
}
#include <stdio.h>
#include <limits.h>
const char* secondLargest(int arr[], int n, int *secondLargest) {
if (n < 2) return "Array should have at least two numbers";
int firstLargest = INT_MIN;
*secondLargest = INT_MIN;
for (int i = 0; i < n; i++) {
if (arr[i] > firstLargest) {
*secondLargest = firstLargest;
firstLargest = arr[i];
} else if (arr[i] > *secondLargest && arr[i] != firstLargest) {
*secondLargest = arr[i];
}
}
if (*secondLargest == INT_MIN) return "No second largest found";
return NULL;
}
int main() {
int arr[] = {0, 3, 5, 2, 7, 9};
int n = sizeof(arr) / sizeof(arr[0]);
int second;
const char* msg = secondLargest(arr, n, &second);
if (msg) printf("%s\n", msg);
else printf("%d\n", second);
return 0;
}
public class SecondLargest {
public static String secondLargest(int[] arr) {
if (arr.length < 2) return "Array should have at least two numbers";
int firstLargest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
for (int num : arr) {
if (num > firstLargest) {
secondLargest = firstLargest;
firstLargest = num;
} else if (num > secondLargest && num != firstLargest) {
secondLargest = num;
}
}
if (secondLargest == Integer.MIN_VALUE) return "No second largest found";
return String.valueOf(secondLargest);
}
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 = second = float('-inf')
for num in arr:
if num > first:
second = first
first = num
elif num > second and num != first:
second = num
return second
arr = [0, 3, 5, 2, 7, 9]
print(second_largest(arr))
