Largest Permutation Discussions | Algorithms | HackerRank

Largest Permutation

Sort by

recency

|

383 Discussions

|

  • + 0 comments

    Here is my c++ solution O(n), you also have a vidéo explanation here : https://youtu.be/baZOM6KLSWM

    vector<int> largestPermutation(int k, vector<int> arr) {
        map<int, int> indexes;
        for(int i = 0; i < arr.size(); i++) indexes[arr[i]] = i;
        
        int Mx = arr.size(), position = 0;
        while(k && position < arr.size()){
            if(arr[position] != Mx){
                int temps = arr[position];
                arr[position] = Mx;
                arr[indexes[Mx]] = temps;
                k--;
                indexes[temps] = indexes[Mx];
            }
            Mx--;
            position++;
        }
        
        return arr;
    }
    
  • + 0 comments

    Python3

    def largestPermutation(k, arr):
        # Write your code here
            
        n = len(arr) 
        D =  { v:k for k,v in enumerate(arr) } # dict lookup >>> arr.index()
        
        c = 0
        for i in range(n): 
            if arr[i]!=n-i:  # In sorted array, n-i should be at index i
                D[arr[i]] = D[n-i] # if not then update indexes
                arr[i],arr[D[n-i]]=n-i,arr[i] # do swap
                c = c + 1
    
            if c == k: #break if runs out of swaps
                break
        return arr
    
  • + 0 comments

    Java:

    public static List<Integer> largestPermutation(int k, List<Integer> arr) {
      int n = arr.size();
      int max = n;
      int[] indexMap = new int[max + 1];
    
      for (int i = 0; i < n; i++) {
        indexMap[arr.get(i)] = i;
      }
    
      for (int i = 0; i < n && k > 0; i++) {
        int currentValue = arr.get(i);
        int targetValue = n - i;
        if (currentValue != targetValue) {
          int targetIndex = indexMap[targetValue];
          arr.set(targetIndex, currentValue);
          arr.set(i, targetValue);
          indexMap[currentValue] = targetIndex;
          indexMap[targetValue] = i;
          k--;
        }
      }
      return arr;
    }
    
  • + 0 comments
    def largestPermutation(k, arr):
        n = len(arr)
        position_map = {value: idx for idx, value in enumerate(arr)}
        
        current_max = max(arr)
        for i in range(n):
            if k == 0:
                break
            
            if arr[i] != current_max:
                # Swap arr[i] and current_max
                arr[position_map[current_max]], arr[i] = arr[i], current_max
                # Update position_map
                position_map[arr[position_map[current_max]]] = position_map[current_max]
                position_map[current_max] = i
                # Decrement k
                k -= 1
            
            # Update current_max to the next largest number
            current_max -= 1
        
        return arr
    
  • + 0 comments

    Ruby

    def largestPermutation(k, arr)    
        array_swap = Array.new(arr.length)   
        arr.length.times { |i| array_swap[arr[i]] = i}        
        arr.length.times do |i|
            next if arr[i] == arr.length-i
            if k > 0
                index_change = array_swap[arr.length-i]
                array_swap[arr[i]] = index_change
                arr[i], arr[index_change] = arr[index_change], arr[i]    
                k -= 1
            end        
        end
        arr    
    end