Count Symmetric Integers

Easy
570.

Count Symmetric Integers

Count Symmetric Integers is an easy coding problem on math and enumeration, asked in Amazon and Infosys interviews.

An integer x is symmetric if it has an even number of digits 2 * n and the sum of its first n digits equals the sum of its last n digits. Numbers with an odd digit count are never symmetric.

Return how many integers in the inclusive range [low, high] are symmetric.

Example 1

Input
low = 1, high = 100
Output
9
Explanation
11, 22, 33, 44, 55, 66, 77, 88 and 99.

Example 2

Input
low = 1200, high = 1230
Output
4
Explanation
1203, 1212, 1221 and 1230.

Example 3

Input
low = 100, high = 999
Output
0
Explanation
Every value has three digits.

Constraints

  • 1 <= low <= high <= 10000
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number} low
 * @param {number} high
 * @return {number}
 */
var countSymmetricIntegers = function(low, high) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Count Symmetric Integers — Solution in Python, Java & C++

Count Symmetric Integers solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Count Symmetric Integers is an easy coding problem on math and enumeration, asked in Amazon and Infosys interviews.

  • Difficulty: Easy
  • Topics: Math, Enumeration
  • Asked at: Amazon, Infosys
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

An integer x is symmetric if it has an even number of digits 2 * n and the sum of its first n digits equals the sum of its last n digits. Numbers with an odd digit count are never symmetric.

Return how many integers in the inclusive range [low, high] are symmetric.

Example 1

Input: low = 1, high = 100
Output: 9
Explanation: 11, 22, 33, 44, 55, 66, 77, 88 and 99.

Example 2

Input: low = 1200, high = 1230
Output: 4
Explanation: 1203, 1212, 1221 and 1230.

Example 3

Input: low = 100, high = 999
Output: 0
Explanation: Every value has three digits.

Constraints

  • 1 <= low <= high <= 10000

Hints for Count Symmetric Integers

  1. Hint 1

    The range is at most 10,000 values, so testing each one is fine.

  2. Hint 2

    Reject an odd digit count immediately.

  3. Hint 3

    Otherwise split the digit string in half and compare the two sums.

How to solve Count Symmetric Integers

A direct scan over the range with a cheap per-number test — the digit count filter throws away most candidates instantly.

Approach

  1. Loop n from low to high.
  2. Convert n to a string; skip it if the length is odd.
  3. Sum the first half's digits and the second half's digits.
  4. Count the number when the two sums match.

Why it works

The definition is checked literally for every candidate, so the count is exact. The odd-length short-circuit keeps the inner work small.

Complexity

  • Time — O((high - low) · d)
  • Space — O(1)

Pitfalls

  • Confusing symmetric with palindromic — 1203 is symmetric but not a palindrome.
  • Forgetting to skip odd-length numbers makes the halves ambiguous.
  • The range is inclusive at both ends.

Reference solution

Python

def countSymmetricIntegers(low: int, high: int) -> int:
    count = 0
    for n in range(low, high + 1):
        s = str(n)
        if len(s) % 2 != 0:
            continue
        half = len(s) // 2
        if sum(int(c) for c in s[:half]) == sum(int(c) for c in s[half:]):
            count += 1
    return count

Java

public static int countSymmetricIntegers(int low, int high) {
    int count = 0;
    for (int n = low; n <= high; n++) {
        String s = Integer.toString(n);
        if (s.length() % 2 != 0) continue;
        int half = s.length() / 2;
        int a = 0, b = 0;
        for (int i = 0; i < half; i++) a += s.charAt(i) - '0';
        for (int i = half; i < s.length(); i++) b += s.charAt(i) - '0';
        if (a == b) count++;
    }
    return count;
}

C++

int countSymmetricIntegers(int low, int high) {
    int count = 0;
    for (int n = low; n <= high; n++) {
        string s = to_string(n);
        if (s.size() % 2 != 0) continue;
        int half = (int) s.size() / 2;
        int a = 0, b = 0;
        for (int i = 0; i < half; i++) a += s[i] - '0';
        for (int i = half; i < (int) s.size(); i++) b += s[i] - '0';
        if (a == b) count++;
    }
    return count;
}

JavaScript

var countSymmetricIntegers = function(low, high) {
    let count = 0;
    for (let n = low; n <= high; n++) {
        const s = String(n);
        if (s.length % 2 !== 0) continue;
        const half = s.length / 2;
        let a = 0, b = 0;
        for (let i = 0; i < half; i++) a += s.charCodeAt(i) - 48;
        for (let i = half; i < s.length; i++) b += s.charCodeAt(i) - 48;
        if (a === b) count++;
    }
    return count;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 337 math problems · the whole catalogue