Largest Permutation


Problem Statement :


You are given an unordered array of unique integers incrementing from . You can swap any two elements a limited number of times. Determine the largest lexicographical value array that can be created by executing no more than the limited number of swaps.

Example


The following arrays can be formed by swapping the  with the other elements:

[2,1,3,4]
[3,2,1,4]
[4,2,3,1]
The highest value of the four (including the original) is . If , we can swap to the highest possible value: .

Function Description

Complete the largestPermutation function in the editor below. It must return an array that represents the highest value permutation that can be formed.

largestPermutation has the following parameter(s):

int k: the maximum number of swaps
int arr[n]: an array of integers
Input Format

The first line contains two space-separated integers  and , the length of  and the maximum swaps that can be performed. The second line contains  distinct space-separated integers from  to  as  where .


Solution :



title-img


                            Solution in C :

In C :






#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

   int n,l,k,i,j=0;
    scanf("%d %d",&n,&k);
    int arr[n];
    for(i=0;i<n;i++)
        {
        scanf("%d",&arr[i]);
    }
    i=0;
    while(j<k && i<n)
        {
        if(arr[i]!=n-i)
            {
            for(l=i;l<n;l++)
                {if(arr[l]==n-i) //swap
                {
                    int tmp=arr[l];
                    arr[l]=arr[i];
                    arr[i]=tmp;
                    break;
                }
                }
            j++;
        }
        i++;
       
    }
    for(i=0;i<n;i++)
        {
        printf("%d ",arr[i]);
    }
    
    return 0;
}
                        

                        Solution in C++ :

In C++ :







#include<iostream>
using namespace std;
int main()
{
int t,n,k;
{
cin>>n>>k;
int a[n+1],pos[n+1];
for(int i=1;i<=n;i++)
{
cin>>a[i];
pos[a[i]]=i; //position of a[i] will be i
}
for(int i=1;i<=n;i++)
{
if(!k) //no more swapping can be done
break;
else
{
if(a[i]!=n-i+1)
{
k--;
int temp = a[i]; //swapping a[i] with n-i+1
a[i] = n-i+1;
a[pos[n-i+1]] = temp;
pos[temp] = pos[n-i+1]; //swapping a[i]'s and a[n-i+1]'s positions
pos[n-i+1] = i;
}
}
}
for(int i=1;i<=n;i++) //output array after swapping is done
cout<<a[i]<<" ";
}

}
                    

                        Solution in Java :

In Java :






import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scc = new Scanner(System.in);
        int n = scc.nextInt();
        int k = scc.nextInt();
        int arr[] = new int[n];
        for(int i=0; i<n; i++){
            arr[i] = scc.nextInt();
        }
        
        
        for(int i=0; i<k && i<n; i++){
            int j;
            for(j=i; j<n; j++){
                if(arr[j]==n-i){
                    break;
                }
            }
            if(j!=i){
               int temp = arr[j];
               arr[j] = arr[i];
               arr[i] = temp;
            }
            else{
                k++;
            }
        }
        
        for(int i=0; i<n; i++)
            System.out.print(arr[i]+" ");
    }
}
                    

                        Solution in Python : 
                            
In  Python3 :



n, k = map(int, input().split())
p = list(map(lambda s: int(s) - 1, input().split()))

where = [-1] * n
for i, x in enumerate(p):
    where[x] = i
    
for i in range(n):
    if k == 0:
        break
    if p[i] != n - 1 - i:
        bad_pos = where[n - 1 - i]
        bad_num = p[i]
        p[i], p[bad_pos] = p[bad_pos], p[i]
        where[bad_num], where[n - 1 - i] = where[n - 1 - i], where[bad_num]
        k -= 1
    
print(' '.join(map(lambda x: str(x + 1), p)))
                    

View More Similar Problems

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →

Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →

Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →

Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

View Solution →