My code is giving TLE although i have not nested any loop for Unlock problem

#include <bits/stdc++.h>
using namespace std;
int main()
{
int input;
cin>>input;
int arr[input];
int xhor=0;
for(int i=0;i<input;i++)
{
cin>>arr[i];
xhor=xhor^arr[i];
}
int temp=xhor;
int i=0;
while(xhor>0)
{
int temp=(xhor&1);
if(temp>0)
break;
i++;
xhor=xhor>>1;
}
int mask=1<<i;
int lor=0;
for(int i=0;i<input;i++)
{
if(((arr[i])&mask)>0)
lor=lor^arr[i];
}
int b=lor^xhor;
if(lor<b)
cout<<lor<<" “<<b<<endl;
else
cout<<b<<” "<<lor<<endl;

}

Hey @aayuushh are you sure that you posted this under right question? Because solution of the “unlock” problem doesn’t require xor or anything like that.

Hi sorry i posted the wrong code for unlock problem
this is the code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll,ll> mp;
int search(ll input)
{
auto it=mp.begin();
while(it!=mp.end())
{
if(it->second==input)
{
return it->first;
}
it++;
}
}
int main()
{
ll n,k;
cin>>n>>k;
if (k>n)
{
for(int i=n;i>=1;i–)
{
cout<<n<<" ";
}
}
else
{

for(ll i=1;i<=n;i++)
{
	cin>>mp[i];
	
}
int temp=n;
int j=1;
while(k>0)
{
	if(mp[j]==temp)
	{
		++j;
		temp=temp-1;
		
	}
	else
	{
		ll a=mp[j];
		ll l=search(temp);
		mp[j]=temp;
	mp[l]=a;
		++j;
		temp=temp-1;
		--k;
	}
	
}
auto it=mp.begin();
while(it!=mp.end())
{
	cout<<(it->second)<<" ";
	it++;
}

}
}

This code is giving wrong answer in test case 0 but passes test case 1 2 3

Hey it is giving tle because in some cases it is possible to make largest permutation before k swaps in that case while(k < 0) would never satisfy please try while (j < n) because j always increases in an iteration.

Actually I have given the changed code now it is not giving tle but giving wrong answer in test case 0 while it passes test case 1 ,2,3

Oh in that case you’ll have to change if(k > n) block, to if(k >= n) and if that condition is true print i from n to 1 instead of kust printing n, n times.
So,
if (k>n)
{
for(int i=n;i>=1;i–)
{
cout<<n<<" ";
}
}

will change to
if (k>=n)
{
for(int i=n;i>=1;i–)
{
cout<<i<<" ";
}
}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.