diff --git a/dynamic_programming/friendspairing.cpp b/dynamic_programming/friendspairing.cpp new file mode 100644 index 0000000..e919e0c --- /dev/null +++ b/dynamic_programming/friendspairing.cpp @@ -0,0 +1,33 @@ + +// C++ program solution friends pairing problem +// C++ program for solution of +// friends pairing problem +#include +using namespace std; + +// Returns count of ways n people +// can remain single or paired up. +int countFriendsPairings(int n) +{ + int dp[n + 1]; + + // Filling dp[] in bottom-up manner using + // recursive formula explained above. + for (int i = 0; i <= n; i++) + { + if (i <= 2) + dp[i] = i; + else + dp[i] = dp[i - 1] + (i - 1) * dp[i - 2]; + } + + return dp[n]; +} + +// Driver code +int main() +{ + int n = 4; + cout << countFriendsPairings(n) << endl; + return 0; +}