Count of elements of an array present in every row of NxM matrix
Last Updated :
26 Aug, 2022
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 1st row, all three elements in array z[] are present
In 2nd row, only 589 in array z[] are present
In 3rd row, only 27 in array z[] are present
In 4th 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 ith 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 ith 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++
// 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 occurrence of all elements
for (int j = 0; j < l; j++) {
if (mp[z[j]])
count += 1;
}
// print the occurrence 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;
}
Java
// Java program to print the count of
// elements present in the NxM matrix
import java.util.*;
class GFG
{
// Function to print the count of
// elements present in the NxM matrix
static void printCount(int a[][], 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
Map<Integer,Integer> mp = new HashMap<>();
// mark all elements in the n-th row
for (int j = 0; j < m; j++)
mp.put(a[i][j], 1);
int count = 0;
// check for occurrence of all elements
for (int j = 0; j < l; j++)
{
if (mp.containsKey(z[j]))
count += 1;
}
// print the occurrence of all elements
System.out.println("row" +(i + 1) + " = " + count);
}
}
// Driver Code
public static void main(String[] args)
{
// NxM matrix
int a[][] = { { 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 = a.length;
int m = 5;
int l = arr.length;
printCount(a, n, m, arr, l);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to print the count of
# elements present in the NxM matrix
# Function to print the count of
# elements present in the NxM matrix
def printCount(a, n, m, z, l):
# iterate in the n rows
for i in range(n):
# map to mark elements in N-th row
mp = dict()
# mark all elements in the n-th row
for j in range(m):
mp[a[i][j]] = 1
count = 0
# check for occurrence of all elements
for j in range(l):
if z[j] in mp.keys():
count += 1
# print the occurrence of all elements
print("row", i + 1, " = ", count )
# Driver Code
# NxM matrix
a = [[ 8, 27, 39, 589, 23 ],
[ 23, 34, 589, 12, 45 ],
[ 939, 32, 27, 12, 78 ],
[ 23, 349, 48, 21, 32 ]]
# elements array
arr = [ 589, 39, 27 ]
n = len(a)
m = 5
l = len(arr)
printCount(a, n, m, arr, l)
# This code is contributed by mohit kumar 29
C#
// C# program to print the count of
// elements present in the NxM matrix
using System;
using System.Collections.Generic;
class GFG
{
// Function to print the count of
// elements present in the NxM matrix
static void printCount(int [,]a, 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
Dictionary<int,int> mp = new Dictionary<int,int>();
// mark all elements in the n-th row
for (int j = 0; j < m; j++)
mp.Add(a[i,j], 1);
int count = 0;
// check for occurrence of all elements
for (int j = 0; j < l; j++)
{
if (mp.ContainsKey(z[j]))
count += 1;
}
// print the occurrence of all elements
Console.WriteLine("row" +(i + 1) + " = " + count);
}
}
// Driver Code
public static void Main(String[] args)
{
// NxM matrix
int [,]a = { { 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 = a.GetLength(0);
int m = 5;
int l = arr.Length;
printCount(a, n, m, arr, l);
}
}
/* This code is contributed by PrinciRaj1992 */
JavaScript
<script>
// JavaScript program to print the count of
// elements present in the NxM matrix
// Function to print the count of
// elements present in the NxM matrix
function printCount(a,n,m,z,l)
{
// iterate in the n rows
for (let i = 0; i < n; i++)
{
// map to mark elements in N-th row
let mp = new Map();
// mark all elements in the n-th row
for (let j = 0; j < m; j++)
mp.set(a[i][j], 1);
let count = 0;
// check for occurrence of all elements
for (let j = 0; j < l; j++)
{
if (mp.has(z[j]))
count += 1;
}
// print the occurrence of all elements
document.write("row" +(i + 1) +
" = " + count+"<br>");
}
}
// Driver Code
// NxM matrix
let a = [[ 8, 27, 39, 589, 23 ],
[ 23, 34, 589, 12, 45 ],
[ 939, 32, 27, 12, 78 ],
[ 23, 349, 48, 21, 32 ]];
// elements array
let arr=[ 589, 39, 27];
let n = a.length;
let m = 5;
let l = arr.length;
printCount(a, n, m, arr, l);
// This code is contributed by patel2127
</script>
Outputrow1 = 3
row2 = 1
row3 = 1
row4 = 0
Time Complexity: O(N*M)
Similar Reads
Count digits present in each element of a given Matrix Given a matrix arr[][] of dimensions M * N, the task is to count the number of digits of every element present in the given matrix. Examples: Input: arr[][] = { { 27, 173, 5 }, { 21, 6, 624 }, { 5, 321, 49 } }Output: 2 3 12 1 31 3 2 Input: arr[][] = { {11, 12, 33 }, { 64, 57, 61 }, { 74, 88, 39 } }O
5 min read
Common elements in all rows of a given matrix Given an m x n matrix, find all common elements present in all rows in O(mn) time and one traversal of matrix.Example: Input:mat[4][5] = {{1, 2, 1, 4, 8}, {3, 7, 8, 5, 1}, {8, 7, 7, 3, 1}, {8, 1, 2, 7, 9}, };Output: 1 8 or 8 18 and 1 are present in all rows.A simple solution is to consider every ele
7 min read
Count of numbers in given range L to R which are present in a Matrix Given a matrix(mat[][]), which is sorted row and column-wise in increasing order. Two integers are given, L and R, our task is to count number of elements of the matrix within the range [L, R].Examples: Input: L = 3, R = 11, matrix = {{1, 6, 9} {2, 7, 11} {3, 8, 12}} Output: 6 Explanation: The eleme
8 min read
Count of matrices (of different orders) with given number of elements Given a number N denotes the total number of elements in a matrix, the task is to print all possible order of matrix. An order is a pair (m, n) of integers where m is number of rows and n is number of columns. For example, if the number of elements is 8 then all possible orders are: (1, 8), (2, 4),
5 min read
Maximize count of rows consisting of equal elements by flipping columns of a Matrix Given a binary matrix, mat[][] of dimensions N * M, the task is to maximize the count of rows consisting only of equal elements by selecting any column of the matrix and flipping all the elements of that column in each operation. Print the maximum number of rows that can be made to form equal elemen
9 min read
Count rows in a matrix that consist of same element Given a matrix mat[][], the task is to count the number of rows in the matrix that consists of the same elements. Examples: Input: mat[][] = {{1, 1, 1}, {1, 2, 3}, {5, 5, 5}} Output: 2 All the elements of the first row and all the elements of the third row are the same. Input: mat[][] = {{1, 2}, {4,
9 min read