Count of elements of an array present in every row of NxM matrix
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++
#include <bits/stdc++.h>
using namespace std;
void printCount( int a[][5], int n, int m, int z[], int l)
{
for ( int i = 0; i < n; i++) {
unordered_map< int , int > mp;
for ( int j = 0; j < m; j++)
mp[a[i][j]] = 1;
int count = 0;
for ( int j = 0; j < l; j++) {
if (mp[z[j]])
count += 1;
}
cout << "row" << i + 1 << " = " << count << endl;
}
}
int main()
{
int a[][5] = { { 8, 27, 39, 589, 23 },
{ 23, 34, 589, 12, 45 },
{ 939, 32, 27, 12, 78 },
{ 23, 349, 48, 21, 32 } };
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
import java.util.*;
class GFG
{
static void printCount( int a[][], int n, int m,
int z[], int l)
{
for ( int i = 0 ; i < n; i++)
{
Map<Integer,Integer> mp = new HashMap<>();
for ( int j = 0 ; j < m; j++)
mp.put(a[i][j], 1 );
int count = 0 ;
for ( int j = 0 ; j < l; j++)
{
if (mp.containsKey(z[j]))
count += 1 ;
}
System.out.println( "row" +(i + 1 ) + " = " + count);
}
}
public static void main(String[] args)
{
int a[][] = { { 8 , 27 , 39 , 589 , 23 },
{ 23 , 34 , 589 , 12 , 45 },
{ 939 , 32 , 27 , 12 , 78 },
{ 23 , 349 , 48 , 21 , 32 } };
int arr[] = { 589 , 39 , 27 };
int n = a.length;
int m = 5 ;
int l = arr.length;
printCount(a, n, m, arr, l);
}
}
|
Python3
def printCount(a, n, m, z, l):
for i in range (n):
mp = dict ()
for j in range (m):
mp[a[i][j]] = 1
count = 0
for j in range (l):
if z[j] in mp.keys():
count + = 1
print ( "row" , i + 1 , " = " , count )
a = [[ 8 , 27 , 39 , 589 , 23 ],
[ 23 , 34 , 589 , 12 , 45 ],
[ 939 , 32 , 27 , 12 , 78 ],
[ 23 , 349 , 48 , 21 , 32 ]]
arr = [ 589 , 39 , 27 ]
n = len (a)
m = 5
l = len (arr)
printCount(a, n, m, arr, l)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void printCount( int [,]a, int n, int m,
int []z, int l)
{
for ( int i = 0; i < n; i++)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
for ( int j = 0; j < m; j++)
mp.Add(a[i,j], 1);
int count = 0;
for ( int j = 0; j < l; j++)
{
if (mp.ContainsKey(z[j]))
count += 1;
}
Console.WriteLine( "row" +(i + 1) + " = " + count);
}
}
public static void Main(String[] args)
{
int [,]a = { { 8, 27, 39, 589, 23 },
{ 23, 34, 589, 12, 45 },
{ 939, 32, 27, 12, 78 },
{ 23, 349, 48, 21, 32 } };
int []arr = { 589, 39, 27 };
int n = a.GetLength(0);
int m = 5;
int l = arr.Length;
printCount(a, n, m, arr, l);
}
}
|
Javascript
<script>
function printCount(a,n,m,z,l)
{
for (let i = 0; i < n; i++)
{
let mp = new Map();
for (let j = 0; j < m; j++)
mp.set(a[i][j], 1);
let count = 0;
for (let j = 0; j < l; j++)
{
if (mp.has(z[j]))
count += 1;
}
document.write( "row" +(i + 1) +
" = " + count+ "<br>" );
}
}
let a = [[ 8, 27, 39, 589, 23 ],
[ 23, 34, 589, 12, 45 ],
[ 939, 32, 27, 12, 78 ],
[ 23, 349, 48, 21, 32 ]];
let arr=[ 589, 39, 27];
let n = a.length;
let m = 5;
let l = arr.length;
printCount(a, n, m, arr, l);
</script>
|
Output
row1 = 3
row2 = 1
row3 = 1
row4 = 0
Time Complexity: O(N*M)
Last Updated :
26 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...