Check if binary string multiple of 3 using DFA
Given a string of binary characters, check if it is multiple of 3 or not.
Examples :
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 1 : 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 :
C++
#include <bits/stdc++.h>
using namespace std;
bool isMultiple3( char c[], int size)
{
char state = '0' ;
for ( int i = 0; i < size; i++) {
char digit = c[i];
switch (state) {
case '0' :
if (digit == '1' )
state = '1' ;
break ;
case '1' :
if (digit == '0' )
state = '2' ;
else
state = '0' ;
break ;
case '2' :
if (digit == '0' )
state = '1' ;
break ;
}
}
if (state == '0' )
return true ;
return false ;
}
int main()
{
int size = 5;
char c[] = { '1' , '0' , '1' , '0' , '1' };
if (isMultiple3(c, size))
cout << "YES\n" ;
else
cout << "NO\n" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean isMultiple3( char c[], int size)
{
char state = '0' ;
for ( int i = 0 ; i < size; i++) {
char digit = c[i];
switch (state) {
case '0' :
if (digit == '1' )
state = '1' ;
break ;
case '1' :
if (digit == '0' )
state = '2' ;
else
state = '0' ;
break ;
case '2' :
if (digit == '0' )
state = '1' ;
break ;
}
}
if (state == '0' )
return true ;
return false ;
}
public static void main (String[] args)
{
int size = 5 ;
char c[] = { '1' , '0' , '1' , '0' , '1' };
if (isMultiple3(c, size))
System.out.println ( "YES" );
else
System.out.println ( "NO" );
}
}
|
Python3
def CheckDivisibilty(A):
oddbits = 0 ;
evenbits = 0 ;
for counter in range ( len (A)):
if (A[counter] = = '1' ):
if (counter % 2 = = 0 ):
evenbits + = 1 ;
else :
oddbits + = 1 ;
if ( abs (oddbits - evenbits) % 3 = = 0 ):
print ( "Yes" + "");
else :
print ( "No" + "");
if __name__ = = '__main__' :
A = "10101" ;
CheckDivisibilty(A);
|
C#
using System;
class GFG {
static bool isMultiple3( char []c, int size)
{
char state = '0' ;
for ( int i = 0; i < size; i++)
{
char digit = c[i];
switch (state)
{
case '0' :
if (digit == '1' )
state = '1' ;
break ;
case '1' :
if (digit == '0' )
state = '2' ;
else
state = '0' ;
break ;
case '2' :
if (digit == '0' )
state = '1' ;
break ;
}
}
if (state == '0' )
return true ;
return false ;
}
public static void Main ()
{
int size = 5;
char []c = { '1' , '0' , '1' , '0' , '1' };
if (isMultiple3(c, size))
Console.WriteLine ( "YES" );
else
Console.WriteLine ( "NO" );
}
}
|
PHP
<?php
function isMultiple3( $c , $size )
{
$state = '0' ;
for ( $i = 0; $i < $size ; $i ++)
{
$digit = $c [ $i ];
switch ( $state )
{
case '0' :
if ( $digit == '1' )
$state = '1' ;
break ;
case '1' :
if ( $digit == '0' )
$state = '2' ;
else
$state = '0' ;
break ;
case '2' :
if ( $digit == '0' )
$state = '1' ;
break ;
}
}
if ( $state == '0' )
return true;
return false;
}
$size = 5;
$c = array ( '1' , '0' , '1' , '0' , '1' );
if (isMultiple3( $c , $size ))
echo "YES\n" ;
else
echo "NO\n" ;
?>
|
Javascript
<script>
function isMultiple3(c, size)
{
let state = '0' ;
for (let i = 0; i < size; i++) {
let digit = c[i];
switch (state) {
case '0' :
if (digit == '1' )
state = '1' ;
break ;
case '1' :
if (digit == '0' )
state = '2' ;
else
state = '0' ;
break ;
case '2' :
if (digit == '0' )
state = '1' ;
break ;
}
}
if (state == '0' )
return true ;
return false ;
}
let size = 5;
let c = [ '1' , '0' , '1' , '0' , '1' ];
if (isMultiple3(c, size))
document.write ( "YES" );
else
document.write ( "NO" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Approach 2:We will check if the difference between the number of non-zero odd bit positions and non-zero even bit positions is divisible by 3 or not.
Mathematically -> |odds-evens| divisible by 3.
Code:
C++
#include <bits/stdc++.h>
using namespace std;
void CheckDivisibilty(string A)
{
int oddbits = 0, evenbits = 0;
for ( int counter = 0; counter < A.length(); counter++) {
if (A[counter] == '1' ) {
if (counter % 2 == 0) {
evenbits++;
}
else {
oddbits++;
}
}
}
if ( abs (oddbits - evenbits) % 3 == 0) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
int main()
{
string A = "10101" ;
CheckDivisibilty(A);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void CheckDivisibilty(String A)
{
int oddbits = 0 , evenbits = 0 ;
for ( int counter = 0 ; counter < A.length(); counter++)
{
if (A.charAt(counter) == '1' )
{
if (counter % 2 == 0 ) {
evenbits++;
}
else {
oddbits++;
}
}
}
if (Math.abs(oddbits - evenbits) % 3 == 0 ) {
System.out.print( "Yes" + "\n" );
}
else {
System.out.print( "No" + "\n" );
}
}
public static void main(String[] args)
{
String A = "10101" ;
CheckDivisibilty(A);
}
}
|
Python3
def CheckDivisibilty(A):
oddbits = 0 ;
evenbits = 0 ;
for counter in range ( len (A)):
if (A[counter] = = '1' ):
if (counter % 2 = = 0 ):
evenbits + = 1 ;
else :
oddbits + = 1 ;
if ( abs (oddbits - evenbits) % 3 = = 0 ):
print ( "Yes" + "");
else :
print ( "No" + "");
if __name__ = = '__main__' :
A = "10101" ;
CheckDivisibilty(A);
|
C#
using System;
public class GFG
{
static void CheckDivisibilty(String A)
{
int oddbits = 0, evenbits = 0;
for ( int counter = 0; counter < A.Length; counter++)
{
if (A[counter] == '1' )
{
if (counter % 2 == 0) {
evenbits++;
}
else {
oddbits++;
}
}
}
if (Math.Abs(oddbits - evenbits) % 3 == 0) {
Console.Write( "Yes" + "\n" );
}
else {
Console.Write( "No" + "\n" );
}
}
public static void Main(String[] args)
{
String A = "10101" ;
CheckDivisibilty(A);
}
}
|
Javascript
<script>
function CheckDivisibilty( A) {
var oddbits = 0, evenbits = 0;
for ( var counter = 0; counter < A.length; counter++) {
if (A[counter] == '1' ) {
if (counter % 2 == 0) {
evenbits++;
} else {
oddbits++;
}
}
}
if (Math.abs(oddbits - evenbits) % 3 == 0) {
document.write( "Yes" + "\n" );
} else {
document.write( "No" + "\n" );
}
}
var A = "10101" ;
CheckDivisibilty(A);
</script>
|
Time Complexity: O(n) where n is the number of bits.
Auxiliary Space: O(1)
Last Updated :
15 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...