Dyck Path

Last Updated : 16 Aug, 2026

Given an n × n grid with the top-left cell indexed as (0, 0), a Dyck path is a staircase path that starts at the bottom-left cell (n − 1, 0) and ends at the top-right cell (0, n − 1).

The path must always remain on or above the diagonal joining (n − 1, 0) and (0, n − 1). 

Given an integer n, find the total number of distinct Dyck paths. Some valid Dyck paths for different values of n are illustrated below.

90

Examples: 

Input: n = 4
Output: 14
Explanation: Refer to the diagram above.

Input: n = 3
Output: 5
Explanation: Refer to the diagram above.

Try It Yourself
redirect icon

[Naive Approach] Recursive Backtracking - O(2 ^ (2n)) Time and O(n) Space

The idea is to recursively try both possible moves (Up and Right) at every step. We count a path only if it never goes below the diagonal and reaches the destination.

Working of Approach:

  • Start from the bottom-left cell.
  • Recursively make an Up move or a Right move.
  • If the path goes below the diagonal, stop exploring it.
  • When n Up moves and n Right moves are completed, count it as one valid Dyck path.
C++
#include <iostream>
using namespace std;

int countPaths(int up, int right, int n)
{
    // Path goes below the diagonal
    if (right > up)
        return 0;

    // Exceeded the required number of moves
    if (up > n || right > n)
        return 0;

    // Reached the destination after n Up and n Right moves
    if (up == n && right == n)
        return 1;

    // Try both possible moves
    return countPaths(up + 1, right, n) + countPaths(up, right + 1, n);
}

int dyckPaths(int n)
{
    return countPaths(0, 0, n);
}

int main()
{
    int n = 4;

    cout << dyckPaths(n);

    return 0;
}
Java
public class GFG {
    // Function to count the number of paths
    static int countPaths(int up, int right, int n)
    {
        // Path goes below the diagonal
        if (right > up)
            return 0;

        // Exceeded the required number of moves
        if (up > n || right > n)
            return 0;

        // Reached the destination after n Up and n Right
        // moves
        if (up == n && right == n)
            return 1;

        // Try both possible moves
        return countPaths(up + 1, right, n)
            + countPaths(up, right + 1, n);
    }

    // Function to return the number of Dyck paths
    static int dyckPaths(int n)
    {
        return countPaths(0, 0, n);
    }

    public static void main(String[] args)
    {
        int n = 4;

        System.out.println(dyckPaths(n));
    }
}
Python
def countPaths(up, right, n):
    # Path goes below the diagonal
    if right > up:
        return 0

    # Exceeded the required number of moves
    if up > n or right > n:
        return 0

    # Reached the destination after n Up and n Right moves
    if up == n and right == n:
        return 1

    # Try both possible moves
    return countPaths(up + 1, right, n) + countPaths(up, right + 1, n)


def dyckPaths(n):
    return countPaths(0, 0, n)


if __name__ == '__main__':
    n = 4
    print(dyckPaths(n))
C#
using System;

public class GFG {
    // Function to count the number of paths
    public static int countPaths(int up, int right, int n)
    {
        // Path goes below the diagonal
        if (right > up)
            return 0;

        // Exceeded the required number of moves
        if (up > n || right > n)
            return 0;

        // Reached the destination after n Up and n Right
        // moves
        if (up == n && right == n)
            return 1;

        // Try both possible moves
        return countPaths(up + 1, right, n)
            + countPaths(up, right + 1, n);
    }

    // Function to return the number of Dyck paths
    public static int dyckPaths(int n)
    {
        return countPaths(0, 0, n);
    }

    public static void Main()
    {
        int n = 4;

        Console.WriteLine(dyckPaths(n));
    }
}
JavaScript
function countPaths(up, right, n)
{
    // Path goes below the diagonal
    if (right > up)
        return 0;

    // Exceeded the required number of moves
    if (up > n || right > n)
        return 0;

    // Reached the destination after n Up and n Right moves
    if (up == n && right == n)
        return 1;

    // Try both possible moves
    return countPaths(up + 1, right, n)
           + countPaths(up, right + 1, n);
}

