0

I was solving https://www.codechef.com/COOK74/problems/TALAZY

First submission (Wrong answer): https://www.codechef.com/viewsolution/11557512

Second submission (Accepted): https://www.codechef.com/viewsolution/11560985

The change was changing

if(n % 2){ ...

to

if(n & 1){ ...

Does it make the difference?

Edit: After people pointed out the change about 'long' to 'long long', I have edited the question.

Shridhar R Kulkarni
  • 6,653
  • 3
  • 37
  • 57
  • 2
    Please refrain asking questions about online code judge engines here. It's very unlikely that anyone could tell you where you failed from their test cases, as these aren't disclosed usually. Even if what you tested was running at your local environment, you may have missed to test some edge cases which are applied in the online challenge. Be creative and try to find them. Additionally there's probably no value for such questions in the long term, other than cheating the online contest, and nothing is learned. – πάντα ῥεῖ Sep 19 '16 at 09:36
  • The code is same in both cases. So I don't think there is any issue of missing any test cases. My focus is why the change in operator works? both statements work the same. – Shridhar R Kulkarni Sep 19 '16 at 09:38
  • 1
    You can get rid of the test and write `x = (n+1) >> 1;` – Dominique Lorre Sep 19 '16 at 09:40
  • Nice one. Thanks ! – Shridhar R Kulkarni Sep 19 '16 at 09:41
  • 1
    There are several change between the 2 codes. – Jarod42 Sep 19 '16 at 09:56
  • Consider moving the examples into the question body, so that people don't have to risk clicking links to external sites which may contain malware, become unavailable etc. –  Sep 19 '16 at 09:56
  • How do I move the examples into the question body? – Shridhar R Kulkarni Sep 19 '16 at 10:00
  • As of the down-vote, should the question be flagged or deleted by me? – Shridhar R Kulkarni Sep 19 '16 at 10:13

3 Answers3

1

Despite what the online judge says, the modulus approach is better by a country mile. Although pre C++11, the sign of any remainder is implementation defined, this is not relevant when testing that there's no remainder at all. Added to that, from C++11 onwards the sign of the remainder, if any, must match the signed of the input n.

The flashier n & 1 is implementation-defined for negative n: it depends on whether your platform uses 1's complement, 2's complement or signed-magnitude for signed integral types.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

In your wrong answer submission n,m and b are declared as "long" and in the other one it is declared as "long long" , whereas the limit given for them is 10^8. The range of long is platform dependent for more reference u can check "What's the difference between long long and long". Try changing it to long long.

Community
  • 1
  • 1
Ashu
  • 185
  • 5
0

The problem in your solution isn't the way you try to figure out whether a number is odd or even.

The problem in a overflow.

In our second solution you changed the value type of counters n,b,m and x from long to long long. This makes the difference. Indeed you can easily verify also changing the odd/even code in you second solution with the approach you used in your first attempt.

It will return the same results

GrayMouser
  • 11
  • 2