Consider a coding competition on neveropen practice. Now there are n distinct participants taking part in the competition. A single participant can make pair with at most one other participant. We need count the number of ways in which n participants participating in the coding competition.
Examples :
Input : n = 2
Output : 2
2 shows that either both participant
can pair themselves in one way or both
of them can remain single.
Input : n = 3
Output : 4
One way : Three participants remain single
Three More Ways : [(1, 2)(3)], [(1), (2,3)]
and [(1,3)(2)]
Method 1:
1) Every participant can either pair with another participant or can remain single.
2) Let us consider X-th participant, he can either remain single or he can pair up with someone from [1, x-1].
Below is the implementation of the above idea:
C++
#include<iostream>
using namespace std;
int numberOfWays( int x)
{
if (x==0 || x==1)
return 1;
else
return numberOfWays(x-1) +
(x-1)*numberOfWays(x-2);
}
int main()
{
int x = 3;
cout << numberOfWays(x) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int numberOfWays( int x)
{
if (x== 0 || x== 1 )
return 1 ;
else
return numberOfWays(x- 1 ) +
(x- 1 )*numberOfWays(x- 2 );
}
public static void main (String[] args) {
int x = 3 ;
System.out.println( numberOfWays(x));
}
}
|
Python3
def numberOfWays (x):
if x = = 0 or x = = 1 :
return 1
else :
return (numberOfWays(x - 1 ) +
(x - 1 ) * numberOfWays(x - 2 ))
x = 3
print (numberOfWays(x))
|
C#
using System;
class GFG {
static int numberOfWays( int x)
{
if (x == 0 || x == 1)
return 1;
else
return numberOfWays(x - 1) +
(x - 1) * numberOfWays(x - 2);
}
public static void Main ()
{
int x = 3;
Console.WriteLine(numberOfWays(x));
}
}
|
Javascript
<script>
function numberOfWays(x)
{
if (x == 0 || x == 1)
return 1;
else
return numberOfWays(x - 1) + (x - 1) * numberOfWays(x - 2);
}
var x = 3;
document.write(numberOfWays(x));
</script>
|
PHP
<?php
function numberOfWays( $x )
{
if ( $x == 0 || $x == 1)
return 1;
else
return numberOfWays( $x - 1) +
( $x - 1) * numberOfWays( $x - 2);
}
$x = 3;
echo numberOfWays( $x );
?>
|
Time Complexity: O(2x)
Auxiliary Space: O(log(x)), due to recursive call stack
Since there are overlapping subproblems, we can optimize it using dynamic programming.
C++
#include<iostream>
using namespace std;
int numberOfWays( int x)
{
int dp[x+1];
dp[0] = dp[1] = 1;
for ( int i=2; i<=x; i++)
dp[i] = dp[i-1] + (i-1)*dp[i-2];
return dp[x];
}
int main()
{
int x = 3;
cout << numberOfWays(x) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int numberOfWays( int x)
{
int dp[] = new int [x+ 1 ];
dp[ 0 ] = dp[ 1 ] = 1 ;
for ( int i= 2 ; i<=x; i++)
dp[i] = dp[i- 1 ] + (i- 1 )*dp[i- 2 ];
return dp[x];
}
public static void main (String[] args) {
int x = 3 ;
System.out.println(numberOfWays(x));
}
}
|
Python3
def numberOfWays (x):
dp = []
dp.append( 1 )
dp.append( 1 )
for i in range ( 2 ,x + 1 ):
dp.append(dp[i - 1 ] + (i - 1 ) * dp[i - 2 ])
return (dp[x])
x = 3
print (numberOfWays(x))
|
C#
using System;
class GFG {
static int numberOfWays( int x)
{
int []dp = new int [x+1];
dp[0] = dp[1] = 1;
for ( int i = 2; i <= x; i++)
dp[i] = dp[i - 1] +
(i - 1) * dp[i - 2];
return dp[x];
}
public static void Main ()
{
int x = 3;
Console.WriteLine(numberOfWays(x));
}
}
|
Javascript
<script>
function numberOfWays( x)
{
let dp = Array(x + 1).fill(0);
dp[0] = dp[1] = 1;
for ( i = 2; i <= x; i++)
dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
return dp[x];
}
let x = 3;
document.write(numberOfWays(x));
</script>
|
PHP
<?php
function numberOfWays( $x )
{
$dp [0] = 1;
$dp [1] = 1;
for ( $i = 2; $i <= $x ; $i ++)
$dp [ $i ] = $dp [ $i - 1] + ( $i - 1) *
$dp [ $i - 2];
return $dp [ $x ];
}
$x = 3;
echo numberOfWays( $x ) ;
?>
|
Time Complexity : O( x )
Auxiliary Space: O( x )
Efficient approach: Space optimization O(1)
In previous approach we the current value dp[i] is only depend upon the previous 2 values i.e. dp[i-1] and dp[i-2]. So to optimize the space we can keep track of previous and current values by the help of three variables prev1, prev2 and curr which will reduce the space complexity from O(x) to O(1).
Implementation Steps:
- Create 2 variables prev1 and prev2 to keep track o previous values of DP.
- Initialize base case prev1 = prev2 = 1.
- Create a variable curr to store current value.
- Iterate over subproblem using loop and update curr.
- After every iteration update prev1 and prev2 for further iterations.
- At last return curr.
Implementation:
C++
#include <iostream>
using namespace std;
int numberOfWays( int x)
{
int curr;
int prev1 = 1, prev2 = 1;
for ( int i = 2; i <= x; i++) {
curr = prev2 + (i - 1) * prev1;
prev1 = prev2;
prev2 = curr;
}
return curr;
}
int main()
{
int x = 3;
cout << numberOfWays(x) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int numberOfWays( int x)
{
int curr = 0 ;
int prev1 = 1 , prev2 = 1 ;
for ( int i = 2 ; i <= x; i++) {
curr = prev2 + (i - 1 ) * prev1;
prev1 = prev2;
prev2 = curr;
}
return curr;
}
public static void main(String[] args)
{
int x = 3 ;
System.out.println(numberOfWays(x));
}
}
|
Python3
def numberOfWays(x):
curr = 0
prev1, prev2 = 1 , 1
for i in range ( 2 , x + 1 ):
curr = prev2 + (i - 1 ) * prev1
prev1, prev2 = prev2, curr
return curr
if __name__ = = '__main__' :
x = 3
print (numberOfWays(x))
|
C#
using System;
namespace NumberOfWays
{
class Program
{
static int NumberOfWays( int x)
{
int curr = 0;
int prev1 = 1, prev2 = 1;
for ( int i = 2; i <= x; i++)
{
curr = prev2 + (i - 1) * prev1;
prev1 = prev2;
prev2 = curr;
}
return curr;
}
static void Main( string [] args)
{
int x = 3;
Console.WriteLine(NumberOfWays(x));
Console.ReadKey();
}
}
}
|
Javascript
function numberOfWays(x) {
let curr = 0;
let prev1 = 1,
prev2 = 1;
for (let i = 2; i <= x; i++) {
curr = prev2 + (i - 1) * prev1;
prev1 = prev2;
prev2 = curr;
}
return curr;
}
let x = 3;
console.log(numberOfWays(x));
|
Time Complexity : O( x )
Auxiliary Space: O( 1 )
This article is contributed by nikunj_agarwal. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!