Handshakes That Don't Cross - Problem

You are given an even number of people numPeople that stand around a circle. Each person must shake hands with exactly one other person, resulting in numPeople / 2 handshakes total.

Return the number of ways these handshakes could occur such that none of the handshakes cross. Since the answer could be very large, return it modulo 10⁹ + 7.

Two handshakes cross if the line segments connecting the pairs of people intersect when drawn inside the circle.

Input & Output

Example 1 — Basic Case
$ Input: numPeople = 4
Output: 2
💡 Note: Person 0 can shake hands with person 1 (leaving 2,3 as a pair) or with person 3 (leaving 1,2 as a pair). Both arrangements have no crossing handshakes.
Example 2 — Larger Circle
$ Input: numPeople = 6
Output: 5
💡 Note: With 6 people in a circle, there are 5 different ways to arrange 3 handshakes without any crossings. This corresponds to the 3rd Catalan number.
Example 3 — Minimum Case
$ Input: numPeople = 2
Output: 1
💡 Note: Only one way exists: the two people shake hands with each other. No crossings possible.

Constraints

  • 2 ≤ numPeople ≤ 1000
  • numPeople is even

Visualization

Tap to expand
Non-Crossing Handshakes Problem: 4 People0123Input: 4 peopleArrangement 1: (0,1)(3,2)Arrangement 2: (0,3)(1,2)Output: 2 ways
Understanding the Visualization
1
Input
4 people arranged in a circle
2
Process
Find all ways to pair without crossings
3
Output
Count = 2 valid arrangements
Key Takeaway
🎯 Key Insight: This problem follows the Catalan number sequence - a fundamental pattern in combinatorics representing non-crossing structures.
Asked in
Google 15 Microsoft 12 Facebook 8
28.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen