//Sir what is the more optimized way in which I can solve this problem?

#include<bits/stdc++.h>
using namespace std;

int main() {

int n;
long long int k;
cin>>n>>k;

int a[n];

for(auto i = 0; i < n; i++) {
    cin>>a[i]; 
}                                 

for(auto i = 0; i < k; i++) {
    int max = 0;
    int x = a[i];
    for(int j = i + 1; j < n; j++) {
        if(a[j] >= x) {
            max = j;
        }
    }
    int temp = a[i];
    a[i] = a[max];
    a[max] = temp;
}
 

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

return 0;

}

Hey @yashme7125

Sir can you tell the approach you had followed?

Hey @yashme7125
Just make a map first with key as Number and value as index at which its present

then start from beggining of array and keep moving forward until k!=0 or u reach the end
Here just keep swapping th numbers at correct position using hashmap u created

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.