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.
- Prepare
- Algorithms
- Greedy
- Largest Permutation
- Discussions
Largest Permutation
Largest Permutation
+ 0 comments Shows wrong answer for some of test cases,Can anyone help?
n = 0 for i in range(k): a = arr.index(max(arr[n:])) if arr[a] != arr[n]: arr[a], arr[n] = arr[n], arr[a] n = n+1 else: n = n+1 return arr
+ 0 comments Python
def largestPermutation(k, arr): n = len(arr) swap_count = 0 for i in range(n): arr_int = arr[i] lex_int = n - i if lex_int != arr_int: swap_i = arr.index(lex_int) arr[swap_i] = arr_int arr[i] = lex_int swap_count += 1 if swap_count == k: break return arr
+ 0 comments def largestPermutation(k, arr): # Write your code here
arr_sorted=sorted(arr,reverse=True) if k>=len(arr): return arr_sorted arr1=arr for i in range(k): arr_en=[[q,p] for p,q in enumerate(arr1)] arr_en=sorted(arr_en,reverse=True) print(arr_en) x=arr_en[i][0] indx=arr_en[i][1] arr1[i],arr1[indx]=x,arr1[i] return arr1 whats wrong with my code
+ 0 comments Here is problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-largest-permutation-problem-solution.html
+ 0 comments index={} n=len(arr) for i in range(n): index[arr[i]]=i swap=i=0 while swap<k and i<n: if arr[i]<n-i: idx=index[n-i] arr[i],arr[idx]=arr[idx],arr[i] index[arr[idx]]=idx index[n-i]=i swap+=1 i+=1 return arr
Load more conversations
Sort 353 Discussions, By:
Please Login in order to post a comment