Back to September Back to Year 2026
EASY 08 Sep 2026 View on LeetCode

3870. Count Commas in Range

</> Solution

/**
 * Problem: LeetCode 3870 - Count Commas in Range
 * Difficulty: Easy
 * Language: Java
 * Author: CodeByTushu
 */
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(1)

SPACE COMPLEXITY

O(1)

TOPICS

Math