Given n friends, each one can remain single or can be paired up with some other friend. Each friend can be paired only once. Find out the total number of ways in which friends can remain single or can be paired up.
Examples:
Input : n = 3
Output : 4
Explanation:
{1}, {2}, {3} : all single
{1}, {2, 3} : 2 and 3 paired but 1 is single.
{1, 2}, {3} : 1 and 2 are paired but 3 is single.
{1, 3}, {2} : 1 and 3 are paired but 2 is single.
Note that {1, 2} and {2, 1} are considered same.
Mathematical Explanation:
The problem is simplified version of how many ways we can divide n elements into multiple groups.
(here group size will be max of 2 elements).
In case of n = 3, we have only 2 ways to make a group:
1) all elements are individual(1,1,1)
2) a pair and individual (2,1)
In case of n = 4, we have 3 ways to form a group:
1) all elements are individual (1,1,1,1)
2) 2 individuals and one pair (2,1,1)
3) 2 separate pairs (2,2)
For n-th person there are two choices:1) n-th person remains single, we recur for f(n – 1)2) n-th person pairs up with any of the remaining n – 1 persons. We get (n – 1) * f(n – 2)Therefore we can recursively write f(n) as:f(n) = f(n – 1) + (n – 1) * f(n – 2)
Since the above recursive formula has overlapping subproblems, we can solve it using Dynamic Programming.
ans += fact.elementAt(n) / (twos * fact.elementAt(ones) *
fact.elementAt((n - ones) / 2));
ones -= 2;
twos *= 2;
}
returnans;
}
// Driver code
publicstaticvoidmain(String[] args)
{
fact = newVector<>();
fact.add(1);
preComputeFact(100);
intn = 4;
System.out.print(countFriendsPairings(n) +"\n");
}
}
// This code is contributed by umadevi9616
Python3
# Python3 soln using mathematical approach
# factorial array is stored dynamically
fact =[1]
defpreComputeFact(n):
fori inrange(1, n+1):
fact.append((fact[i-1]*i))
# Returns count of ways n people
# can remain single or paired up.
defcountFriendsPairings(n):
ones =n
twos =1
ans =0
while(ones >=0):
# pow of 1 will always be one
ans =ans +(fact[n]//(twos*fact[ones]*fact[(n-ones)//2]))
ones =ones -2
twos =twos *2
return(ans)
# Driver Code
# pre-compute factorial
preComputeFact(1000)
n =4
print(countFriendsPairings(n))
# solution contributed by adarsh_007
C#
// C# program to implement the approach
usingSystem;
usingSystem.Collections.Generic;
publicclassGFG
{
// initializing the fact list
staticList<int> fact = newList<int>();
// computing the next n values of fact
staticvoidpreComputeFact(intn)
{
for(inti = 1; i <= n; i++) {
fact.Add(fact[i - 1] * i);
}
}
// Returns count of ways n people
// can remain single or paired up.
staticintcountFriendsPairings(intn)
{
intones = n;
inttwos = 1;
intans = 0;
while(ones >= 0) {
// pow of 1 will always be one
ans += fact[n]
/ (twos * fact[ones]
* fact[(n - ones) / 2]);
ones -= 2;
twos *= 2;
}
returnans;
}
// driver code
staticpublicvoidMain()
{
// initializing the first element of fact
fact.Add(1);
preComputeFact(100);
intn = 4;
Console.Write(countFriendsPairings(n));
}
}
// this code is contributed by phasing17
Javascript
<script>
// Javascript soln using mathematical approach
// factorial array is stored dynamically
let fact = [1];
functionpreComputeFact(n)
{
for(let i=1;i<n+1;i++)
{
fact.push((fact[i-1]*i));
}
}
// Returns count of ways n people
// can remain single or paired up.
functioncountFriendsPairings(n)
{
let ones = n
let twos = 1;
let ans = 0;
while(ones >= 0)
{
// pow of 1 will always be one
ans = ans + Math.floor(fact[n]/(twos*fact[ones]*
fact[(n-ones)/2]))
ones = ones - 2
twos = twos * 2
}
returnans;
}
// Driver Code
// pre-compute factorial
preComputeFact(1000)
n = 4
document.write(countFriendsPairings(n))
// This code is contributed by ab2127
</script>
Output
10
Time Complexity: O(n) Auxiliary Space: O(n)
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!