Count positive integers with 0 as a digit and maximum ‘d’ digits

Given a number d, representing the number of digits of a number. Find the total count of positive integers which have at-least one zero in them and consist d or less digits.

Examples:
Input : d = 1
Output : 0
There's no natural number of 1 digit that contains a zero.

Input : d = 2
Output : 9

Input : d = 3
Output : 180
For d = 3, we've to count numbers from 1 to 999, that have 
atleast one zero in them.
Similarly for d=4, we'd check every number from 1 to 9999. 

We strongly recommend that you click here and practice it, before moving on to the solution.


This is mainly an extension of below post.

Count ‘d’ digit positive integers with 0 as a digit.

If we observe carefully the problem is very similar to the one which we had discussed in our first set. For a given d, we can get the required answer if we find numbers that have 0s and consist of digits 1, 2, 3….., d. Finally we can add them to get the output.



Below is the program for the same.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the count of positive integer of a
// given number of digits that contain atleast one zero
#include<bits/stdc++.h>
using namespace std;
  
// Returns count of 'd' digit integers have 0 as a digit
int findCount(int d)
{
    return 9*(pow(10,d-1) - pow(9,d-1));
}
  
// utility function to count the required answer
int findCountUpto(int d)
{
    // Count of numbers with digits smaller than
    // or equal to d.
    int totalCount = 0;
    for (int i=1; i<=d; i++)
        totalCount += findCount(i);
  
    return totalCount;
}
  
