Number of cells in the right and left diagonals passing through (x, y) in a matrix
Last Updated :
20 Oct, 2022
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++
#include <bits/stdc++.h>
using namespace std;
void count_left_right( int n, int m, int x, int y)
{
int left = 0, right = 0;
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;
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);
}
int main()
{
int row = 4;
int col = 3;
int x = 2;
int y = 2;
count_left_right(row, col, x, y);
}
|
Java
class GFG {
static void count_left_right( int n, int m, int x, int y)
{
int left = 0 , right = 0 ;
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 ;
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);
}
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
def count_left_right(n, m, x, y):
left = 0
right = 0
left_upper_part = min (x - 1 , y - 1 )
left_lower_part = min (n - x, m - y)
left = left_upper_part + left_lower_part + 1
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)
if __name__ = = "__main__" :
row = 4
col = 3
x = 2
y = 2
count_left_right(row, col, x, y)
|
C#
using System;
class Program {
static void count_left_right( int n, int m, int x, int y)
{
int left = 0, right = 0;
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;
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);
}
static void Main()
{
int row = 4;
int col = 3;
int x = 2;
int y = 2;
count_left_right(row, col, x, y);
}
}
|
PHP
<?php
function count_left_right( $n , $m , $x , $y )
{
$left = 0;
$right = 0;
$left_upper_part = min( $x - 1, $y - 1);
$left_lower_part = min( $n - $x , $m - $y );
$left = $left_upper_part +
$left_lower_part + 1;
$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 ;
}
$row = 4;
$col = 3;
$x = 2;
$y = 2;
count_left_right( $row , $col , $x , $y );
?>
|
Javascript
<script>
function count_left_right(n, m, x, y)
{
let left = 0, right = 0;
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;
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));
}
let row = 4;
let col = 3;
let x = 2;
let y = 2;
count_left_right(row, col, x, y);
</script>
|
Time complexity: O(1)
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...