Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

To solve this problem, you need to follow these steps:

  1. Read the size of the matrices N, M from the input.
  2. Create two 2D arrays (matrices) A and B of size NxM.
  3. Read the elements of matrix A from the input and store them in the matrix A.
  4. Read the elements of matrix B from the input and store them in the matrix B.
  5. Initialize an empty list to store the sums.
  6. Loop from i=0 to N. For each i, calculate the sum of the i-th row of matrix A and the (N-i-1)-th row of matrix B. Append this sum to the list.
  7. Print the elements of the list.

Here is a Python code that implements these steps:

N, M = map(int, input().split())
A = [list(map(int, input().split

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, find their product.Input FormatThe first line of input contains T - the number of test cases. The first line of each test case contains N1, M1 - the size of the 1st matrix. It is followed by N1 lines each containing M1 integers - elements of the 1st matrix. The next line contains N2, M2 - the size of the 2nd matrix. It is followed by N2 lines each containing M2 integers - elements of the 2nd matrix. Note that M1 = N2.Output FormatFor each test case, print the resultant product matrix, separated by a new line.Constraints1 <= T <= 1001 <= N1,M1,N2,M2 <= 50-100 <= mat[i][j] <= 100

Print 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] <= 102

Accept two square matrices A and B of dimensions n×n as input and compute their product AB.The first line of the input will contain the integer n. This is followed by 2n lines. Out of these, each of the first n lines is a sequence of comma-separated integers that denotes one row of the matrix A. Each of the last n lines is a sequence of comma-separated integers that denotes one row of the matrix B.Your output should again be a sequence of n lines, where each line is a sequence of comma-separated integers that denotes a row of the matrix AB.CODE WITH ALGORITHM# CODE WITH ALGORITHM# 1. Accept an integer 'n' as input.# 2. Initialize empty lists 'A' and 'B' to store matrices.# 3. Loop 'n' times to accept matrix A:#     3.1. Initialize an empty list 'row'.#     3.2. Split the input line by ',' and convert each element to an integer, adding it to 'row'.#     3.3. Append 'row' to matrix 'A'.# 4. Loop 'n' times to accept matrix B:#     4.1. Initialize an empty list 'row'.#     4.2. Split the input line by ',' and convert each element to an integer, adding it to 'row'.#     4.3. Append 'row' to matrix 'B'.# 5. initialize matrix 'C' as a zero-matrix:#     5.1. Loop 'n' times to initialize each element of 'C' to 0.# 6. [Perform matrix product:]#     6.1. Loop 'n' times to iterate through rows of 'A':#         6.1.1. Loop 'n' times to iterate through columns of 'B':#             6.1.1.1. Initialize a variable 'esum' to 0.#             6.1.1.2. Loop 'n' times to iterate through the elements of 'A' and 'B':#                 6.1.1.2.1. Add the product of corresponding elements from 'A' and 'B' to 'esum'.#             6.1.1.3. Update the corresponding element in 'C' with 'esum'.#             6.1.1.4. If it's not the last element in the row:#                 6.1.1.4.1. Print the element followed by a comma.#                 6.1.1.4.2. Else, print the last element in the row without a comma.Sample Test CasesDownload All Test Case 1InputExpected OutputActual Output21,23,45,67,819,2243,50Test Case 2InputExpected OutputActual Output31,0,00,1,00,0,12,3,45,9,110,12,132,3,45,9,110,12,13

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)

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.