-
Notifications
You must be signed in to change notification settings - Fork 11
/
CS_19_FindSecondLargestAndSecondSmallest.cpp
63 lines (59 loc) · 1.37 KB
/
CS_19_FindSecondLargestAndSecondSmallest.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
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
#include <bits/stdc++.h>
using namespace std;
int getSecondLargest(vector<int> &arr, int n)
{
int largest = arr[0];
int slargest = INT_MIN;
for (int i = 1; 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 getSecondSmallest(vector<int> &arr, int n)
{
int smallest = arr[0];
int ssmallest = INT_MAX;
for (int i = 1; i < n; i++)
{
if (arr[i] < smallest)
{
ssmallest = smallest;
smallest = arr[i];
}
else if (arr[i] != smallest && arr[i] < ssmallest)
{
ssmallest = arr[i];
}
}
return ssmallest;
}
vector<int> getSecondOrderElements(int n, vector<int> a)
{
int slargest = getSecondLargest(a, n);
int ssmallest = getSecondSmallest(a, n);
return {slargest, ssmallest};
}
int main()
{
cout << "Enter the number of elements in the array: ";
int n;
cin >> n;
cout << "Enter the elements of the array: ";
vector<int> a(n);
for (int i = 0; i < n; ++i)
{
cin >> a[i];
}
vector<int> ans = getSecondOrderElements(n, a);
cout << "Second largest: " << ans[0] << ", Second smallest: " << ans[1] << endl;
return 0;
}