Skip to main content
Back to problems
Leetcode
Medium
Math
Number Theory
Combinatorics
Count Commas in Range

Count how many comma characters appear when writing every integer in an inclusive range with standard thousands separators.

Acceptance 0%
Problem Statement

Count Commas in Range

You are given two integers l and r with l <= r. Consider writing every integer from l to r inclusive using standard decimal formatting with commas as thousands separators.

For example:

  • 1000 is written as 1,000
  • 1234567 is written as 1,234,567
  • 999 has no comma

Your task is to compute the total number of comma characters that appear across all numbers in the range.

Return the total count.

Notes

  • Only commas in the usual thousands-separator positions are counted.
  • No commas are inserted for numbers below 1000.
  • The range is inclusive.

Input Format

  • The first line contains two integers l and r.
  • l <= r.

Output Format

  • Return a single integer: the total number of comma characters used when formatting all integers in [l, r] inclusive.

Constraints

  • 0 <= l <= r
  • The answer fits in a 64-bit signed integer.
  • The range may be large enough that iterating over every number is not intended.
Examples
Sample cases returned by the problem API.

Example 1

Input

l = 1, r = 20

Output

0

Explanation

All numbers from 1 to 20 have fewer than 1000, so none contain commas.

Example 2

Input

l = 998, r = 1002

Output

3

Explanation

998 and 999 have 0 commas. 1000, 1001, and 1002 each have 1 comma. Total = 3.

Show 1 more example

Example 3

Input

l = 1000, r = 2005

Output

1006

Explanation

Every number from 1000 to 2005 contains exactly one comma, and there are 1006 such numbers.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.