Posand codechef long

Question link:-https://www.codechef.com/OCT20B/problems/POSAND/

solution link:-https://www.codechef.com/viewsolution/38711580

Please check the code , I was getting wrong answer for two test cases . I don’t know what I did wrong but as far as I know this code should have worked. let me know if you know the reasonn of failure.

1 Like

You are checking if N is a power of 2 first, Then the condition for n == 1 won’t work because you have already printed “-1” for it. Answer for N = 1 is 1 your code is giving -1.

Just wrong placements of conditions. :confused:

# cook your dish here
import math
 
# Function to check
# if x is power of 2
def isPowerOfTwo(n):
    return (math.ceil(math.log2(n)) ==
            math.floor(math.log2(n)));

seq = [i for i in range(1,100000)]
seq[0] = 2
seq[1] = 3
seq[2] = 1
for i in seq:
    if i <= 3:
        pass
    elif isPowerOfTwo(i-1):
        seq[i-1] = i-1
        seq[i-2] = i
            
t = int(input())
while t>0:
    t=t-1
    n=int(input())
    if n==1:
        print('1')
    elif n==3:
        print('1 3 2')
    elif isPowerOfTwo(n):
        print(-1)
    else:
        print(' '.join([str(i) for i in seq[0:n]]))

        # for i in range(1,n+1):
        #     if(i == 1):
        #         pass
        #     else:
        #         if(seq[i-1] & seq[i-2] == 0):
        #             print('anomaly' + str(seq[i-1]) + ':' + str(seq[i-2]) + 'for n=' + str(i))

My solution. Even with this, the task#3 fails. This is more than placement of conditions

if you are talking about input 1 it’s not even the test case I think I by mistake removed it but before that also (when It was printing 1 for input 1)it was showing the same error .

PS:- I am getting error in test case of original constraint (I got 50 points for this solution )

can you tell any test cases in which my code fails to give correct solution?

Partial

If you are talking about this solution, It is failing because your code is giving “-1” on when N==1, but correct answer is “1” at input “1”.

Correct

Which you have corrected here.

The mistake which you made was in this part:

		if (a != 0 && (a & (a - 1)) == 0)
	{
		cout << -1 <<endl;
		continue;
		
	}
	else if (a == 1)
	{
		cout << 1 <<endl;
		continue;
	}

Here you are checking if N is power of 2, So 1 is basically power of 2 so it will cout “-1”

But in correct solution you are checking:

    int n;
    cin >> n;
    if (n == 1)
    {
        cout << 1 << endl;
        continue;
    }

You are checking if n is 1 then you are doing cout “1” which is correct.

You can see by giving input 1 in both partial and correct solutions.

I used your correct solution with N = 1: https://ideone.com/QSUO63
I used your partial solution with N = 1: https://ideone.com/Y3grUf

Getting different values for N = 1.

This problem has contradictory constraints. In the first place, they mention 1<=i<N. Which clearly implies N>1. In the bottom, they mention N>=1. This is not just contradictory but also wrong. You can’t have a sequence of a single element if your condition requires at least 2 elements to hold true.