Talentd
FleetCode

3260. Find the Largest Palindrome Divisible by K

Hard17.0% acceptance2 companies

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:

Input: n = 3, k = 5

Output: "595"

Explanation:

595 is the largest k-palindromic integer with 3 digits.

Example 2:

Input: n = 1, k = 4

Output: "8"

Explanation:

4 and 8 are the only k-palindromic integers with 1 digit.

Example 3:

Input: n = 5, k = 6

Output: "89898"

Constraints:

  • 1 <= n <= 105
  • 1 <= k <= 9

Asked at 2 companies

Hints open one at a time

Hint 1
It must have a solution since we can have all digits equal to k.
Hint 2
Use string dp, store modulus along with length of number currently formed.
Hint 3
Is it possible to solve greedily using divisibility rules?
16:25
Still stuck?Read 2 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def largestPalindrome(self, n: int, k: int) -> str:
3
Starter codePython 3Spaces: 4
Examples
Input
3
5
Input
1
4
Input
5
6
Your code is saved on this device.Solution