// Driver Code
int main()
{
    int d = 1;
    cout << findCountUpto(d) << endl;
  
    d = 2;
    cout << findCountUpto(d) << endl;
  
    d = 4;
    cout << findCountUpto(d) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the count of 
// positive integer of agiven number
// of digits that contain atleast one zero
import java.io.*;
import java.math.*;
  
class GFG {
      
    // Returns count of 'd' digit 
    // integers have 0 as a digit
    static int findCount(int d)
    {
        return 9 * (int)((Math.pow(10, d - 1
                         - Math.pow(9, d - 1)));
    }
      
    // utility function to count
    // the required answer
    static int findCountUpto(int d)
    {
        // Count of numbers with digits 
        // smaller than or equal to d.
        int totalCount = 0;
        for (int i = 1; i <= d; i++)
            totalCount += findCount(i);
      
        return totalCount;
    }
      
    // Driver Code
    public static void main(String args[])
    {
        int d = 1;
        System.out.println(findCountUpto(d));
      
        d = 2;
        System.out.println( findCountUpto(d) );
      
        d = 4;
        System.out.println(findCountUpto(d));
    }
}
  
  
/*This code is contributed by Nikita Tiwari.*/

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find the
# count of natural numbers upto a
# given number of digits that
# contain atleast one zero
import math
  
  
# Utility function to calculate
# the count of natural numbers
# upto a given number of digits
# that contain atleast one zero
def findCountUpto(d) :
    # Sum of two GP series
    GP1_Sum = 9*((int)((math.pow(10,d))-1)//9)
    GP2_Sum = 9*((int)((math.pow(9,d))-1)//8)
   
    return GP1_Sum - GP2_Sum
  
  
# Driver Code
d = 1
print(findCountUpto(d))
  
d = 2
print(findCountUpto(d))
  
d = 4
print(findCountUpto(d))
  
  
# This code is contributed by Nikita Tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the count of 
// positive integer of agiven number
// of digits that contain atleast 
// one zero
using System;
  
class GFG {
      
    // Returns count of 'd' digit 
    // integers have 0 as a digit
    static int findCount(int d)
    {
        return 9 * (int)((Math.Pow(10, d - 1) 
                        - Math.Pow(9, d - 1)));
    }
      
    // utility function to count
    // the required answer
    static int findCountUpto(int d)
    {
        // Count of numbers with digits 
        // smaller than or equal to d.
        int totalCount = 0;
        for (int i = 1; i <= d; i++)
            totalCount += findCount(i);
      
        return totalCount;
    }
      
    // Driver Code
    public static void Main()
    {
        int d = 1;
        Console.WriteLine(findCountUpto(d));
      
        d = 2;
        Console.WriteLine( findCountUpto(d) );
      
        d = 4;
        Console.WriteLine(findCountUpto(d));
    }
}
  
// This code is contributed by Sam007

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the count
// of positive integer of a given 
// number of digits that contain
// atleast one zero
  
// Returns count of 'd' digit 
// integers have 0 as a digit
function findCount($d)
{
    return 9 * (pow(10, $d - 1) - 
                 pow(9, $d - 1));
}
  
// function to count 
// the required answer
function findCountUpto($d)
{
      
    // Count of numbers with
    // digits smaller than
    // or equal to d.
    $totalCount = 0;
    for ($i = 1; $i <= $d; $i++)
        $totalCount += findCount($i);
  
    return $totalCount;
}
  
// Driver Code
{
    $d = 1;
    echo findCountUpto($d),"\n" ;
  
    $d = 2;
    echo findCountUpto($d),"\n" ;
  
    $d = 4;
    echo findCountUpto($d) ;
    return 0;
}
  
// This code is contributed by nitin mittal.
?>

chevron_right


Output :

0
9
2619 

Time Complexity : O(d)
Auxiliary Space : O(1)

Can we make the solution more efficient ?
Yes, if we see closely, the required answer is obtained using the sum of following two Geometric Progressions:

i'th term of G.P. 1 = 9*10i - 1  where 1 <= i <= d
i'th term of G.P. 2 = 9*9i - 1 where 1 <= i <= d
The final answer is nothing but,
Answer=\sum_{k=1}^{d}(G.P1)-\sum_{k=1}^{d}(G.P2)
   
Sum of G.P 1 = 9*(10d - 1)/(10-1) 
             = 9*(10d - 1)/9
Similarly,
Sum of G.P 2 = 9*(9d - 1)/(9-1) 
             = 9*(9d - 1)/8
Using the above facts, we can optimize the solution to run in O(1) 

Below is an efficient program for the same.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the count of natural numbers upto a
// given number of digits that contain atleast one zero
#include<bits/stdc++.h>
using namespace std;
  
// Utility function to calculate the count of natural numbers
// upto a given number of digits that contain atleast one zero
int findCountUpto(int d)
{
    // Sum of two GP series
    int GP1_Sum = 9*((pow(10,d)-1)/9);
    int GP2_Sum = 9*((pow(9,d)-1)/8);
  
    return GP1_Sum - GP2_Sum;
}
  
// Driver Code
int main()
{
    int d = 1;
    cout << findCountUpto(d) << endl;
  
    d = 2;
    cout << findCountUpto(d) << endl;
  
    d = 4;
    cout << findCountUpto(d) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the count
// of natural numbers upto a
// given number of digits 
// that contain atleast one zero
import java.io.*;
import java.math.*;
  
class GFG {
      
    // Utility function to calculate 
    // the count of natural numbers
    // upto a given number of digits 
    // that contain atleast one zero
    static int findCountUpto(int d)
    {
        // Sum of two GP series
        int GP1_Sum = 9 * ((int)((Math.pow(10, d)) - 1) / 9);
        int GP2_Sum = 9 * ((int)((Math.pow(9, d)) - 1) / 8);
      
        return GP1_Sum - GP2_Sum;
    }
      
    // Driver Code
    public static void main(String args[])
    {
        int d = 1;
        System.out.println(findCountUpto(d));
          
        d = 2;
        System.out.println(findCountUpto(d));
          
        d = 4;
        System.out.println(findCountUpto(d));
          
    }
}
  
  
/* This code is contributed by Nikita Tiwari.*/

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find the
# count of positive integer of a
# given number of digits that
# contain atleast one zero
import math
  
# Returns count of 'd' digit
# integers have 0 as a digit
def findCount(d) :
    return 9*(pow(10,d-1) - pow(9,d-1));
  
  
# utility function to count
# the required answer
def findCountUpto(d) :
  
    # Count of numbers with
    # digits smaller than
    # or equal to d.
    totalCount = 0
    for i in range(1,d+1) :
        totalCount = totalCount + findCount(i)
   
    return totalCount
  
   
# Driver Code
d = 1
print(findCountUpto(d))
  
d = 2
print(findCountUpto(d))
  
d = 4
print(findCountUpto(d))
  
  
# This code is contributed by Nikita Tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the count
// of natural numbers upto a
// given number of digits 
// that contain atleast one zero
using System;
  
class GFG {
      
    // Utility function to calculate 
    // the count of natural numbers
    // upto a given number of digits 
    // that contain atleast one zero
    static int findCountUpto(int d)
    {
          
        // Sum of two GP series
        int GP1_Sum = 9 * ((int)((Math.Pow(10, 
                                d)) - 1) / 9);
        int GP2_Sum = 9 * ((int)((Math.Pow(9,
                                d)) - 1) / 8);
      
        return GP1_Sum - GP2_Sum;
    }
      
    // Driver Code
    public static void Main()
    {
        int d = 1;
        Console.WriteLine(findCountUpto(d));
          
        d = 2;
        Console.WriteLine(findCountUpto(d));
          
        d = 4;
        Console.WriteLine(findCountUpto(d));
          
    }
}
  
// This code is contributed by Sam007

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the count
// of natural numbers upto a
// given number of digits that 
// contain atleast one zero
  
// function to calculate the 
// count of natural numbers
// upto a given number of digits
// that contain atleast one zero
function findCountUpto($d)
{
      
    // Sum of two GP series
    $GP1_Sum = 9 * ((pow(10, $d) - 1) / 9);
    $GP2_Sum = 9 * ((pow(9, $d) - 1) / 8);
  
    return $GP1_Sum - $GP2_Sum;
}
  
    // Driver Code
    $d = 1;
    echo findCountUpto($d),"\n";
  
    $d = 2;
    echo findCountUpto($d),"\n";
  
    $d = 4;
    echo findCountUpto($d) ,"\n";
      
// This code is contributed by anuj_67.
?>

chevron_right


Output :

0
9
2619 

Time Complexity : O(1)
Auxiliary Space : O(1)

In the next set we’d see another problem of increased difficulty that can be solved using very similar technique.

This article is contributed by Ashutosh Kumar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Sam007, nitin mittal, vt_m



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.