-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecondlargestArray.cpp
38 lines (38 loc) · 1.07 KB
/
secondlargestArray.cpp
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
35
36
37
38
#include<bits/stdc++.h>
using namespace std;
// int secLargest(int arr[],int n){
// //1 pass finding largest number
// int largest=INT_MIN; better approach
// for(int i=0;i<n;i++){
// if(arr[i]>largest){
// largest=arr[i];
// }
// }
// //2 pass finding second largest number
// int secMax=-1;
// for(int i=0;i<n;i++){
// if(arr[i]>secMax && arr[i]<largest){
// secMax=arr[i];
// }
// }
// return secMax;
// }
int secLargest(int arr[],int n){
int largest=0;
int slargest=-1; //optimal solution
for(int i=0;i<n;i++){
if(arr[i]>largest){
slargest=largest;
largest=arr[i];
}
else if(arr[i]<largest && arr[i]>slargest){
slargest=arr[i];
}
}
return slargest;
}
int main(){
int arr[]={1,4,6,2,3,7};
int n=sizeof(arr)/sizeof(arr[0]);
cout<<"Second Largest number of the array : "<<secLargest(arr,n)<<endl;
}