My code is giving TLE in one case. Please help.
Unlock question STL
#include #include #define ll long long using namespace std; bool compare(ll a , ll b) { return b<a; } int main() { ll n,k; cin>>n>>k; ll arr[n]; for(ll i=0;i<n;i++) cin>>arr[i]; for(ll i=0;i<n;i++) { if(arr[i]!=*max_element(arr+i,arr+n)) {swap(arr[i],*max_element(arr+i,arr+n)); k–; } if(!k) break; } for(int i=0;i<n;i++) cout<<arr[i]<<" "; return 0; }
ur solution time complexity is O(n^2) thats why it is giving tle.
imporve it using hashing