Sum of k-Mirror Numbers - Solution & Explanation
Problem Statement
A k-mirror number is a positive integer without leading zeros that reads the same both forward and backward in base-10 as well as in base-k.
- For example,
9is a 2-mirror number. The representation of9in base-10 and base-2 are9and1001respectively, which read the same both forward and backward. - On the contrary,
4is not a 2-mirror number. The representation of4in base-2 is100, which does not read the same both forward and backward.
Given the base k and the number n, return the sum of the n smallest k-mirror numbers.
Example 1:
Input: k = 2, n = 5
Output: 25
Explanation:
The 5 smallest 2-mirror numbers and their representations in base-2 are listed as follows:
base-10 base-2
1 1
3 11
5 101
7 111
9 1001
Their sum = 1 + 3 + 5 + 7 + 9 = 25.
Example 2:
Input: k = 3, n = 7
Output: 499
Explanation:
The 7 smallest 3-mirror numbers are and their representations in base-3 are listed as follows:
base-10 base-3
1 1
2 2
4 11
8 22
121 11111
151 12121
212 21212
Their sum = 1 + 2 + 4 + 8 + 121 + 151 + 212 = 499.
Example 3:
Input: k = 7, n = 17 Output: 20379000 Explanation: The 17 smallest 7-mirror numbers are: 1, 2, 3, 4, 5, 6, 8, 121, 171, 242, 292, 16561, 65656, 2137312, 4602064, 6597956, 6958596
Constraints:
2 <= k <= 91 <= n <= 30
Approach Overview
Problem Overview: A k-mirror number is a positive integer that is palindromic in both base‑10 and base‑k. Given integers k and n, compute the sum of the first n numbers that satisfy this property. Since n ≤ 30, the challenge is not large input size but efficiently generating candidates without scanning huge ranges of integers.
Approach 1: Brute-force Generation (Time: O(X log X), Space: O(1))
The direct strategy iterates through natural numbers starting from 1. For each number, check if it is a palindrome in base‑10 by comparing characters from both ends. Then convert the number to base‑k and check if the resulting representation is also a palindrome. If both conditions hold, add the number to the result and continue until n valid numbers are found.
This approach relies heavily on repeated base conversions and palindrome checks. Each conversion takes O(log_k V) time where V is the current number. Since many numbers are rejected before finding valid k‑mirror numbers, the search range grows quickly. The method is conceptually simple and useful for understanding the property of k‑mirror numbers, but it wastes time testing numbers that cannot possibly be valid.
Approach 2: Palindrome Construction (Time: O(n log_k V), Space: O(1))
A more efficient strategy generates only numbers that are already palindromes in base‑10. Instead of scanning every integer, construct palindromes digit by digit. For a given length, build the first half and mirror it to produce the full number. This enumeration guarantees every candidate is a base‑10 palindrome.
For each generated palindrome, convert it to base‑k and verify whether that representation is also a palindrome. If it passes the check, add it to the running sum. Continue generating palindromes in increasing order of length until n valid numbers are collected.
The key insight is reducing the search space. Palindromes are extremely sparse compared to all integers, so constructing them directly avoids millions of unnecessary checks. Base conversion still costs O(log_k V), but the number of candidates tested is much smaller.
Because the solution revolves around generating numeric patterns and verifying symmetry, the problem fits well under math and enumeration. The palindrome property itself is another key concept often reused in problems involving digit manipulation and number construction, closely related to techniques from palindrome problems.
Recommended for interviews: The palindrome construction approach is the one interviewers expect. Brute force demonstrates understanding of base conversion and palindrome checks, but constructing palindromes shows stronger algorithmic thinking because you shrink the candidate space dramatically. In practice, generating base‑10 palindromes and validating them in base‑k gives a clean and efficient solution that easily handles the constraint of finding the first 30 k‑mirror numbers.
Approach 1: Brute-force Generation
This approach involves a brute-force method where we generate all numbers starting from 1 and check if they are palindromic in both base-10 and base-k. We keep a count until we reach the desired number of k-mirror numbers.
This solution defines functions to check for palindromes and convert numbers to base-k. By iterating through numbers and checking both conditions, we ensure the numbers are k-mirror.
Code
Python
C++
Java
JavaScript
C#
Complexity
Time Complexity: O(n*m), where n is the number of k-mirror numbers required, and m depends on the number being iterated over. Space Complexity: O(1) for space used in calculations.
Approach 2: Palindrome Construction
This approach optimizes the search by constructing palindromic numbers directly. For each half-length, generate palindromes and convert to base-k to verify the k-mirror condition. This approach eliminates non-palindromes from checks.
In this Python solution, we pre-generate numeric palindromes instead of inspecting each number linearly, thereby enhancing efficiency. By iterating over possible palindromic lengths, we streamline the search space for k-mirror numbers.
Code
Python
C++
Java
JavaScript
C#
Complexity
Time Complexity: Depending on the implementation details of palindrome generation and validation, it is much less than brute-force for larger n. Space Complexity: Depends on palindromic generation results.
Approach 3: Half Enumeration + Mathematics
For a k-mirror number, we can divide it into two parts: the first half and the second half. For numbers with even length, the first and second halves are exactly the same; for numbers with odd length, the first and second halves are the same, but the middle digit can be any digit.
We can enumerate the numbers in the first half, and then construct the complete k-mirror number based on the first half. The specific steps are as follows:
- Enumerate Lengths: Start enumerating the length of the numbers from 1, until we find enough k-mirror numbers that meet the requirements.
- Calculate the Range of the First Half: For a number of length
l, the range of the first half is[10^{(l-1)/2}, 10^{(l+1)/2}). - Construct k-Mirror Numbers: For each number
iin the first half, if the length is even, useidirectly as the first half; if the length is odd, divideiby 10 to get the first half. Then reverse the digits of the first half and append them to form the complete k-mirror number. - Check k-Mirror Numbers: Convert the constructed number to base
kand check whether it is a palindrome. - Accumulate the Result: If it is a k-mirror number, add it to the result and decrease the counter
n. Whennreaches 0, return the result.
The time complexity mainly depends on the length being enumerated and the range of the first half. Since the maximum value of n is 30, the number of enumerations is limited in practice. The space complexity is O(1), since only a constant amount of extra space is
Code
Python
Java
C++
Go
TypeScript
Complexity Comparison
| Approach | Complexity |
|---|---|
| Brute-force Generation | Time Complexity: O(n*m), where n is the number of k-mirror numbers required, and m depends on the number being iterated over. Space Complexity: O(1) for space used in calculations. |
| Palindrome Construction | Time Complexity: Depending on the implementation details of palindrome generation and validation, it is much less than brute-force for larger n. Space Complexity: Depends on palindromic generation results. |
| Half Enumeration + Mathematics | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute-force Generation | O(X log X) | O(1) | Useful for understanding the property of k‑mirror numbers or when constraints are extremely small. |
| Palindrome Construction | O(n log_k V) | O(1) | Best approach. Generates only base‑10 palindromes and checks base‑k symmetry, drastically reducing candidate numbers. |
Video Solution
Sum of k-Mirror Numbers | Super Detailed | Minute Details | Leetcode 2081 | codestorywithMIK • codestorywithMIK • 11,489 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Sum of k-Mirror Numbers easy or hard?
Sum of k-Mirror Numbers Python/Java solution
How to solve Sum of k-Mirror Numbers in O(n)?
What is the best approach for Sum of k-Mirror Numbers?
Is Sum of k-Mirror Numbers asked at Google/Amazon/Meta?
What data structure is used in Sum of k-Mirror Numbers?
What is the time complexity of Sum of k-Mirror Numbers?
Ready to solve this problem?
Practice Sum of k-Mirror Numbers with our built-in code editor and test cases.
Practice on FleetCode