3602. Hexadecimal and Hexatrigesimal Conversion
Description
You are given an integer n
.
Return the concatenation of the hexadecimal representation of n2
and the hexatrigesimal representation of n3
.
A hexadecimal number is defined as a base-16 numeral system that uses the digits 0 – 9
and the uppercase letters A - F
to represent values from 0 to 15.
A hexatrigesimal number is defined as a base-36 numeral system that uses the digits 0 – 9
and the uppercase letters A - Z
to represent values from 0 to 35.
Example 1:
Input: n = 13
Output: "A91P1"
Explanation:
n2 = 13 * 13 = 169
. In hexadecimal, it converts to(10 * 16) + 9 = 169
, which corresponds to"A9"
.n3 = 13 * 13 * 13 = 2197
. In hexatrigesimal, it converts to(1 * 362) + (25 * 36) + 1 = 2197
, which corresponds to"1P1"
.- Concatenating both results gives
"A9" + "1P1" = "A91P1"
.
Example 2:
Input: n = 36
Output: "5101000"
Explanation:
n2 = 36 * 36 = 1296
. In hexadecimal, it converts to(5 * 162) + (1 * 16) + 0 = 1296
, which corresponds to"510"
.n3 = 36 * 36 * 36 = 46656
. In hexatrigesimal, it converts to(1 * 363) + (0 * 362) + (0 * 36) + 0 = 46656
, which corresponds to"1000"
.- Concatenating both results gives
"510" + "1000" = "5101000"
.
Constraints:
1 <= n <= 1000
Solutions
Solution 1: Simulation
We define a function \(\textit{f}(x, k)\), which converts an integer \(x\) to its string representation in base \(k\). This function constructs the result string by repeatedly taking the modulus and dividing.
For a given integer \(n\), we compute \(n^2\) and \(n^3\), then convert them to hexadecimal and base-36 strings, respectively. Finally, we concatenate these two strings and return the result.
The time complexity is \(O(\log n)\), and the space complexity is \(O(1)\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|