Given a matrix of N rows and M columns, given two points on the matrix; the task is to count the number of cells that are equidistant from given two points. Any traversal either in the horizontal direction or vertical direction or both ways is considered valid but the diagonal path is not valid.
Examples:
Input: 5 5
2 4
5 3
Output: 5
Explanation:
Out of all cells, these are the points (3, 1);(3, 2);(3, 3);(4, 4);(4, 5)
which satisfy given condition.Input: 4 3
2 3
4 1
Output: 4
Explanation:
Out of all cells, these are the points (1, 1);(2, 1);(3, 2);(4, 3)
which satisfy given condition.
Approach:
 Every cell of the matrix is traversed.
 Let ‘A’ be the distance between current cell and first point and similarly ‘B’ be the distance between current cell and second point.
 Distance between two points is calculated using Manhattan distance. If A & B are equal, the count is incremented.
Below is the implementation of the above approach:
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std;
int numberOfPoints( int N, int M, int x1,
int y1, int x2, int y2)
{ // Initializing count
int count = 0;
// Traversing through rows.
for ( int i = 1; i <= N; i++) {
// Traversing through columns.
for ( int j = 1; j <= M; j++) {
// By using Manhattan Distance, the distance between
// the current point to given two points is calculated.
// If distances are equal
// the count is incremented by 1.
if ( abs (i  x1) + abs (j  y1)
== abs (i  x2) + abs (j  y2))
count++;
}
}
return count;
} // Driver Code int main()
{ int n = 5;
int m = 5;
int x1 = 2;
int y1 = 4;
int x2 = 5;
int y2 = 3;
cout << numberOfPoints(n, m, x1, y1, x2, y2);
} 
//Java implementation of the above approach import java.util.*;
import java.lang.Math;
public class GFG {
public static int numberOfPoints( int N, int M, int x1,
int y1, int x2, int y2)
{
int count = 0 , i, j;
// Traversing through rows.
for (i = 1 ; i <= N; i++) {
// Traversing through columns.
for (j = 1 ; j <= M; j++) {
// By using Manhattan Distance, distance between
// current point to given two points is calculated
// If distances are equal
// the count is incremented by 1.
if (Math.abs(i  x1) + Math.abs(j  y1)
== Math.abs(i  x2) + Math.abs(j  y2))
count += 1 ;
}
}
return count;
}
// Driver Code
public static void main(String[] args)
{
int n = 5 ;
int m = 5 ;
int x1 = 2 ;
int y1 = 4 ;
int x2 = 5 ;
int y2 = 3 ;
System.out.println(numberOfPoints(n, m, x1, y1, x2, y2));
}
} 
# Python implementation of the above approach def numberPoints(N, M, x1, y1, x2, y2):
# Initializing count = 0
count = 0
# Traversing through rows.
for i in range ( 1 , N + 1 ):
# Traversing through columns.
for j in range ( 1 , M + 1 ):
# By using Manhattan Distance,
# distance between current point to
# given two points is calculated
# If distances are equal the
# count is incremented by 1.
if ( abs (i  x1) + abs (j  y1)) = = ( abs (i  x2) + abs (j  y2)):
count + = 1
return count
# Driver Code N = 5
M = 5
x1 = 2
y1 = 4
x2 = 5
y2 = 3
print (numberPoints(N, M, x1, y1, x2, y2))

// C# implementation of the above approach using System;
class GFG
{ static int numberOfPoints( int N, int M, int x1,
int y1, int x2, int y2)
{
int count = 0, i, j;
// Traversing through rows.
for (i = 1; i <= N; i++)
{
// Traversing through columns.
for (j = 1; j <= M; j++)
{
// By using Manhattan Distance, distance between
// current point to given two points is calculated
// If distances are equal
// the count is incremented by 1.
if (Math.Abs(i  x1) + Math.Abs(j  y1)
== Math.Abs(i  x2) + Math.Abs(j  y2))
count += 1;
}
}
return count;
}
// Driver Code
public static void Main()
{
int n = 5;
int m = 5;
int x1 = 2;
int y1 = 4;
int x2 = 5;
int y2 = 3;
Console.WriteLine(numberOfPoints(n, m, x1, y1, x2, y2));
}
} // This code is contributed by AnkitRai01 
5
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a studentfriendly price and become industry ready.
Recommended Posts:
 Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
 Total number of triplets (A, B, C) in which the points B and C are Equidistant to A
 Count of cells in a matrix whose adjacent cells's sum is prime Number
 Minimum Numbers of cells that are connected with the smallest path between 3 given cells
 Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix
 Number of Integral Points between Two Points
 Prime points (Points that split a number into two primes)
 Maximize XOR subsequence possible by equidistant elements from both ends
 Number of cells in a matrix that satisfy the given condition
 Shortest distance between two cells in a matrix or grid
 Find whether there is path between two cells in matrix
 Minimum Distance from a given Cell to all other Cells of a Matrix
 Check if a given string can be formed using characters of adjacent cells of a Matrix
 Count of ways to select K consecutive empty cells from a given Matrix
 Find cells in Matrix that are not visited by Robot for given movements
 Maximize path sum from topleft cell to all other cells of a given Matrix
 Number of cells in the right and left diagonals passing through (x, y) in a matrix
 Find Number of Even cells in a Zero Matrix after Q queries
 Total number of cells covered in a matrix after D days
 Min number of moves to traverse entire Matrix through connected cells with equal values
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AnkitRai01