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
This solution is based on the modularity of each digit at its position in the 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>
using namespace std;
int myCountX( int N, int X)
{
int x, a, r;
int e;
e = ( int )( log10 (N));
r = 0;
while (e >= 0) {
x = N / ( int ) pow (10, e);
x %= 10;
a = x * e * ( int ) pow (10, e - 1);
r += a;
if (x == X) {
a = (N % ( int ) pow (10, e)) + 1;
if (X == 0)
a -= ( int ) pow (10, e);
r += a;
}
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;
}
|
Java
import java.util.Scanner;
public class Main {
static int myCountX( int N, int X) {
int x, a, r;
int e;
e = ( int ) (Math.log10(N));
r = 0 ;
while (e >= 0 ) {
x = N / ( int ) Math.pow( 10 , e);
x %= 10 ;
a = x * e * ( int ) Math.pow( 10 , e - 1 );
r += a;
if (x == X) {
a = (N % ( int ) Math.pow( 10 , e)) + 1 ;
if (X == 0 )
a -= ( int ) Math.pow( 10 , e);
r += a;
}
if (x > X && X != 0 ) {
a = ( int ) Math.pow( 10 , e);
r += a;
}
e--;
}
return r;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N, X;
N = 1000 ;
X = 0 ;
System.out.println(myCountX(N, X));
scanner.close();
}
}
|
Python3
import math
def myCountX(N, X):
e = int (math.log10(N))
r = 0
while e > = 0 :
x = N / / 10 * * e % 10
a = x * e * 10 * * (e - 1 )
r + = a
if x = = X:
a = N % 10 * * e + 1
if X = = 0 :
a - = 10 * * e
r + = a
if x > X and X ! = 0 :
a = 10 * * e
r + = a
e - = 1
return r
if __name__ = = "__main__" :
N = 1000
X = 0
print (myCountX(N, X))
|
C#
using System;
class Program
{
static int MyCountX( int N, int X)
{
int x, a, r;
int e;
e = ( int )Math.Log10(N);
r = 0;
while (e >= 0)
{
x = N / ( int )Math.Pow(10, e);
x %= 10;
a = x * e * ( int )Math.Pow(10, e - 1);
r += a;
if (x == X)
{
a = (N % ( int )Math.Pow(10, e)) + 1;
if (X == 0)
a -= ( int )Math.Pow(10, e);
r += a;
}
if (x > X && X != 0)
{
a = ( int )Math.Pow(10, e);
r += a;
}
e--;
}
return r;
}
static void Main()
{
int N, X;
N = 1000;
X = 0;
Console.WriteLine(MyCountX(N, X));
}
}
|
Javascript
function myCountX(N, X) {
let x, a, r;
let e;
e = Math.floor(Math.log10(N));
r = 0;
while (e >= 0) {
x = Math.floor(N / Math.pow(10, e));
x %= 10;
a = x * e * Math.pow(10, e - 1);
r += a;
if (x === X) {
a = (N % Math.pow(10, e)) + 1;
if (X === 0)
a -= Math.pow(10, e);
r += a;
}
if (x > X && X !== 0) {
a = Math.pow(10, e);
r += a;
}
e--;
}
return r;
}
function main() {
let N, X;
N = 1000;
X = 0;
console.log(myCountX(N, X));
}
main();
|
Output:
192
Time Complexity : O(logn)
Auxiliary Space : O(1)
This article is contributed by Antonio D’Angelico (CODERLOVER). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Nov, 2023
Like Article
Save Article