Número de celdas en las diagonales derecha e izquierda que pasan por (x, y) en una array

Dados cuatro enteros fila, columna, x e y donde fila y columna son el número de filas y columnas de una array 2-D y x e y son las coordenadas de una celda en la misma array, la tarea es encontrar el número de celdas en la diagonal izquierda y derecha a la que está asociada la celda (x, y) de la array.
Ejemplos: 
 

Entrada: fila = 4, columna = 3, x = 2, y = 2 
Salida: 3 3 
 

El número de celdas en las diagonales izquierda y derecha de (2, 2) son 3 y 3 respectivamente.
Entrada: fila = 4, columna = 5, x = 2, y = 2 
Salida: 4 3 
 

Acercarse: 
 

  • Calcule el número de celdas en la parte superior izquierda y la parte inferior derecha de la diagonal izquierda de la celda (x, y) por separado. Luego súmalos para obtener el número de celdas en la diagonal izquierda.
  • Del mismo modo, calcule el número de celdas en la parte superior derecha y la parte inferior izquierda de la diagonal derecha de la celda (x, y) por separado.

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

C++

// C++ implementation of the approach
#include<bits/stdc++.h>
using namespace std;
 
    // Function to return the number of cells
    // in the left and the right diagonal of
    // the matrix for a cell (x, y)
    void count_left_right(int n, int m, int x, int y)
    {
        int left = 0, right = 0;
 
        // number of cells in the left diagonal
        int left_upper_part = min(x-1, y-1);
        int left_lower_part = min(n-x, m-y);
        left = left_upper_part + left_lower_part + 1;
 
        // number of cells in the right diagonal
        int right_upper_part = min(m-y, x-1);
        int right_lower_part = min(y-1, n-x);
        right = right_upper_part + right_lower_part + 1;
 
        cout<<(left)<<" "<<(right);
    }
 
    // Driver code
    int main()
    {
        int row = 4;
        int col = 3;
        int x = 2;
        int y = 2;
 
        count_left_right(row, col, x, y);
    }
// This code is contributed by
// Sanjit_Prasad

Java

// Java implementation of the approach
 
class GFG {
 
    // Function to return the number of cells
    // in the left and the right diagonal of
    // the matrix for a cell (x, y)
    static void count_left_right(int n, int m
                                    , int x, int y)
    {
        int left = 0, right = 0;
 
        // number of cells in the left diagonal
        int left_upper_part = Math.min(x - 1, y - 1);
        int left_lower_part = Math.min(n - x, m - y);
        left = left_upper_part + left_lower_part + 1;
 
        // number of cells in the right diagonal
        int right_upper_part = Math.min(m - y, x - 1);
        int right_lower_part = Math.min(y - 1, n - x);
        right = right_upper_part + right_lower_part + 1;
 
        System.out.println(left + " " + right);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int row = 4;
        int col = 3;
        int x = 2;
        int y = 2;
 
        count_left_right(row, col, x, y);
    }
}

Python 3

# Python 3 implementation of the approach
 
# Function to return the number of cells
# in the left and the right diagonal of
# the matrix for a cell (x, y)
def count_left_right(n, m, x, y):
     
    left = 0
    right = 0
 
    # number of cells in the left diagonal
    left_upper_part = min(x - 1, y - 1)
    left_lower_part = min(n - x, m - y)
    left = left_upper_part + left_lower_part + 1
 
    # number of cells in the right diagonal
    right_upper_part = min(m - y, x - 1)
    right_lower_part = min(y - 1, n - x)
    right = right_upper_part + right_lower_part + 1
 
    print(left, right)
 
# Driver code
if __name__ == "__main__":
     
    row = 4
    col = 3
    x = 2
    y = 2
 
    count_left_right(row, col, x, y)
 
# This code is contributed by ChitraNayal

C#

// C# implementation of the above approach
 
using System;
 
class Program
{
    // Function to return the number of cells
    // in the left and the right diagonal of
    // the matrix for a cell (x, y)
    static void count_left_right(int n, int m
                            , int x, int y)
    {
        int left = 0, right = 0;
         
        // number of cells in the left diagonal
        int left_upper_part = Math.Min(x - 1, y - 1);
        int left_lower_part = Math.Min(n - x, m - y);
        left = left_upper_part + left_lower_part + 1;
         
        // number of cells in the right diagonal
        int right_upper_part = Math.Min(m - y, x - 1);
        int right_lower_part = Math.Min(y - 1, n - x);
        right = right_upper_part + right_lower_part + 1;
        Console.WriteLine(left + " " + right);
    }
     
    //Driver code
    static void Main()
    {
        int row = 4;
        int col = 3;
        int x = 2;
        int y = 2;
        count_left_right(row, col, x, y);
         
    }
// This code is contributed by ANKITRAI1
}

PHP

<?php
// PHP implementation of the approach
 
// Function to return the number of cells
// in the left and the right diagonal of
// the matrix for a cell (x, y)
function count_left_right($n, $m, $x, $y)
{
    $left = 0;
    $right = 0;
 
    // number of cells in the left diagonal
    $left_upper_part = min($x - 1, $y - 1);
    $left_lower_part = min($n - $x, $m - $y);
    $left = $left_upper_part +
            $left_lower_part + 1;
 
    // number of cells in the right diagonal
    $right_upper_part = min($m - $y, $x - 1);
    $right_lower_part = min($y - 1, $n - $x);
    $right = $right_upper_part +
             $right_lower_part + 1;
 
    echo $left, " " , $right;
}
 
// Driver code
$row = 4;
$col = 3;
$x = 2;
$y = 2;
 
count_left_right($row, $col, $x, $y);
 
// This code is contributed by jit_t
?>

Javascript

<script>
 
// Javascript implementation of the approach
 
    // Function to return the number of cells
    // in the left and the right diagonal of
    // the matrix for a cell (x, y)
    function count_left_right(n, m, x, y)
    {
        let left = 0, right = 0;
 
        // number of cells in the left diagonal
        let left_upper_part = Math.min(x-1, y-1);
        let left_lower_part = Math.min(n-x, m-y);
        left = left_upper_part + left_lower_part + 1;
 
        // number of cells in the right diagonal
        let right_upper_part = Math.min(m-y, x-1);
        let right_lower_part = Math.min(y-1, n-x);
        right = right_upper_part + right_lower_part + 1;
 
        document.write((left) + " " + (right));
    }
 
    // Driver code
        let row = 4;
        let col = 3;
        let x = 2;
        let y = 2;
 
        count_left_right(row, col, x, y);
 
// This code is contributed by souravmahato348.
</script>
Producción: 

3 3

 

Publicación traducida automáticamente

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