Non-crossing lines to connect points in a circle

Last Updated : 20 May, 2026

Given n persons sitting around a round table, each person can shake hands with exactly one other person. Find the number of ways such that no two handshakes cross each other.

  • n is always even.
  • Each person participates in exactly one handshake.

Examples:

Input: n = 2
Output: 1
Explanation: For 2 people, the possible non-crossing handshakes are: [1-2]

Input: n = 4
Output: 2
Explanation: For 4 people, the possible non-crossing handshakes are:
[1–2], [3–4]
[1–4], [2–3]

2056958081
Try It Yourself
redirect icon

[Naive Approach] Using Recursion - O(2^n) Time O(n) Space

The Idea is to try all possible ways to form handshakes by fixing one person and pairing it with every possible valid person. Recursively solve for the remaining people.

C++
#include <iostream>
#include <vector>
using namespace std;

// function to find handshakes
int count(int n)
{
    // if n is odd, no valid handshake
    if (n % 2 == 1)
        return 0;

    // base case
    if (n == 0)
        return 1;

    int res = 0;

    // try all possible pairings
    for (int i = 0; i < n; i += 2)
    {
        res += count(i) * count(n - 2 - i);
    }

    return res;
}


// Driver Code
int main()
{

    int n = 4;
    cout << count(n);

    return 0;
}
C
#include <stdio.h>

// function to find handshakes
int count(int n)
{
    // if n is odd, no valid handshake
    if (n % 2 == 1)
        return 0;

    // base case
    if (n == 0)
        return 1;

    int res = 0;

    // try all possible pairings
    for (int i = 0; i < n; i += 2)
    {
        res += count(i) * count(n - 2 - i);
    }

    return res;
}

// Driver Code
int main()
{
    int n = 4;
    printf("%d", count(n));
    return 0;
}
Java
public class GfG {
    
    // function to find handshakes
    static int count(int n) {
        
        // if n is odd, no valid handshake
        if (n % 2 == 1)
            return 0;

        // base case
        if (n == 0)
            return 1;

        int res = 0;

        // try all possible pairings
        for (int i = 0; i < n; i += 2) {
            res += count(i) * count(n - 2 - i);
        }

        return res;
    }

    // Driver Code
    public static void main(String[] args) {
        int n = 4;
        System.out.println(count(n));
    }
}
Python
# function to find handshakes
def count(n):
    
    # if n is odd, no valid handshake
    if n % 2 == 1:
        return 0

    # base case
    if n == 0:
        return 1

    res = 0

    # try all possible pairings
    for i in range(0, n, 2):
        res += count(i) * count(n - 2 - i)

    return res

# Driver Code
if __name__ == "__main__":
    n = 4
    print(count(n))
C#
using System;

public class GfG
{
    // function to find handshakes
    public int count(int n)
    {
        if (n % 2 == 1)
            return 0;

        if (n == 0)
            return 1;

        int res = 0;

        for (int i = 0; i < n; i += 2)
        {
            res += count(i) * count(n - 2 - i);
        }

        return res;
    }

    // Driver Code
    public static void Main()
    {
        int n = 4;
        
        GfG obj = new GfG(); 
        Console.WriteLine(obj.count(n)); 
    }
}
JavaScript
// function to find handshakes
function count(n) {
    
    // if n is odd, no valid handshake
    if (n % 2 === 1)
        return 0;

    // base case
    if (n === 0)
        return 1;

    let res = 0;

    // try all possible pairings
    for (let i = 0; i < n; i += 2) {
        res += count(i) * count(n - 2 - i);
    }

    return res;
}

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

Output
2

Time Complexity : O(2^n) 
Auxiliary Space : O(n)

[Better Approach] Using Bottom-Up Dynamic Programming - (n^2) Time O(n) Space

The Idea is to use a DP array to store results of subproblems and build the solution iteratively using the Catalan recurrence.

C++
#include <iostream>
#include <vector>
using namespace std;

// function to find no of handshakes
int count(int n)
{

    // if n is odd, return 0
    if (n % 2 == 1)
        return 0;

    vector<int> dp(n + 1, 0);

    // base case
    dp[0] = 1;

    // fill dp array
    for (int i = 2; i <= n; i += 2)
    {
        for (int j = 0; j < i; j += 2)
        {
            dp[i] += dp[j] * dp[i - 2 - j];
        }
    }

    return dp[n];
}

