You are given two positive integers low
and high
.
An integer x
consisting of 2 * n
digits is symmetric if the sum of the first n
digits of x
is equal to the sum of the last n
digits of x
. Numbers with an odd number of digits are never symmetric.
Return the number of symmetric integers in the range [low, high]
.
For example:
low = 1
and high = 100
, the output should be 9. The symmetric integers are: 11, 22, 33, 44, 55, 66, 77, 88, and 99.low = 1200
and high = 1230
, the output should be 4. The symmetric integers are: 1203, 1212, 1221, and 1230.How would you approach this problem efficiently, considering that 1 <= low <= high <= 10^4
?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to counting symmetric integers involves checking every single number within the given range. For each number, we determine if it's symmetric based on a specific rule: If the sum of the digits in the first half of the number equals the sum of the digits in the second half, the number is symmetric.
Here's how the algorithm would work step-by-step:
def count_symmetric_integers_brute_force(low, high):
symmetric_integer_count = 0
for current_number in range(low, high + 1):
number_string = str(current_number)
number_length = len(number_string)
# Symmetric integers must have an even number of digits
if number_length % 2 != 0:
continue
half_length = number_length // 2
first_half = number_string[:half_length]
second_half = number_string[half_length:]
first_half_sum = 0
for digit in first_half:
first_half_sum += int(digit)
second_half_sum = 0
for digit in second_half:
second_half_sum += int(digit)
# Check if the number is symmetric based on the sums
if first_half_sum == second_half_sum:
symmetric_integer_count += 1
return symmetric_integer_count
To efficiently count symmetric integers within a range, we first need to define what makes an integer symmetric. Then, instead of checking every number in the range individually, we will identify patterns and constraints that allow us to quickly recognize or generate symmetric numbers, significantly reducing the number of checks needed.
Here's how the algorithm would work step-by-step:
def count_symmetric_integers(low_range, high_range):
symmetric_count = 0
for number in range(low_range, high_range + 1):
number_string = str(number)
number_length = len(number_string)
# Symmetry requires at least two digits
if number_length < 2:
continue
if number_length % 2 == 0:
# Check for even-length symmetry
left_half = number_string[:number_length // 2]
right_half = number_string[number_length // 2:]
if left_half == right_half[::-1]:
symmetric_count += 1
else:
# Odd length numbers cannot be symmetric.
continue
return symmetric_count
Case | How to Handle |
---|---|
low > high | Return 0 immediately as the input range is invalid. |
low == high and low is a symmetric number | Return 1 if low is a symmetric number, otherwise 0. |
low and high are single-digit numbers | Return 0 because no single-digit number is symmetric according to the problem definition. |
low is a single-digit number and high is a two-digit or larger number. | Start the iteration from 11 (smallest two digit symmetric integer) if low is less than 11. |
Integer overflow when reversing a large number | Use a data type that can accommodate larger values or handle overflow appropriately (e.g., use long or check for overflow during reversal). |
Input range contains leading zeros | The definition in the prompt implies that we're given integers, and integer data types don't have leading zeroes, thus, not an issue. |
All numbers within the range are not symmetric | The algorithm should correctly iterate through the range and return 0 if no symmetric numbers are found. |
Maximum possible values for low and high (close to Integer.MAX_VALUE) | Ensure the reversal and comparison logic doesn't cause integer overflow or incorrect results at the boundaries. |