-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathambitiousKid.cpp
38 lines (30 loc) · 953 Bytes
/
ambitiousKid.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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int minPositives = INT_MAX;
int maxNegatives = INT_MIN;
if (find(arr.begin(), arr.end(), 0) != arr.end()) {
cout << 0 << endl;
} else {
for (int num : arr) {
if (num > 0) {
minPositives = min(minPositives, num);
} else if (num < 0) {
maxNegatives = max(maxNegatives, num);
}
}
int stepsToZeroPositives = (minPositives != INT_MAX) ? minPositives : INT_MAX;
int stepsToZeroNegatives = (maxNegatives != INT_MIN) ? abs(maxNegatives) : INT_MAX;
int minStepsToZero = min(stepsToZeroPositives, stepsToZeroNegatives);
cout << minStepsToZero << endl;
}
return 0;
}