Leetcode 2843: Count Symmetric Integers

grid47
grid47
Exploring patterns and algorithms
Jan 27, 2024 5 min read

You are given two integers low and high. Return the number of symmetric integers in the range [low, high]. A symmetric integer has an even number of digits, and the sum of the first half of its digits equals the sum of the second half. Numbers with an odd number of digits are never symmetric.
Problem
Approach
Steps
Complexity
Input: The input consists of two integers, low and high, representing the range of numbers to check for symmetry.
Example: low = 1, high = 50
Constraints:
• 1 <= low <= high <= 10^4
Output: Return the number of symmetric integers within the range [low, high].
Example: 4
Constraints:
• The output should be a single integer indicating the number of symmetric numbers in the range.
Goal: To find the count of symmetric integers in the given range [low, high].
Steps:
• Loop through all integers in the range [low, high].
• Convert each integer to a string to check its symmetry.
• Check if the number has an even number of digits.
• For numbers with an even number of digits, check if the sum of the first half of digits equals the sum of the second half.
Goal: The input range [low, high] has constraints as follows:
Steps:
• 1 <= low <= high <= 10^4
• The number of digits in low and high is at most 4.
Assumptions:
• The input will always contain valid positive integers.
• The number of digits for each number is not too large, ensuring the solution can run efficiently.
Input: low = 1, high = 50
Explanation: The symmetric numbers between 1 and 50 are: 11, 22, 33, and 44. These numbers have an even number of digits, and the sum of the digits in the first half is equal to the sum of the digits in the second half.

Input: low = 800, high = 850
Explanation: The symmetric numbers between 800 and 850 are: 808, 818. These numbers are symmetric because they have an even number of digits, and the sum of the first half equals the sum of the second half.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus