ALGOQ001 - Editorial

PROBLEM LINK:

Practice

DIFFICULTY:

CakeWalk

PREREQUISITES:

None at all image

PROBLEM:

Print an integer X denoting the number of basic logic gates in the given string.

EXPLANATION:

Find the count of ‘+’, ‘.’ and ‘- ‘ (without quotes) in the given string for that
take a counter and initialize it with zero now run a for loop traverse the string from beginning to end, use if condition to find the gates, if found any then increase the counter after the loop ends print the value of counter which will be your answer.

SOLUTIONS:

Solution
#include <bits/stdc++.h>
using namespace std;
// Function to count 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] == '-') {

ans++;
}

}
// Print the count

// of gates required

cout << ans<<endl;

}
// Driver Code

int main()

{

// Input

int t;

cin>>t;

while(t--)

{
int n;

cin>>n;

string S;
cin>>S;
// Function call to count the

// total number of gates required

numberOfGates(S);

}
}