Given that there are p people in a party. Each person can either join dance as a single individual or as a pair with any other. The task is to find the number of different ways in which p people can join the dance.

**Examples:**

Input :p = 3Output :4 Let the three people be P1, P2 and P3 Different ways are: {P1, P2, P3}, {{P1, P2}, P3}, {{P1, P3}, P2} and {{P2, P3}, P1}.Input :p = 2Output :2 The groups are: {P1, P2} and {{P1, P2}}.

**Approach:** The idea is to use dynamic programming to solve this problem. There are two situations: Either the person join dance as single individual or as a pair. For the first case the problem reduces to finding the solution for p-1 people. For the second case, there are p-1 choices to select an individual for pairing and after selecting an individual for pairing the problem reduces to finding solution for p-2 people as two people among p are already paired.

So the formula for dp is:

dp[p] = dp[p-1] + (p-1) * dp[p-2].

Below is the implementation of the above approach:

## C++

`// CPP program to find number of ways to ` `// pair people in party ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find number of ways to ` `// pair people in party ` `int` `findWaysToPair(` `int` `p) ` `{ ` ` ` `// To store count of number of ways. ` ` ` `int` `dp[p + 1]; ` ` ` ` ` `dp[1] = 1; ` ` ` `dp[2] = 2; ` ` ` ` ` `// Using the recurrence defined find ` ` ` `// count for different values of p. ` ` ` `for` `(` `int` `i = 3; i <= p; i++) { ` ` ` `dp[i] = dp[i - 1] + (i - 1) * dp[i - 2]; ` ` ` `} ` ` ` ` ` `return` `dp[p]; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `p = 3; ` ` ` `cout << findWaysToPair(p); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find number of ways to ` `// pair people in party ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find number of ways to ` `// pair people in party ` `static` `int` `findWaysToPair(` `int` `p) ` `{ ` ` ` `// To store count of number of ways. ` ` ` `int` `dp[] = ` `new` `int` `[p + ` `1` `]; ` ` ` ` ` `dp[` `1` `] = ` `1` `; ` ` ` `dp[` `2` `] = ` `2` `; ` ` ` ` ` `// Using the recurrence defined find ` ` ` `// count for different values of p. ` ` ` `for` `(` `int` `i = ` `3` `; i <= p; i++) ` ` ` `{ ` ` ` `dp[i] = dp[i - ` `1` `] + (i - ` `1` `) * dp[i - ` `2` `]; ` ` ` `} ` ` ` ` ` `return` `dp[p]; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `p = ` `3` `; ` ` ` `System.out.println(findWaysToPair(p)); ` `} ` `} ` ` ` `// This code is contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find number of ` `# ways to pair people in party ` ` ` `# Function to find number of ways ` `# to pair people in party ` `def` `findWays(p): ` ` ` ` ` `# To store count of number of ways. ` ` ` `dp ` `=` `[` `0` `] ` `*` `(p ` `+` `1` `) ` ` ` `dp[` `1` `] ` `=` `1` ` ` `dp[` `2` `] ` `=` `2` ` ` ` ` `# Using the recurrence defined find ` ` ` `# count for different values of p. ` ` ` `for` `i ` `in` `range` `(` `3` `, p ` `+` `1` `): ` ` ` `dp[i] ` `=` `(dp[i ` `-` `1` `] ` `+` ` ` `(i ` `-` `1` `) ` `*` `dp[i ` `-` `2` `]) ` ` ` `return` `dp[p] ` ` ` `# Driver code ` `p ` `=` `3` `print` `(findWays(p)) ` ` ` `# This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find number of ways to ` `// pair people in party ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find number of ways to ` `// pair people in party ` `public` `static` `int` `findWaysToPair(` `int` `p) ` `{ ` ` ` `// To store count of number of ways. ` ` ` `int` `[] dp = ` `new` `int` `[p + 1]; ` ` ` ` ` `dp[1] = 1; ` ` ` `dp[2] = 2; ` ` ` ` ` `// Using the recurrence defined find ` ` ` `// count for different values of p. ` ` ` `for` `(` `int` `i = 3; i <= p; i++) ` ` ` `{ ` ` ` `dp[i] = dp[i - 1] + (i - 1) * dp[i - 2]; ` ` ` `} ` ` ` `return` `dp[p]; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(` `string` `[] args) ` `{ ` ` ` `int` `p = 3; ` ` ` `Console.WriteLine(findWaysToPair(p)); ` `} ` `} ` ` ` `// This code is contributed by shrikanth13 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find number of ways ` `// to pair people in party ` ` ` `// Function to find number of ways to ` `// pair people in party ` `function` `findWaysToPair(` `$p` `) ` `{ ` ` ` `// To store count of number of ways. ` ` ` `$dp` `= ` `array` `(); ` ` ` ` ` `$dp` `[1] = 1; ` ` ` `$dp` `[2] = 2; ` ` ` ` ` `// Using the recurrence defined find ` ` ` `// count for different values of p. ` ` ` `for` `(` `$i` `= 3; ` `$i` `<= ` `$p` `; ` `$i` `++) ` ` ` `{ ` ` ` `$dp` `[` `$i` `] = ` `$dp` `[` `$i` `- 1] + ` ` ` `(` `$i` `- 1) * ` `$dp` `[` `$i` `- 2]; ` ` ` `} ` ` ` ` ` `return` `$dp` `[` `$p` `]; ` `} ` ` ` `// Driver code ` `$p` `= 3; ` `echo` `findWaysToPair(` `$p` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(p)

**Auxiliary Space:** O(p)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count ways to distribute m items among n people
- Count of ways to distribute N items among 3 people with one person receiving maximum
- Minimum and Maximum number of pairs in m teams of n people
- Check if all people can vote on two machines
- Count of groups among N people having only one leader in each group
- Number of ways to represent a number as sum of k fibonacci numbers
- Number of ways to get a given sum with n number of m-faced dices
- Number of distinct ways to represent a number as sum of K unique primes
- Count the number of ways to tile the floor of size n x m using 1 x m size tiles
- Number of ways to cut a stick of length N into K pieces
- Count number of ways to reach a given score in a game
- Count number of ways to cover a distance
- Count number of ways to partition a set into k subsets
- Bell Numbers (Number of ways to Partition a Set)
- Count number of ways to fill a "n x 4" grid using "1 x 4" tiles
- Number of ways to make mobile lock pattern
- Count ways to spell a number with repeated digits
- Count number of ways to reach destination in a Maze
- Ways to represent a number as a sum of 1's and 2's
- Count number of ways to jump to reach end

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.