-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathSecond_largest.cpp
More file actions
71 lines (68 loc) · 1.77 KB
/
Second_largest.cpp
File metadata and controls
71 lines (68 loc) · 1.77 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* Naive Solution to find Second largest element */
#include<iostream>
using namespace std;
//Function finds largest element
int getlargest(int arr[], int n)
{
int max = 0;
for (int i = 0; i < n; i++)
{
/* code */
if(arr[i]> arr[max])
max = i;
}
return max;
}
/*function check for second largest element and returns index of the element */
int getseclargest(int arr[], int n)
{
int largest = getlargest(arr, n);
int result = -1;
for (int i = 0; i<n; i++)
{
if(arr[i] != arr[largest])
{
if(result == -1)
result = i;
else if(arr[i] > arr[result])
result = i;
}
}
return result;
}
/* Efficient Method */
int get_second_largest(int arr[], int n)
{
int sec_largest = -1, largest = 0;
for (int i = 1; i < n; i++)
{
if (arr[i] > arr[largest])
{
sec_largest = largest;
largest = i;
}
else if (arr[i] != arr[largest])
{
if (sec_largest == -1 || arr[i] > arr[sec_largest])
{
sec_largest = i;
}
}
}
return sec_largest;
}
int main()
{
int arr[]={3, 5, 7, 6, 3, 9, 8};
int x;
cout<<"Want to solve with Naive Method: Press 1, \n Want an efficient solution: Press 2"<<endl;
cin>>x;
int s_largest = 0;
switch(x){
case 1: s_largest = getseclargest(arr, 7);
cout<<"Second Largest is: "<<arr[s_largest]<<"\n";break;
case 2: s_largest = get_second_largest(arr, 7);
cout<<"Second Largest Using Efficient method: "<<arr[s_largest]<<"\n"<<endl;break;
default: cout<<"Select correct key\n";
}
}