{x}
blog image

Sum of k-Mirror Numbers

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, 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.
  • On the contrary, 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 <= 9
  • 1 <= n <= 30

Solution Explanation

This problem asks to find the sum of the n smallest k-mirror numbers. A k-mirror number is a positive integer that reads the same forwards and backward in base-10 and base-k. The solution efficiently generates these numbers and sums them.

Approach

The solution cleverly iterates through potential k-mirror numbers by constructing palindromes. It does this by considering the first half of the number and then mirroring it to create a full palindrome. This significantly reduces the search space compared to checking all numbers.

The algorithm works as follows:

  1. Iterate through lengths: The outer loop iterates through the lengths (l) of potential k-mirror numbers. This is because a k-mirror number's base-10 representation and base-k representation both need to be palindromes. The length determines the number of digits.

  2. Generate palindromes: The inner loop generates potential palindromes in base 10. It uses Math.pow(10, (l - 1) / 2) and Math.pow(10, (l + 1) / 2) to determine the range of numbers to consider for the first half of the palindrome. The code then mirrors this first half to create the full palindrome (v).

  3. Check in base-k: The generated palindrome (v) is converted to base-k using Long.toString(v, k). The check function then verifies if this base-k representation is also a palindrome.

  4. Sum and count: If both base-10 and base-k representations are palindromes, the number v is added to the running sum ans. The counter n is decremented. The loop continues until n reaches 0 (we've found n k-mirror numbers).

  5. Return the sum: Finally, the function returns the total sum ans.

Code Explanation (Java)

class Solution {
    public long kMirror(int k, int n) {
        long ans = 0; // Initialize the sum of k-mirror numbers
        for (int l = 1;; ++l) { // Iterate through lengths of numbers
            int x = (int) Math.pow(10, (l - 1) / 2); // Lower bound for the first half of the palindrome
            int y = (int) Math.pow(10, (l + 1) / 2); // Upper bound for the first half of the palindrome
            for (int i = x; i < y; i++) { // Iterate through possible first halves
                long v = i; // Start with the first half
                for (int j = l % 2 == 0 ? i : i / 10; j > 0; j /= 10) { // Mirror the first half to create a palindrome
                    v = v * 10 + j % 10; // Append the mirrored digits
                }
                String ss = Long.toString(v, k); // Convert the palindrome to base-k
                if (check(ss.toCharArray())) { // Check if the base-k representation is also a palindrome
                    ans += v; // Add to the sum
                    if (--n == 0) { // Decrement n; if we've found enough numbers, return the sum
                        return ans;
                    }
                }
            }
        }
    }
 
    private boolean check(char[] c) { // Helper function to check if a string is a palindrome
        for (int i = 0, j = c.length - 1; i < j; i++, j--) {
            if (c[i] != c[j]) { // Compare characters from both ends
                return false;
            }
        }
        return true; // It's a palindrome
    }
}

Time Complexity Analysis

The time complexity is difficult to express precisely due to the nested loops and the unpredictable number of k-mirror numbers. However, it's bounded by the number of digits considered and the base k. In the worst case (large n and large k), the complexity could approach O(10m * logk(10m)) where m is the maximum number of digits needed. This is because the outer loop iterates over the number of digits, and the inner loop iterates over numbers with that many digits. The conversion to base k takes logarithmic time. However, due to efficient palindrome generation, it's significantly faster than a brute-force approach that checks every number.

Space Complexity Analysis

The space complexity is dominated by the conversion to base-k and the palindrome check, which use space proportional to the maximum number of digits in the largest k-mirror number encountered. This is approximately O(log n), where n is the input n. This is because the number of digits in the largest considered number grows logarithmically with n.