Given a string of binary characters, check if it is multiple of 3 or not.
Input : 1 0 1 0 Output : NO Explanation : (1 0 1 0) is 10 and hence not a multiple of 3 Input : 1 1 0 0 Output : YES Explanation : (1 1 0 0) is 12 and hence a multiple of 3
Approach : One simple method is to convert the binary number into its decimal representation and then check if it is a multiple of 3 or not. Now, when it comes to DFA (Deterministic Finite Automata), there is no concept of memory i.e. you cannot store the string when it is provided, so the above method would not be applicable. In simple terms, a DFA takes a string as input and process it. If it reaches final state, it is accepted, else rejected. As you cannot store the string, so input is taken character by character.
The DFA for given problem is :
As, when a number is divided by 3, there are only 3 possibilities. The remainder can be either 0, 1 or 2. Here, state 0 represents that the remainder when the number is divided by 3 is 0. State 1 represents that the remainder when the number is divided by 3 is 1 and similarly state 2 represents that the remainder when the number is divided by 3 is 2. So if a string reaches state 0 in the end, it is accepted otherwise rejected.
Below is the implementation of above approach :
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- DFA based division
- Check if binary representations of 0 to N are present as substrings in given binary string
- Check if a number is multiple of 9 using bitwise operators
- Write an Efficient Method to Check if a Number is Multiple of 3
- Efficiently check whether n is a multiple of 4 or not
- Check whether a very large number of the given form is a multiple of 3.
- Find the largest multiple of 3 | Set 1 (Using Queue)
- Check divisibility of binary string by 2^k
- Check whether the binary equivalent of a number ends with given string or not
- String obtained by reversing and complementing a Binary string K times
- Check if left and right shift of any string results into given string
- Efficiently check if a string has all unique characters without using any additional data structure
- Find the largest multiple of 2, 3 and 5
- Find the largest multiple of 3 from array of digits | Set 2 (In O(n) time and O(1) space)
- Find the multiple of x which is closest to a^b
- Round to next smaller multiple of 8
- Round to next greater multiple of 8
- Smallest multiple of 3 which consists of three given non-zero digits
- Count of integers in an Array whose length is a multiple of K
- Count of elements in an Array whose set bits are in a multiple of K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.