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:
- Count of N-digit numbers having digit XOR as single digit
- 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
- Largest number less than N with digit sum greater than the digit sum of N
- Nth number whose sum of digit is multiple of 10
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
- Biggest integer which has maximum digit sum in range from 1 to n
- Smallest and Largest sum of two n-digit numbers
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Find the largest N digit multiple of N
- Count all N digit numbers whose digits are multiple of X
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Smallest multiple of a given number made of digits 0 and 9 only
- Smallest and Largest N-digit number starting and ending with N
- Minimum decrements to make integer A divisible by integer B
- Count of m digit integers that are divisible by an integer n
- Check if digit cube limit of an integer arrives at fixed point or a limit cycle
- Count 'd' digit positive integers with 0 as a digit
- Check if frequency of each digit is less than the digit
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.