Tried the implementation as per editorial, still getting WA for test case 4. Not sure what I’m doing wrong.
Code link: CodeChef: Practical coding for everyone
Tried the implementation as per editorial, still getting WA for test case 4. Not sure what I’m doing wrong.
Code link: CodeChef: Practical coding for everyone
Your code is failing following test case type :-
1
4 4
2 5 5 9
your code (O/P) :- 0 0 2 9
Correct O/P :- 0 0 0 11
you first loop is running till ( i < n-1 ) change it to ( i < n )
while( 1 )
{
if( x == 0 )
break;
while( i < n && a[ i ] == 0 ) // here change to ( i < n ) from ( i < n-1)
i++;
if( i == n - 1 )
break;
```
yep I forgot to add this
if(( x % 2 && n == 2 )) // in this conditon if we have any odd value of x and n==2 we need to apply xor on last two
{
a[ n - 1 ] ^= 1;
a[ n - 2 ] ^= 1;
}
It will work fine after those changes see here :- CodeChef: Practical coding for everyone
That worked, thanks for the help.
welcome