30. Is Binary Number Multiple of 3
The problem can be found at the following link: Question Link
My approach
Through observation, it has been determined that odd powers of
2
yield a remainder of2
when divided by3
, while even powers yield a remainder of1
. 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 by3
or not.
Time and Auxiliary Space Complexity
Time Complexity:
O(N)
, whereN
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.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. 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 getlost01/gfg-potd repository.
Last updated