Related Articles
Minimum number of basic logic gates required to realize given Boolean expression
• Difficulty Level : Easy
• Last Updated : 30 Apr, 2021

Given a string S of length N representing a boolean expression, the task is to find the minimum number of AND, OR, and NOT gates required to realize the given expression.

Examples:

Input: S = “A+B.C”
Output: 2
Explanation: Realizing the expression requires 1 AND gate represented by ‘.’ and 1 OR gate represented by ‘+’.

Input: S = “(1 – A). B+C”
Output: 3
Explanation: Realizing the expression requires 1 AND gate represented by ‘.’ and 1 OR gate represented by ‘+’ and 1 NOT gate represented by ‘-‘.

Approach: Follow the steps below to solve the problem:

1. Iterate over the characters of the string.
2. Initialize, count of gates to 0.
3. If the current character is either ‘.’ or ‘+’, or ‘1’, then increment the count of gates by 1
4. Print the count of gates required.

Below is the implementation of the above approach:

## C++

 `// C++ implementataion of``// the above approach``#include ``using` `namespace` `std;` `// Function to coutn the total``// number of gates required to``// realize the boolean expression S``void` `numberOfGates(string s)``{``    ``// Length of the string``    ``int` `N = s.size();` `    ``// Stores the count``    ``// of total gates``    ``int` `ans = 0;` `    ``// Traverse the string``    ``for` `(``int` `i = 0; i < (``int``)s.size(); i++) {` `        ``// AND, OR and NOT Gate``        ``if` `(s[i] == ``'.'` `|| s[i] == ``'+'``            ``|| s[i] == ``'1'``) {``            ``ans++;``        ``}``    ``}` `    ``// Print the count``    ``// of gates required``    ``cout << ans;``}` `// Driver Code``int` `main()``{``    ``// Input``    ``string S = ``"(1-A).B+C"``;` `    ``// Function call to count the``    ``// total number of gates required``    ``numberOfGates(S);``}`

## Java

 `// Java implementataion of``// the above approach``class` `GFG{` `// Function to coutn the total``// number of gates required to``// realize the boolean expression S``static` `void` `numberOfGates(String s)``{``    ` `    ``// Length of the string``    ``int` `N = s.length();` `    ``// Stores the count``    ``// of total gates``    ``int` `ans = ``0``;` `    ``// Traverse the string``    ``for``(``int` `i = ``0``; i < (``int``)s.length(); i++)``    ``{``        ` `        ``// AND, OR and NOT Gate``        ``if` `(s.charAt(i) == ``'.'` `||``            ``s.charAt(i) == ``'+'` `||``            ``s.charAt(i) == ``'1'``)``        ``{``            ``ans++;``        ``}``    ``}` `    ``// Print the count``    ``// of gates required``    ``System.out.println(ans);``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ` `    ``// Input``    ``String S = ``"(1-A).B+C"``;` `    ``// Function call to count the``    ``// total number of gates required``    ``numberOfGates(S);``}``}` `// This code is contributed by user_qa7r`

## Python3

 `# Python3 implementataion of``# the above approach` `# Function to coutn the total``# number of gates required to``# realize the boolean expression S``def` `numberOfGates(s):` `    ``# Length of the string``    ``N ``=` `len``(s)` `    ``# Stores the count``    ``# of total gates``    ``ans ``=` `0` `    ``# Traverse the string``    ``for` `i ``in` `range``(``len``(s)):` `        ``# AND, OR and NOT Gate``        ``if` `(s[i] ``=``=` `'.'` `or` `s[i] ``=``=` `'+'` `or``            ``s[i] ``=``=` `'1'``):``            ``ans ``+``=` `1` `    ``# Print the count``    ``# of gates required``    ``print``(ans, end ``=` `"")` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``# Input``    ``S ``=` `"(1-A).B+C"` `    ``# Function call to count the``    ``# total number of gates required``    ``numberOfGates(S)` `# This code is contributed by AnkThon`

## C#

 `// C# implementataion of``// the above approach``using` `System;``public` `class` `GFG``{` `// Function to coutn the total``// number of gates required to``// realize the boolean expression S``static` `void` `numberOfGates(``string` `s)``{``    ` `    ``// Length of the string``    ``int` `N = s.Length;` `    ``// Stores the count``    ``// of total gates``    ``int` `ans = 0;` `    ``// Traverse the string``    ``for``(``int` `i = 0; i < s.Length; i++)``    ``{``        ` `        ``// AND, OR and NOT Gate``        ``if` `(s[i] == ``'.'` `||``            ``s[i] == ``'+'` `||``            ``s[i] == ``'1'``)``        ``{``            ``ans++;``        ``}``    ``}` `    ``// Print the count``    ``// of gates required``    ``Console.WriteLine(ans);``}` `// Driver Code``public` `static` `void` `Main(``string``[] args)``{``    ` `    ``// Input``    ``string` `S = ``"(1-A).B+C"``;` `    ``// Function call to count the``    ``// total number of gates required``    ``numberOfGates(S);``}``}` `// This code is contributed by AnkThon`

## Javascript

 ``
Output:
`3`

Time Complexity: O(N)
Auxiliary Space: O(1)

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up