Given two integers X and Y, find the minimal number with the sum of digits X, which is strictly greater than Y.
Input: X = 18, Y = 99
189 is the smallest number greater than 99 having sum of digits = 18.
Input: X = 12, Y = 72
75 is the smallest number greater than 72 that has sum of digits = 12.
Naive Approach: The naive approach is iterate from Y + 1 and check if any number whose sum of digits is X or not. If we found any such number then print that number.
Time Complexity: O((R – Y)*log10N), where R is the maximum number till where we iterate and N is the number in the range [Y, R]
Auxiliary Space: O(1)
Efficient Approach: The idea is to iterate through the digits of Y from right to left, and try to increase the current digit and change the digits to the right in order to make the sum of digits equal to X. Below are the steps:
- If we are considering the (k + 1)th digit from the right and increasing it, then it is possible to make the sum of k least significant digits to be any number in the range [0, 9k].
- When such a position is found, then stop the process and print the number at that iteration.
- If k least significant digits have sum M (where 0 ≤ M ≤ 9k), then obtain the answer greedily:
- Traverse from the right to the left and insert 9 and subtract 9 from the sum of digits.
- Once, the sum is less than 9, place the remaining sum.
Below is the implementation of the above approach:
Time Complexity: O(log10Y)
Auxiliary Space: O(log10Y)
- Smallest subarray of size greater than K with sum greater than a given value
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
- Smallest number greater than or equal to N having sum of digits not exceeding S
- Smallest number greater than or equal to N using only digits 1 to K
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Highest and Smallest power of K less than and greater than equal to N respectively
- Smallest power of 2 which is greater than or equal to sum of array elements
- Smallest subarray from a given Array with sum greater than or equal to K
- Smallest number greater than or equal to N divisible by K
- Smallest Special Prime which is greater than or equal to a given number
- Largest number less than N with digit sum greater than the digit sum of N
- Smallest number with given sum of digits and sum of square of digits
- Smallest power of 4 greater than or equal to N
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Find smallest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Smallest number greater than n that can be represented as a sum of distinct power of k
- Count the number of words having sum of ASCII values less than and greater than k
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Numbers with sum of digits equal to the sum of digits of its all prime factor
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.