Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution 1

This problem is about finding

Solution 2

This problem is about finding the sum of the elements of all diagonals in a 2D matrix of size NxN. Here are the steps to solve this problem:

  1. First, read the number of test cases T.

  2. For each test case, read the size of the matrix N.

  3. Create a 2D matrix of size NxN and read the elements of the matrix.

  4. To find the sum of the elements of all diagonals, you need to iterate through the matrix.

  5. Start from the top left corner of the matrix and move towards the bottom right corner to calculate the

This problem has been solved

Similar Questions

Diagonal Traversal of Matrix bookmark_borderdescriptionGiven 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

Write a program to obtain a matrix and find the sum of its diagonal elements.Note: Only square matrix.Input format :The input consists of the number of rows and columns separated by a space.The second line of the input is matrix elements.Output format :The output prints the sum of diagonal elements.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :3 31 2 34 5 67 8 9Output 1 :15Input 2 :4 412 23 45 5678 89 98 8765 54 32 2114 25 36 58Output 2 :191

iven a 2D square matrix, rotate it by 90 degrees clockwise.Note:Try to solve it by first scanning the matrix, then doing an in-place rotation, and then printing the rotated matrix.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 [NxN]. It is followed by N lines each containing N integers - matrix elements.Output FormatFor each test case, print the rotated matrix, separated by a new line.Constraints1 <= T <= 1001 <= N <= 100-100 <= ar[i][j] <= 100ExampleInput41121 24 331 2 38 9 47 6 55-44 25 -52 69 -517 22 51 27 -44-79 28 -78 1 -4765 -77 -14 -21 -6-96 43 -21 -20 90OutputTest Case #1:1Test Case #2:4 13 2Test Case #3:7 8 16 9 25 4 3Test Case #4:-96 65 -79 17 -4443 -77 28 22 25-21 -14 -78 51 -52-20 -21 1 27 6990 -6 -47 -44 -5

Given an array of integers of size N, print the sum of sum of all subarrays.Input FormatFirst line of input contains T - number of test cases. Its followed by 2T lines, the first line contains N - size of the array and second line contains the elements of the array.Output FormatFor each test case, print the sum of sum of all subarrays, separated by newline.Constraints10 points1 <= T <= 1001 <= N <= 10220 points1 <= T <= 1001 <= N <= 10370 points1 <= T <= 10001 <= N <= 104General Constraints-106 <= arr[i] <= 106ExampleInput333 4 521 231 -3 4Output4063ExplanationTest Case 1:[3] + [3,4] + [3,4,5] + [4] + [4,5] + [5] = 40Test Case 2:[1] + [1,2] + [2] = 6Test Case 3:[1] + [1,-3] + [1,-3,4] + [-3] + [-3,4] + [4] = 3

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

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.