Programa para hallar el Producto de elementos diagonales de una array

Dada una array N * N, la tarea es encontrar el producto de los elementos de la diagonal izquierda y derecha. 
Ejemplos: 
 

Input: arr[] = 1 2 3 4
               5 6 7 8 
               9 7 4 2
               2 2 2 1
Output: 9408
Explanation:
Product of left diagonal = 1 * 4 * 6 * 1 = 24
Product of right diagonal = 4 * 7 * 7 * 2 = 392
Total product = 24 * 392 = 9408

Input: arr[] = 2 1 2 1 2
               1 2 1 2 1
               2 1 2 1 2
               1 2 1 2 1
               2 1 2 1 2  
Output : 512
Explanation:
Product of left diagonal = 2 * 2 * 2 * 2 * 2 = 32
Product of right diagonal = 2 * 2 * 2 * 2 * 2 = 32
But we have a common element in this case so
Total product = (32 * 32)/2  = 512

Acercarse: 
 

  • Necesitamos encontrar los elementos de la diagonal principal y la diagonal secundaria de la array. Por favor refiérase a este artículo para este [ Programa para imprimir las Diagonales de una Array
     
  • En este método, usamos un bucle, es decir, un bucle para calcular el producto de las diagonales principal y secundaria.
  • Divida la respuesta por el elemento medio para la array de tamaño impar

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

CPP

// C++ Program to find the Product
// of diagonal elements of a matrix
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the product of diagonals
int productDiagonals(int arr[][100], int n)
{
 
    int product = 1;
    // loop for calculating product of both
    // the principal and secondary diagonals
    for (int i = 0; i < n; i++) {
 
        // For principal diagonal index of row
        // is equal to index of column
        product = product * arr[i][i];
 
        // For secondary diagonal index
        // of column is n-(index of row)-1
        product = product * arr[i][n - i - 1];
    }
 
    // Divide the answer by middle element for
    // matrix of odd size
    if (n % 2 == 1) {
        product = product / arr[n / 2][n / 2];
    }
 
    return product;
}
 
// Driver code
int main()
{
    int arr1[100][100] = { { 1, 2, 3, 4 },
                           { 5, 6, 7, 8 },
                           { 9, 7, 4, 2 },
                           { 2, 2, 2, 1 } };
    // Function calling
    cout << productDiagonals(arr1, 4) << endl;
 
    int arr2[100][100] = { { 2, 1, 2, 1, 2 },
                           { 1, 2, 1, 2, 1 },
                           { 2, 1, 2, 1, 2 },
                           { 1, 2, 1, 2, 1 },
                           { 2, 1, 2, 1, 2 } };
    // Function calling
    cout << productDiagonals(arr2, 5) << endl;
    return 0;
}

Java

// Java Program to find the Product
// of diagonal elements of a matrix
import java.util.*;
 
class GFG
{
 
// Function to find the product of diagonals
static int productDiagonals(int arr[][], int n)
{
 
    int product = 1;
    // loop for calculating product of both
    // the principal and secondary diagonals
    for (int i = 0; i < n; i++)
    {
 
        // For principal diagonal index of row
        // is equal to index of column
        product = product * arr[i][i];
 
        // For secondary diagonal index
        // of column is n-(index of row)-1
        product = product * arr[i][n - i - 1];
    }
 
    // Divide the answer by middle element for
    // matrix of odd size
    if (n % 2 == 1)
    {
        product = product / arr[n / 2][n / 2];
    }
 
    return product;
}
 
// Driver code
public static void main(String[] args)
{
    int arr1[][] = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 7, 4, 2 },
                        { 2, 2, 2, 1 } };
    // Function calling
    System.out.print(productDiagonals(arr1, 4) + "\n");
 
    int arr2[][] = { { 2, 1, 2, 1, 2 },
                        { 1, 2, 1, 2, 1 },
                        { 2, 1, 2, 1, 2 },
                        { 1, 2, 1, 2, 1 },
                        { 2, 1, 2, 1, 2 } };
    // Function calling
    System.out.print(productDiagonals(arr2, 5) + "\n");
}
}
 
// This code is contributed by PrinciRaj1992

Python3

# Python3 Program to find the Product
# of diagonal elements of a matrix
 
