I am not getting right answer . what is mistake?

#include
#include
#define int long long int
using namespace std;

void unlock(int arr[],int n, int k){
for(int i=1; i<=k; i++){
for(int j=1; j<n; j++){
if(arr[j-1] < arr[j] ){
int mEle = *max_element(arr+j, arr+n);
swap(arr[j-1],mEle);
}
}
}
return;
}

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

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

return 0;
}

hello @Mr_Sidd
pls share ur code using cb ide

I don’t know how to share with cb ide

@Mr_Sidd

paste ur code here ->https://ide.codingblocks.com/

and then press ctrl + s and save,
a link will be generated in ur search bar,
share that link with me

@Mr_Sidd

image

here for each k u r running n times which is not correct(because u r performing n*k operations).

remove the outer loop .
inside inner loop.
perform swapping only when k>0 and do k–.

still im not desired ans

check now->

u need to optimise it furthur using hasing.

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.