Given N rows with M elements each and an array arr[] of L numbers, the task is to print the count of elements of that array present in every row of the matrix.

**Examples:**

Input: {8 27 39 589 23 23 34 589 12 45 939 32 27 12 78 23 349 48 21 32}, arr[] = {589, 39, 27}Output: 1st row - 3 2nd row - 1 3rd row - 1 4th row - 0 In 1^{st}row, all three elements in array z[] are present In 2^{nd}row, only 589 in array z[] are present In 3^{rd}row, only 27 in array z[] are present In 4^{th}row, none of the elements are present.Input: {1, 2, 3 4, 5, 6}, arr[] = {2, 3, 4}Output: 1st row - 2 2nd row - 1

A naive **approach** is to iterate for every element in the array arr[] and for i^{th} row do a linear search for every element in the array arr[]. Count the number of elements and print the result for every row.

**Time Complexity:** O(N*M*L)

An **efficient approach** is to iterate for all the elements in the i^{th} row of the matrix. Mark all elements using a hash table. Iterate in the array of numbers in the Z array, check if the number is present in the hash-table. Increase the count for every element present. Once all the elements are checked, print the count.

Below is the implementation of the above approach:

`// C++ program to print the count of ` `// elements present in the NxM matrix ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print the count of ` `// elements present in the NxM matrix ` `void` `printCount(` `int` `a[][5], ` `int` `n, ` `int` `m, ` `int` `z[], ` `int` `l) ` `{ ` ` ` `// iterate in the n rows ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `// map to mark elements in N-th row ` ` ` `unordered_map<` `int` `, ` `int` `> mp; ` ` ` ` ` `// mark all elements in the n-th row ` ` ` `for` `(` `int` `j = 0; j < m; j++) ` ` ` `mp[a[i][j]] = 1; ` ` ` ` ` `int` `count = 0; ` ` ` ` ` `// check for occurence of all elements ` ` ` `for` `(` `int` `j = 0; j < l; j++) { ` ` ` `if` `(mp[z[j]]) ` ` ` `count += 1; ` ` ` `} ` ` ` ` ` `// print the occurence of all elements ` ` ` `cout << ` `"row"` `<< i + 1 << ` `" = "` `<< count << endl; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `// NxM matrix ` ` ` `int` `a[][5] = { { 8, 27, 39, 589, 23 }, ` ` ` `{ 23, 34, 589, 12, 45 }, ` ` ` `{ 939, 32, 27, 12, 78 }, ` ` ` `{ 23, 349, 48, 21, 32 } }; ` ` ` ` ` `// elements array ` ` ` `int` `arr[] = { 589, 39, 27 }; ` ` ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `int` `m = 5; ` ` ` ` ` `int` `l = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `printCount(a, n, m, arr, l); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

row1 = 3 row2 = 1 row3 = 1 row4 = 0

**Time Complexity:** O(N*M)

## Recommended Posts:

- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Count elements smaller than or equal to x in a sorted matrix
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- Count elements that are divisible by at-least one element in another array
- Count occurrences of the average of array elements with a given number
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j
- Program to print elements of a Matrix row-wise skipping alternate elements
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Maximum sub-matrix area having count of 1's one more than count of 0's
- Move matrix elements in given direction and add elements with same value
- Program to swap upper diagonal elements with lower diagonal elements of matrix.
- Rotate Matrix Elements
- Sum of alternate elements of a N x N matrix
- Sum of all even frequency elements in Matrix
- Sum of all odd frequency elements in a Matrix

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.