Watch 10 video solutions for Sum of k-Mirror Numbers, a hard level problem involving Math, Enumeration. This walkthrough by codestorywithMIK has 11,411 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
9 is a 2-mirror number. The representation of 9 in base-10 and base-2 are 9 and 1001 respectively, which read the same both forward and backward.4 is not a 2-mirror number. The representation of 4 in base-2 is 100, 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 <= 30Problem 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 | 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. |