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.
functionlargestPermutation(k,arr){// Write your code hereconstoriginalArr=Array.from(arr)letindex=0letchanged=0arr.sort((a,b)=>b-a)if(k===0)returnoriginalArrif(k>=originalArr.length)returnarrfor(leti=0;i<=originalArr.length-1;i++){for(letind=0;ind<=originalArr.length-1;ind++){if(originalArr[ind]===arr[i]){index=indbreak}}if(index>i){[originalArr[i],originalArr[index]]=[arr[i],originalArr[i]]changed++if(changed===k)break}}returnoriginalArr}
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 →
Javascript