# C++ Program to find number of working components

C++Server Side ProgrammingProgramming

Suppose, there is an electronic control board that implies the status of all the components in a system. Each component status is depicted using two LEDs; if any one of them is lit then that component is working. In the board, there are n rows of LED lights, and m components status are in a particular row; that means there are 2*m LED lights in each row. A given array 'grid' depicts the status of the board, if a light is lit then it is '1', otherwise '0'. We have to find out the number of components currently working from the given board status.

## Problem Category

Various problems in programming can be solved through different techniques. To solve a problem, we have to devise an algorithm first, and to do that we have to study the particular problem in detail. A recursive approach can be used if there is a recurring appearance of the same problem over and over again; alternatively, we can use iterative structures also. Control statements such as if-else and switch cases can be used to control the flow of logic in the program. Efficient usage of variables and data structures provides an easier solution and a lightweight, low-memory-requiring program. We have to look at the existing programming techniques, such as Divide-and-conquer, Greedy Programming, Dynamic Programming, and find out if they can be used. This problem can be solved by some basic logic or a brute-force approach. Follow the following contents to understand the approach better.

So, if the input of our problem is like n = 3, m = 3, grid =

 0 1 0 1 0 0 1 1 0 1 1 0 0 0 0 1 0 0

Then the output will be 6.

## Steps

To solve this, we will follow these steps −

ans := 0
for initialize i := 0, when i < n, update (increase i by 1), do:
for initialize j := 0, when j < 2 * m, update j = j + 2, do:
if grid[i, j] + grid[i, j + 1] > 0, then:
(increase ans by 1)
print(ans)

## Example

Let us see the following implementation to get better understanding −

#include<bits/stdc++.h>
using namespace std;
void solve(int n, int m, vector<vector<int>> grid) {
int ans = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < 2 * m; j = j + 2) {
if (grid[i][j] + grid[i][j + 1] > 0)
ans++;
}
}
cout<< ans;
}
int main() {
int n = 3, m = 3;
vector<vector<int>> grid = {{0, 1, 0, 1, 0, 0}, {1, 1, 0, 1, 1, 0}, {0, 0, 0, 1, 0, 0}};
solve(n, m, grid);
return 0;
}

## Input

3, 3, {{0, 1, 0, 1, 0, 0}, {1, 1, 0, 1, 1, 0}, {0, 0, 0, 1, 0, 0}}

## Output

6