How to Check if a Binary Number is Divisible by 3The problem is to check whether the given binary number is divisible by 3 or a multiple of 3. This problem is very popular in the programming world and asked in software engineering interviews by none other than Amazon, Microsoft, Adobe, etc. The binary number can be in the form of a string or an integer. We need to take care of that also before going for the implementation. Example 1:Example 2:In this article, we will learn different approaches to solving this problem and discuss the best approach to check if a binary number is a multiple of three. Method 1 - Converting to DecimalThe simplest approach is to convert the binary number into a decimal equivalent. And check if it is divisible by 3 or not. To convert the binary number to decimal:
Check if the number is divisible by 3; if yes, the binary number is also divisible by 3. Below is the Python implementation of the same: Output: Is binary1 = 11 divisible by 3? True Is binary2 = 1100 divisible by 3? True Is binary3 = 1011 divisible by 3? False Time Complexity = O(n): Where n is the number of digits in the binary number. Space Complexity - O(1): It uses constant space. Method 2 - Using DFA (Deterministic Finite Automata) - State Machine ApproachA more efficient approach is to design a DFA that simulates the division process. The state machine or DFA will have three states: 0, 1, and 2, representing the remainder of the division of the binary number by three. The diagrammatical representation of the DFA is Given below: We will start from the initial state of 0 and iterate over the binary digits from left to right, updating the state according to the transitions:
After iterating through all the digits, if the final state is 0, the binary number is divisible by three, and we return True. Otherwise, False. Below is the Python implementation of the same: Output: Is binary1 = 11 divisible by 3? True Is binary2 = 1100 divisible by 3? True Is binary3 = 1011 divisible by 3? False Time Complexity = O(n): Where n is the number of digits in the binary number. Space Complexity - O(1): It uses constant space for state variables. But this approach is not the most efficient and also requires prior knowledge of automata. Method 3 - Bit Manipulation - (|odd bits - even bits|) divisible by 3This is the most efficient and elegant approach to determining the divisibility of a binary number by three. We will count the number of ones at odd positions and even positions separately. If the difference between these counts is divisible by three, then the binary number is divisible by three. In general, a binary number N can be represented as: N = bn 2n-1+ bn-1 2n-2+ bn-22n-3+ … + b222 + b121 + b020 Where bi represents the binary digit (0 or 1), and n represents the highest power of 2. Now, let us examine the remainder when each power of 2 is divided by 3: We can observe that the remainders alternate between 1 and 2 as the powers of 2 increase. From the above pattern, the expression (2^k) % 3 can be simplified as follows: So, for a binary number to be divisible by 3, the difference between the count of ones at an even place and an odd place must be divisible by three. In a decimal system, if the sum of the digits of a number is divisible by three, then the number is also divisible by 3. Another approach one can follow for ones at even places, we count 2, and ones at odd places, we count 1 If the count is divisible by 3. The binary number would be divisible by 3. The Python Implementation of Bit Manipulation is given below: In our approach, we calculate the counts of ones at even and odd positions separately and check if the difference is divisible by 3. If yes, then the binary number is divisible by three. Otherwise, it is not divisible. Output: Is binary1 = 11 divisible by 3? True Is binary2 = 1100 divisible by 3? True Is binary3 = 1011 divisible by 3? False Explanation:
Time Complexity = O(n), where n is the number of digits in the binary number. Space Complexity = O(1) as we only need a few variables to store the counters. Best Approach:
C++ Implementation: Output: C Implementation: Output: CONCLUSION:We can use various approaches to check whether a binary number is divisible by three.
We can solve the problem in linear time complexity using the last approach. Also, it is the recommended approach for checking binary divisibility by three. Next TopicStock Buy and Sell Problem |