- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given an array of integer type elements and a matrix or 2-D array of given row and column size and the task is to calculate the count of elements of an array that are present in each row of a matrix.

**Input**

int arr = { 2, 4, 6} and int matrix[row][col] = { { 2, 4, 6 }, {3, 4, 6}, {6, 2, 1}}

**Output**

Elements of array in row 1 are: 3 Elements of array in row 2 are: 2 Elements of array in row 3 are: 2

**Explanation**

we are having array containing 2, 4 and 6 as elements and now we will check the occurrences of 3 elements in every row of matrix by matching the elements of an array with the elements of a matrix, like, 2, 4 and 6 all are present in first row of matrix so the count of elements for row 1 is 3, similarly, count of elements for row 2 is 2 as only 4 and 6 are there and count of elements for row 3 is 2 as only 2 and 6 are there.

**Input**

int arr = { 1, 3} and int matrix[row][col] = { { 1, 4, 6 }, {3, 1, 6}, {6, 2, 4}}

**Output**

Elements of array in row 1 are: 1 Elements of array in row 2 are: 2 Elements of array in row 3 are: 0

**Explanation**

we are having array containing 1 and 3 as elements and now we will check the occurrences of 2 elements in every row of matrix by matching the elements of an array with theelements of a matrix, like, only 1 is present in first row of matrix so the count of elements for row 1 is 1, similarly, count of elements for row 2 is 2 as 1 and 3 both are there and count of elements for row 3 is 0 as none of 1 and 3 are there.

There can be multiple approaches to solve the given problem i.e. naive approach and efficient approach. So let’s first look at the **naive approach**.

Input an array of integer elements and matrix of row and column size

Calculate the size of an array and pass the array, matrix and size of array to the function for further processing

Take a temporary variable count to store the count of elements present in the matrix row.

Start loop FOR from 0 till the row size of a matrix

Inside the loop, start FOR from 0 till the size of an array

Set a temp with arr[k]

Start another loop FOR from 0 till the column size of a matrix

Inside the loops, check IF temp = matrix[i][j] then increment the count by 1

Set count to 0 to refresh it after the change of every row

Print the value of count before change of every row.

Input an array of integer elements and matrix of row and column size

Calculate the size of an array and pass the array, matrix and size of array to the function for further processing

Start loop FOR from 0 till the row size of a matrix

Create a variable of type unordered_map

Start another loop FOR from 0 till the column size of a matrix

Set an unordered map with matrix[i][j] as 1

Take a temporary variable count to store the count of elements present in the matrix row.

Inside the loop, start FOR from 0 till the size of an array

Check IF um[arr[j]]==1 then increment the count by 1

Print the value of count before change of every row.

#include<bits/stdc++.h> using namespace std; #define row 3 #define col 3 void arr_matrix(int matrix[row][col], int arr[], int size){ int count = 0; //for matrix row for(int i=0; i<row; i++){ //for array for(int k=0 ; k<size ; k++){ int temp = arr[k]; //for matrix col for(int j = 0; j<col; j++){ if(temp == matrix[i][j]){ count++; } } } cout<<"Elements of array in row "<< i + 1 <<" are: " << count << endl; count = 0; } } int main(){ int matrix[row][col] = { { 2, 4, 6 }, {3, 4, 6}, {6, 2, 1}}; int arr[] = { 2, 4, 6}; int size = sizeof(arr) / sizeof(arr[0]); arr_matrix(matrix, arr, size); }

If we run the above code it will generate the following output −

Elements of array in row 1 are: 3 Elements of array in row 2 are: 2 Elements of array in row 3 are: 2

#include <bits/stdc++.h> using namespace std; #define row 3 #define col 3 void arr_matrix(int matrix[row][col], int arr[], int size){ for (int i = 0; i < row; i++){ unordered_map<int, int> um; for (int j = 0; j < col; j++){ um[matrix[i][j]] = 1; } int count = 0; for (int j = 0; j < size; j++) { if (um[arr[j]]) count++; } cout<<"Elements of array in row "<< i + 1 <<" are: " << count << endl; } } int main(){ int matrix[row][col] = { { 2, 4, 6 }, {3, 4, 6}, {6, 2, 1}}; int arr[] = { 2, 4, 6}; int size = sizeof(arr) / sizeof(arr[0]); arr_matrix(matrix, arr, size); }

If we run the above code it will generate the following output −

Elements of array in row 1 are: 3 Elements of array in row 2 are: 2 Elements of array in row 3 are: 2

- Related Questions & Answers
- Total number of elements present in an array in C#
- Minimum value among AND of elements of every subset of an array in C++
- Get average of every group of n elements in an array JavaScript
- Deep count of elements of an array using JavaScript
- Count and Sum of composite elements in an array in C++
- XOR of Sum of every possible pair of an array in C++
- Python - Count the frequency of matrix row length
- Count number of elements in an array with MongoDB?
- Check if a key is present in every segment of size k in an array in C++
- Maximum sum of elements from each row in the matrix in C++
- Count number of even and odd elements in an array in C++
- Count of index pairs with equal elements in an array in C++
- Divide every element of one array by other array elements in C++ Program
- How to find the variance of row elements of a matrix in R?
- Program to count number of elements present in a set of elements with recursive indexing in Python

Advertisements