The Wayback Machine - https://web.archive.org/web/20240704005856/https://www.geeksforgeeks.org/count-paths-with-distance-equal-to-manhattan-distance/
Open In App

Count paths with distance equal to Manhattan distance

Last Updated : 13 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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, 5
print(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.


Javascript




<script>
 
// javascript implementation of the approach
 
// Function to return the value of nCk
function binomialCoeff(n, k)
{
    var res = 1;
    var i;
    // 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
    var m = Math.abs(x1 - x2);
 
    // Difference between the 'y'
    // coordinates of the given points
    var n = Math.abs(y1 - y2);
 
    return (binomialCoeff(m + n, n));
}
 
// Driver code
    var x1 = 2, y1 = 3, x2 = 4, y2 = 5;
    document.write(countPaths(x1, y1, x2, y2));
 
</script>


Output: 

6

 

Time complexity: O(k) where k=abs(y1 – y2)

Auxiliary space: O(1)



Similar Reads

Find 4 points with equal Manhattan distance between any pair
Given an integer N, Find 4 points in a 2D plane having integral coordinates, such that the Manhattan distance between any pair of points is equal to N. Examples: Input: N = 6Output: { {0, -3}, {3, 0}, {-3, 0}, {0, 3} }Explanation: It can be easily calculated that Manhattan distance between all possible pairs is 6. Input: N = 11Output: -1Explanation
7 min read
Find the integer points (x, y) with Manhattan distance atleast N
Given a number N, the task is to find the integer points (x, y) such that 0 &lt;= x, y &lt;= N and Manhattan distance between any two points will be atleast N.Examples: Input: N = 3 Output: (0, 0) (0, 3) (3, 0) (3, 3) Input: N = 4 Output: (0, 0) (0, 4) (4, 0) (4, 4) (2, 2) Approach: Manhattan Distance between two points (x1, y1) and (x2, y2) is: |x
6 min read
Minimum Manhattan distance covered by visiting every coordinates from a source to a final vertex
Given an array arr[] of co-ordinate points and a source and final co-ordinate point, the task is to find the minimum manhattan distance covered from the source to the final vertex such that every point of the array is visited exactly once. Manhattan Distance = [Tex]\left | x2 - x1 \right | + \left | y2 - y1 \right | [/Tex] Examples: Input: source =
11 min read
Maximum Manhattan distance between a distinct pair from N coordinates
Given an array arr[] consisting of N integer coordinates, the task is to find the maximum Manhattan Distance between any two distinct pairs of coordinates. The Manhattan Distance between two points (X1, Y1) and (X2, Y2) is given by |X1 – X2| + |Y1 – Y2|. Examples: Input: arr[] = {(1, 2), (2, 3), (3, 4)}Output: 4Explanation:The maximum Manhattan dis
14 min read
Calculate the Manhattan Distance between two cells of given 2D array
Given a 2D array of size M * N and two points in the form (X1, Y1) and (X2 , Y2) where X1 and X2 represents the rows and Y1 and Y2 represents the column. The task is to calculate the Manhattan distance between the given points. Examples: Input: M = 5, N = 5, X1 = 1, Y1 = 2, X2 = 3, Y2 = 3Output: 3Explanation: As per the definition, the Manhattan th
4 min read
Check if any point exists in a plane whose Manhattan distance is at most K from N given points
Given two arrays A[] and B[] consisting of X and Y coordinates of N distinct points in a plane, and a positive integer K, the task is to check if there exists any point P in the plane such that the Manhattan distance between the point and all the given points is at most K. If there exists any such point P, then print "Yes". Otherwise, print "No". E
7 min read
Pairs with same Manhattan and Euclidean distance
In a given Cartesian plane, there are N points. The task is to find the Number of Pairs of points(A, B) such that Point A and Point B do not coincide.Manhattan Distance and the Euclidean Distance between the points should be equal. Note: Pair of 2 points(A, B) is considered same as Pair of 2 points(B, A). Manhattan Distance = |x2-x1|+|y2-y1|Euclide
11 min read
Count of points such that sum of Manhattan distances is minimized
Given N points in K dimensional space in a 2D array Points[][], where 1≤ N ≤ 105 and 1 ≤ K ≤ 5. The task is to determine the number of points (with integer coordinates) such that the sum of Manhattan distances from these points to the N points is minimized. Manhattan distance is the sum of distances between two points measured along axes at right a
7 min read
Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths
Given a matrix mat[][] with N rows and M columns. The task is to find the minimum number of changes required in the matrix such that every path from top left to bottom right is a palindromic path. In a path only right and bottom movements are allowed from one cell to another cell.Examples: Input: mat[][] = {{1, 2}, {3, 1}} Output: 0 Explanation: Ev
15+ min read
Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Set 2
Given a matrix mat[][] with N rows and M columns. The task is to find the minimum number of changes required in the matrix such that every path from top left to bottom right is a palindromic path. In a path only right and bottom movements are allowed from one cell to another cell. Examples: Input: M = 2, N = 2, mat[M][N] = {{0, 0}, {0, 1}} Output:
12 min read
three90RightbarBannerImg