<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Berlin Techs</title>
    <description>The latest articles on DEV Community by Berlin Techs (@berlin_techs_e93c88bf0a5b).</description>
    <link>https://dev.to/berlin_techs_e93c88bf0a5b</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3298954%2Ff20e589c-bd57-43e6-827d-9ab0ca103302.png</url>
      <title>DEV Community: Berlin Techs</title>
      <link>https://dev.to/berlin_techs_e93c88bf0a5b</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/berlin_techs_e93c88bf0a5b"/>
    <language>en</language>
    <item>
      <title>🏂 Beginner-Friendly Guide: "Sum of k-Mirror Numbers" – LeetCode 2081 (python| JavaScript | Python )</title>
      <dc:creator>Berlin Techs</dc:creator>
      <pubDate>Fri, 27 Jun 2025 05:13:41 +0000</pubDate>
      <link>https://dev.to/berlin_techs_e93c88bf0a5b/beginner-friendly-guide-sum-of-k-mirror-numbers-leetcode-2081-python-javascript-python--51i4</link>
      <guid>https://dev.to/berlin_techs_e93c88bf0a5b/beginner-friendly-guide-sum-of-k-mirror-numbers-leetcode-2081-python-javascript-python--51i4</guid>
      <description>&lt;p&gt;👋 Introduction&lt;br&gt;
In the world of programming puzzles, palindrome-based problems always offer a unique challenge—especially when they span across multiple number bases. Imagine trying to find numbers that not only look the same forwards and backwards in base-10, but also in base-k. That’s the crux of the k-mirror number challenge.&lt;/p&gt;

&lt;p&gt;Let’s dive into the formal problem definition to see how this works:&lt;/p&gt;

&lt;p&gt;🧠 Problem Summary&lt;br&gt;
You're given two integers:&lt;/p&gt;

&lt;p&gt;k: the base in which we check for palindromes&lt;br&gt;
n: the number of k-mirror numbers to find&lt;br&gt;
A k-mirror number is a positive integer that:&lt;/p&gt;

&lt;p&gt;Is a palindrome in base-10&lt;br&gt;
Is also a palindrome in base-k&lt;br&gt;
Your task: return the sum of the first n such numbers.&lt;/p&gt;

&lt;p&gt;🧩 Intuition&lt;br&gt;
This is a palindrome-generation and filtering problem. The challenge lies in generating candidate numbers efficiently and checking their representations in different bases.&lt;/p&gt;

&lt;p&gt;Key Observations:&lt;/p&gt;

&lt;p&gt;Not every decimal palindrome is a k-palindrome (i.e., palindrome in base-k)&lt;br&gt;
Instead of checking all numbers, generate only palindromes in base-10 (which drastically reduces search space)&lt;br&gt;
For each generated number, convert it to base-k and check if that string is also a palindrome&lt;br&gt;
🪄 Approach&lt;br&gt;
Start from the smallest base-10 palindromes: 1-digit numbers.&lt;br&gt;
Use a helper function to generate the next decimal palindrome.&lt;br&gt;
For each candidate:&lt;br&gt;
Convert it to base-k&lt;br&gt;
Check if that base-k representation is a palindrome&lt;br&gt;
If it is, add it to the sum and count&lt;br&gt;
Stop when we find n such numbers&lt;br&gt;
💻 C++ Code&lt;br&gt;
class Solution {&lt;br&gt;
public:&lt;br&gt;
    bool isPalindrome(string s) {&lt;br&gt;
        return s == string(s.rbegin(), s.rend());&lt;br&gt;
    }&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;string toBaseK(long long num, int k) {
    string res;
    while (num &amp;gt; 0) {
        res += to_string(num % k);
        num /= k;
    }
    reverse(res.begin(), res.end());
    return res;
}

long long kMirror(int k, int n) {
    long long sum = 0;
    int len = 1;
    while (n &amp;gt; 0) {
        for (int half = pow(10, (len - 1) / 2); half &amp;lt; pow(10, (len + 1) / 2) &amp;amp;&amp;amp; n &amp;gt; 0; ++half) {
            string h = to_string(half);
            string r = h;
            reverse(r.begin(), r.end());
            string full = h + (len % 2 ? r.substr(1) : r);
            long long num = stoll(full);
            if (isPalindrome(toBaseK(num, k))) {
                sum += num;
                --n;
            }
        }
        ++len;
    }
    return sum;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;br&gt;
💻 JavaScript Code&lt;br&gt;
function isPalindrome(s) {&lt;br&gt;
    return s === s.split('').reverse().join('');&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;function toBaseK(num, k) {&lt;br&gt;
    return num.toString(k);&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;var kMirror = function(k, n) {&lt;br&gt;
    let sum = 0;&lt;br&gt;
    let len = 1;&lt;br&gt;
    while (n &amp;gt; 0) {&lt;br&gt;
        let lower = Math.pow(10, Math.floor((len - 1) / 2));&lt;br&gt;
        let upper = Math.pow(10, Math.ceil((len) / 2));&lt;br&gt;
        for (let half = lower; half &amp;lt; upper &amp;amp;&amp;amp; n &amp;gt; 0; half++) {&lt;br&gt;
            let h = String(half);&lt;br&gt;
            let r = h.split('').reverse().join('');&lt;br&gt;
            let full = h + (len % 2 ? r.slice(1) : r);&lt;br&gt;
            let num = parseInt(full);&lt;br&gt;
            if (isPalindrome(toBaseK(num, k))) {&lt;br&gt;
                sum += num;&lt;br&gt;
                n--;&lt;br&gt;
            }&lt;br&gt;
        }&lt;br&gt;
        len++;&lt;br&gt;
    }&lt;br&gt;
    return sum;&lt;br&gt;
};&lt;br&gt;
🐍 Python Code&lt;br&gt;
class Solution:&lt;br&gt;
    def kMirror(self, k: int, n: int) -&amp;gt; int:&lt;br&gt;
        def is_palindrome(s):&lt;br&gt;
            return s == s[::-1]&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    def to_base_k(num, k):
        res = ""
        while num:
            res = str(num % k) + res
            num //= k
        return res

    def generate_palindromes():
        length = 1
        while True:
            for half in range(10 ** ((length - 1) // 2), 10 ** ((length + 1) // 2)):
                s = str(half)
                yield int(s + s[-2::-1] if length % 2 else s + s[::-1])
            length += 1

    ans = 0
    count = 0
    for num in generate_palindromes():
        if is_palindrome(to_base_k(num, k)):
            ans += num
            count += 1
            if count == n:
                break
    return ans
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
      <category>python</category>
    </item>
  </channel>
</rss>
