Sum of upper and lower triangles | Practice | GeeksforGeeks

Given a square matrix of size N*N, print the sum of upper and lower triangular elements. Upper Triangle consists of elements on the diagonal and above it. The lower triangle consists of elements on the diagonal and below it. 

Example 1:
Input:
N = 3 
mat[][] = {{6, 5, 4},
           {1, 2, 5}
           {7, 9, 7}}
Output: 29 32
Explanation:
The given matrix is
6 5 4
1 2 5
7 9 7
The elements of upper triangle are
6 5 4
  2 5
    7
Sum of these elements is 6+5+4+2+5+7=29
The elements of lower triangle are
6
1 2
7 9 7
Sum of these elements is 6+1+2+7+9+7= 32.
Example 2:
Input:
N = 2
mat[][] = {{1, 2},
           {3, 4}}
Output: 7 8
Explanation:
Upper triangular matrix:
1 2
  4
Sum of these elements are 7.
Lower triangular matrix:
1
3 4
Sum of these elements are 8.


Your Task:
You don't need to read input or print anything. Complete the function sumTriangles() that takes matrix and its size N as input parameters and returns the list of integers containing the sum of upper and lower triangle. 

Expected Time Complexity: O(N * N)
Expected Auxiliary Space: O(1)

Constraints: 
1 <= N <= 100
1 <= matrix[i][j] <= 100
0
nikhilbarot16026 days ago
C++ Solution:
 
Time-Complexity: O (n * n) (0.05/1.05)
All test-cases passed
 
vector<int> sumTriangles(const vector<vector<int> >& matrix, int n)
    {
        vector<int> res;
        int sum = 0;
        
        for(int i = 0; i < n; i++){
            for(int j = i; j < n; j++){
                sum = sum + matrix[i][j];
            }
        }
        res.push_back(sum);
        
        sum = 0;
        for(int i = n - 1; i >= 0; i--){
            for(int j = i; j >= 0; j--){
                sum = sum + matrix[i][j];
            }
        }
        res.push_back(sum);
        
        return res;
        
    }
+1
sailendrachettri3 weeks ago
C++ Iterative approach
 vector<int> sumTriangles(const vector<vector<int> >& matrix, int n)
    {
        int lower_t = 0;
        int upper_t = 0;
        
        for(int i = 0; i < n; i++){
            for(int j = 0; j <= i; j++){
                lower_t += matrix[i][j];
                // cout<<matrix[i][j]<<" ";
            }
            // cout<<endl;
        }
        
        for(int i = 0; i < n; i++){
            for(int j = i; j < n; j++){
                upper_t += matrix[i][j];
                // cout<<matrix[i][j];
            }
            // cout<<endl;
        }
        
        return {upper_t, lower_t};
    }
+1
kartikeyashokgautam1 month ago
Neat and Clean JAVA Solution :-
 
static ArrayList<Integer> sumTriangles(int matrix[][], int n)   {   int diaSum = 0;   int lowSum = 0;   int uppSum = 0;      ArrayList<Integer> al = new ArrayList<>();        for(int i=0;i<n;i++)     {         for(int j=0;j<n;j++)     {         // Diagonal Sum         if(i==j)         {             diaSum+=matrix[i][j];         }                   // Lower Sum           if(i>j)         {             lowSum+=matrix[i][j];         }                  // Upper Sum            if(i<j)         {             uppSum+=matrix[i][j];         }     }          }          al.add(uppSum+diaSum);     al.add(lowSum+diaSum);        return al;          }
 
+1
kalyaniworkmail1 month ago
 static ArrayList<Integer> sumTriangles(int matrix[][], int n)   {       // code here       ArrayList<Integer> valList = new ArrayList<Integer>();       int sum1 = 0,sum2=0, diaSum = 0;       for(int i=0;i<matrix.length;i++){           for(int j=i+1;j<matrix.length;j++){               sum1 = sum1 + matrix[i][j]; //sum of upper edges               sum2 = sum2 + matrix[j][i]; //sum of lower edges           }           diaSum = diaSum + matrix[i][i]; //sum of diagonal       }               valList.add(sum1+diaSum);        valList.add(sum2+diaSum);        return valList;   }
0
sachinsingh42231 month ago
vector<int> sumTriangles(const vector<vector<int> >& matrix, int n)   {       // code here       vector<int>v;
      int sum=0;       for(int i=0;i<n;i++){           sum+=matrix[i][i];       }       int U_sum=0;       for(int i=0;i<n;i++){           for(int j=i+1;j<n;j++){               U_sum+=matrix[i][j];           }       }       U_sum+=sum;       int D_sum=0;       for(int i=n-1;i>=0;i--){           for(int j=i-1;j>=0;j--){               D_sum+=matrix[i][j];           }       }       D_sum+=sum;       v.push_back(U_sum);       v.push_back(D_sum);       return v;                        }
0
pandasibasis741 month ago
vector<int> sumTriangles(const vector<vector<int> >& matrix, int n)   {       // code here       vector<int> ans;       int lowersum = 0;       int uppersum = 0;       for(int i=0;i<n;i++)       {           for(int j=0;j<=i;j++)           {               lowersum += matrix[i][j];           }       }       for(int i=0;i<n;i++)       {           for(int j=n-1;j>=i;j--)           {               uppersum += matrix[i][j];           }       }              ans.push_back(uppersum);       ans.push_back(lowersum);       return ans;   }
0
moonu21032 months ago
Sum of upper and lower triangles (Java Solution)
 
class Solution
{
    //Function to return sum of upper and lower triangles of a matrix.
    static ArrayList<Integer> sumTriangles(int arr[][], int n)
    {
        // code here
        int upperSum = 0, lowerSum = 0;
        for (int i=0; i<n;i++)
        {
            for (int j=i; j<n; j++)
            {
                upperSum += arr[i][j];
                lowerSum += arr[j][i];
            }
        }
        ArrayList<Integer> res = new ArrayList<>();
        res.add(upperSum);
        res.add(lowerSum);
        return res;
    }
}
 
+1
sunghunet2 months ago
vector<int> sumTriangles(const vector<vector<int> >& matrix, int n)
{
    // diag
    int diagSum = 0;
    for (int i = 0; i < n; i++) {
        diagSum += matrix[i][i];
    }

    int lowerSum = 0, upperSum = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            upperSum += matrix[i][j];
            lowerSum += matrix[j][i];
        }
    }
    return vector<int>({ upperSum+ diagSum, lowerSum+ diagSum });
}
0
dsaconquered2 months ago
 vector<int> sumTriangles(const vector<vector<int> >& matrix, int n)   {       // code here       int uppersum=0;        int lowersum=0;        for(int i=0;i<n;i++){            for(int j=0;j<n;j++){                if(i==j || i<j){                    uppersum+=matrix[i][j];                }               if(i==j || i>j){                    lowersum+=matrix[i][j];                }            }        }        vector<int>ans;        ans.push_back(uppersum);        ans.push_back(lowersum);        return ans;   }
0
imabheek2 months ago
/*JAVA*/ int sum_upper = 0, sum_lower = 0;       for(int i = 0; i < n; i++){           for(int j = 0; j < n; j++){               if(i <= j){sum_upper += matrix[i][j];}               if(i >= j){sum_lower += matrix[i][j];} } }  ArrayList<Integer> ls = new ArrayList<Integer>();       ls.add(sum_upper);       ls.add(sum_lower);       return ls;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.