C++ Program to Find the Second Largest Number in an Array (C++) with Explanation
C++
Hard
Variables
34 views
2 min read
364 words
This problem helps you practice core C++ fundamentals in a practical way. It builds intuition around second, largest, will. Let’s break it down step by step so you can implement it confidently.
c++
array
loops
logic
hard
Problem Statement
In this problem, you will write a C++ program to find the second largest number in an array.
You will take N numbers as input from the user and store them in an array.
Then, using variables and loops, you will find the largest and the second largest value.
This question helps you practice arrays, loops, conditions, and careful logic in C++.
Input Format
First line contains an integer N (size of array).
Second line contains N space-separated integers.
Output Format
Print the second largest number.
If not possible, print a message.
Constraints
2 ≤ N ≤ 10^5
-10^9 ≤ array[i] ≤ 10^9
Concept Explanation
In C++, an array is used to store multiple values of the same type.
To find the second largest number, we must understand the difference between largest and second largest values.
First, we keep track of the largest number using a variable.
Then, we keep another variable for the second largest number.
While looping through the array, we compare each element.
If a number is greater than the largest,
then the old largest becomes the second largest.
If a number is smaller than the largest but greater than the second largest,
then it becomes the new second largest.
This logic must be handled carefully.
Duplicate values and small array sizes are important edge cases.
This type of question is common in coding tests and interviews.
Step-by-Step Logic
Step 1: Understand the goal: find the second largest number.
Step 2: Choose the main idea: array + loop + comparison.
Step 3: Take input size N and array elements.
Step 4: Initialize largest and secondLargest.
Step 5: Loop through the array.
Step 6: Update largest and secondLargest using conditions.
Step 7: Print the second largest value.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
#include <iostream>
#include <climits>
using namespace std;
int main() {
int n;
cout << "Enter size of array: ";
cin >> n;
if (n < 2) {
cout << "Second largest number not possible";
return 0;
}
int arr[n];
cout << "Enter array elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
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];
}
}
if (secondLargest == INT_MIN) {
cout << "Second largest number not found";
} else {
cout << "Second Largest Number: " << secondLargest;
}
return 0;
}
Output Example
Output:
Second Largest Number: 15
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Notes & Extra Practice
Examples
Array: 5 3 9 1 6
Largest: 9
Second Largest: 6
Solution Guide
Tags
c++
array
loops
logic
hard
Problem
In this problem, you will write a C++ program to find the second largest number in an array.
You will take N numbers as input from the user and store them in an array.
Then, using variables and loops, you will find the largest and the second largest value.
This question helps you practice arrays, loops, conditions, and careful logic in C++.
Input / Output
Input
First line contains an integer N (size of array).
Second line contains N space-separated integers.
Output
Print the second largest number.
If not possible, print a message.
Constraints
2 ≤ N ≤ 10^5
-10^9 ≤ array[i] ≤ 10^9
Examples
Output:
Second Largest Number: 15
Array: 5 3 9 1 6
Largest: 9
Second Largest: 6
Explanation
Concept Explanation
In C++, an array is used to store multiple values of the same type.
To find the second largest number, we must understand the difference between largest and second largest values.
First, we keep track of the largest number using a variable.
Then, we keep another variable for the second largest number.
While looping through the array, we compare each element.
If a number is greater than the largest,
then the old largest becomes the second largest.
If a number is smaller than the largest but greater than the second largest,
then it becomes the new second largest.
This logic must be handled carefully.
Duplicate values and small array sizes are important edge cases.
This type of question is common in coding tests and interviews.
Step-by-Step Explanation
Step 1: Understand the goal: find the second largest number.
Step 2: Choose the main idea: array + loop + comparison.
Step 3: Take input size N and array elements.
Step 4: Initialize largest and secondLargest.
Step 5: Loop through the array.
Step 6: Update largest and secondLargest using conditions.
Step 7: Print the second largest value.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Official Solution
#include <iostream>
#include <climits>
using namespace std;
int main() {
int n;
cout << "Enter size of array: ";
cin >> n;
if (n < 2) {
cout << "Second largest number not possible";
return 0;
}
int arr[n];
cout << "Enter array elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
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];
}
}
if (secondLargest == INT_MIN) {
cout << "Second largest number not found";
} else {
cout << "Second Largest Number: " << secondLargest;
}
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!