Back to Month
EASY 08 Sep 2026 View on LeetCode

3870. Count Commas in Range

</> Solution

class Solution {
    public int countCommas(int n) {
        int result = 0;
        long threshold = 1000; 
        while (threshold <= n) {
            result += (n - threshold + 1);
            threshold *= 1000;
        }
        return result;
    }
}

TIME COMPLEXITY

O(log₁₀₀₀(n))

SPACE COMPLEXITY

O(1)

TOPICS

Counting Math Simulation