-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxMin1.cpp
55 lines (52 loc) · 969 Bytes
/
maxMin1.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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct node{
int max,min;
};
node maxMin(vector<int> &v,int n){
node res;
int i;
if(n & 1){
res.max=v[0];
res.min=v[0];
i=1;
}
else{
if(v[0] > v[1]){
res.max=v[0];
res.min=v[1];
}
else{
res.max=v[1];
res.min=v[0];
}
i=2;
}
while(i<n-1){
if(v[i] > v[i+1]){
if(res.max < v[i]) res.max=v[i];
if(res.min > v[i+1]) res.min=v[i+1];
}
else{
if(res.max < v[i+1]) res.max=v[i+1];
if(res.min > v[i]) res.min=v[i];
}
i+=2;
}
return res;
}
int main()
{
int n=0,x=0;
cin >> n;
vector<int> v;
for(int i=0;i<n;i++){
cin >> x;
v.push_back(x);
}
node res=maxMin(v,n);
cout << res.max << " " << res.min;
return 0;
}