Sum of non | C ++ implementation of the above approach ; Function to return a vector which consists the sum of four portions of the matrix ; Iterating through the matrix ; Condition for selecting all values before the second diagonal of metrics ; Top portion of the matrix ; Left portion of the matrix ; Bottom portion of the matrix ; Right portion of the matrix ; Adding all the four portions into a vector ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfParts ( int * arr , int N ) { int sum_part1 = 0 , sum_part2 = 0 , sum_part3 = 0 , sum_part4 = 0 ; int totalsum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i + j < N - 1 ) { if ( i < j and i != j and i + j ) sum_part1 += ( arr + i * N ) [ j ] ; else if ( i != j ) sum_part2 += ( arr + i * N ) [ j ] ; } else { if ( i > j and i + j != N - 1 ) sum_part3 += ( arr + i * N ) [ j ] ; else { if ( i + j != N - 1 and i != j ) sum_part4 += ( arr + i * N ) [ j ] ; } } } } totalsum = sum_part1 + sum_part2 + sum_part3 + sum_part4 ; return totalsum ; } int main ( ) { int N = 4 ; int arr [ N ] [ N ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; cout << sumOfParts ( ( int * ) arr , N ) ; }