Count paths with distance equal to Manhattan distance
Last Updated :
13 Sep, 2022
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: 6
Input: 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++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll binomialCoeff(int n, int k)
{
ll res = 1;
if (k > n - k)
k = n - k;
for (int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
ll countPaths(int x1, int y1, int x2, int y2)
{
int m = abs(x1 - x2);
int n = abs(y1 - y2);
return (binomialCoeff(m + n, n));
}
int main()
{
int x1 = 2, y1 = 3, x2 = 4, y2 = 5;
cout << countPaths(x1, y1, x2, y2);
return 0;
}
|
Java
class GfG
{
static long binomialCoeff(int n, int k)
{
long res = 1;
if (k > n - k)
k = n - k;
for (int i = 0; i < k; ++i)
{
res *= (n - i);
res /= (i + 1);
}
return res;
}
static long countPaths(int x1, int y1, int x2, int y2)
{
int m = Math.abs(x1 - x2);
int n = Math.abs(y1 - y2);
return (binomialCoeff(m + n, n));
}
public static void main(String[] args)
{
int x1 = 2, y1 = 3, x2 = 4, y2 = 5;
System.out.println(countPaths(x1, y1, x2, y2));
}
}
|
Python3
def binomialCoeff(n, k):
res = 1
if (k > n - k):
k = n - k
for i in range(k):
res *= (n - i)
res //= (i + 1)
return res
def countPaths(x1, y1, x2, y2):
m = abs(x1 - x2)
n = abs(y1 - y2)
return (binomialCoeff(m + n, n))
x1, y1, x2, y2 = 2, 3, 4, 5
print(countPaths(x1, y1, x2, y2))
|
C#
using System;
class GFG
{
static long binomialCoeff(int n, int k)
{
long res = 1;
if (k > n - k)
k = n - k;
for (int i = 0; i < k; ++i)
{
res *= (n - i);
res /= (i + 1);
}
return res;
}
static long countPaths(int x1, int y1,
int x2, int y2)
{
int m = Math.Abs(x1 - x2);
int n = Math.Abs(y1 - y2);
return (binomialCoeff(m + n, n));
}
public static void Main()
{
int x1 = 2, y1 = 3, x2 = 4, y2 = 5;
Console.Write(countPaths(x1, y1, x2, y2));
}
}
|
PHP
<?php
function binomialCoeff($n, $k)
{
$res = 1;
if ($k > $n - $k)
$k = $n - $k;
for ($i = 0; $i < $k; ++$i)
{
$res *= ($n - $i);
$res /= ($i + 1);
}
return $res;
}
function countPaths($x1, $y1, $x2, $y2)
{
$m =abs($x1 - $x2);
$n = abs($y1 - $y2);
return (binomialCoeff($m + $n, $n));
}
{
$x1 = 2; $y1 = 3; $x2 = 4; $y2 = 5;
echo(countPaths($x1, $y1, $x2, $y2));
}
|
Javascript
<script>
function binomialCoeff(n, k)
{
var res = 1;
var i;
if (k > n - k)
k = n - k;
for (i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
function countPaths(x1, y1, x2, y2)
{
var m = Math.abs(x1 - x2);
var n = Math.abs(y1 - y2);
return (binomialCoeff(m + n, n));
}
var x1 = 2, y1 = 3, x2 = 4, y2 = 5;
document.write(countPaths(x1, y1, x2, y2));
</script>
|
Time complexity: O(k) where k=abs(y1 – y2)
Auxiliary space: O(1)
Please Login to comment...