
Asked in UBS
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
4mo
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...
Top C Developer Interview Questions Asked at UBS
Q. Preorder Traversal of a BST Problem Statement Given an array PREORDER representi...read more
Q. BST Node Deletion Problem Given a binary search tree (BST) and a key value K, yo...read more
Q. Sort 0 1 2 Problem Statement Given an integer array arr of size 'N' containing o...read more
Interview Questions Asked to C Developer at Other Companies
Top Skill-Based Questions for UBS C Developer
Algorithms Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
Operating Systems Interview Questions and Answers
250 Questions
C++ Interview Questions and Answers
150 Questions
C Interview Questions and Answers
100 Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

