Why is this giving Time Limit Exceeded


#1

#include<bits/stdc++.h>
using namespace std;

int main()
{
string A;
string temp="";
while(cin>>A)
{
int rem=0;
while(true)
{
// cout<<“vaps andar”<<endl;
int i=0;
while(i<A.length())
{
if(rem==1)
{
// cout<<"when rem ==1 "<<endl;
int number = 10+(A[i]-‘0’);
if(number%2==0)
{
rem=0;
char c = number/2 + ‘0’;
// cout<<c<<endl;
temp=temp+c;
}
else
{
rem=1;
char c = number/2 + ‘0’;
// cout<<c<<endl;
temp=temp+c;
}
}
else
{
//cout<<“when rem==0”<<endl;
if(A[i]==‘0’)
{
temp=temp+‘0’;
rem=0;
}
else if(A[i]>=‘2’)
{
int number = A[i]-‘0’;
if(number%2==0)
{
rem=0;
char c = number/2 + ‘0’;
// cout<<c<<endl;
temp=temp+c;
}
else
{
rem=1;
char c = number/2 + ‘0’;
//cout<<c<<endl;
temp=temp+c;
}
}
else if(A[i]==‘1’)
{
temp=temp+‘0’;
rem=1;
}
}
++i;
//cout<<“temp is”<<temp<<endl;
//cout<<“rem is”<<rem<<endl;
}

  A.clear();
  int p=0;

  while(temp[p]=='0')
    {

      ++p;
    }
  
  while(p<temp.length())
  {
    A=A+temp[p];
    ++p;
  }

  temp.clear();

  if(rem)
   {
     cout<<"Not a power of 2";
     break;
   }
  if(A=="1" && rem==0)
    {
      cout<<"powe of two"<<endl;
      break;
    }

}
}
return 0;
}


#2

Use vector instead of string
s=s+c takes O(n) time
push_back operation in vector takes O(1) time


#3

s=s+c also takes O(1) time. It is IB problem