Showing time limit error in 4th test cse

#include
using namespace std;
void swap(int *arr,int n)
{int max=arr[0];
int j=0;
for(int i=0;i<n;i++)
{
if(arr[i]>=max)
{
max=arr[i];
j=i;
}
}
if(j!=0)
{
int k=arr[0];
arr[0]=max;
arr[j]=k;
}
if(j==0)
{
swap(arr+1,n-1);
}

}

int main() {
int n,k;
cin>>n>>k;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
for(int i=0;i<k;i++)
{
swap(arr+i,n-i);

}
for(int i=0;i<n;i++)
{
 cout<<arr[i]<<" ";
}
return 0;

}

your complexity is O(n*k). which wll give a tle.

try reducing your complexity.
it can be done only in O(n)

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.