Q-You are given an integer N. Consider the sequence containing the integers 1,2,…,N in increasing order (each exactly once). Find the length of the longest subarray in this sequence such that the bitwise AND of all elements in the subarray is positive.
i tried the following code to solve the problem but it is not working can anyone tell why or give a test case where it fails
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t,n,i,c;
cin>>t;
while (t--)
{
i = 1;
cin>>n;
while(i)
{
if(pow(2,i)<n)
{
i++;
}
else if (pow(2,i)==n)
{
break;
}
else
{
i--;
break;
}
}
if(n-pow(2,i)+1>pow(2,i)-pow(2,i-1))
{
cout<<n-pow(2,i)+1<<endl;
}
else{
cout<<pow(2,i)/2<<endl;
}
}
}