Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Short trick to find number of states in DFA that accepts set of all binary numbers which are mod by n

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Suppose we have a question :

Que: Construct minimal state DFA that accepts set of all binary no. which is 2 mod 5(say)
Ans: 5 states

For solving these type of questions there is a traditional way of constructing the respective DFA for that problem. The problem in that traditional approach is that, it is time consuming and not all people can construct DFA perfectly in one go (which will lead to wrong answer). 

So, here is a trick for solving these type of questions in just few seconds. Follow the steps shows in the diagram below  and after some practice it will be on your finger tips.


  1. If n is odd, then minimum of states will be n.
  2. if n is not even:
    • Check, if n is equal to the format 2^k (like 4 = 2^2, 8 = 2^3) , where k is any whole number.
    • if n= 2^k. then minimum no of states will be k+1.
    • But, if not n != 2^k
      • Check if n/2 is odd, then minimum states will be n/2+1.
      • If n/2 is even, then divide the no by 2 again and again till we get an odd digit , then add no of times the no was divided to get odd value [ ((n/2))/2….odd + m ]. The result of that sum will be the minimum no of states.
EXAMPLE 1: 2 mod 5 where r=2,n=5
1. 5 is odd 
    Therefore, ans is 5 states (n states)
EXAMPLE 2: 4 mod 8 where r=4,n=8
1. 8 is even (so it can't be n states)
2. check n=2^k format
    Therefore, ans is 4 states(k+1 states)
EXAMPLE 3: 10 mod 24 where r=10,n=24
1. 24 is even (so it can't be n states)
2. 24 != 2^k (so it can't be k+1 states)
3. n/2 is even (24/2=12, so it can't be n/2+1 states)
4. Divide n by 2 till we get odd no and keep a count on how many times we are dividing
    24/2=12 , m=1
    12/2=6 , m=2
    6/2=3 , m=3 (odd found)
    Therefore answer is 6 states (odd + m states)
My Personal Notes arrow_drop_up
Last Updated : 13 Feb, 2023
Like Article
Save Article
Similar Reads