-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3260-FindTheLargestPalindromeDivisibleByK.go
More file actions
83 lines (71 loc) · 2.24 KB
/
3260-FindTheLargestPalindromeDivisibleByK.go
File metadata and controls
83 lines (71 loc) · 2.24 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
// 3260. 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:
// 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 <= 10^5
// 1 <= k <= 9
import "fmt"
import "bytes"
import "math/bits"
func largestPalindrome(n int, k int) string {
five, sixes, eights := byte('5'), "66", "888"
nine := []byte{'9'}
pattern6 := []string{"77", "8"}
pattern7 := []string{"7", "77", "5", "77", "7", "99", "4", "44", "6", "44", "4", "99"}
res := bytes.Repeat(nine, n) // will be used as is for k = 1, 3, 9
switch k {
case 2, 4, 8:
wrap := min(bits.Len(uint(k)) - 1, n)
copy(res[:wrap], eights)
copy(res[n - wrap:], eights)
case 5:
res[0], res[n - 1] = five, five
case 6:
if n < 3 { return sixes[:n] }
res[0], res[n - 1] = eights[0], eights[0]
copy(res[(n - 1) / 2:], pattern6[n % 2])
case 7:
copy(res[(n - 1) / 2:], pattern7[(n - 1) % 12])
}
return string(res)
}
func main() {
// Example 1:
// Input: n = 3, k = 5
// Output: "595"
// Explanation:
// 595 is the largest k-palindromic integer with 3 digits.
fmt.Println(largestPalindrome(3, 5)) // "595"
// Example 2:
// Input: n = 1, k = 4
// Output: "8"
// Explanation:
// 4 and 8 are the only k-palindromic integers with 1 digit.
fmt.Println(largestPalindrome(1, 4)) // "8"
// Example 3:
// Input: n = 5, k = 6
// Output: "89898"
fmt.Println(largestPalindrome(5, 6)) // "89898"
fmt.Println(largestPalindrome(1, 1)) // "9"
//fmt.Println(largestPalindrome(10000, 9)) // "999...9"
fmt.Println(largestPalindrome(1, 9)) // "9"
//fmt.Println(largestPalindrome(10000, 1)) // "999...9"
}