The Wayback Machine - https://web.archive.org/web/20210301162730/https://www.geeksforgeeks.org/queries-to-count-numbers-from-a-given-range-which-are-divisible-the-sum-of-their-digits/
Skip to content
Related Articles

Related Articles

Queries to count numbers from a given range which are divisible the sum of their digits
  • Last Updated : 01 Mar, 2021

Given an array Q[][] consisting of N queries of the form {L, R}, the task for each query is to find the total count of the numbers from the range [L, R] which are divisible by the sum of their digits.

Examples:

Input: Q[][]= {{5, 9}, {5, 20}}
Output: 
5
9
Explanation: 
Query 1: The numbers in the range [5, 9] which are divisible by the sum of their digits are {5, 6, 7, 8, 9}.
Query 2: The numbers in the range [5, 20] which are divisible by the sum of their digits are {5, 6, 7, 8, 9, 10, 12, 18, 20}.  

Input: Q[][] = {{1, 30}, {13, 15}}
Output: 
17
0
Explanation: 
Query 1: The numbers in the range [5, 9] which are divisible by the sum of their digits are {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30}.
Query 2: No such number exists in the range [13, 15].

Approach: The problem can be solved using the inclusion-exclusion principle and prefix sum array technique. 
Follow the steps below to solve the given problem:



  1. Initialize an array arr[] to store at every ith index, the count of numbers up to i which are divisible by the sum of their digits. 
  2. Iterate over the range [1, 105] using a variable, say i, and for every ith element, check if i is divisible by the sum of its digits.
    • If found to be true, set arr[i] = 1.
    • Otherwise, set arr[i] = 0.
  3. Modify the array arr[] to its prefix sum array.
  4. Traverse the array Q[][] and for each query, calculate the total count of required numbers from the range [L, R], which is equal to arr[R] – arr[L – 1].

Below is the implementation of the above approach:

C++




// C++ program of the
// above approach
#include <bits/stdc++.h>
using namespace std;
  
int arr[100005];
  
// Function to check if the number x
// is divisible by its sum of digits
bool isDivisible(int x)
{
    // Stores sum of digits of x
    int sum = 0;
  
    // Temporarily store x
    int temp = x;
  
    // Calculate sum
    // of digits of x
    while (x != 0) {
        sum += x % 10;
        x /= 10;
    }
  
    // If x is not divisible by sum
    if (temp % sum)
        return false;
  
    // Otherwise
    else
        return true;
}
  
// Function to perform
// the precomputation
void precompute()
{
    // Iterate from i equals 1 to 1e5
    for (int i = 1; i <= 100000; i++) {
        // Check if i is divisible
        // by sum of its digits
        if (isDivisible(i))
            arr[i] = 1;
        else
            arr[i] = 0;
    }
  
    // Convert arr[] to prefix sum array
    for (int i = 1; i <= 100000; i++) {
        arr[i] = arr[i] + arr[i - 1];
    }
}
  
// Driver code
int main()
{
    // Given queries
    vector<pair<int, int> > Q = { { 5, 9 }, { 5, 20 } };
  
    precompute();
  
    for (auto it : Q) {
        // Using inclusion-exclusion
        // principle, calculate the result
        cout << arr[it.second] - arr[it.first - 1] << "\n";
    }
  
    return 0;
}


Output:

5
9

Time Complexity: O(N)
Auxiliary Space: O(maxm), where maxm denotes the maximum value of R.

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :