Given a number N, the task is to minimize the number by changing at most K digits. Note that the number should not contain any leading zeros.
Input: N = 91945, K = 3
Input: N = 1, K = 0
- Replace the first digit with 1 if its not already 1 and update K accordingly.
- Now for the rest of the digits, replace the next K – 1 non-zero digits with a 0.
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 that can be formed by changing at most K digits
- Minimize difference after changing all odd elements to even
- Minimize the sum of digits of A and B such that A + B = N
- Find smallest number with given number of digits and sum of digits under given constraints
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find the minimum number possible by changing at most one digit
- Removing a number from array without changing its arithmetic mean
- Check if a number is power of k using base changing method
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Number of steps to sort the array by changing order of three elements in each step
- Minimum number of digits to be removed so that no two consecutive digits are same
- Find the average of k digits from the beginning and l digits from the end of the given number
- Check if the sum of digits of number is divisible by all of its digits
- Smallest number with given sum of digits and sum of square of digits
- Minimize the cost to split a number