function dyckPaths(n) { return countPaths(0, 0, n); }

// Driver Code
let n = 4;
console.log(dyckPaths(n));

Output
14

[Expected Approach] Using Catalan Number Formula - O(n) Time and O(1) Space

The idea is to observe that the number of Dyck paths in an n × n grid is equal to the nth Catalan number. Instead of generating all paths, compute the Catalan number using the binomial coefficient formula, which gives the answer in linear time.

The number of Dyck paths from (n-1, 0) to (0, n-1) can be given by the Catalan number C(n).
C_n=\frac{(2n)!}{(n+1)!n1}=\prod_{k=2}^{n}\frac{n+k}{k} \ for\ n\geq 0               

Working of Approach:

  • Compute the central binomial coefficient C(2n, n) iteratively.
  • Divide the result by n + 1.
  • The obtained value is the nth Catalan number.
  • Return this value as the total number of Dyck paths.

Let us understand with an example:
Input: n = 4

  • Compute the central binomial coefficient: C(8,4)=70.
  • Divide it by (n + 1) = 5 to obtain the Catalan number.
  • So, Catalan(4) = 70 / 5 = 14.
  • Therefore, there are 14 valid Dyck paths (balanced up-right paths) for n = 4.
C++
#include <iostream>
using namespace std;

int dyckPaths(int n)
{

    long long res = 1;

    // Compute the central binomial coefficient C(2n, n)
    // using the multiplicative formula to avoid factorial computation
    for (int i = 0; i < n; i++)
    {
        res *= (2 * n - i);
        res /= (i + 1);
    }

    // Number of Dyck paths is the nth Catalan number
    // Catalan(n) = C(2n, n) / (n + 1)
    return (int)(res / (n + 1));
}

int main()
{
    int n = 4;

    cout << dyckPaths(n);

    return 0;
}
Java
import java.util.*;

public class GFG {
    public static int dyckPaths(int n)
    {
        long res = 1;

        // Compute the central binomial coefficient C(2n, n)
        // using the multiplicative formula to avoid
        // factorial computation
        for (int i = 0; i < n; i++) {
            res *= (2 * n - i);
            res /= (i + 1);
        }

        // Number of Dyck paths is the nth Catalan number
        // Catalan(n) = C(2n, n) / (n + 1)
        return (int)(res / (n + 1));
    }

    public static void main(String[] args)
    {
        int n = 4;
        System.out.println(dyckPaths(n));
    }
}
Python
def dyckPaths(n):
    res = 1

    # Compute the central binomial coefficient C(2n, n)
    # using the multiplicative formula to avoid factorial computation
    for i in range(n):
        res *= (2 * n - i)
        res //= (i + 1)

    # Number of Dyck paths is the nth Catalan number
    # Catalan(n) = C(2n, n) / (n + 1)
    return int(res / (n + 1))


if __name__ == '__main__':
    n = 4
    print(dyckPaths(n))
C#
using System;

public class GFG {
    public static int dyckPaths(int n)
    {
        long res = 1;

        // Compute the central binomial coefficient C(2n, n)
        // using the multiplicative formula to avoid
        // factorial computation
        for (int i = 0; i < n; i++) {
            res *= (2 * n - i);
            res /= (i + 1);
        }

        // Number of Dyck paths is the nth Catalan number
        // Catalan(n) = C(2n, n) / (n + 1)
        return (int)(res / (n + 1));
    }

    public static void Main()
    {
        int n = 4;
        Console.WriteLine(dyckPaths(n));
    }
}
JavaScript
function dyckPaths(n)
{
    let res = 1;

    // Compute the central binomial coefficient C(2n, n)
    // using the multiplicative formula to avoid factorial
    // computation
    for (let i = 0; i < n; i++) {
        res *= (2 * n - i);
        res = Math.floor(res / (i + 1));
    }

    // Number of Dyck paths is the nth Catalan number
    // Catalan(n) = C(2n, n) / (n + 1)
    return Math.floor(res / (n + 1));
}

// Driver Code
let n = 4;
console.log(dyckPaths(n));

Output
14
Comment