-
Notifications
You must be signed in to change notification settings - Fork 0
/
largerNElemsArray.cpp
58 lines (41 loc) · 1.01 KB
/
largerNElemsArray.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
//
// Created by OLX on 23/02/22.
//
#include <iostream>
#include <vector>
using namespace std;
void largerTwoElemsArray(vector<int> v) {
int first =INT_MIN, second = INT_MIN;
for(int i=0;i<v.size();i++) {
if(v[i]>first) {
second = first;
first = v[i];
} else if (v[i]>second) {
second = v[i];
}
}
cout<<first<<" "<<second<<endl;
return;
}
void largerThreeElemsArray(vector<int> v) {
int first =INT_MIN, second = INT_MIN, third = INT_MIN;
for(int i=0;i<v.size();i++) {
if(v[i]>first) {
third = second;
second = first;
first = v[i];
} else if (v[i]>second) {
third = second;
second = v[i];
} else if (v[i]>third) {
third = v[i];
}
}
cout<<first<<" "<<second<<" "<<third;
return;
}
int main() {
largerTwoElemsArray(vector<int> {1,2,3,4,4});
largerThreeElemsArray(vector<int> {1,2,1,3,4,9});
return 0;
}