# Function to find the product of diagonals
def productDiagonals(arr, n):
 
    product = 1;
     
    # loop for calculating product of both
    # the principal and secondary diagonals
    for i in range(n):
 
        # For principal diagonal index of row
        # is equal to index of column
        product = product * arr[i][i];
 
        # For secondary diagonal index
        # of column is n-(index of row)-1
        product = product * arr[i][n - i - 1];
     
    # Divide the answer by middle element for
    # matrix of odd size
    if (n % 2 == 1):
        product = product // arr[n // 2][n // 2];
 
    return product;
 
# Driver code
if __name__ == '__main__':
    arr1 = [[ 1, 2, 3, 4 ],[ 5, 6, 7, 8 ],
            [ 9, 7, 4, 2 ],[ 2, 2, 2, 1 ]];
 
    # Function calling
    print(productDiagonals(arr1, 4));
 
    arr2 = [[ 2, 1, 2, 1, 2 ],[ 1, 2, 1, 2, 1 ],
            [ 2, 1, 2, 1, 2 ],[ 1, 2, 1, 2, 1 ],
            [ 2, 1, 2, 1, 2 ]];
 
    # Function calling
    print(productDiagonals(arr2, 5));
     
# This code is contributed by 29AjayKumar

C#

// C# Program to find the Product
// of diagonal elements of a matrix
using System;
 
class GFG
{
 
// Function to find the product of diagonals
static int productDiagonals(int [,]arr, int n)
{
 
    int product = 1;
     
    // loop for calculating product of both
    // the principal and secondary diagonals
    for (int i = 0; i < n; i++)
    {
 
        // For principal diagonal index of row
        // is equal to index of column
        product = product * arr[i,i];
 
        // For secondary diagonal index
        // of column is n-(index of row)-1
        product = product * arr[i,n - i - 1];
    }
 
    // Divide the answer by middle element for
    // matrix of odd size
    if (n % 2 == 1)
    {
        product = product / arr[n / 2,n / 2];
    }
 
    return product;
}
 
// Driver code
public static void Main(String[] args)
{
    int [,]arr1 = { { 1, 2, 3, 4 },
                    { 5, 6, 7, 8 },
                    { 9, 7, 4, 2 },
                    { 2, 2, 2, 1 } };
     
    // Function calling
    Console.Write(productDiagonals(arr1, 4) + "\n");
 
    int [,]arr2 = { { 2, 1, 2, 1, 2 },
                    { 1, 2, 1, 2, 1 },
                    { 2, 1, 2, 1, 2 },
                    { 1, 2, 1, 2, 1 },
                    { 2, 1, 2, 1, 2 } };
     
    // Function calling
    Console.Write(productDiagonals(arr2, 5) + "\n");
}
}
 
// This code is contributed by 29AjayKumar

Javascript

<script>
 
 
// JavaScript Program to find the Product
// of diagonal elements of a matrix
 
// Function to find the product of diagonals
function productDiagonals(arr, n)
{
 
    var product = 1;
    // loop for calculating product of both
    // the principal and secondary diagonals
    for (var i = 0; i < n; i++) {
 
        // For principal diagonal index of row
        // is equal to index of column
        product = product * arr[i][i];
 
        // For secondary diagonal index
        // of column is n-(index of row)-1
        product = product * arr[i][n - i - 1];
    }
 
    // Divide the answer by middle element for
    // matrix of odd size
    if (n % 2 == 1) {
        product =
        product / arr[parseInt(n / 2)][parseInt(n / 2)];
    }
 
    return product;
}
 
// Driver code
var arr1 = [ [ 1, 2, 3, 4 ],
                       [ 5, 6, 7, 8 ],
                       [ 9, 7, 4, 2 ],
                       [ 2, 2, 2, 1 ] ];
// Function calling
document.write( productDiagonals(arr1, 4) + "<br>");
var arr2 = [ [ 2, 1, 2, 1, 2 ],
                       [ 1, 2, 1, 2, 1 ],
                       [ 2, 1, 2, 1, 2 ],
                       [ 1, 2, 1, 2, 1 ],
                       [ 2, 1, 2, 1, 2 ] ];
// Function calling
document.write( productDiagonals(arr2, 5));
 
</script>
Producción: 

9408
512

 

Complejidad de tiempo: O(N)
 

Publicación traducida automáticamente

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