![]() |
VOOZH | about |
Given two integers N and X (1<=N<=X<=1e9), the task is to find if it is possible to represent N as exactly X powers of 3.
Examples:
Input: N = 9, X = 5
Output: Yes
Explanation: 9 = 3^1 + 3^1 + 3^0 + 3^0 + 3^0Input: N = 9, X = 4
Output: No
Approach: To solve the problem follow the below idea:
The idea is to use the ternary (base-3) representation of the integer N.
Below is the implementation for the above approach:
YES
Time Complexity: O(log3(N))
Auxiliary Space: O(1)