Suma de todas las partes de una Array cuadrada dividida por sus diagonales

Dada una array 2D arr[][] de N*N dimensiones, la tarea es encontrar la suma de los elementos de las cuatro partes de la array dividida por las diagonales sin incluir los elementos diagonales en ninguna de las cuatro partes.
Ejemplo: 
 

Entrada: arr[][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} } 
Salida: 2 4 6 8 
Explicación: 
(1, 5, 9) y ( 3, 5, 7) son diagonales de la array arr[][] . Por lo tanto, la suma de las partes es: 
Superior = 2 
Izquierda = 4 
Derecha = 6 
Inferior = 8
Entrada: arr[][] = { {1, 3, 1, 5}, {2, 2, 4, 1}, {5, 0, 2, 3}, { 1, 3, 3, 5} } 
Salida: 4 7 4 6 
Explicación: 
(1, 2, 2, 5) y (5, 4, 0, 1) son diagonales de arr[] [] array. Por lo tanto, la suma de las partes es: 
Arriba = 3 + 1 = 4 
Izquierda = 2 + 5 = 7 
Derecha = 1 + 3 = 4 
Abajo = 3 + 3 = 6 
 

Acercarse: 
 

Como se muestra en la figura anterior, después de que la array de tamaño NxN se divide por diagonales. Observamos las siguientes propiedades: 
 

  1. Si la suma del índice de la fila y la columna es menor que N – 1 , pertenece a la parte superior o a la izquierda. 
    • Si el índice de columna es mayor que el índice de fila, pertenece a la parte superior .
    • De lo contrario, pertenece a la parte izquierda .
  2. De lo contrario, pertenece a la parte derecha o a la parte inferior.
    • Si el índice de columna es mayor que el índice de fila, pertenece a la parte derecha .
    • De lo contrario, pertenece a la parte de Down .

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
 
// Function to calculate the
// sum of all parts of matrix
void SumOfPartsOfMetrics(int* arr,
                         int N)
{
 
    // To store the sum of all four
    // parts of the diagonals
    int top, bottom, left, right;
 
    // Initialise respective sum
    // as zero
    top = bottom = right = left = 0;
 
    // Traversing the matrix
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
 
            // If i + j < N -1
            // then it belongs to
            // top or left
            if (i + j < N - 1 && i != j) {
 
                // Belongs to top
                if (i < j) {
                    top += (arr + i * N)[j];
                }
 
                // Belongs to left
                else {
                    left += (arr + i * N)[j];
                }
            }
 
            // If i+j > N - 1 then
            // it belongs to right
            // or bottom
            else if (i + j > N - 1 && i != j) {
 
                // Belongs to right
                if (i > j) {
                    bottom += (arr + i * N)[j];
                }
 
                // Belongs to bottom
                else {
                    right += (arr + i * N)[j];
                }
            }
        }
    }
    cout << top << ' ' << left
         << ' ' << right << ' '
         << bottom << endl;
}
 
// Driver Code
int main()
{
    int N = 4;
    int arr[N][N] = { { 1, 3, 1, 5 },
                      { 2, 2, 4, 1 },
                      { 5, 0, 2, 3 },
                      { 1, 3, 3, 5 } };
    // Function call to find print
    // sum of al parts
    SumOfPartsOfMetrics((int*)arr, N);
    return 0;
}

Java

// Java program for the above approach
class GFG {
 
// Function to calculate the
// sum of all parts of matrix
static void SumOfPartsOfMetrics(int [][]arr,
                                int N)
{
     
    // To store the sum of all four
    // parts of the diagonals
    // Initialise respective sum
    // as zero
    int top = 0, bottom = 0;
    int left = 0, right = 0;
 
    // Traversing the matrix
    for(int i = 0; i < N; i++)
    {
       for(int j = 0; j < N; j++)
       {
           
          // If i + j < N -1
          // then it belongs to
          // top or left
          if (i + j < N - 1 && i != j)
          {
               
              // Belongs to top
              if (i < j)
              {
                  top += arr[i][j];
              }
               
              // Belongs to left
              else
              {
                  left += arr[i][j];
              }
          }
           
          // If i+j > N - 1 then
          // it belongs to right
          // or bottom
          else if (i + j > N - 1 && i != j)
          {
               
              // Belongs to right
              if (i > j)
              {
                  bottom += arr[i][j];
              }
               
              // Belongs to bottom
              else
              {
                  right += arr[i][j];
              }
          }
       }
    }
    System.out.println(top + " " + left + " " +
                     right + " " + bottom);
}
     
// Driver Code
public static void main (String[] args)
{
    int N = 4;
    int arr[][] = { { 1, 3, 1, 5 },
                    { 2, 2, 4, 1 },
                    { 5, 0, 2, 3 },
                    { 1, 3, 3, 5 } };
                         
    // Function call to find print
    // sum of al parts
    SumOfPartsOfMetrics(arr, N);
}
}
 
