Queries to count numbers from a given range which are divisible the sum of their digits
Last Updated :
15 Jun, 2022
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:
- 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.
- 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.
- Modify the array arr[] to its prefix sum array.
- 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++
#include <bits/stdc++.h>
using namespace std;
int arr[100005];
bool isDivisible( int x)
{
int sum = 0;
int temp = x;
while (x != 0) {
sum += x % 10;
x /= 10;
}
if (temp % sum)
return false ;
else
return true ;
}
void precompute()
{
for ( int i = 1; i <= 100000; i++) {
if (isDivisible(i))
arr[i] = 1;
else
arr[i] = 0;
}
for ( int i = 1; i <= 100000; i++) {
arr[i] = arr[i] + arr[i - 1];
}
}
int main()
{
vector<pair< int , int > > Q = { { 5, 9 }, { 5, 20 } };
precompute();
for ( auto it : Q) {
cout << arr[it.second] - arr[it.first - 1] << "\n" ;
}
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
static int arr[] = new int [ 100005 ];
static boolean isDivisible( int x)
{
int sum = 0 ;
int temp = x;
while (x != 0 ) {
sum += x % 10 ;
x /= 10 ;
}
if (temp % sum != 0 )
return false ;
else
return true ;
}
static void precompute()
{
for ( int i = 1 ; i <= 100000 ; i++) {
if (isDivisible(i))
arr[i] = 1 ;
else
arr[i] = 0 ;
}
for ( int i = 1 ; i <= 100000 ; i++) {
arr[i] = arr[i] + arr[i - 1 ];
}
}
public static void main(String[] args)
{
int Q[][] = { { 5 , 9 }, { 5 , 20 } };
precompute();
for ( int q[] : Q) {
System.out.println(arr[q[ 1 ]] - arr[q[ 0 ] - 1 ]);
}
}
}
|
Python3
arr = [ 0 ] * 100005
def isDivisible(x):
sum = 0 ;
temp = x;
while (x ! = 0 ):
sum + = x % 10 ;
x = x / / 10 ;
if (temp % sum ! = 0 ):
return False ;
else :
return True ;
def precompute():
for i in range ( 1 , 100000 + 1 ):
if (isDivisible(i)):
arr[i] = 1 ;
else :
arr[i] = 0 ;
for i in range ( 1 , 100000 + 1 ):
arr[i] = arr[i] + arr[i - 1 ];
Q = [[ 5 , 9 ], [ 5 , 20 ]];
precompute();
for q in Q:
print (arr[q[ 1 ]] - arr[q[ 0 ] - 1 ]);
|
C#
using System;
class GFG{
static int []arr = new int [100005];
static bool isDivisible( int x)
{
int sum = 0;
int temp = x;
while (x != 0)
{
sum += x % 10;
x /= 10;
}
if (temp % sum != 0)
return false ;
else
return true ;
}
static void precompute()
{
for ( int i = 1; i <= 100000; i++)
{
if (isDivisible(i))
arr[i] = 1;
else
arr[i] = 0;
}
for ( int i = 1; i <= 100000; i++)
{
arr[i] = arr[i] + arr[i - 1];
}
}
public static int [] GetRow( int [,] matrix, int row)
{
var rowLength = matrix.GetLength(1);
var rowVector = new int [rowLength];
for ( var i = 0; i < rowLength; i++)
rowVector[i] = matrix[row, i];
return rowVector;
}
public static void Main(String[] args)
{
int [,]Q = { { 5, 9 }, { 5, 20 } };
int []q;
precompute();
for ( int i = 0; i < Q.GetLength(0); i++)
{
q = GetRow(Q,i);
Console.WriteLine(arr[q[1]] - arr[q[0] - 1]);
}
}
}
|
Javascript
<script>
let arr = new Array(100005).fill(0);
function isDivisible(x) {
let sum = 0;
let temp = x;
while (x != 0) {
sum += x % 10;
x = Math.floor(x / 10);
}
if (temp % sum != 0)
return false ;
else
return true ;
}
function precompute() {
for (let i = 1; i <= 100000; i++) {
if (isDivisible(i))
arr[i] = 1;
else
arr[i] = 0;
}
for (let i = 1; i <= 100000; i++) {
arr[i] = arr[i] + arr[i - 1];
}
}
let Q = [[5, 9], [5, 20]];
precompute();
for (let q of Q)
{
document.write(arr[q[1]] - arr[q[0] - 1] + " <br>" );
}
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(maxm), where maxm denotes the maximum value of R.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...