Skip to main content

Count Commas in Range - Solution & Explanation

EasyMath5 min read
Practice this problem

Problem Statement

You are given an integer n.

Return the total number of commas used when writing all integers from [1, n] (inclusive) in standard number formatting.

In standard formatting:

  • A comma is inserted after every three digits from the right.
  • Numbers with fewer than 4 digits contain no commas.

 

Example 1:

Input: n = 1002

Output: 3

Explanation:

The numbers "1,000", "1,001", and "1,002" each contain one comma, giving a total of 3.

Example 2:

Input: n = 998

Output: 0

Explanation:

All numbers from 1 to 998 have fewer than four digits. Therefore, no commas are used.

 

Constraints:

  • 1 <= n <= 105

Approach Overview

Problem Overview: Given two integers low and high, count how many comma separators appear if every number in that inclusive range is written using standard thousands formatting (e.g., 1,000, 1,000,000). Each number contributes floor((digits-1)/3) commas based on its digit length.

Approach 1: Brute Force Formatting (O(n log n) time, O(1) space)

Iterate through every integer from low to high. For each number, compute its digit length using log10 or string conversion. The number of commas contributed by that value equals (digits - 1) / 3 using integer division. Accumulate this value across the entire range. This approach is easy to reason about but becomes slow when the range is large because you process every number individually.

Approach 2: Digit Range Counting / Brain Teaser (O(log n) time, O(1) space)

Instead of iterating through each value, group numbers by digit length. All numbers with the same digit length produce the same number of commas. For example, numbers with 4–6 digits each contain one comma, while numbers with 7–9 digits contain two. Compute how many numbers of each digit length fall within [low, high], then multiply by floor((digits-1)/3). This converts the problem into counting intervals like [1000, 999999] intersected with the query range.

This method works because comma placement depends only on digit length, not the specific number. The number of digit groups is small (at most around 12–18 for typical constraints), so you only iterate through digit boundaries rather than the full range. The technique relies on simple arithmetic and is a common pattern in math and counting problems where ranges can be aggregated instead of enumerated.

Recommended for interviews: The digit-range math approach. Brute force shows the correct observation about how commas relate to digit length, but the optimized solution demonstrates stronger math reasoning and range counting skills. Interviewers typically expect the O(log n) grouping method once you recognize that comma count depends only on the number of digits.

Solution

Numbers from 1 to 999 contain no commas, so when n is less than or equal to 999, the answer is 0.

Since the range of n is [1, 10^5], when n is greater than or equal to 1000, each number contains exactly one comma, so the answer is n - 999.

Therefore, the answer is max(0, n - 999).

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force IterationO(n log n)O(1)Small ranges where iterating every number is acceptable
Digit Range Counting (Brain Teaser)O(log n)O(1)Large ranges where grouping by digit length avoids iterating through every value

Video Solution

3870. Count Commas in Range (Leetcode Easy)Programming Live with Larry549 views views

Watch 5 more video solutions →

Frequently Asked Questions

Is Count Commas in Range easy or hard?
Count Commas in Range is classified as an Easy problem. The main insight is recognizing that comma placement depends only on the number of digits. Once you group numbers by digit length, the implementation becomes a straightforward math calculation.
Count Commas in Range Python/Java solution
Implement the math approach by iterating over possible digit lengths, computing the overlap of each digit interval with [low, high], and multiplying by the comma count floor((digits-1)/3). The same logic translates directly across Python, Java, C++, Go, and TypeScript since it only uses integer arithmetic.
How to solve Count Commas in Range in O(log n)?
Calculate the digit length ranges that intersect with the query interval [low, high]. For each digit length d, determine how many numbers in the range have that length. Each of those numbers contributes floor((d-1)/3) commas. Summing these contributions across all digit groups produces the final answer in logarithmic time.
What is the best approach for Count Commas in Range?
The optimal solution groups numbers by digit length and counts how many numbers of each length fall within the range. Every number with the same digit length produces the same number of commas, equal to floor((digits-1)/3). By multiplying this comma count with the number of values in each digit interval, the problem can be solved in O(log n) time and O(1) space.
Is Count Commas in Range asked at Google/Amazon/Meta?
This type of question appears as a math-style brain teaser in interviews that test number properties and range counting. Similar problems have been asked at companies like Amazon and Google where candidates are expected to replace brute force iteration with mathematical aggregation.
What data structure is used in Count Commas in Range?
No complex data structure is required. The solution relies on arithmetic calculations, digit length checks, and simple range counting. The core idea comes from math and number grouping rather than arrays, trees, or hash maps.
What is the time complexity of Count Commas in Range?
The optimized math approach runs in O(log n) time because it only iterates through digit-length boundaries such as 1–3 digits, 4–6 digits, and so on. The brute force approach takes O(n log n) time since it processes every number in the range and computes its digit length individually.

Ready to solve this problem?

Practice Count Commas in Range with our built-in code editor and test cases.

Practice on FleetCode