2

Remove K Digits

Difficulty: MEDIUM
Avg. time to solve
25 min
Success Rate
75%

Problem Statement
Suggest Edit

You are given a non-negative integer ‘NUM’ in the form of a string and provided with an integer ‘K’. You need to find out the smallest integer possible by removing exactly ‘K’ digits from ‘NUM.’

Note :

 ‘NUM’ does not have leading zeros except when ‘NUM’ is equal to zero.

Input Format:

The first line contains an integer, ‘T’, which denotes the number of test cases to be run. Then, the T test cases follow.

The first line of each test case contains a string ‘NUM’ and integer ‘K.’

Output format:

For each test case, print a single string, the minimum integer that can be possibly made by removing ‘K’ digits from ‘NUM’.

Output for each test case will be printed in a separate line.

Note :

You don’t have to print anything. It has already been taken care of. Just implement the given function.

Constraints

1 <= T <=10
2 <= |NUM| <= 10^5
0 <= K <= |NUM|

Where ‘T’ denotes the number of test cases and |’NUM’| denotes the length of the initial string ‘NUM’ and ‘K’ denotes the number of digits to be removed. 

Time limit: 1 second

Sample input 1:

1
3214
2

Sample output 1:

14

Explanation for sample input 1:

You are allowed to remove exactly 2 digits from the initial number. We get “21” on removing the first digit: ‘3’ and the last digit: ‘4’. All other possible answers are greater than “21”.

Sample input 2:

2
541
3
123
1

Sample output 2:

0
12

Explanation for sample input 2:

For the first test case, on removing 3 digits the initial number becomes empty i.e “0”.
For the second test case, we can remove the last digit “3” to get the smallest possible answer “12”. Other possible answers are “23”, “13” and all of them are greater than “12”.
Reset Code
Full screen
copy-code
Console