LeetCode Entry

2843. Count Symmetric Integers

11.04.2025 easy 2025 kotlin rust

Numbers with equal half' sums

2843. Count Symmetric Integers easy blog post substack youtube 1.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/955

Problem TLDR

Numbers with equal half’ sums #easy

Intuition

Brute. Force.

Approach

  • convert to string and don’t

Complexity

  • Time complexity: \(O((h - l)lg(h))\)

  • Space complexity: \(O(1)\)

Code


    fun countSymmetricIntegers(low: Int, high: Int) =
        (low..high).count {
            val s =  "$it".map { it - '0' }; val n = s.size
            n % 2 < 1 && s.sum() == s.take(n / 2).sum() * 2
        }


    pub fn count_symmetric_integers(low: i32, high: i32) -> i32 {
        (low..=high).filter(|&x| {
            let (mut a, mut b, mut s, mut h, mut c) = (x, x, 0, 0, 0);
            while a > 0  { s += a % 10; a /= 10; c += 1;
                         if c % 2 > 0 { h += b % 10; b /= 10 }}
            c & 1 < 1 && s == h * 2
        }).count() as _
    }


    int countSymmetricIntegers(int low, int high) {
        int r = 0;
        for (int x = low; x <= high; ++x) {
            int a = x, b = x, s = 0, h = 0, c = 0;
            while (a) {
                s += a % 10; a /= 10; ++c;
                if (c % 2) { h += b % 10; b /= 10; }
            }
            r += c % 2 < 1 && s == h * 2;
        }
        return r;
    }