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

• Last Updated : 22 Apr, 2021

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#includeusing 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 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}



Javascript


Output:
3 3

My Personal Notes arrow_drop_up