getting TLE for NEG2

link:
http://www.spoj.pl/problems/NEG2/
i tried the following solution for the above problem but i am getting TLE please help

#include<iostream>
using namespace std;
void convert(int n)
{
     string s="";
     if(n==0)
     {
             printf("0\n");
             return;
     }
     int i=-2;
     
     while(n)
     {
             if(n%i<0)
             {
             
             s=(char)(((n%i)+2)+'0')+s;
             n=(n/i)+1;
             }
             else
             {
             s=(char)((n%i)+'0')+s;
             n=n/i;
             }
     
     }
    cout<<s;
     cout<<"\n";
     
}
int main()
{
    int t;
    scanf("%d",&t);
    int n;
    while(t--)
    {
              scanf("%d",&n);
              convert(n);
              
    }

}
1 Like

There is a small mistake in your code .You just have to remove/Comment two lines -

    int t;
    //scanf("%d",&t);/*COMMENT THIS LINE */
	int n;
	//while(t--)     /*COMMENT THIS LINE */
	{
			  scanf("%d",&n);
			  convert(n);

	}

}

The Reason For TLE is - per file contains only one test case , So you don’t have to ask for the input -number of Testcases .The input contains only a single integer .

In your code ,the while loop was never terminating [and your program halts at scanf("%d",&n)]. :):slight_smile:

3 Likes