Skip to main content

Program for Total Sum on the Boundaries and Diagonals

Total Sum on the Boundaries and Diagonals





Send Feedback

For a given two-dimensional square matrix of size (N x N). Find the total sum of elements on both the diagonals and at all the four boundaries.

Input format:

The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.


First line of each test case or query contains a single integer value, 'N' representing the 'rows' and 'columns' for the two-dimensional square matrix.


Second line onwards, the next 'N' lines or rows represent the ith row values.


Each of the ith row constitutes 'N' column values separated by a single space.

Output format:

For each test case, print the single integer denoting the sum.


Output for every test case will be printed in a seperate line.

Constraints:

1 <= t <= 10^2

0 <= N <= 10^3

Time Limit: 1sec

Sample input 1:

1

3

1 2 3

4 5 6

7 8 9

Sample Output 1:

45

Explanation for Sample Output 1:

The boundary elements are 1, 2, 3, 6, 9, 8, 7 and 4. 


The first-diagonal elements are 1, 5 and 9. 


The second-diagonal elements are 3, 5 and 7.


We just need to add all these numbers making sure that no number is added twice. For example, '1' is both a boundary element and a first-diagonal element similarly, '5' contributes to both the diagonals but they won't be added twice.


Hence, we add up, [1 + 2 + 3 + 6 + 9 + 8 + 7 + 4 + 5] to give 45 as the output.

Sample input 2:

2

5

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

4

1 2 3 10

4 5 6 11

7 8 9 12

13 14 15 16

Sample Output 2:

273

136


Program for Total Sum on the Boundaries and Diagonals in C++


#include <iostream>

using namespace std;


int main()

{

    int t;

    cin>>t;

    while(t--){

        int sum = 0;

        int n;

        cin>>n;

        int** arr = new int* [n];

        for(int i=0;i<n;i++){

            arr[i] = new int [n];

            for(int j=0;j<n;j++){

                cin>>arr[i][j];

            }

        }

        for(int i=0;i<n;i++){

            for(int j=0;j<n;j++){

                if(j==0 || i==0 || i==j||j==n-1 || i==n-1  || i+j==n-1 ){

                    sum+= arr[i][j];

                }

            }

        }

        cout<<sum;

    }


    return 0;

}


Comments

Popular posts from this blog

TechGig Code Gladiators 2023 Coding Solutions

TechGig Open Coding Round Code Gladiators 2023 Answers Round 1  - Problem 1 Forest Fire Coding Solution Figure out the minimum energy level P such that they can get exactly X animals to transport.  Test Case: 5 4  1 3 2 4 5 Output: 2 Coding Solution in Python 3: from bisect import bisect_left #Telegram - @PLACEMENTLELO n, m = map ( int , input ().split()) a = list ( map ( int , input ().split())) a.sort() #Telegram - @PLACEMENTLELO if n - bisect_left(a, a[n-m]) != m: print ( -1 ) else : print (a[n - m]) #Telegram - @PLACEMENTLELO ------------------------------------------- Round 2  - Problem 2 The Magic Coding Solution The cost of using wand for operation on element equal to absolute difference between value of element and desired value after operation. Test Case: 5 3 1 2 3 4 5 5 2 1 Output: 10 7 10 Coding Solution in Python 3: from bisect import bisect_left #Telegram - @PLACEMENTLELO n, m = map ( int , input ().split()) A = list ( map ( int , input ()...

Program for Oscillating Prices of Chakri Coding Problem

Oscillating Prices of Chakri Diwali is here. While everyone here is busy texting "Happy Diwali" wishes to everybody else, Coder has some other plans and wants to earn some money this season. Now, the Apex court has allowed the sale of only green crackers this Diwali. Out of all green crackers, "Chakri" is most popular. Because of the irregular supply of "Chakri", the price of "Chakri" is oscillating daily. Coder saw a business opportunity in this. He/She got a price list for coming N days from an insider in the market union. Prices in the list are for 1 unit of a large packet of "Chakri". Each large packet contains 100 units of Chakri. Now, due to financial limitations, Coder can transact only 1 large packet (100 units of "Chakri") in the market. You have to tell maximum profit possible, given that he/she can transact atmost one time. Note: 1. Transaction refers to the act of buying and selling.       2. "Chakri" can...

GEEKS FOR GEEKS WEEKLY CODING CONTEST 103 SOLUTIONS

GEEKS FOR GEEKS WEEKLY CODING CONTEST 103 SOLUTIONS Refueling Code Answer C++ Refueling First GFG Weekly Coding Contest 103 class Solution{     public:     long long refueling(int X)     {         long long a, b;         for(int i=0; i<X; i++){             a = pow(2,i);             if(a == X)return a;             else if(a>X){                 b = pow(2,i-1);                 if(abs(X-a) == abs(X-b))return a;                 if(abs(X-a) < abs(X-b))return a;                 else return b;             }         }         return -1;     } }; Input: X = 3,  Output: 4 Input:  X = 2,...