Find the Largest Palindrome Divisible by K

You are given two positive integers n and k.

An integer x is called k-palindromic if:

  • x is a palindrome.
  • x is divisible by k.

Return the largest integer having n digits, as a string, that is k-palindromic.

Note that the integer must not have leading zeros.

Example 1
Inputn = 3, k = 5
Output"595"
595 is the largest k-palindromic integer with 3 digits.
Example 2
Inputn = 1, k = 4
Output"8"
4 and 8 are the only k-palindromic integers with 1 digit, so 8 is the largest.

Constraints

  • 1 <= n <= 10^5
  • 1 <= k <= 9

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate