Given two points (x1, y1) and (x2, y2) in 2-D coordinate system. The task is to count all the paths whose distance is equal to the Manhattan distance between both the given points.
Examples:
Input: x1 = 2, y1 = 3, x2 = 4, y2 = 5
Output: 6Input: x1 = 2, y1 = 6, x2 = 4, y2 = 9
Output: 10
Approach: The Manhattan distance between the points (x1, y1) and (x2, y2) will be abs(x1 – x2) + abs(y1 – y2)
Let abs(x1 – x2) = m and abs(y1 – y2) = n
Every path with distance equal to the Manhattan distance will always have m + n edges, m horizontal and n vertical edges. Hence this is a basic case of Combinatorics which is based upon group formation. The idea behind this is the number of ways in which (m + n) different things can be divided into two groups, one containing m items and the other containing n items which is given by m + nCn i.e. (m + n)! / m! * n!.

Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define ll long long int // Function to return the value of nCk ll binomialCoeff(int n, int k) { ll res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / // [k * (k-1) *---* 1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } // Function to return the number of paths ll countPaths(int x1, int y1, int x2, int y2) { // Difference between the 'x' // coordinates of the given points int m = abs(x1 - x2); // Difference between the 'y' // coordinates of the given points int n = abs(y1 - y2); return (binomialCoeff(m + n, n)); } // Driver code int main() { int x1 = 2, y1 = 3, x2 = 4, y2 = 5; cout << countPaths(x1, y1, x2, y2); return 0; } |
Java
// Java implementation of the approach class GfG { //static long ll long long int // Function to return the value of nCk static long binomialCoeff(int n, int k) { long res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / // [k * (k-1) *---* 1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } // Function to return the number of paths static long countPaths(int x1, int y1, int x2, int y2) { // Difference between the 'x' // coordinates of the given points int m = Math.abs(x1 - x2); // Difference between the 'y' // coordinates of the given points int n = Math.abs(y1 - y2); return (binomialCoeff(m + n, n)); } // Driver code public static void main(String[] args) { int x1 = 2, y1 = 3, x2 = 4, y2 = 5; System.out.println(countPaths(x1, y1, x2, y2)); } } // This code is contributed by // Prerna Saini. |
Python3
# Python3 implementation of the approach # Function to return the value of nCk def binomialCoeff(n, k): res = 1 # Since C(n, k) = C(n, n-k) if (k > n - k): k = n - k # Calculate value of # [n * (n-1) *---* (n-k+1)] / # [k * (k-1) *---* 1] for i in range(k): res *= (n - i) res //= (i + 1) return res # Function to return the number # of paths def countPaths(x1, y1, x2, y2): # Difference between the 'x' # coordinates of the given points m = abs(x1 - x2) # Difference between the 'y' # coordinates of the given points n = abs(y1 - y2) return (binomialCoeff(m + n, n)) # Driver code x1, y1, x2, y2 = 2, 3, 4, 5print(countPaths(x1, y1, x2, y2)) # This code is contributed # by Mohit Kumar |
C#
// C# implementation of the approach using System; class GFG { // Function to return the value of nCk static long binomialCoeff(int n, int k) { long res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / // [k * (k-1) *---* 1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } // Function to return the number of paths static long countPaths(int x1, int y1, int x2, int y2) { // Difference between the 'x' // coordinates of the given points int m = Math.Abs(x1 - x2); // Difference between the 'y' // coordinates of the given points int n = Math.Abs(y1 - y2); return (binomialCoeff(m + n, n)); } // Driver code public static void Main() { int x1 = 2, y1 = 3, x2 = 4, y2 = 5; Console.Write(countPaths(x1, y1, x2, y2)); } } // This code is contributed by // Akanksha Rai |
PHP
<?php // PHP implementation of the approach //static long ll long long int // Function to return the value of nCk function binomialCoeff($n, $k) { $res = 1; // Since C(n, k) = C(n, n-k) if ($k > $n - $k) $k = $n - $k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / // [k * (k-1) *---* 1] for ($i = 0; $i < $k; ++$i) { $res *= ($n - $i); $res /= ($i + 1); } return $res; } // Function to return the number of paths function countPaths($x1, $y1, $x2, $y2) { // Difference between the 'x' // coordinates of the given points $m =abs($x1 - $x2); // Difference between the 'y' // coordinates of the given points $n = abs($y1 - $y2); return (binomialCoeff($m + $n, $n)); } // Driver code { $x1 = 2; $y1 = 3; $x2 = 4; $y2 = 5; echo(countPaths($x1, $y1, $x2, $y2)); } // This code is contributed by // Code_Mech. |
6
Recommended Posts:
- Pairs with same Manhattan and Euclidean distance
- Find the integer points (x, y) with Manhattan distance atleast N
- Maximum Manhattan distance between a distinct pair from N coordinates
- Sum of Manhattan distances between all pairs of points
- Find a point such that sum of the Manhattan distances is minimized
- Find the original coordinates whose Manhattan distances are given
- Distance of chord from center when distance between center and another equal length chord is given
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count of palindromic plus paths in a given Matrix
- Count of paths in given Binary Tree with odd bitwise AND for Q queries
- Minimize count of unique paths from top left to bottom right of a Matrix by placing K 1s
- Count all possible paths from top left to bottom right of a Matrix without crossing the diagonal
- Count unique paths with given sum in an N-ary Tree
- Count of root to leaf paths whose permutation is palindrome in a Binary Tree
- Number of Paths of Weight W in a K-ary tree
- Print all shortest paths between given source and destination in an undirected graph
- Count of binary strings of length N having equal count of 0's and 1's
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Split an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
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.


