New update is available. Click here to update.

Last Updated: 15 Mar, 2021

Difficulty: Easy

```
The first line of input contains an integer ‘T’ representing the number of test cases.
The first and the only line of each test case contains the integer ‘N’ denoting the number.
```

```
For each test case, on a separate line, output one integer 0 or 1. Print 1, if N is a power of 3, or 0 otherwise.
Print the output of each test case in a separate line.
```

```
You do not need to print anything. It has already been taken care of. Just implement the given function.
```

```
1 <= T <= 10
1 <= N <= 10^9
Time limit: 1 sec
```

Approach: The idea here is to keep dividing ‘N’ by 3 every time till ‘N’ is divisible by 3. If at the end N is greater than 1 then the number will not be divisible by 3.

The algorithm is as follows :

- Declare a while loop with condition that ‘N’ % 3 == 0.
- Divide ‘N’ by 3, ‘N’ = ‘N’ / 3.

- If ‘N’ > 1, return 0 because the given integer is not a power of three.
- Otherwise, return 1.

SIMILAR PROBLEMS

Merge Two Sorted Arrays Without Extra Space

Posted: 19 Nov, 2022

Difficulty: Moderate

Ninja And The Strictly Increasing Array

Posted: 27 Nov, 2022

Difficulty: Moderate

Maximum GCD

Posted: 8 Dec, 2022

Difficulty: Hard

Prime?

Posted: 9 Dec, 2022

Difficulty: Easy

Co-Prime

Posted: 14 Dec, 2022

Difficulty: Hard

Popular Interview Problems: