2843. Count Symmetric Integers
MediumView on LeetCode
Time: O(n * d)
Space: O(1)
Problem Overview
Count symmetric integers (equal digit sums in first and second halves).
Intuition
Count symmetric integers (equal digit sums in first and second halves). Only even-digit numbers qualify.
Algorithm
- 1For each n in [low, high]: if odd digits: skip. Sum first half digits = sum second half digits.
Common Pitfalls
- •Only even-length numbers can be symmetric. Convert to string, split halves, compare sums.
2843.cs
C#
// Approach: For even-digit numbers check if digit sum of first half equals second half.
// Time: O(n * d) Space: O(1)
public class Solution
{
public int CountSymmetricIntegers(int low, int high)
{
int ans = 0;
for (int num = low; num <= high; ++num)
{
if (IsSymmetricInteger(num))
++ans;
}
return ans;
}
private bool IsSymmetricInteger(int num)
{
if (num >= 10 && num <= 99)
return num / 10 == num % 10;
if (num >= 1000 && num <= 9999)
{
int left = num / 100;
int right = num % 100;
return left / 10 + left % 10 == right / 10 + right % 10;
}
return false;
}
}Was this solution helpful?
Related Problems
- 12. Integer to Roman(Medium)
- 13. Roman to Integer(Easy)
- 29. Divide Two Integers(Medium)
- 66. Plus One(Easy)
- 67. Add Binary(Easy)
- 70. Climbing Stairs(Easy)