Largest Permutation Discussions | Algorithms | HackerRank
We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
deflargestPermutation(k,arr):n=len(arr)position_map={value:idxforidx,valueinenumerate(arr)}current_max=max(arr)foriinrange(n):ifk==0:breakifarr[i]!=current_max:# Swap arr[i] and current_maxarr[position_map[current_max]],arr[i]=arr[i],current_max# Update position_mapposition_map[arr[position_map[current_max]]]=position_map[current_max]position_map[current_max]=i# Decrement kk-=1# Update current_max to the next largest numbercurrent_max-=1returnarr
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Largest Permutation
You are viewing a single comment's thread. Return to all comments →