Count Commas in Range - Solution & Explanation
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
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Iteration | O(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 Larry • 549 views views
Watch 5 more video solutions →Frequently Asked Questions
Is Count Commas in Range easy or hard?
Count Commas in Range Python/Java solution
How to solve Count Commas in Range in O(log n)?
What is the best approach for Count Commas in Range?
Is Count Commas in Range asked at Google/Amazon/Meta?
What data structure is used in Count Commas in Range?
What is the time complexity of Count Commas in Range?
Ready to solve this problem?
Practice Count Commas in Range with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor