Skip to content

Commit d05d148

Browse files
committed
CODECHEF COOK134A CHEF7UP
1 parent e1de561 commit d05d148

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

Codechef solutions/CHEF7UP.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
5+
int main() {
6+
ios_base::sync_with_stdio(false);
7+
8+
int t = 1;
9+
cin >> t;
10+
while(t--) {
11+
int n;
12+
cin >> n;
13+
int x; cin >> x;
14+
vector<int> v(n);
15+
for(auto &i : v) cin >> i;
16+
17+
sort(v.begin(), v.end());
18+
19+
if (x > v.back()) {
20+
int ans = 0;
21+
for(int j = n-1;j>=0;j--) {
22+
if(abs(v[j] - x)%2 == 1) ans++;
23+
else break;
24+
}
25+
cout << ans << " " << (ans == n ? 1: -1) << '\n';
26+
}
27+
else if (x < v.front()) {
28+
int ans = 0;
29+
for(int j = 0;j<n;j++){
30+
if(abs(v[j] - x)%2 == 1) ans++;
31+
else break;
32+
}
33+
cout << ans << " " << (ans == n ? 1: -1) << '\n';
34+
}
35+
else{
36+
int i = -1, j = -1;
37+
for(int k = 0;k<n;k++) {
38+
if (v[k] < x) i = k;
39+
if (v[k] > x and j == -1)j = k;
40+
}
41+
int ans =0;
42+
43+
while(i >= 0 || j < n) {
44+
int a=0, b=0;
45+
if(i >= 0)a = abs(v[i] - x)%2;
46+
if(j < n) b =abs(v[j] - x)%2;
47+
if(a == 0 and b== 0)break;
48+
49+
if (a == 1) {
50+
i--;
51+
ans++;
52+
}
53+
if (b == 1) {
54+
j++;
55+
ans++;
56+
}
57+
}
58+
cout << ans << " " << (ans == n ? 1: -1) << '\n';
59+
60+
}
61+
62+
}
63+
64+
65+
}

0 commit comments

Comments
 (0)