#2843
Count Symmetric Integers
EasyMathEnumeration
Approaches
💡
Intuition
Time Space
The brute force approach checks each number in the range from low to high. For each number, it converts it to a string, splits it into two halves, and compares the sums of the two halves to determine if it's symmetric.
⚙️
Algorithm
6 steps- 1Step 1: Initialize a counter to zero.
- 2Step 2: Loop through each number from low to high.
- 3Step 3: Convert the number to a string and check if its length is even.
- 4Step 4: Split the string into two halves and calculate the sum of digits for both halves.
- 5Step 5: If the sums are equal, increment the counter.
- 6Step 6: Return the counter.
solution.py12 lines
1# Full working Python code
2low, high = 1, 100
3count = 0
4for num in range(low, high + 1):
5 s = str(num)
6 if len(s) % 2 == 0:
7 mid = len(s) // 2
8 left_sum = sum(int(d) for d in s[:mid])
9 right_sum = sum(int(d) for d in s[mid:])
10 if left_sum == right_sum:
11 count += 1
12print(count)Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.