Numbers in a Range with given Digital Root
Last Updated :
22 Jun, 2022
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++
// C++ implementation of the approach
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
// Function to return the count
// of required numbers
int countNumbers(int L, int R, int K)
{
if (K == 9)
K = 0;
// Count of numbers present
// in given range
int totalnumbers = R - L + 1;
// Number of groups of 9 elements
// starting from L
int factor9 = totalnumbers / 9;
// Left over elements not covered
// in factor 9
int rem = totalnumbers % 9;
// One Number in each group of 9
int ans = factor9;
// To check if any number in rem
// satisfy the property
for (int i = R; i > R - rem; i--) {
int rem1 = i % 9;
if (rem1 == K)
ans++;
}
return ans;
}
// Driver code
int main()
{
int L = 10;
int R = 22;
int K = 3;
cout << countNumbers(L, R, K);
return 0;
}
Java
// Java implementation of the approach
class GFG {
// Function to return the count
// of required numbers
static int countNumbers(int L, int R, int K) {
if (K == 9) {
K = 0;
}
// Count of numbers present
// in given range
int totalnumbers = R - L + 1;
// Number of groups of 9 elements
// starting from L
int factor9 = totalnumbers / 9;
// Left over elements not covered
// in factor 9
int rem = totalnumbers % 9;
// One Number in each group of 9
int ans = factor9;
// To check if any number in rem
// satisfy the property
for (int i = R; i > R - rem; i--) {
int rem1 = i % 9;
if (rem1 == K) {
ans++;
}
}
return ans;
}
// Driver code
public static void main(String[] args) {
int L = 10;
int R = 22;
int K = 3;
System.out.println(countNumbers(L, R, K));
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python3 implementation of the approach
# Function to return the count
# of required numbers
def countNumbers(L, R, K):
if (K == 9):
K = 0
# Count of numbers present
# in given range
totalnumbers = R - L + 1
# Number of groups of 9 elements
# starting from L
factor9 = totalnumbers // 9
# Left over elements not covered
# in factor 9
rem = totalnumbers % 9
# One Number in each group of 9
ans = factor9
# To check if any number in rem
# satisfy the property
for i in range(R, R - rem, -1):
rem1 = i % 9
if (rem1 == K):
ans += 1
return ans
# Driver code
L = 10
R = 22
K = 3
print(countNumbers(L, R, K))
# This code is contributed
# by mohit kumar
C#
// C# implementation of the approach
using System ;
class GFG
{
// Function to return the count
// of required numbers
static int countNumbers(int L, int R, int K)
{
if (K == 9)
{
K = 0;
}
// Count of numbers present
// in given range
int totalnumbers = R - L + 1;
// Number of groups of 9 elements
// starting from L
int factor9 = totalnumbers / 9;
// Left over elements not covered
// in factor 9
int rem = totalnumbers % 9;
// One Number in each group of 9
int ans = factor9;
// To check if any number in rem
// satisfy the property
for (int i = R; i > R - rem; i--)
{
int rem1 = i % 9;
if (rem1 == K)
{
ans++;
}
}
return ans;
}
// Driver code
public static void Main()
{
int L = 10;
int R = 22;
int K = 3;
Console.WriteLine(countNumbers(L, R, K));
}
}
/* This code is contributed by Ryuga */
PHP
<?php
// PHP implementation of the approach
// Function to return the count
// of required numbers
function countNumbers($L, $R, $K)
{
if ($K == 9)
$K = 0;
// Count of numbers present
// in given range
$totalnumbers = $R - $L + 1;
// Number of groups of 9 elements
// starting from L
$factor9 = intval($totalnumbers / 9);
// Left over elements not covered
// in factor 9
$rem = $totalnumbers % 9;
// One Number in each group of 9
$ans = $factor9;
// To check if any number in rem
// satisfy the property
for ($i = $R; $i > $R - $rem; $i--)
{
$rem1 = $i % 9;
if ($rem1 == $K)
$ans++;
}
return $ans;
}
// Driver code
$L = 10;
$R = 22;
$K = 3;
echo countNumbers($L, $R, $K);
// This code is contributed by Ita_c
?>
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the count
// of required numbers
function countNumbers(L, R, K)
{
if (K == 9)
{
K = 0;
}
// Count of numbers present
// in given range
var totalnumbers = R - L + 1;
// Number of groups of 9 elements
// starting from L
var factor9 = totalnumbers / 9;
// Left over elements not covered
// in factor 9
var rem = totalnumbers % 9;
// One Number in each group of 9
var ans = factor9;
// To check if any number in rem
// satisfy the property
for(var i = R; i > R - rem; i--)
{
var rem1 = i % 9;
if (rem1 == K)
{
ans++;
}
}
return ans;
}
// Driver Code
var L = 10;
var R = 22;
var K = 3;
document.write(Math.round(countNumbers(L, R, K)));
// This code is contributed by Ankita saini
</script>
Time Complexity: O(R)
Auxiliary Space: O(1)
Similar Reads
Count numbers with unit digit k in given range Here given a range from low to high and given a number k.You have to find out the number of count which a number has same digit as kExamples: Input: low = 2, high = 35, k = 2 Output: 4 Numbers are 2, 12, 22, 32 Input: low = 3, high = 30, k = 3 Output: 3 Numbers are 3, 13, 23 A naive approach is to t
6 min read
Digital Root of a given large number using Recursion Given a large number num in the form of string with length as N, the task is to find its digital root. The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains t
7 min read
Print a number containing K digits with digital root D Given a digital root 'D' and number of digits 'K'. The task is to print a number containing K digits that has its digital root equal to D. Print '-1' if such a number does not exist. Examples: Input: D = 4, K = 4 Output: 4000 No. of digits is 4. Sum of digits is also 4. Input: D = 0, K = 1 Output: 0
4 min read
Find a Number X whose sum with its digits is equal to N Given a positive number N. We need to find number(s) such that sum of digits of those numbers to themselves is equal to N. If no such number is possible print -1. Here N \in [1, 1000000000] Examples: Input : N = 21Output : X = 15Explanation : X + its digit sum = 15 + 1 + 5 = 21 Input : N = 5Output :
15+ min read
Digital Root (repeated digital sum) of the given large integer The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as ne
5 min read