Encontrar el elemento convergente de las diagonales en una array cuadrada

Dada una array cuadrada, la tarea es encontrar el elemento de la array donde convergen las diagonales derecha e izquierda de esta array cuadrada.
Ejemplo: 
 

Input: n = 5, matrix = 
[ 1 2 3 4 5
  5 6 7 8 6
  9 5 6 8 7
  2 3 5 6 8
  1 2 3 4 5 ]
Output: 6

Input: n = 4, matrix = 
[ 1 2 3 4
  5 6 7 8
  9 0 1 2 
  4 5 6 1 ]
Output: NULL
Here there no converging element at all. 
Hence the answer is null.

Acercarse: 
 

  • Si el número de filas y columnas de la array es par, simplemente imprimimos NULL porque no habría ningún elemento convergente en el caso de un número par de filas y columnas.
  • Si el número de filas y columnas de la array es impar, encuentre el valor medio de n como 
     
mid = n/2
  • El arr[mid][mid] en sí mismo es el elemento diagonal convergente.

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

C++

// C++ program to find the converging element
// of the diagonals in a square matrix
 
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
 
// Driver code
int main()
{
    int n = 5;
    int a[][5] = { { 1, 2, 3, 4, 5 },
                   { 5, 6, 7, 8, 6 },
                   { 9, 5, 6, 8, 7 },
                   { 2, 3, 5, 6, 8 },
                   { 1, 2, 3, 4, 5 } };
 
    int convergingele, mid;
    int i, j;
 
    // If n is even, then convergence
    // element will be null.
    if (n % 2 == 0) {
        printf("NULL\n");
    }
 
    else {
        // finding the mid
        mid = n / 2;
 
        // finding the converging element
        convergingele = a[mid][mid];
 
        printf("%d\n", convergingele);
    }
}

Java

// Java program to find the converging element
// of the diagonals in a square matrix
class GFG
{
 
    // Driver code
    public static void main(String args[])
    {
        int n = 5;
        int a[][] = {{1, 2, 3, 4, 5},
                     {5, 6, 7, 8, 6},
                     {9, 5, 6, 8, 7},
                     {2, 3, 5, 6, 8},
                     {1, 2, 3, 4, 5}};
 
        int convergingele, mid;
        int i, j;
 
        // If n is even, then convergence
        // element will be null.
        if (n % 2 == 0)
        {
            System.out.printf("NULL\n");
        }
        else
        {
            // finding the mid
            mid = n / 2;
 
            // finding the converging element
            convergingele = a[mid][mid];
 
            System.out.printf("%d\n", convergingele);
        }
    }
}
 
// This code is contributed by Rajput-Ji

Python3

# Python3 program to find the converging element
# of the diagonals in a square matrix
 
# Driver code
n = 5
a = [[ 1, 2, 3, 4, 5 ],
     [ 5, 6, 7, 8, 6 ],
     [ 9, 5, 6, 8, 7 ],
     [ 2, 3, 5, 6, 8 ],
     [ 1, 2, 3, 4, 5 ]]
 
# If n is even, then convergence
# element will be null.
if (n % 2 == 0):
    print("NULL")
else :
     
    # finding the mid
    mid = n // 2
 
    # finding the converging element
    convergingele = a[mid][mid]
 
    print(convergingele)
 
# This code is contributed by Mohit Kumar

C#

// C# program to find the converging element
// of the diagonals in a square matrix
using System;
     
class GFG
{
 
    // Driver code
    public static void Main(String []args)
    {
        int n = 5;
        int [,]a = {{1, 2, 3, 4, 5},
                    {5, 6, 7, 8, 6},
                    {9, 5, 6, 8, 7},
                    {2, 3, 5, 6, 8},
                    {1, 2, 3, 4, 5}};
 
        int convergingele, mid;
 
        // If n is even, then convergence
        // element will be null.
        if (n % 2 == 0)
        {
            Console.Write("NULL\n");
        }
        else
        {
            // finding the mid
            mid = n / 2;
 
            // finding the converging element
            convergingele = a[mid,mid];
 
            Console.Write("{0}\n", convergingele);
        }
    }
}
 
// This code is contributed by 29AjayKumar

Javascript

<script>
// Javascript program to find the converging element
// of the diagonals in a square matrix
 
// Driver code
    let n = 5;
    let a = [ [ 1, 2, 3, 4, 5 ],
                   [ 5, 6, 7, 8, 6 ],
                   [ 9, 5, 6, 8, 7 ],
                   [ 2, 3, 5, 6, 8 ],
                   [ 1, 2, 3, 4, 5 ] ];
 
    let convergingele, mid;
    let i, j;
 
    // If n is even, then convergence
    // element will be null.
    if (n % 2 == 0) {
        document.write("NULL<br>");
    }
 
    else
    {
     
        // finding the mid
        mid = parseInt(n / 2);
 
        // finding the converging element
        convergingele = a[mid][mid];
 
        document.write(convergingele + "<br>");
    }
 
// This code is contributed by subhammahato348.
</script>
Producción: 

6

 

Publicación traducida automáticamente

Artículo escrito por sunilkannur98 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 *