30. Is Binary Number Multiple of 3
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
Through observation, it has been determined that odd powers of 2
yield a remainder of 2
when divided by 3
, while even powers yield a remainder of 1
. For instance:
For any given binary string, we can determine the final remainder by calculating the sum of remainders corresponding to each set
bit. For example:
Therefore, through the calculation of remainders for every set bit in the provided binary string, we can ascertain the ultimate remainder. Finally, it is crucial to verify whether the final remainder
is divisible by 3
or not.
Time Complexity: O(N)
, where N
is the length of the binary string. We traverse the binary string once to calculate the remainders.
Auxiliary Space Complexity: O(1)
since we are not using any extra space that scales with the input.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.