Given two integers N and K, the task is to find the minimum integer X that can be added to N so that the sum of the digits of the newly formed number does not exceed K.
Input: N = 1, K = 1
The sum of the digits of the given number is 1, which is already equal to K(=1).
Input: N = 11, K = 1
Adding the number 89 to the given number 11 results to 100.
The sum of digits of the new number formed is 1 which does not exceed K(=1).
Therefore, the minimum number that can be added is 89.
Approach: Follow the steps below to solve the problem:
- Check if the sum of the digits of the given number N does not exceed K or not. If found to be true, then the least number added is 0.
- Now, start calculating the sum of digits from the unit’s place and continue until the sum of digits exceeds K.
- Now, the part of N having a sum of the digits greater than or equal to K is found. So, eliminate the last digit of that part so that the sum of the digits becomes less than K.
- Now, add 1 to the newly obtained number as it will keep the sum of the digits less than or equal to K.
- Now, to obtain the new number which exceeds N and has the number of digits less than or equal to K, multiply the number with 10P + 1, where P is the count of digits up to which sum did not exceed K.
- Now subtract N from the new number to get the result X.
- Print the value of X after completing the above steps.
Below is the implementation of the above approach:
Time Complexity: O(log10N)
Auxiliary Space: O(1)
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 sum of prime numbers added to make an array non-decreasing
- Minimize elements to be added to a given array such that it contains another given array as its subsequence
- Minimize elements to be added to a given array such that it contains another given array as its subsequence | Set 2
- Minimum number to be added to all digits of X to make X > Y
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Minimum digits to be removed to make either all digits or alternating digits same
- Minimum value to be added to X such that it is at least Y percent of N
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Maximum difference elements that can added to a set
- Minimize the sum of digits of A and B such that A + B = N
- Smallest number with given sum of digits and sum of square of digits
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Minimum elements to be added in a range so that count of elements is divisible by K
- Minimum numbers with one's place as 9 to be added to get N
- Find the minimum number to be added to N to make it a prime number
- Find the minimum number to be added to N to make it a power of K
- Minimize the number by changing at most K digits
- Minimum number of digits to be removed so that no two consecutive digits are same
- Sum of first N natural numbers with all powers of 2 added twice
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.