Given two positive integers N and M, the task is to find the smallest positive integer which is divisible by N and whose digit sum is M. Print -1 if no such integer exists within the range of int.
Input: N = 13, M = 32 Output: 8879 8879 is divisible by 13 and its Sum of digits of 8879 is 8+8+7+9 = 32 i.e. equals to M Input: N = 8, M = 32; Output: 8888
Approach : Start with N and iterate over all the multiples of n, and check whether its digit sum is equal to m or not. This problem can be solved in logn(INT_MAX) * log10(INT_MAX) time. The efficiency of this approach can be increased by starting the iteration with a number which has at least m/9 digit.
Below is the implementation of the approach:
- Smallest N digit number which is a multiple of 5
- Smallest integer greater than n such that it consists of digit m exactly k times
- Nth number whose sum of digit is multiple of 10
- Smallest multiple of 3 which consists of three given non-zero digits
- Smallest multiple of N formed using the given set of digits
- Count of m digit integers that are divisible by an integer n
- Biggest integer which has maximum digit sum in range from 1 to n
- Smallest multiple of a given number made of digits 0 and 9 only
- Smallest integer which has n factors or more
- Largest and smallest digit of a number
- Smallest and Largest sum of two n-digit numbers
- Smallest K digit number divisible by X
- Smallest Integer to be inserted to have equal sums
- Smallest integer > 1 which divides every element of the given array
- Smallest and Largest N-digit perfect squares
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.