Easy method for this ques


#1

first from function between i got the where our count exist like (here j is fix to 0)
3,4 exist when 101 i.e i=2 and j=0
5 and 6 1001 i=3,j=0
7,8,9,10 are in range 10001 i=4 j=0
so when we sub the the no of count
like till 1111 count =6
so 7= 10001
8=10101
here notice that the element between i and j are palindrome
so if the no of element is even
0000
next palindrome 0110 here w divide this like n/2 first part
that will be
first second
part part
00 00
01 10
10 01
11 11
and these first part are nothing but 0 1 2 3 so just when counting first part just mirror image them
for odd
00 0 00 0
00 1 00 1
01 0 10 2
01 1 1 0 3
10 0 0 1 4
same till all possible for i=6 j=0

int between(int i,int j)
{
int n=i-j-1;
return pow(2,(n+1)/2);
}
int Solution::solve(int A) {
vector ans(31);
if(A==1)return 1;
int i=1;
int j=0;
int count=0;
int temp=1;
int val=0;
while(temp<A)
{
temp+=between(i,j);
//cout<<temp<<endl;
if(temp>=A){break;}
i++;
count=temp;
}
ans[i]=1;
ans[j]=1;
if(A==temp)
{
for(int k=0;k<=i;++k)
{
val=val*2+1;
}
return val;
}
//cout<<count<<" "<<temp<<endl;

    temp=(A-count)-1;
    int n=(i-j)/2;
    int k=0;
    int a=0;
    //cout<<n<<endl;
    if((i-j-1)%2!=0)
    {
        ans[n]=temp%2;
        temp=temp/2;
        a=0;
        k=1;
    }
    else{a=1;}
    //cout<<temp<<endl;
    while(temp>0&&n+a+k<i)
    {
        ans[n+a+k]=temp%2;
        ans[n-k]=temp%2;
        k++;
        temp=temp/2;
        //cout<<temp<<endl;
    }
for(int k=0;k<=i;++k)
{
    val=val*2+ans[k];
}
return val;

}