A doubt in CENS20D

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:

I know that. If i is n-1, j will be n but it should be less than n it won’t work.

My Solution : CodeChef: Practical coding for everyone
Ps: just optimising :slight_smile:

1 Like