0% found this document useful (0 votes)
14 views2 pages

Find The Occurrences of Digit D in The Range (0..n) - GeeksforGeeks

The document explains how to count the occurrences of a digit d in the range from 0 to a given number n. It provides examples and a C++ implementation of the algorithm to perform this counting. The method involves calculating the occurrences based on the position of each digit in the number using modular arithmetic.

Uploaded by

210108007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Find The Occurrences of Digit D in The Range (0..n) - GeeksforGeeks

The document explains how to count the occurrences of a digit d in the range from 0 to a given number n. It provides examples and a C++ implementation of the algorithm to perform this counting. The method involves calculating the occurrences based on the position of each digit in the number using modular arithmetic.

Uploaded by

210108007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

13/04/2024, 13:07 Find the occurrences of digit d in the range [0..

n] - GeeksforGeeks

Find the occurrences of digit d in the range


[0..n]
Last Updated : 16 Nov, 2023
Given a number n and a digit d, count all occurrences of d in range from 0 to
n.
Examples:

Input : n = 25
d = 2
Output : 9
The occurrences are 2, 12, 20, 21
22 (Two occurrences), 23, 24, 25
Input : n = 25
d = 3
Output :3
The occurrences are 3, 13, 23
Input : n = 32
d = 3
Output : 6
The occurrences are 3, 13, 23, 30, 31, 32

Recommended Practice

How Many X’s? Try It!

DSAThis
Practice Mathematical
solution is basedAlgorithm
on theMathematical
modularity Algorithms Pythagorean
of each digit at itsTriplet Fibonacci
position in theNumber E
number.

This modularity is calculated from the power of 10 related to the position in


the number in empirical method.

C++

#include <iostream>
#include <math.h>

https://www.geeksforgeeks.org/find-the-occurrences-of-y-in-the-range-of-x/ 1/12
13/04/2024, 13:07 Find the occurrences of digit d in the range [0..n] - GeeksforGeeks

using namespace std;

int myCountX(int N, int X)


{
int x, a, r;
int e;

// The loop is executed for every digit of N


e = (int)(log10(N));
r = 0;
while (e >= 0) {
// Calculation of next digit in decrescent order of
// power of 10
x = N / (int)pow(10, e);
x %= 10;
// Modularity based on power of 10
a = x * e * (int)pow(10, e - 1);
r += a;
// If the digit is the searched one then the
// remainder of division by the current power of 10
// is added to result because a number of occurances
// equal to this remainder is when the digit is
// present with this position
if (x == X) {
a = (N % (int)pow(10, e)) + 1;
// But if the searched digit is equal to 0 then
// there aren't number with the most significant
// digit equal to 0
if (X == 0)
a -= (int)pow(10, e);
r += a;
}
// If the digit is greater than the searched one and
// the searched digit isn't 0 then the number of all
// number with the most significat digit equal to
// the searched one must be added to result
if (x > X && X != 0) {
a = (int)pow(10, e);
r += a;
}
e--;
}
return r;
}

int main()
{
int N, X;

N = 1000;
X = 0;
cout << myCountX(N, X);
return 0;
https://www.geeksforgeeks.org/find-the-occurrences-of-y-in-the-range-of-x/ 2/12

You might also like