From 26ba2df6d1b83d71c4232207f979e4631605c34a Mon Sep 17 00:00:00 2001 From: Srishti Sinha Date: Mon, 23 Jun 2025 09:33:17 +0530 Subject: [PATCH 1/6] Create README.md --- 2081. Sum of k-Mirror Numbers/README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 2081. Sum of k-Mirror Numbers/README.md diff --git a/2081. Sum of k-Mirror Numbers/README.md b/2081. Sum of k-Mirror Numbers/README.md new file mode 100644 index 00000000..25bdfcea --- /dev/null +++ b/2081. Sum of k-Mirror Numbers/README.md @@ -0,0 +1,14 @@ +# 2081. Sum of k-Mirror Numbers +Hard +--- +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. Its representation in base-10 is 9, and in base-2 is 1001, both of which are palindromes. + +- 4 is not a 2-mirror number because its base-2 form is 100, which is not a palindrome. + +Given the base k and the number n, return the sum of the n smallest k-mirror numbers. + + From f6aa0694f82c66d6885a02249979cef44621992e Mon Sep 17 00:00:00 2001 From: Srishti Sinha Date: Mon, 23 Jun 2025 09:33:44 +0530 Subject: [PATCH 2/6] Update README.md --- 2081. Sum of k-Mirror Numbers/README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/2081. Sum of k-Mirror Numbers/README.md b/2081. Sum of k-Mirror Numbers/README.md index 25bdfcea..5d68c9f7 100644 --- a/2081. Sum of k-Mirror Numbers/README.md +++ b/2081. Sum of k-Mirror Numbers/README.md @@ -11,4 +11,18 @@ For example: 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 base-2 representations are: + + base-10 base-2 + 1 1 + 3 11 + 5 101 + 7 111 + 9 1001 + +Sum = 1 + 3 + 5 + 7 + 9 = 25. From 45f6746decf432583c8d67190c9993e7efc4bfeb Mon Sep 17 00:00:00 2001 From: Srishti Sinha Date: Mon, 23 Jun 2025 09:34:47 +0530 Subject: [PATCH 3/6] Update README.md --- 2081. Sum of k-Mirror Numbers/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/2081. Sum of k-Mirror Numbers/README.md b/2081. Sum of k-Mirror Numbers/README.md index 5d68c9f7..c53e90cb 100644 --- a/2081. Sum of k-Mirror Numbers/README.md +++ b/2081. Sum of k-Mirror Numbers/README.md @@ -11,7 +11,8 @@ For example: Given the base k and the number n, return the sum of the n smallest k-mirror numbers. -# Example 1 +### Example 1 +``` Input: k = 2, n = 5 Output: 25 Explanation: @@ -25,4 +26,5 @@ The 5 smallest 2-mirror numbers and their base-2 representations are: 9 1001 Sum = 1 + 3 + 5 + 7 + 9 = 25. +``` From 9d572e36abaef5f20b03e62deb9e24de8a10cae7 Mon Sep 17 00:00:00 2001 From: Srishti Sinha Date: Mon, 23 Jun 2025 09:36:18 +0530 Subject: [PATCH 4/6] Update README.md --- 2081. Sum of k-Mirror Numbers/README.md | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/2081. Sum of k-Mirror Numbers/README.md b/2081. Sum of k-Mirror Numbers/README.md index c53e90cb..ff67317e 100644 --- a/2081. Sum of k-Mirror Numbers/README.md +++ b/2081. Sum of k-Mirror Numbers/README.md @@ -28,3 +28,37 @@ The 5 smallest 2-mirror numbers and their base-2 representations are: Sum = 1 + 3 + 5 + 7 + 9 = 25. ``` +### Example 2 +``` +Input: k = 3, n = 7 +Output: 499 +Explanation: +The 7 smallest 3-mirror numbers and their base-3 representations are: + + base-10 base-3 + 1 1 + 2 2 + 4 11 + 8 22 + 121 11111 + 151 12121 + 212 21212 + +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 + +``` From 59cdb963e54fc7af4fa1b3cc58fab41812271d5c Mon Sep 17 00:00:00 2001 From: Srishti Sinha Date: Mon, 23 Jun 2025 09:36:44 +0530 Subject: [PATCH 5/6] Update README.md --- 2081. Sum of k-Mirror Numbers/README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/2081. Sum of k-Mirror Numbers/README.md b/2081. Sum of k-Mirror Numbers/README.md index ff67317e..01dd87bb 100644 --- a/2081. Sum of k-Mirror Numbers/README.md +++ b/2081. Sum of k-Mirror Numbers/README.md @@ -57,8 +57,6 @@ The 17 smallest 7-mirror numbers are: # Constraints ``` -- 2 <= k <= 9 - -- 1 <= n <= 30 - +2 <= k <= 9 +1 <= n <= 30 ``` From 813fa865a4f8a33702bf35310553d1933f3b8059 Mon Sep 17 00:00:00 2001 From: Srishti Sinha Date: Mon, 23 Jun 2025 09:40:24 +0530 Subject: [PATCH 6/6] Add files via upload --- 2081. Sum of k-Mirror Numbers/Solution.java | 58 +++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 2081. Sum of k-Mirror Numbers/Solution.java diff --git a/2081. Sum of k-Mirror Numbers/Solution.java b/2081. Sum of k-Mirror Numbers/Solution.java new file mode 100644 index 00000000..44e93f9e --- /dev/null +++ b/2081. Sum of k-Mirror Numbers/Solution.java @@ -0,0 +1,58 @@ +public class Solution { + public long kMirror(int k, int n) { + long ans = 0; + int cnt = 0; + long[] pow10 = new long[12]; + pow10[0] = 1; + for (int i = 1; i < 12; i++) pow10[i] = pow10[i - 1] * 10; + + while (cnt < n) { + int len = 1; + for (; ; len++) { + int half = (len + 1) / 2; + long start = pow10[half - 1], end = pow10[half]; + for (long h = (half == 1 ? 1 : start); h < end && cnt < n; h++) { + long t = h, rev = 0, x = h; + if ((len & 1) == 1) t /= 10; + while (t > 0) { + rev = rev * 10 + t % 10; + t /= 10; + } + long p = x * pow10[len - half] + rev; + if (isPal(p, k)) { + ans += p; + cnt++; + if (cnt == n) return ans; + } + } + } + } + return ans; + } + + private boolean isPal(long x, int k) { + long t = x, rev = 0; + while (t > 0) { + rev = rev * k + t % k; + t /= k; + } + return rev == x; + } + + // Main function to test the solution + public static void main(String[] args) { + Solution sol = new Solution(); + + int k1 = 2, n1 = 5; + System.out.println("Input: k = " + k1 + ", n = " + n1); + System.out.println("Output: " + sol.kMirror(k1, n1)); // Expected: 25 + + int k2 = 3, n2 = 7; + System.out.println("\nInput: k = " + k2 + ", n = " + n2); + System.out.println("Output: " + sol.kMirror(k2, n2)); // Expected: 499 + + int k3 = 7, n3 = 17; + System.out.println("\nInput: k = " + k3 + ", n = " + n3); + System.out.println("Output: " + sol.kMirror(k3, n3)); // Expected: 20379000 + } +}