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
Question
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
Solution
Sure, here is a Python solution for your problem:
def sum_rows(A, B):
N = len(A)
M = len(B[0])
result = [[0]*M for _ in range(N)]
for i in range(N):
for j in range(M):
result[i][j] = A[i][j] + B[N-i-1][j]
return result
This function sum_rows takes two 2D lists A and B as input, which represent the matrices. The function first initializes a new 2D list result with the same dimensions as A and B, filled with zeros.
Then it iterates over each row i and each column j of the matrices. For each cell, it adds the value from the i-th row of A and the (N-i-1)-th row of B, and stores the result in the corresponding cell of result.
Finally, it returns the result matrix, which contains the sums of the corresponding rows from A and B.
Please note that this solution assumes that A and B have the same dimensions, as per the problem statement. If they don't, you'll need to add some error checking code to handle this case.
Similar Questions
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
Given a 2D matrix of size NxN, print the sum of the elements of all its diagonals.Input FormatThe first line of input contains T - the number of test cases. The first line of each test case contains the N - the size of the matrix. Each of the next N lines contains N integers - the elements of the matrix.Output FormatFor each test case, print the sum of the elements of all the diagonals, separated by a new line. Refer to samples for more clarity.Constraints1 <= T <= 1001 <= N <= 100-100 <= ar[i][j] <= 100ExampleInput43-5 0 42 8 -63 7 11-425 -2-4 16-2 -3 -6 -5 50 38 7 10 -5 -3 306 3 70 9 -20 -7-9 9 -6 7 3 2-1 7 7 6 -4 38 5 6 -9 40 8Output4 -6 4 9 3-4-2 6 -43 80 -15 -29 22 86 51 13 4 4 8ExplanationTest Case 1Sum of the elements of the 1st diagonal: 4Sum of the elements of the 2nd diagonal: 0 + -6 = -6Sum of the elements of the 3rd diagonal: -5 + 8 + 1 = 4Sum of the elements of the 4th diagonal: 2 + 7 = 9Sum of the elements of the 5th diagonal: 3Test Case 2Sum of the elements of the 1st and only diagonal: -4Test Case 3Sum of the elements of the 1st diagonal: -2Sum of the elements of the 2nd diagonal: 5 + 1 = 6Sum of the elements of the 3rd diagonal: -4
Max Ones in a RowMax Score: 20Given a M x N binary matrix, find the 0-indexed position of the row that contains the maximum count of ones, and the number of ones in that row. In case there are multiple rows that have the maximum count of ones, the row with the smallest row number should be selected.Input FormatThe first line of input contains integers M and N separated by space. For the next M lines, each line contains N elements separated by spaces.Output FormatPrint the index of the row, followed by the number of ones in it.Constraints1 ≤ M, N ≤ 100ExampleInput
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
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
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.