// This code is contributed by AnkitRai01

Python3

# Python3 program for the above approach
 
# Function to calculate the
# sum of all parts of matrix
def SumOfPartsOfMetrics(arr, N):
 
    # To store the sum of all four
    # parts of the diagonals
    # Initialise respective sum
    # as zero
    top = bottom = right = left = 0;
 
    # Traversing the matrix
    for i in range(N):
        for j in range(N):
 
            # If i + j < N -1
            # then it belongs to
            # top or left
            if (i + j < N - 1 and i != j):
 
                # Belongs to top
                if (i < j):
                    top += arr[i][j];
             
                # Belongs to left
                else:
                    left += arr[i][j];
 
            # If i+j > N - 1 then
            # it belongs to right
            # or bottom
            elif (i + j > N - 1 and i != j):
 
                # Belongs to right
                if (i > j):
                    bottom += arr[i][j];
 
                # Belongs to bottom
                else:
                    right += arr[i][j];
             
    print(top, left, right, bottom);
 
# Driver Code
if __name__ == "__main__":
 
    N = 4;
    arr = [ [ 1, 3, 1, 5 ],
            [ 2, 2, 4, 1 ],
            [ 5, 0, 2, 3 ],
            [ 1, 3, 3, 5 ] ];
             
    # Function call to find print
    # sum of al parts
    SumOfPartsOfMetrics(arr, N);
     
# This code is contributed by AnkitRai01

C#

// C# program for the above approach
using System;
 
class GFG {
 
// Function to calculate the
// sum of all parts of matrix
static void SumOfPartsOfMetrics(int [,]arr,
                                int N)
{
         
    // To store the sum of all four
    // parts of the diagonals
    // Initialise respective sum
    // as zero
    int top = 0, bottom = 0;
    int left = 0, right = 0;
     
    // Traversing the matrix
    for(int i = 0; i < N; i++)
    {
       for(int j = 0; j < N; j++)
       {
 
          // If i + j < N -1
          // then it belongs to
          // top or left
          if (i + j < N - 1 && i != j)
          {
               
              // Belongs to top
              if (i < j)
              {
                  top += arr[i, j];
              }
               
              // Belongs to left
              else
              {
                  left += arr[i, j];
              }
          }
           
          // If i+j > N - 1 then
          // it belongs to right
          // or bottom
          else if (i + j > N - 1 && i != j)
          {
                 
            // Belongs to right
            if (i > j)
            {
                bottom += arr[i, j];
            }
             
            // Belongs to bottom
            else
            {
                right += arr[i, j];
            }
          }
       }
    }
    Console.WriteLine(top + " " + left + " " +
                    right + " " + bottom);
}
         
// Driver Code
public static void Main (string[] args)
{
    int N = 4;
    int [,]arr = { { 1, 3, 1, 5 },
                   { 2, 2, 4, 1 },
                   { 5, 0, 2, 3 },
                   { 1, 3, 3, 5 } };
                             
    // Function call to find print
    // sum of al parts
    SumOfPartsOfMetrics(arr, N);
}
}
 
// This code is contributed by AnkitRai01

Javascript

<script>
// Javascript program for the above approach
 
// Function to calculate the
// sum of all parts of matrix
function SumOfPartsOfMetrics(arr, N)
{
 
    // To store the sum of all four
    // parts of the diagonals
    let top, bottom, left, right;
 
    // Initialise respective sum
    // as zero
    top = bottom = right = left = 0;
 
    // Traversing the matrix
    for (let i = 0; i < N; i++) {
        for (let j = 0; j < N; j++) {
 
            // If i + j < N -1
            // then it belongs to
            // top or left
            if (i + j < N - 1 && i != j) {
 
                // Belongs to top
                if (i < j) {
                    top += arr[i][j];
                }
 
                // Belongs to left
                else {
                    left += arr[i][j];
                }
            }
 
            // If i+j > N - 1 then
            // it belongs to right
            // or bottom
            else if (i + j > N - 1 && i != j) {
 
                // Belongs to right
                if (i > j) {
                    bottom += arr[i][j];
                }
 
                // Belongs to bottom
                else {
                    right += arr[i][j];
                }
            }
        }
    }
    document.write(top + ' ' + left
         + ' ' + right + ' '
         + bottom + "<br>");
}
 
// Driver Code
    let N = 4;
    let arr = [ [ 1, 3, 1, 5 ],
                      [ 2, 2, 4, 1 ],
                      [ 5, 0, 2, 3 ],
                      [ 1, 3, 3, 5 ] ];
    // Function call to find print
    // sum of al parts
    SumOfPartsOfMetrics(arr, N);
 
// This code is contributed by rishavmahato348.
</script>
Producción: 

4 7 4 6

 

Complejidad del tiempo: O(N 2 )
 

Publicación traducida automáticamente

Artículo escrito por manthanpandey039 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *