Help me in solving LUCNUM problem

My issue

In problem statement it is clearly mentioned that 1 is also a lucky number but when my code code runs with test case having total cases=1, and n=1, then it gives output 1 because it is a lucky number but the ide shows it is wrong answer and this test case has expected no output.

My code

#include <stdio.h>

int main(void) {
    int t,i=1;
    scanf("%d",&t);
    while(i<=t){
        int n,c=0,d=1,p=0;
        scanf("%d",&n);
        
       while(n>0){
           if(n%2==0){
               p++;
           }
           n=n/2;
         
       }
       
     if(p%2==0){
     printf("%d\n",d);
        
       }
       else{
           printf("%d\n",c);
       }
        
        i++;
    }
	// your code goes here
	return 0;
}


Learning course: Prepare for your DSA interviews
Problem Link: CodeChef: Practical coding for everyone

@sunnygupta9968
your logic is absolutely correct .
use long long int instead of int.
and also make cases for odd numbers separately .
like this code

#include <stdio.h>

int main(void) {
       int t;
       scanf("%d\n",&t);
       while(t--){
         long long int n;
         scanf("%lld",&n);
         int count2=0;
         if (n%2==1){
             printf("1\n");
         }
         else {
             while (n%2==0){
                 n=n/2;
             count2++;
             }
             if (count2%2==0){
                 printf("1\n");
             }
             else {
                 printf("0\n");
             }
         }
       }
	return 0;
}

Hey there are two issues, one which is mentioned earlier – the use of a bigger data type.
The second issue is the erroneous counter for odd numbers. For eg: Let’s take the number 13
Now according to your logic, it would first enter the loop, now as 13%2!=0, p wouldn’t increment. Now it would go through the statement n=n/2, which mean 13 would become 6.
And here comes the error, in reality 13 has only 2^0 in it’s prime factorization but according to your logic , p would be incremented once more ( as 6%2==0).
So now p=1, would not be divisible by 2. And hence 13 would give an output 0 where the correct output should’ve been 1.

Here is the corrected code. Making the number half, only when it is divisible else you break the loop

#include <stdio.h>


int main() {
    int t,i=0;
    scanf("%d",&t);
    while(i<t){
        long long n,c=0,d=1,p=0;
        scanf("%lld",&n);
        
       while(n>0){
           if(n%2==0){
               n=n/2;  //shifting it here
               p++;
           }
           
           else{
               break; //breaking the loop if it is odd
           }
         
       }
       
     if(p%2==0){
         printf("%d\n",d);
        
       }
       
     else{
           printf("%d\n",c);
       }
        
        ++i;
    }
	// your code goes here
	return 0;
}

Have also changed the data type. Hope it helps🐻

1 Like

Thankyou for your explanation with a test case. I have done the the problem with the help of your suggestion.

1 Like