Skip to main content
Back to problems
Leetcode
Medium
Math
Number Theory
Strings
Recursion
Google
Sum Of K Mirror Numbers

Find the sum of the first nn positive integers that are palindromic in base 10 and also in base kk.

Acceptance 100%
Problem Statement

Problem

A positive integer is called kk-mirror if it is a palindrome in both base 10 and base kk.

Given two integers nn and kk, return the sum of the first nn positive integers that are kk-mirror numbers.

A number is a palindrome if it reads the same forward and backward in its base representation.

Clarification

You should consider positive integers only, and the numbers should be counted in increasing order.

Goal

Generate the first nn numbers that satisfy both palindrome conditions, then return their sum.

Input Format

  • One line containing two integers nn and kk.
  • nn is the count of kk-mirror numbers to find.
  • kk is the base used for the second palindrome check.

Output Format

  • Return a single integer: the sum of the first nn kk-mirror numbers.

Constraints

  • 1n1 \le n
  • 2k2 \le k
  • The numbers searched are positive integers.
  • The answer fits in a 64-bit signed integer for the intended test data.
Examples
Sample cases returned by the problem API.

Example 1

Input

n = 5, k = 2

Output

25

Explanation

The first five positive integers that are palindromic in both base 10 and base 2 are 1, 3, 5, 7, and 9. Their sum is 25.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.