InterviewBit Academy is now Scaler!
Learn Tech Skills from Scratch @ Scaler EDGE

Largest Permutation


Problem Description

Given an integer array A of size N consisting of unique integers from 1 to N. You can swap any two integers atmost B times.

Return the largest lexicographical value array that can be created by executing atmost B swaps.



Problem Constraints

1 <= N <= 106

1 <= B <= 109



Input Format

First argument is an integer array A of size N.

Second argument is an integer B.



Output Format

Return an integer array denoting the largest lexicographical value array that can be created by executing atmost B swaps.



Example Input

Input 1:

 A = [1, 2, 3, 4]
 B = 1

Input 2:

 A = [3, 2, 1]
 B = 2



Example Output

Output 1:

 [4, 2, 3, 1]

Output 2:

 [3, 2, 1]



Example Explanation

Explanation 1:

 In one swap we can swap (1, 4) so that the array becomes : [4, 2, 3, 1].

Explanation 2:

 Array is already the largest lexicographical value array.



NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Largest Permutation on Interview Code Editor
Hints
  • Hint 1
  • Solution Approach
  • Complete Solution
Asked In:

Discussion


Loading...
Click here to start solving coding interview questions