Knowee
Questions
Features
Study Tools

You are given a matrix and an array. For each row of the given matrix, for each element of that row, check if it is present in the given array. Print the count of elements present for every row.Input FormatThe first line of input contains N, M - the size of the matrix. It is followed by N lines each containing M integers - elements of the matrix. The next line of the input contains the A - the size of the array. The next line of the input contains the array elements.Output FormatFor each row, print the count of the number of elements present, separated by a new line.Constraints1 <= N, M, A <= 100-100 <= mat[i][j], ar[i] <= 100ExampleInput3 45 9 -2 2-3 4 1 92 -2 1 -255 1 -2 2 6Output314ExplanationThe first row of the matrix is (5 9 -2 2), out of this, 3 elements (5 -2 2), except 9, are present in the given array (5 1 -2 2 6)The second row of the matrix is (-3 4 1 9), out of this, only 1 is present in the given array (5 1 -2 2 6)The third row of the matrix is (2 -2 1 -2), all the 4 elements are present in the given array (5 1 -2 2 6)

Question

You are given a matrix and an array. For each row of the given matrix, for each element of that row, check if it is present in the given array. Print the count of elements present for every row.Input FormatThe first line of input contains N, M - the size of the matrix. It is followed by N lines each containing M integers - elements of the matrix. The next line of the input contains the A - the size of the array. The next line of the input contains the array elements.Output FormatFor each row, print the count of the number of elements present, separated by a new line.Constraints1 <= N, M, A <= 100-100 <= mat[i][j], ar[i] <= 100ExampleInput3 45 9 -2 2-3 4 1 92 -2 1 -255 1 -2 2 6Output314ExplanationThe first row of the matrix is (5 9 -2 2), out of this, 3 elements (5 -2 2), except 9, are present in the given array (5 1 -2 2 6)The second row of the matrix is (-3 4 1 9), out of this, only 1 is present in the given array (5 1 -2 2 6)The third row of the matrix is (2 -2 1 -2), all the 4 elements are present in the given array (5 1 -2 2 6)

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

To solve this problem, you can follow these steps:

  1. Read the size of the matrix (N, M) from the input.
  2. Create an empty matrix of size N x M.
  3. Loop over the range N (number of rows in the matrix) and for each iteration, read a line from the input, split it into M elements, convert each element to an integer and add it to the current row of the matrix.
  4. Read the size of the array (A) from the input.
  5. Read the array from the input, split it into A elements, convert each element to an integer and store it in a list.
  6. Loop over each row in the matrix and for each row, loop over each element in the row. If the element is in the array, increment a counter. After checking all elements in the row, print the counter and reset it to 0 for the next row.

Here is a Python code that implements these steps:

N, M = map(int, input().split())
matrix = [list(map(int, input().split())) for _ in range(N)]
A = int(input())
array = set(map(int, input().split()))

for row in matrix:
    count = sum(1 for element in row if element in array)
    print(count)

This code uses a set to store the array elements because checking if an element is in a set is faster than checking if it is in a list. The sum function with a generator expression is used to count the number of elements in each row that are in the array.

This problem has been solved

Similar Questions

Write a program to obtain a matrix and find the sum of each row and each column.Input format :The first line of the input consists of the value of the number of rows and the number of columns.The second line of the input consists of a matrix.Output format :The output prints the sum of each row and each column.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :4 41 2 3 45 6 7 89 10 11 1213 14 15 16Output 1 :Sum of the row 0 = 10Sum of the row 1 = 26Sum of the row 2 = 42Sum of the row 3 = 58Sum of the column 0 = 28Sum of the column 1 = 32Sum of the column 2 = 36Sum of the column 3 = 40Input 2 :3 398 87 6545 32 2845 56 58Output 2 :Sum of the row 0 = 250Sum of the row 1 = 105Sum of the row 2 = 159Sum of the column 0 = 188Sum of the column 1 = 175Sum of the column 2 = 151

Given 2 matrices A and B of size NxM. You have to find the sum between 1st row of matrix A and Nth row of matrix B, 2nd row of matrix A and (N-1)th row of matrix B and so on.Input FormatFirst line of input contains N,M - the size of the matrices. Its followed by 2xN lines, each containing M integers - elements of the matrices. First N lines for matrix A and the next N lines for matrix B.Output FormatPrint the sum of row 1 of A and row N of B, row 2 of A and row (N-1) of B and so on.Constraints1 <= N, M <= 100-102 <= mat[i][j] <= 102ExampleInput3 33 -2 51 3 44 0 72 2 4-8 4 37 1 9Output23719ExplanationSelf Expla

You will be given a 2-d Matrix, and you need to concatenate values of the row, let's say it as Array 'A', which will contain concatenated values of each row as each value.Now, you need to concatenate the values present in 'A'(in any order) so that the final concatenated number is the maximum of all numbers possible.Input FormatThe first input line contains an integer 'n' denoting the number of rows in the matrix.The second input line contains an integer 'm' denoting the number of columns in the matrix.Next, 'n' input lines contain 'm' space-separated integers denoting matrix elements.Output FormatOutput the final concatenated string.Constraints2 <= n <= 5002 <= m <= 5001<=matrix elements<=105Sample Input331 2 34 5 67 8 9Sample Output789456123ExplanationAfter concatenating the values of each row, we get : "123" , '456" , "789"Now there are many possibilities for concatenating the above 3 values as follows: "123456789" , "456123789" , "789456123" , etc.Out of all possibilities, only "789456123" is the maximum of all, hence this is our answer.

Given a matrix, write a program to calculate the average of all its elements.Input:The input consists of two integers, m and n, representing the number of rows and columns in the matrix, respectively. This is followed by m lines, each containing n integers representing the elements of the matrix.Output:Print the average of all elements in the matrix.

You are given a 2D matrix grid of size m x n. You need to check if each cell grid[i][j] is:Equal to the cell below it, i.e. grid[i][j] == grid[i + 1][j] (if it exists).Different from the cell to its right, i.e. grid[i][j] != grid[i][j + 1] (if it exists).Return true if all the cells satisfy these conditions, otherwise, return false. Example 1:Input: grid = [[1,0,2],[1,0,2]]Output: trueExplanation:All the cells in the grid satisfy the conditions.Example 2:Input: grid = [[1,1,1],[0,0,0]]Output: falseExplanation:All cells in the first row are equal.Example 3:Input: grid = [[1],[2],[3]]Output: falseExplanation:Cells in the first column have different values. Constraints:1 <= n, m <= 100 <= grid[i][j] <= 9

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.