Number of cells in the right and left diagonals passing through (x, y) in a matrix

Given four integers row, col, x and y where row and col are the number of rows and columns of a 2-D Matrix and x and y are the coordinates of a cell in the same matrix, the task is to find number of cells in the left and the right diagonal which the cell (x, y) of the matrix is associated with.
Examples:Â
Input: row = 4, col = 3, x = 2, y = 2Â
Output: 3 3Â
The number of cells in the left and the right diagonals of (2, 2) are 3 and 3 respectively.
Input: row = 4, col = 5, x = 2, y = 2Â
Output: 4 3Â
Approach:Â
- Calculate the number of cells in the upper left part and lower right part of the left diagonal of the cell (x, y) separately. Then sum them up to get the number of cells in the left diagonal.
- Similarly, calculate the number of cells in the upper right part and lower left part of the right diagonal of the cell (x, y) separately.
Below is the implementation of the above approach:
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 codeint 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 codeif __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> |
Output
3 3
Time complexity: O(1)
Auxiliary space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




