A doubt in CENS20D

I have submitted two codes earlier for the problem Priya and And - CENS20D,

this code got WA
for(int i=0;i<n;i++) {
	for(int j=i+1;j<n;j++) {
		if(a[i]&a[j]==a[i]) ans++;
	}
}	
this code got AC
for(int i=0;i<n;i++) {
	for(int j=0;j<n;j++) {
		if((a[i]&a[j])==a[i] and i<j) ans++;
	}
}	

please check the links to the WA code and the AC code .

Thanks in advance :slight_smile:

Same thing happened to me. Very disappointing. I ended up with 2 WAs for no reason which worsened my rank.

3 Likes

Problem was with ( ) == a[i]
I also made the same mistake

4 Likes

its the brackets that’s giving you ac. (a[i] & a[j]) == a[i]

2 Likes

== has higher preference than & operator
That’s why you got WA in first code.

4 Likes

got it. operator precedence, my bad sorry ! :slight_smile:

1 Like

So what’s your question exactly ?
If you are asking that why second one got AC then its because (i<j) was given in question and you need to put the operands of bitwise &, in round brackets.

2 Likes

This is the reason you should turn on compiler warnings. I wrote the first code, and got a warning.

My Geany Compiler Flags are:

Compile (F8):
g++ -std=c++17 -Wshadow -Wall -o "%e" "%f" -O2 -Wno-unused-result

Build (F9):
g++ -std=c++17 -Wshadow -Wall -o "%e" "%f" -g -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG

3 Likes

‘==’ is done first and then ‘&’. The problem is just with the brackets.

2 Likes

Yes but it worked for the samples. That is why I submitted the code in the first place.

2 Likes

In sample all the number were same in the array

3 Likes

Then can you explain why it worked for the sample test case?

1 Like

Just generate some test cases and check which ones have different output on the codes.

So it was intentionally done :no_mouth:

2 Likes

in the first sample all the numbers were the same and in the second one, you don’t get to the point of comparison

Read editorial, you’ll know better. :slightly_smiling_face:

1 Like

I guess so :upside_down_face:

I think first loop should be till n-1 and the brackets .
Do the dry run you will understand .
Ps: we are checking for pairs hence why should we check for last element ?

No, j has to be less than n

I said first loop brother :slight_smile: