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:
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Find Nth even length palindromic number formed using digits X and Y
- Find all strings formed from characters mapped to digits of a number
- Find maximum number that can be formed using digits of a given number
- Find the Largest number with given number of digits and sum of digits
- Find largest number smaller than N with same set of digits
- Minimize the number by changing at most K digits
- 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
- Check if the number formed by the last digits of N numbers is divisible by 10 or not
- Recursive sum of digits of a number formed by repeated appends
- Largest even number that can be formed by any number of swaps
- Find largest sum of digits in all divisors of n
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Largest number not greater than N all the digits of which are odd
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.