// Driver Code
int main()
{

    int n = 4;
    cout << count(n);

    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

// function to find no of handshakes
int count(int n)
{
    // if n is odd, return 0
    if (n % 2 == 1)
        return 0;

    int *dp = calloc(n + 1, sizeof(int));

    // base case
    dp[0] = 1;

    // fill dp array
    for (int i = 2; i <= n; i += 2)
    {
        for (int j = 0; j < i; j += 2)
        {
            dp[i] += dp[j] * dp[i - 2 - j];
        }
    }

    int result = dp[n];
    free(dp);
    return result;
}

// Driver Code
int main()
{
    int n = 4;
    printf("%d", count(n));
    return 0;
}
Java
import java.util.*;

public class GfG {
    // function to find no of handshakes
    public static int count(int n) {
        // if n is odd, return 0
        if (n % 2 == 1)
            return 0;

        int[] dp = new int[n + 1];

        // base case
        dp[0] = 1;

        // fill dp array
        for (int i = 2; i <= n; i += 2) {
            for (int j = 0; j < i; j += 2) {
                dp[i] += dp[j] * dp[i - 2 - j];
            }
        }

        return dp[n];
    }

    public static void main(String[] args) {
        int n = 4;
        System.out.println(count(n));
    }
}
Python
# function to find no of handshakes
def count(n):
    # if n is odd, return 0
    if n % 2 == 1:
        return 0

    dp = [0] * (n + 1)

    # base case
    dp[0] = 1

    # fill dp array
    for i in range(2, n + 1, 2):
        for j in range(0, i, 2):
            dp[i] += dp[j] * dp[i - 2 - j]

    return dp[n]

# Driver Code
if __name__ == "__main__":
    n = 4
    print(count(n))
C#
using System;

public class GfG {
    public int count(int n)
    {
        if (n % 2 == 1)
            return 0;

        int[] dp = new int[n + 1];
        dp[0] = 1;

        for (int i = 2; i <= n; i += 2) {
            for (int j = 0; j < i; j += 2) {
                dp[i] += dp[j] * dp[i - 2 - j];
            }
        }

        return dp[n];
    }

    public static void Main()
    {
        int n = 4;
        GfG obj = new GfG();          
        Console.WriteLine(obj.count(n));
    }
}
JavaScript
// function to find no of handshakes
function count(n) {
    // if n is odd, return 0
    if (n % 2 === 1)
        return 0;

    let dp = new Array(n + 1).fill(0);

    // base case
    dp[0] = 1;

    // fill dp array
    for (let i = 2; i <= n; i += 2) {
        for (let j = 0; j < i; j += 2) {
            dp[i] += dp[j] * dp[i - 2 - j];
        }
    }

    return dp[n];
}

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

Output
2

Time Complexity : O(n^2) 
Auxiliary Space : O(n)

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

The idea is to use the direct formula of Catalan Number: C_k = \frac{1}{k+1} \times \binom{2k}{k} , where k = n / 2.

C++
#include <iostream>
#include <vector>
using namespace std;

// function to find handshakes
int count(int n)
{

    // if n is odd, return 0
    if (n % 2 == 1)
        return 0;

    int k = n / 2;
    long long res = 1;

    // compute (2k C k)
    for (int i = 0; i < k; i++)
    {
        res = res * (2 * k - i);
        res = res / (i + 1);
    }

    // divide by (k + 1)
    res = res / (k + 1);

    return res;
}

// Driver Code
int main()
{

    int n = 4;
    cout << count(n);

    return 0;
}
C
#include <stdio.h>

// function to find handshakes
int count(int n)
{
    // if n is odd, return 0
    if (n % 2 == 1)
        return 0;

    int k = n / 2;
    long long res = 1;

    // compute (2k C k)
    for (int i = 0; i < k; i++)
    {
        res *= (2 * k - i);
        res /= (i + 1);
    }

    // divide by (k + 1)
    res /= (k + 1);

    return res;
}

// Driver Code
int main()
{
    int n = 4;
    printf("%d", count(n));
    return 0;
}
Java
import java.util.*;

public class GfG {
    // function to find handshakes
    public static long count(int n) {
        // if n is odd, return 0
        if (n % 2 == 1)
            return 0;

        int k = n / 2;
        long res = 1;

        // compute (2k C k)
        for (int i = 0; i < k; i++) {
            res *= (2 * k - i);
            res /= (i + 1);
        }

        // divide by (k + 1)
        res /= (k + 1);

        return res;
    }

    public static void main(String[] args) {
        int n = 4;
        System.out.println(count(n));
    }
}
Python
def count(n):
    # if n is odd, return 0
    if n % 2 == 1:
        return 0

    k = n // 2
    res = 1

    # compute (2k C k)
    for i in range(k):
        res *= (2 * k - i)
        res //= (i + 1)

    # divide by (k + 1)
    res //= (k + 1)

    return res

# Driver Code
if __name__ == "__main__":
    n = 4
    print(count(n))
C#
using System;

public class GfG {
    
    // function to find handshakes
    public int count(int n) {
        
        if (n % 2 == 1)
            return 0;

        int k = n / 2;
        long res = 1;

        for (int i = 0; i < k; i++) {
            res *= (2L * k - i);
            res /= (i + 1);
        }

        res /= (k + 1);

        return (int)res;  
    }

    public static void Main()
    {
        int n = 4;
        GfG obj = new GfG(); 
        Console.WriteLine(obj.count(n)); 
    }
}
JavaScript
function count(n) {
    // if n is odd, return 0
    if (n % 2 === 1)
        return 0;

    let k = Math.floor(n / 2);
    let res = 1;

    // compute (2k C k)
    for (let i = 0; i < k; i++) {
        res *= (2 * k - i);
        res = Math.floor(res / (i + 1));
    }

    // divide by (k + 1)
    res = Math.floor(res / (k + 1));

    return Math.floor(res);
}

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

Output
2

Time Complexity : O(n) 
Auxiliary Space : O(1)

Comment