Last Updated: 30 Sep, 2025

Digit Zero Tally

Moderate

Problem statement

You are given a positive integer N. Your task is to calculate the total number of times the digit '0' appears in the decimal representations of all integers from 1 to N, inclusive.


Input Format:
A single line containing the integer N.


Output Format:
Print a single integer representing the total count of the digit '0'.


Note:
The problem counts the total occurrences of the digit, not the number of integers that contain the digit. For example, for N=100, the number 10 contributes one '0' and the number 100 contributes two '0's.

A naive approach of iterating from 1 to N and counting zeros in each number as a string would be O(N * log10(N)), which is too slow for the given constraints.

The optimal solution involves a digit-by-digit analysis, often called Digit DP. It calculates the contribution of zeros at each position (units, tens, hundreds, etc.) across the entire range, leading to a much faster O(log10(N)) solution.