Given two integers N and K, the task is to count all N digits numbers whose sum of digits is divisible by K.
Examples:
Input: N = 2, K = 7
Output: 12
Explanation: 2 digits numbers with sum divisible by 7 are: {16, 25, 34, 43, 52, 59, 61, 68, 70, 77, 86, 95}.
Therefore, the required output is 12.Input: N = 1, K = 2
Output: 4
Naive Approach: The simplest approach is to traverse all the numbers from the range [10(N – 1), 10N – 1] and check if the sum of all the digits of a number that lies within the range is divisible by K or not. For every number for which the condition is found to be true, increase count. Finally, print the count.
Time Complexity: O(10N – 10N – 1 – 1)
Auxiliary Space: O(1)
Efficient Approach: The idea is to use Digit DP technique to optimize the above approach. Below is the recurrence relation:
sum: represents sum of digits
st: check if a number contains any leading 0.
Follow the steps below to solve the problem:
- Initialize a 3D array dp[N][K][st] to compute and store the values of all subproblems of the above recurrence relation.
- Finally, return the value of dp[N][sum%K][st].
C++
// C++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std; #define M 1000 // Function to count the N digit numbers // whose sum is divisible by K int countNum(int N, int sum, int K, bool st, int dp[M][M][2]) { // Base case if (N == 0 and sum == 0) { return 1; } if (N < 0) { return 0; } // If already computed // subproblem occurred if (dp[N][sum][st] != -1) { return dp[N][sum][st]; } // Store the count of N digit numbers // whoe sum is divisible by K int res = 0; // Check if the number does not contan // any leading 0. int start = st == 1 ? 0 : 1; // Recurrence relation for (int i = start; i <= 9; i++) { res += countNum(N - 1, (sum + i) % K, K, (st | i > 0), dp); } return dp[N][sum][st] = res; } // Driver Code int main() { int N = 2, K = 7; // Stores the values of // overlapping subproblems int dp[M][M][2]; memset(dp, -1, sizeof(dp)); cout << countNum(N, 0, K, 0, dp); } |
Java
// Java program to implement // the above approach import java.util.*;import java.lang.*; class GFG{ static final int M = 1000;// Function to count the N digit numbers// whose sum is divisible by Kstatic int countNum(int N, int sum, int K, int st, int dp[][][]){ // Base case if (N == 0 && sum == 0) { return 1; } if (N < 0) { return 0; } // If already computed // subproblem occurred if (dp[N][sum][st] != -1) { return dp[N][sum][st]; } // Store the count of N digit numbers // whoe sum is divisible by K int res = 0; // Check if the number does not contan // any leading 0. int start = st == 1 ? 0 : 1; // Recurrence relation for(int i = start; i <= 9; i++) { res += countNum(N - 1, (sum + i) % K, K, ((st | i) > 0) ? 1 : 0, dp); } return dp[N][sum][st] = res;}// Driver codepublic static void main (String[] args){ int N = 2, K = 7; // Stores the values of // overlapping subproblems int[][][] dp = new int[M][M][2]; for(int[][] i : dp) for(int[] j : i) Arrays.fill(j, -1); System.out.print(countNum(N, 0, K, 0, dp));}}// This code is contributed by offbeat |
Output:
12
Time Complexity:O(10*N*K)
Auxiliary Space: O(N*K)
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.
Recommended Posts:
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Check if the sum of digits of number is divisible by all of its digits
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count the numbers with N digits and whose suffix is divisible by K
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Minimum digits to be removed to make either all digits or alternating digits same
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Smallest number with given sum of digits and sum of square of digits
- Count of n digit numbers whose sum of digits equals to given sum
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- N digit numbers divisible by 5 formed from the M digits
- Check if the number formed by the last digits of N numbers is divisible by 10 or not
- Count numbers which are divisible by all the numbers from 2 to 10
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.
Improved By : offbeat

