weird output

I'm trying to solve this problem https://codeforces.com/problemset/problem/1360/C.
Here is my code:
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
  #include<bits/stdc++.h>
#include<math.h>
using namespace std;

int main(){
	int i, j, x, y, t, n, p1, p2, a[50], u;
	cin>>t;
	for(i=0;i<t;i++){
		cin>>n;
		u=0;
		p1=0;
		p2=0;
		for(j=0;j<n;j++){
			cin>>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<n;x++){
				for(y=0;y<n;y++){
					if(abs(a[x]-a[y])==1){
						cout<<"YES\n";
						u++;
						break;
					}
				}
				if(abs(a[x]-a[y])==1){
				break;
				}
			}
		}
		else
			cout<<"NO\n";
		if(p1%2==1&&p2%2==1&&u==0)
			cout<<"NO\n";
	}
	return 0;
}

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

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

To this:
1
2
3
4
5
if(abs(a[x]-a[y])==1){
	cout<<"YES"<<abs(a[x]-a[y])<<"\n"
	u++;
	break;
}

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 (<cmath>) 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.
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
#include <iostream>
#include <sstream>
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
Topic archived. No new replies allowed.