Problem Statement:
Write a function secondLargest(arr) that returns the second largest distinct number in an array.
Requirements:
- The array must contain at least two elements.
- If all elements are equal, return:
No second largest found. - If the array has fewer than two elements, return:
Array should have at least two numbers.
Examples:
Input: arr = [0, 3, 5, 2, 7, 9] → Output: 7
Input: arr = [4, 4, 4, 4] → Output: No second largest found
Input: arr = [5] → Output: Array should have at least two numbers
Input: arr = [10, 20] → Output: 10
Constraints:
Time Complexity: O(n) — Single pass through the array.
Space Complexity: O(1) — Constant space.
Approach:
- Check array length. If fewer than 2 elements, return appropriate message.
- Use two variables:
firstLargestandsecondLargest. - Ignore duplicates by checking
num !== firstLargestbefore assigningsecondLargest.
Visualisation:
function secondLargest(arr) {
if (arr.length < 2) return "Array should have at least two numbers";
let first = -Infinity, second = -Infinity;
for (let num of arr) {
if (num > first) {
second = first;
first = num;
} else if (num > second && num !== first) {
second = num;
}
}
return second === -Infinity ? "No second largest found" : second;
}
console.log(secondLargest([0, 3, 5, 2, 7, 9])); // 7
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
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, 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));
}
}
#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;
}
#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;
}
using System;
class Program {
static string SecondLargest(int[] arr, out int second) {
second = int.MinValue;
if (arr.Length < 2) return "Array should have at least two numbers";
int first = int.MinValue;
foreach (int num in arr) {
if (num > first) {
second = first;
first = num;
} else if (num > second && num != first) {
second = num;
}
}
return (second == int.MinValue) ? "No second largest found" : null;
}
static void Main() {
int[] arr = { 0, 3, 5, 2, 7, 9 };
int second;
string msg = SecondLargest(arr, out second);
if (msg != null)
Console.WriteLine(msg);
else
Console.WriteLine("Second largest: " + second);
}
}
