forked from Vishal-Aggarwal0305/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecondLargest.cpp
More file actions
34 lines (31 loc) · 769 Bytes
/
SecondLargest.cpp
File metadata and controls
34 lines (31 loc) · 769 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;
int main(){
int n, largest_num, second_largest;
cout<<"Enter number of elements: ";
cin>>n;
int num[n];
cout<< "Enter the elements of array:";
for(int i=0; i<n; i++){
cin>>num[i];
}
if(num[0]<num[1]){
largest_num = num[1];
second_largest = num[0];
}
else{
largest_num = num[0];
second_largest = num[1];
}
for (int i = 2; i< n ; i ++) {
if (num[i] > largest_num) {
second_largest = largest_num;
largest_num = num[i];
}
else if (num[i] > second_largest && num[i] != largest_num) {
second_largest = num[i];
}
}
cout<<"Second Largest Element in array is: "<<second_largest;
return 0;
}