### weird output I'm trying to solve this problem https://codeforces.com/problemset/problem/1360/C.
Here is my code:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` `````` #include #include using namespace std; int main(){ int i, j, x, y, t, n, p1, p2, a, u; cin>>t; for(i=0;i>n; u=0; p1=0; p2=0; for(j=0;j>a[j]; if(a[j]%2==1) p1++; else p2++; } if(p1%2==0&&p2%2==0) cout<<"YES\n"; else if(p1%2==1&&p2%2==1){ for(x=0;x

So if the input is:
 ``123`` ``````1 6 1 6 3 10 5 8``````

The output is "NO" when it is supposed to be "YES".
But if I change this:
 ``12345`` ``````if(abs(a[x]-a[y])==1){ cout<<"YES\n" u++; break; }``````

To this:
 ``12345`` ``````if(abs(a[x]-a[y])==1){ cout<<"YES"<

The output is "YES1".
Why does this happen?
Edit: on GNU G++11 the ouptut is "YES" but on GNU G++14 and GNU G++17 the output is "NO".
Last edited on I don't see it but you probably have an undefined behavior in one of the oddball comparisons. Maybe you should use the integer abs, and not the floating point abs
https://www.cplusplus.com/reference/cstdlib/abs/
https://www.cplusplus.com/reference/cmath/abs/

 Since C++11, additional overloads are provided in this header () for the integral types: These overloads effectively cast x to a double before calculations (defined for T being any integral type).

Your result might be so close to 1.0 that it always ends up printing as 1, but any difference at all (which is entirely possible when doing any floating point operation, even on cast integers) means that the result might fail an == 1 test.

Try your own integer only abs() function - it's dead easy to write one. ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include #include using namespace std; istringstream in { R"(7 4 11 14 16 12 2 1 8 4 1 1 1 1 4 1 2 5 6 2 12 13 6 1 6 3 10 5 8 6 1 12 3 10 5 8 )"}; const int MaxVal = 100; int main() { //auto& in = std::cin; int t; in >> t; // 1 <= t <= 1000 while (t-- > 0) { int n; in >> n; // 2 <= n <= 50 (and even) bool seen[MaxVal] {}; int odd = 0; for (int i = 0; i < n; ++i) { int val; in >> val; // 1 <= val <= 100 odd += val % 2; seen[val - 1] = true; } if (odd % 2 == 0) cout << "YES\n"; else { int i = 0; for ( ; i < MaxVal-1 && (!seen[i] || !seen[i + 1]); ++i) ; cout << (i < MaxVal-1 ? "YES\n" : "NO\n"); } } }``````

Last edited on