Find Maximum Number by At-most K Swaps

Given an array of non-negative integers representing the digits of a number and an integer 'K', calculate the maximum possible number by swapping its digits up to 'K' times.

Input:

The first line contains a single integer 'T', representing the number of test cases.
For each test case:
The first line contains two space-separated integers 'N' and 'K' - the number of digits and the maximum number of swaps allowed.
The second line contains 'N' space-separated integers denoting the digits of the number.

Output:

For each test case, output the space-separated digits of the maximum number achievable.

Example:

Input:
T = 2
N = 4, K = 1
digits = [1, 2, 3, 4]
N = 4, K = 2
digits = [5, 3, 7, 0]
Output:
4 3 2 1
7 5 3 0

Constraints:

  • 1 ≤ T ≤ 10
  • 2 ≤ N ≤ 10
  • 0 ≤ K ≤ min(N, 5)
  • 0 ≤ NUM[i] ≤ 9

Note:

  • The input may include leading zeros as in [0,3,5,7], which represents 357.
  • Digits can repeat as in [3, 3, 4, 4], representing 3344.
AnswerBot
2d

Given an array of digits and an integer K, find the maximum number by swapping digits up to K times.

  • Sort the digits in non-increasing order to maximize the number.

  • Swap the digits to achieve the maximu...read more

Help your peers!
Add answer anonymously...
UBS C Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter