3260. Find the Largest Palindrome Divisible by K

3260. Find the Largest Palindrome Divisible by K

Description

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:

1
2
3
Input: n = 3, k = 5

Output: "595"

Explanation:

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

Example 2:

1
2
3
Input: n = 1, k = 4

Output: "8"

Explanation:

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

Example 3:

1
2
3
Input: n = 5, k = 6

Output: "89898"

Constraints:

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

Hints/Notes

Solution

Language: C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Solution {
public:
// the meaning of dp[i][j]: when we are handling the ith digit
// (also n - 1 - i), with remainder = j, can we form the number successfully
// why we can use this dp state? because only the digit and reminder matter, not what we have filled in
vector<vector<int>> dp;
vector<int> div;
int n_, k_, h_;
string res;
string largestPalindrome(int n, int k) {
h_ = (n + 1) / 2;
n_ = n;
k_ = k;
dp.resize(h_, vector<int>(k, -1));
div.resize(n, 1);
res = string(n, '0');
for (int i = 1; i < n; i++) {
div[i] = div[i - 1] * 10 % k;
}
dfs(0, 0);
return res;
}

bool dfs(int i, int j) {
if (i >= h_) {
return j == 0;
}
if (dp[i][j] != -1) {
return dp[i][j];
}
bool ans = false;
for (int k = 9; k >= 0; k--) {
int j2 = j;
if (n_ % 2 && i == h_ - 1) {
j2 = (j2 + k * div[i]) % k_;
} else {
j2 = (j2 + k * div[i] + k * div[n_ - 1 - i]) % k_;
}
if (dfs(i + 1, j2)) {
res[i] = k + '0';
res[n_ - 1 - i] = k + '0';
ans = true;
break;
}
}
dp[i][j] = ans;
return ans;
}
};