Hackerearth – Permutation Problem

By January 2, 2016 No Comments

Here is one more coding challenge that I have solved in HackerEarth.

Problem Statement

You are given an array of N integers which is a permutation of the first N natural numbers. You can swap any two elements of the array. You can make at most K swaps. What is the largest permutation, in numerical order, you can make?

Input Format The first line of the input contains two integers, N and K, the size of the input array and the maximum swaps you can make, respectively. The second line of the input contains a permutation of the first N natural numbers.

Output Format Print the lexicographically largest permutation you can make with at most K swaps.

Constraints 1≤N≤105 1≤K≤109

Sample Input (Plaintext Link)
5 1
4 2 3 5 1
Sample Output (Plaintext Link)
5 2 3 4 1

Here is my solution to the above problem written in Python 2.7

Just replace raw_input with input and it should work fine with Python 3

Credits to the Problem Creation: Hackerearth.


Author Naveen

A Computer Science graduate student, a programmer, photographer, reader and blogger

More posts by Naveen

Leave a Reply