Numbers in a Range with given Digital Root
Given an integer K and a range of consecutive numbers [L, R]. The task is to count the numbers from the given range which have digital root as K (1 ? K ? 9). Digital root is sum of digits of a number until it becomes a single digit number. For example, 256 -> 2 + 5 + 6 = 13 -> 1 + 3 = 4.
Examples:
Input: L = 10, R = 22, K = 3
Output: 2
12 and 21 are the only numbers from the range whose digit sum is 3.
Input: L = 100, R = 200, K = 5
Output: 11
Approach:
- First thing is to note that for any number Sum of Digits is equal to Number % 9. If remainder is 0, then sum of digits is 9.
- So if K = 9, then replace K with 0.
- Task, now is to find count of numbers in range L to R with modulo 9 equal to K.
- Divide the entire range into the maximum possible groups of 9 starting with L (TotalRange / 9), since in each range there will be exactly one number with modulo 9 equal to K.
- Loop over rest number of elements from R to R – count of rest elements, and check if any number satisfies the condition.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int countNumbers( int L, int R, int K)
{
if (K == 9)
K = 0;
int totalnumbers = R - L + 1;
int factor9 = totalnumbers / 9;
int rem = totalnumbers % 9;
int ans = factor9;
for ( int i = R; i > R - rem; i--) {
int rem1 = i % 9;
if (rem1 == K)
ans++;
}
return ans;
}
int main()
{
int L = 10;
int R = 22;
int K = 3;
cout << countNumbers(L, R, K);
return 0;
}
|
Java
class GFG {
static int countNumbers( int L, int R, int K) {
if (K == 9 ) {
K = 0 ;
}
int totalnumbers = R - L + 1 ;
int factor9 = totalnumbers / 9 ;
int rem = totalnumbers % 9 ;
int ans = factor9;
for ( int i = R; i > R - rem; i--) {
int rem1 = i % 9 ;
if (rem1 == K) {
ans++;
}
}
return ans;
}
public static void main(String[] args) {
int L = 10 ;
int R = 22 ;
int K = 3 ;
System.out.println(countNumbers(L, R, K));
}
}
|
Python3
def countNumbers(L, R, K):
if (K = = 9 ):
K = 0
totalnumbers = R - L + 1
factor9 = totalnumbers / / 9
rem = totalnumbers % 9
ans = factor9
for i in range (R, R - rem, - 1 ):
rem1 = i % 9
if (rem1 = = K):
ans + = 1
return ans
L = 10
R = 22
K = 3
print (countNumbers(L, R, K))
|
C#
using System ;
class GFG
{
static int countNumbers( int L, int R, int K)
{
if (K == 9)
{
K = 0;
}
int totalnumbers = R - L + 1;
int factor9 = totalnumbers / 9;
int rem = totalnumbers % 9;
int ans = factor9;
for ( int i = R; i > R - rem; i--)
{
int rem1 = i % 9;
if (rem1 == K)
{
ans++;
}
}
return ans;
}
public static void Main()
{
int L = 10;
int R = 22;
int K = 3;
Console.WriteLine(countNumbers(L, R, K));
}
}
|
PHP
<?php
function countNumbers( $L , $R , $K )
{
if ( $K == 9)
$K = 0;
$totalnumbers = $R - $L + 1;
$factor9 = intval ( $totalnumbers / 9);
$rem = $totalnumbers % 9;
$ans = $factor9 ;
for ( $i = $R ; $i > $R - $rem ; $i --)
{
$rem1 = $i % 9;
if ( $rem1 == $K )
$ans ++;
}
return $ans ;
}
$L = 10;
$R = 22;
$K = 3;
echo countNumbers( $L , $R , $K );
?>
|
Javascript
<script>
function countNumbers(L, R, K)
{
if (K == 9)
{
K = 0;
}
var totalnumbers = R - L + 1;
var factor9 = totalnumbers / 9;
var rem = totalnumbers % 9;
var ans = factor9;
for ( var i = R; i > R - rem; i--)
{
var rem1 = i % 9;
if (rem1 == K)
{
ans++;
}
}
return ans;
}
var L = 10;
var R = 22;
var K = 3;
document.write(Math.round(countNumbers(L, R, K)));
</script>
|
Time Complexity: O(R)
Auxiliary Space: O(1)
Last Updated :
22 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...