Given a positive number N, the target is to find the largest number that can be formed after removing any K digits from N.
Input: N = 6358, K = 1
Input: N = 2589, K = 2
- Iterate a loop K times.
- During every iteration, remove every digit from the current value of N once and store the maximum of all the numbers obtained.
- In order to achieve this we store the maximum of (N / (i * 10)) * i + (N % i) where i ranges from [1, 10l – 1] where l denotes the number of digits of the current value of N.
- Consider this maximum as the current value of N and proceed to the next iteration and repeat the above step.
- Thus, after every iteration, we have the least digit from the current value of N removed. On repeating the process K times, we obtain the largest number possible.
Let us analyze this approach for N = 6358, K = 1
The different possibilities after removal of every digit once are as follows:
(6358 / 10) * 1 + 6358 % 1 = 635 + 0 = 635
(6358 / 100) * 10 + 6358 % 10 = 630 + 8 = 638
(6358 / 1000) * 100 + 6358 % 100 = 600 + 58 = 658
(6358 / 10000) * 1000 + 6358 % 1000 = 0 + 358 = 358
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the Largest number with given number of digits and sum of digits
- Minimum digits to be removed to make either all digits or alternating digits same
- Immediate smallest number after re-arranging the digits of a given number
- Find smallest number with given number of digits and sum of digits
- Number of digits in the nth number made of given four digits
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Find smallest possible Number from a given large Number with same count of digits
- Reduce the number to minimum multiple of 4 after removing the digits
- Largest number smaller than or equal to n and digits in non-decreasing order
- Largest number with prime digits
- Largest number not greater than N all the digits of which are odd
- Find the largest number that can be formed by changing at most K digits
- Find minimum possible digit sum after adding a number d
- Largest smaller number possible using only one swap operation
- Smallest number with given sum of digits and sum of square of digits
- Minimum number of digits to be removed so that no two consecutive digits are same
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Check if the sum of digits of number is divisible by all of its digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.