Given a string str representing a number and an integer K, the task is to find the largest number that can be formed by changing at most K digits in the given number.
Input: str = “569431”, K = 3
Replace first, second and fourth digits with 9.
Input: str = “5687”, K = 2
Approach: In order to get the maximum number possible, leftmost digits must be replaced with 9s. For every digit of the number starting from the leftmost digit, if it is not already 9 and K is greater than 0 then replace it with 9 and decrement K by 1. Repeat these steps for every digit while K is greater than 0. Finally, print the updated number.
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.
- Minimize the number by changing at most K digits
- Find the minimum number possible by changing at most one digit
- Find maximum number that can be formed using digits of a given number
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Find the Largest number with given number of digits and sum of digits
- Largest even number that can be formed by any number of swaps
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Greatest number less than equal to B that can be formed from the digits of A
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Largest square which can be formed using given rectangular blocks
- Find smallest number formed by inverting digits of given number N
- Sum of all numbers that can be formed with permutations of n digits
- Maximum possible time that can be formed from four digits
- Count of alphabets whose ASCII values can be formed with the digits of N
- Check if B can be formed by permuting the binary digits of A
- Largest number not greater than N which can become prime after rearranging its digits
- Find all strings formed from characters mapped to digits of a number
- Find Nth even length palindromic number formed using digits X and Y
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.