
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Count of Elements of an Array in Every Row of NXM Matrix in C++
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.
Approach used in the below program is as follows
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.
Efficient 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
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.
Example (naive approach)
#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); }
Output
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
Example (Efficient approach)
#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); }
Output
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