- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given a number d which represents the number of digits. The goal is to find the count of positive integers with 0 as a digit and have maximum d digits. Count all 1 digit, 2 digit, 3 digit….d digit positive numbers containing at least one 0.

We will first find numbers the count of numbers that have d digits with at least one 0. Let’s say d=3. To make a 3-digit number with at least one 0, possible ways are −

Here d1 can have 1 to 9 : 9 ways d2 can have 0-9 : 10 ways d3 can have 0-9 : 10 ways Total numbers possible: 9 x 10 x 10 = 9 x 102 For d digits, count of numbers: 9 x 10d-1 For d digits, numbers without any 0 are : 9d Total numbers having d digits with at least one 0 = 9 x 10d-1 - 9d = 9 x ( 10d-1 - 9d-1 )

Let us understand with examples

**Input** − d=4

**Output** − Count of positive integers with 0 as a digit and maximum 'd' digits are − 2619

**Explanation** − x digit numbers with at least one 0 −

1 digit numbers : 0 2 digit numbers : 9 3 digit numbers : 171 4 digit numbers: 2439 Total= 9+171+2439 = 2619

**Input** − d=1

**Output** − Count of positive integers with 0 as a digit and maximum 'd' digits are − 0

**Explanation** − 1 to 9 no number has 0 as a digit.

We will use two approaches. A first naive approach using a for a loop. Start traversing from 1 digit upto d digits and calculate numbers using the formula mentioned above. Add returned value to count.

Take the integer d for digits.

Function total_count(int d)) takes a number of digits d and returns a count of numbers with d digits having at least one 0.

Calculate such numbers as temp=9*(pow(10,d-1) - pow(9,d-1));

Return temp.

Function maximum_d(int d) takes the maximum number of digits d and returns count of numbers upto d digits having at least one 0.

Traverse using a loop starting from 1 digit numbers then 2 and so on till d.

For each d calculate numbers as total_count(i). Add this to count.

At last we will get the total count.

Return count as result.

In this approach, we will calculate the count by observing the G.P formed for the above calculations.

Solution is 9 x (10^{d-1}- 9^{d-1}) = 9 x (10^{d}- 1)- 9 x (9^{d}-1) = 9 x (10^{i}- 1) - 9 x (9^{i}- 1) ( 1<=i<=d ) = g.p^{1}- g.p^{2}= 9x(10^{d}-1)/(10-1) - 9x(9^{d}-1)/(9-1) = (10d-1)- (9/8)*(9d-1)

Take d as the maximum number of digits.

Function maximum_d(int d) takes the maximum number of digits d and returns a count of numbers up to d digits having at least one 0.

Using above formula calculate temp_1 as 9*((pow(10,d)-1)/9).

Calculate temp_2 as 9*((pow(9,d)-1)/8).

Set count = temp_1 - temp_2.

Return count as result.

#include<bits/stdc++.h> using namespace std; int total_count(int d){ int temp = 9*(pow(10,d-1) - pow(9,d-1)); return temp; } int maximum_d(int d){ int count = 0; for (int i=1; i<=d; i++){ count = count + total_count(i); } return count; } int main(){ int d = 5; cout<<"Count of positive integers with 0 as a digit and maximum 'd' digits are: "<<maximum_d(d) << endl; return 0; }

If we run the above code it will generate the following output −

Count of positive integers with 0 as a digit and maximum 'd' digits are: 33570

#include<bits/stdc++.h> using namespace std; int maximum_d(int d){ int temp_1 = 9*((pow(10,d)-1)/9); int temp_2 = 9*((pow(9,d)-1)/8); int count = temp_1 - temp_2; return count; } int main(){ int d = 4; cout<<"Count of positive integers with 0 as a digit and maximum 'd' digits are: "<<maximum_d(d) << endl; return 0; }

If we run the above code it will generate the following output −

Count of positive integers with 0 as a digit and maximum 'd' digits are: 2619

- Related Questions & Answers
- Count ‘d’ digit positive integers with 0 as a digit in C++
- Count numbers having 0 as a digit in C++
- Count ways to express ‘n’ as sum of odd integers in C++
- Program to count n digit integers where digits are strictly increasing in Python
- Count Numbers with N digits which consists of odd number of 0's in C++
- Count Numbers with N digits which consists of even number of 0's in C++
- Which Indian ruler collected taxes labeled as ‘chauth' and ‘sardeshmukhi'?
- Count of Numbers in a Range divisible by m and having digit d in even positions in C++
- Count of Numbers in a Range where digit d occurs exactly K times in C++
- ASCII NUL, ASCII 0 (‘0’) and Numeric literal 0?
- Print a number containing K digits with digital root D in C++
- Ways to write N as sum of two or more positive integers in C++
- Maximum of sum and product of digits until number is reduced to a single digit in C++
- Sum a negative number (negative and positive digits) - JavaScript
- Count numbers with same first and last digits in C++

Advertisements