Friends Pairing Problem

Here given code implementation process.

/*
    Java program for
    Friends Pairing Problem
*/
public class Pairing
{
	public void firendPairing(int n)
	{
		// Auxiliary space
		int[] dp = new int[n + 1];
		for (int i = 0; i <= n; i++)
		{
			if (i <= 2)
			{
				// When i less than 3
				dp[i] = i;
			}
			else
			{
				dp[i] = dp[i - 1] + ((i - 1) * dp[i - 2]);
			}
		}
		System.out.println(" Given N : " + n);
		System.out.println(" Number of groups : " + dp[n]);
	}
	public static void main(String[] args)
	{
		Pairing task = new Pairing();
		int n = 5;
		/*
		    n = 5
		    ------------
		    (1)(2)(3)(4)(5)
		    (1)(2)(3) (4,5)
		    (1)(2) (3,4)(5)
		    (1)(2) (3,5)(4)
		    (1) (2,3)(4)(5)
		    (1) (2,3) (4,5)
		    (1) (2,4)(3)(5)
		    (1) (2,4) (3,5)
		    (1) (2,5)(4)(3)
		    (1) (2,5) (4,3)
		    (1,2)(3)(4)(5)
		    (1,2)(3) (4,5)
		    (1,2) (3,4)(5)
		    (1,2) (3,5)(4)
		    (1,3)(2)(4)(5)
		    (1,3)(2) (4,5)
		    (1,3) (2,4)(5)
		    (1,3) (2,5)(4)
		    (1,4)(3)(2)(5)
		    (1,4)(3) (2,5)
		    (1,4) (3,2)(5)
		    (1,4) (3,5)(2)
		    (1,5)(3)(4)(2)
		    (1,5)(3) (4,2)
		    (1,5) (3,4)(2)
		    (1,5) (3,2)(4)
		*/
		task.firendPairing(n);
		n = 3;
		/*
		    n = 3
		    ------------
		    1 : (1)(2)(3)
		    2 : (1) (2,3)
		    3 :  (1,2)(3)
		    4 :  (1,3)(2)
		*/
		task.firendPairing(n);
	}
}

Output

 Given N : 5
 Number of groups : 26
 Given N : 3
 Number of groups : 4
// Include header file
#include <iostream>

using namespace std;
/*
    C++ program for
    Friends Pairing Problem
*/
class Pairing
{
	public: void firendPairing(int n)
	{
		// Auxiliary space
		int dp[n + 1];
		for (int i = 0; i <= n; i++)
		{
			if (i <= 2)
			{
				// When i less than 3
				dp[i] = i;
			}
			else
			{
				dp[i] = dp[i - 1] + ((i - 1) *dp[i - 2]);
			}
		}
		cout << " Given N : " << n << endl;
		cout << " Number of groups : " << dp[n] << endl;
	}
};
int main()
{
	Pairing *task = new Pairing();
	int n = 5;
	/*
	    n = 5
	    ------------
	    (1)(2)(3)(4)(5)
	    (1)(2)(3) (4,5)
	    (1)(2) (3,4)(5)
	    (1)(2) (3,5)(4)
	    (1) (2,3)(4)(5)
	    (1) (2,3) (4,5)
	    (1) (2,4)(3)(5)
	    (1) (2,4) (3,5)
	    (1) (2,5)(4)(3)
	    (1) (2,5) (4,3)
	    (1,2)(3)(4)(5)
	    (1,2)(3) (4,5)
	    (1,2) (3,4)(5)
	    (1,2) (3,5)(4)
	    (1,3)(2)(4)(5)
	    (1,3)(2) (4,5)
	    (1,3) (2,4)(5)
	    (1,3) (2,5)(4)
	    (1,4)(3)(2)(5)
	    (1,4)(3) (2,5)
	    (1,4) (3,2)(5)
	    (1,4) (3,5)(2)
	    (1,5)(3)(4)(2)
	    (1,5)(3) (4,2)
	    (1,5) (3,4)(2)
	    (1,5) (3,2)(4)
	*/
	task->firendPairing(n);
	n = 3;
	/*
	    n = 3
	    ------------
	    1 : (1)(2)(3)
	    2 : (1) (2,3)
	    3 :  (1,2)(3)
	    4 :  (1,3)(2)
	*/
	task->firendPairing(n);
	return 0;
}

Output

 Given N : 5
 Number of groups : 26
 Given N : 3
 Number of groups : 4
// Include namespace system
using System;
/*
    Csharp program for
    Friends Pairing Problem
*/
public class Pairing
{
	public void firendPairing(int n)
	{
		// Auxiliary space
		int[] dp = new int[n + 1];
		for (int i = 0; i <= n; i++)
		{
			if (i <= 2)
			{
				// When i less than 3
				dp[i] = i;
			}
			else
			{
				dp[i] = dp[i - 1] + ((i - 1) * dp[i - 2]);
			}
		}
		Console.WriteLine(" Given N : " + n);
		Console.WriteLine(" Number of groups : " + dp[n]);
	}
	public static void Main(String[] args)
	{
		Pairing task = new Pairing();
		int n = 5;
		/*
		    n = 5
		    ------------
		    (1)(2)(3)(4)(5)
		    (1)(2)(3) (4,5)
		    (1)(2) (3,4)(5)
		    (1)(2) (3,5)(4)
		    (1) (2,3)(4)(5)
		    (1) (2,3) (4,5)
		    (1) (2,4)(3)(5)
		    (1) (2,4) (3,5)
		    (1) (2,5)(4)(3)
		    (1) (2,5) (4,3)
		    (1,2)(3)(4)(5)
		    (1,2)(3) (4,5)
		    (1,2) (3,4)(5)
		    (1,2) (3,5)(4)
		    (1,3)(2)(4)(5)
		    (1,3)(2) (4,5)
		    (1,3) (2,4)(5)
		    (1,3) (2,5)(4)
		    (1,4)(3)(2)(5)
		    (1,4)(3) (2,5)
		    (1,4) (3,2)(5)
		    (1,4) (3,5)(2)
		    (1,5)(3)(4)(2)
		    (1,5)(3) (4,2)
		    (1,5) (3,4)(2)
		    (1,5) (3,2)(4)
		*/
		task.firendPairing(n);
		n = 3;
		/*
		    n = 3
		    ------------
		    1 : (1)(2)(3)
		    2 : (1) (2,3)
		    3 :  (1,2)(3)
		    4 :  (1,3)(2)
		*/
		task.firendPairing(n);
	}
}

Output

 Given N : 5
 Number of groups : 26
 Given N : 3
 Number of groups : 4
package main
import "fmt"
/*
    Go program for
    Friends Pairing Problem
*/

func firendPairing(n int) {
	// Auxiliary space
	var dp = make([] int, n + 1)
	for i := 0 ; i <= n ; i++ {
		if i <= 2 {
			// When i less than 3
			dp[i] = i
		} else {
			dp[i] = dp[i - 1] + ((i - 1) * dp[i - 2])
		}
	}
	fmt.Println(" Given N : ", n)
	fmt.Println(" Number of groups : ", dp[n])
}
func main() {
	
	var n int = 5
	/*
	    n = 5
	    ------------
	    (1)(2)(3)(4)(5)
	    (1)(2)(3) (4,5)
	    (1)(2) (3,4)(5)
	    (1)(2) (3,5)(4)
	    (1) (2,3)(4)(5)
	    (1) (2,3) (4,5)
	    (1) (2,4)(3)(5)
	    (1) (2,4) (3,5)
	    (1) (2,5)(4)(3)
	    (1) (2,5) (4,3)
	    (1,2)(3)(4)(5)
	    (1,2)(3) (4,5)
	    (1,2) (3,4)(5)
	    (1,2) (3,5)(4)
	    (1,3)(2)(4)(5)
	    (1,3)(2) (4,5)
	    (1,3) (2,4)(5)
	    (1,3) (2,5)(4)
	    (1,4)(3)(2)(5)
	    (1,4)(3) (2,5)
	    (1,4) (3,2)(5)
	    (1,4) (3,5)(2)
	    (1,5)(3)(4)(2)
	    (1,5)(3) (4,2)
	    (1,5) (3,4)(2)
	    (1,5) (3,2)(4)
	*/
	firendPairing(n)
	n = 3
	/*
	    n = 3
	    ------------
	    1 : (1)(2)(3)
	    2 : (1) (2,3)
	    3 :  (1,2)(3)
	    4 :  (1,3)(2)
	*/
	firendPairing(n)
}

Output

 Given N : 5
 Number of groups : 26
 Given N : 3
 Number of groups : 4
<?php
/*
    Php program for
    Friends Pairing Problem
*/
class Pairing
{
	public	function firendPairing($n)
	{
		// Auxiliary space
		$dp = array_fill(0, $n + 1, 0);
		for ($i = 0; $i <= $n; $i++)
		{
			if ($i <= 2)
			{
				// When i less than 3
				$dp[$i] = $i;
			}
			else
			{
				$dp[$i] = $dp[$i - 1] + (($i - 1) * $dp[$i - 2]);
			}
		}
		echo(" Given N : ".$n.
			"\n");
		echo(" Number of groups : ".$dp[$n].
			"\n");
	}
}

function main()
{
	$task = new Pairing();
	$n = 5;
	/*
	    n = 5
	    ------------
	    (1)(2)(3)(4)(5)
	    (1)(2)(3) (4,5)
	    (1)(2) (3,4)(5)
	    (1)(2) (3,5)(4)
	    (1) (2,3)(4)(5)
	    (1) (2,3) (4,5)
	    (1) (2,4)(3)(5)
	    (1) (2,4) (3,5)
	    (1) (2,5)(4)(3)
	    (1) (2,5) (4,3)
	    (1,2)(3)(4)(5)
	    (1,2)(3) (4,5)
	    (1,2) (3,4)(5)
	    (1,2) (3,5)(4)
	    (1,3)(2)(4)(5)
	    (1,3)(2) (4,5)
	    (1,3) (2,4)(5)
	    (1,3) (2,5)(4)
	    (1,4)(3)(2)(5)
	    (1,4)(3) (2,5)
	    (1,4) (3,2)(5)
	    (1,4) (3,5)(2)
	    (1,5)(3)(4)(2)
	    (1,5)(3) (4,2)
	    (1,5) (3,4)(2)
	    (1,5) (3,2)(4)
	*/
	$task->firendPairing($n);
	$n = 3;
	/*
	    n = 3
	    ------------
	    1 : (1)(2)(3)
	    2 : (1) (2,3)
	    3 :  (1,2)(3)
	    4 :  (1,3)(2)
	*/
	$task->firendPairing($n);
}
main();

Output

 Given N : 5
 Number of groups : 26
 Given N : 3
 Number of groups : 4
/*
    Node JS program for
    Friends Pairing Problem
*/
class Pairing
{
	firendPairing(n)
	{
		// Auxiliary space
		var dp = Array(n + 1).fill(0);
		for (var i = 0; i <= n; i++)
		{
			if (i <= 2)
			{
				// When i less than 3
				dp[i] = i;
			}
			else
			{
				dp[i] = dp[i - 1] + ((i - 1) * dp[i - 2]);
			}
		}
		console.log(" Given N : " + n);
		console.log(" Number of groups : " + dp[n]);
	}
}

function main()
{
	var task = new Pairing();
	var n = 5;
	/*
	    n = 5
	    ------------
	    (1)(2)(3)(4)(5)
	    (1)(2)(3) (4,5)
	    (1)(2) (3,4)(5)
	    (1)(2) (3,5)(4)
	    (1) (2,3)(4)(5)
	    (1) (2,3) (4,5)
	    (1) (2,4)(3)(5)
	    (1) (2,4) (3,5)
	    (1) (2,5)(4)(3)
	    (1) (2,5) (4,3)
	    (1,2)(3)(4)(5)
	    (1,2)(3) (4,5)
	    (1,2) (3,4)(5)
	    (1,2) (3,5)(4)
	    (1,3)(2)(4)(5)
	    (1,3)(2) (4,5)
	    (1,3) (2,4)(5)
	    (1,3) (2,5)(4)
	    (1,4)(3)(2)(5)
	    (1,4)(3) (2,5)
	    (1,4) (3,2)(5)
	    (1,4) (3,5)(2)
	    (1,5)(3)(4)(2)
	    (1,5)(3) (4,2)
	    (1,5) (3,4)(2)
	    (1,5) (3,2)(4)
	*/
	task.firendPairing(n);
	n = 3;
	/*
	    n = 3
	    ------------
	    1 : (1)(2)(3)
	    2 : (1) (2,3)
	    3 :  (1,2)(3)
	    4 :  (1,3)(2)
	*/
	task.firendPairing(n);
}
main();

Output

 Given N : 5
 Number of groups : 26
 Given N : 3
 Number of groups : 4
#    Python 3 program for
#    Friends Pairing Problem
class Pairing :
	def firendPairing(self, n) :
		#  Auxiliary space
		dp = [0] * (n + 1)
		i = 0
		while (i <= n) :
			if (i <= 2) :
				#  When i less than 3
				dp[i] = i
			else :
				dp[i] = dp[i - 1] + ((i - 1) * dp[i - 2])
			
			i += 1
		
		print(" Given N : ", n)
		print(" Number of groups : ", dp[n])
	

def main() :
	task = Pairing()
	n = 5
	#    n = 5
	#    ------------
	#    (1)(2)(3)(4)(5)
	#    (1)(2)(3) (4,5)
	#    (1)(2) (3,4)(5)
	#    (1)(2) (3,5)(4)
	#    (1) (2,3)(4)(5)
	#    (1) (2,3) (4,5)
	#    (1) (2,4)(3)(5)
	#    (1) (2,4) (3,5)
	#    (1) (2,5)(4)(3)
	#    (1) (2,5) (4,3)
	#    (1,2)(3)(4)(5)
	#    (1,2)(3) (4,5)
	#    (1,2) (3,4)(5)
	#    (1,2) (3,5)(4)
	#    (1,3)(2)(4)(5)
	#    (1,3)(2) (4,5)
	#    (1,3) (2,4)(5)
	#    (1,3) (2,5)(4)
	#    (1,4)(3)(2)(5)
	#    (1,4)(3) (2,5)
	#    (1,4) (3,2)(5)
	#    (1,4) (3,5)(2)
	#    (1,5)(3)(4)(2)
	#    (1,5)(3) (4,2)
	#    (1,5) (3,4)(2)
	#    (1,5) (3,2)(4)
	task.firendPairing(n)
	n = 3
	#    n = 3
	#    ------------
	#    1 : (1)(2)(3)
	#    2 : (1) (2,3)
	#    3 :  (1,2)(3)
	#    4 :  (1,3)(2)
	task.firendPairing(n)

if __name__ == "__main__": main()

Output

 Given N :  5
 Number of groups :  26
 Given N :  3
 Number of groups :  4
#    Ruby program for
#    Friends Pairing Problem
class Pairing 
	def firendPairing(n) 
		#  Auxiliary space
		dp = Array.new(n + 1) {0}
		i = 0
		while (i <= n) 
			if (i <= 2) 
				#  When i less than 3
				dp[i] = i
			else
 
				dp[i] = dp[i - 1] + ((i - 1) * dp[i - 2])
			end

			i += 1
		end

		print(" Given N : ", n, "\n")
		print(" Number of groups : ", dp[n], "\n")
	end

end

def main() 
	task = Pairing.new()
	n = 5
	#    n = 5
	#    ------------
	#    (1)(2)(3)(4)(5)
	#    (1)(2)(3) (4,5)
	#    (1)(2) (3,4)(5)
	#    (1)(2) (3,5)(4)
	#    (1) (2,3)(4)(5)
	#    (1) (2,3) (4,5)
	#    (1) (2,4)(3)(5)
	#    (1) (2,4) (3,5)
	#    (1) (2,5)(4)(3)
	#    (1) (2,5) (4,3)
	#    (1,2)(3)(4)(5)
	#    (1,2)(3) (4,5)
	#    (1,2) (3,4)(5)
	#    (1,2) (3,5)(4)
	#    (1,3)(2)(4)(5)
	#    (1,3)(2) (4,5)
	#    (1,3) (2,4)(5)
	#    (1,3) (2,5)(4)
	#    (1,4)(3)(2)(5)
	#    (1,4)(3) (2,5)
	#    (1,4) (3,2)(5)
	#    (1,4) (3,5)(2)
	#    (1,5)(3)(4)(2)
	#    (1,5)(3) (4,2)
	#    (1,5) (3,4)(2)
	#    (1,5) (3,2)(4)
	task.firendPairing(n)
	n = 3
	#    n = 3
	#    ------------
	#    1 : (1)(2)(3)
	#    2 : (1) (2,3)
	#    3 :  (1,2)(3)
	#    4 :  (1,3)(2)
	task.firendPairing(n)
end

main()

Output

 Given N : 5
 Number of groups : 26
 Given N : 3
 Number of groups : 4
/*
    Scala program for
    Friends Pairing Problem
*/
class Pairing()
{
	def firendPairing(n: Int): Unit = {
		// Auxiliary space
		var dp: Array[Int] = Array.fill[Int](n + 1)(0);
		var i: Int = 0;
		while (i <= n)
		{
			if (i <= 2)
			{
				// When i less than 3
				dp(i) = i;
			}
			else
			{
				dp(i) = dp(i - 1) + ((i - 1) * dp(i - 2));
			}
			i += 1;
		}
		println(" Given N : " + n);
		println(" Number of groups : " + dp(n));
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Pairing = new Pairing();
		var n: Int = 5;
		/*
		    n = 5
		    ------------
		    (1)(2)(3)(4)(5)
		    (1)(2)(3) (4,5)
		    (1)(2) (3,4)(5)
		    (1)(2) (3,5)(4)
		    (1) (2,3)(4)(5)
		    (1) (2,3) (4,5)
		    (1) (2,4)(3)(5)
		    (1) (2,4) (3,5)
		    (1) (2,5)(4)(3)
		    (1) (2,5) (4,3)
		    (1,2)(3)(4)(5)
		    (1,2)(3) (4,5)
		    (1,2) (3,4)(5)
		    (1,2) (3,5)(4)
		    (1,3)(2)(4)(5)
		    (1,3)(2) (4,5)
		    (1,3) (2,4)(5)
		    (1,3) (2,5)(4)
		    (1,4)(3)(2)(5)
		    (1,4)(3) (2,5)
		    (1,4) (3,2)(5)
		    (1,4) (3,5)(2)
		    (1,5)(3)(4)(2)
		    (1,5)(3) (4,2)
		    (1,5) (3,4)(2)
		    (1,5) (3,2)(4)
		*/
		task.firendPairing(n);
		n = 3;
		/*
		    n = 3
		    ------------
		    1 : (1)(2)(3)
		    2 : (1) (2,3)
		    3 :  (1,2)(3)
		    4 :  (1,3)(2)
		*/
		task.firendPairing(n);
	}
}

Output

 Given N : 5
 Number of groups : 26
 Given N : 3
 Number of groups : 4
/*
    Swift 4 program for
    Friends Pairing Problem
*/
class Pairing
{
	func firendPairing(_ n: Int)
	{
		// Auxiliary space
		var dp: [Int] = Array(repeating: 0, count: n + 1);
		var i: Int = 0;
		while (i <= n)
		{
			if (i <= 2)
			{
				// When i less than 3
				dp[i] = i;
			}
			else
			{
				dp[i] = dp[i - 1] + ((i - 1) * dp[i - 2]);
			}
			i += 1;
		}
		print(" Given N : ", n);
		print(" Number of groups : ", dp[n]);
	}
}
func main()
{
	let task: Pairing = Pairing();
	var n: Int = 5;
	/*
	    n = 5
	    ------------
	    (1)(2)(3)(4)(5)
	    (1)(2)(3) (4,5)
	    (1)(2) (3,4)(5)
	    (1)(2) (3,5)(4)
	    (1) (2,3)(4)(5)
	    (1) (2,3) (4,5)
	    (1) (2,4)(3)(5)
	    (1) (2,4) (3,5)
	    (1) (2,5)(4)(3)
	    (1) (2,5) (4,3)
	    (1,2)(3)(4)(5)
	    (1,2)(3) (4,5)
	    (1,2) (3,4)(5)
	    (1,2) (3,5)(4)
	    (1,3)(2)(4)(5)
	    (1,3)(2) (4,5)
	    (1,3) (2,4)(5)
	    (1,3) (2,5)(4)
	    (1,4)(3)(2)(5)
	    (1,4)(3) (2,5)
	    (1,4) (3,2)(5)
	    (1,4) (3,5)(2)
	    (1,5)(3)(4)(2)
	    (1,5)(3) (4,2)
	    (1,5) (3,4)(2)
	    (1,5) (3,2)(4)
	*/
	task.firendPairing(n);
	n = 3;
	/*
	    n = 3
	    ------------
	    1 : (1)(2)(3)
	    2 : (1) (2,3)
	    3 :  (1,2)(3)
	    4 :  (1,3)(2)
	*/
	task.firendPairing(n);
}
main();

Output

 Given N :  5
 Number of groups :  26
 Given N :  3
 Number of groups :  4
/*
    Kotlin program for
    Friends Pairing Problem
*/
class Pairing
{
	fun firendPairing(n: Int): Unit
	{
		// Auxiliary space
		var dp: Array < Int > = Array(n + 1)
		{
			0
		};
		var i: Int = 0;
		while (i <= n)
		{
			if (i <= 2)
			{
				// When i less than 3
				dp[i] = i;
			}
			else
			{
				dp[i] = dp[i - 1] + ((i - 1) * dp[i - 2]);
			}
			i += 1;
		}
		println(" Given N : " + n);
		println(" Number of groups : " + dp[n]);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Pairing = Pairing();
	var n: Int = 5;
	/*
	    n = 5
	    ------------
	    (1)(2)(3)(4)(5)
	    (1)(2)(3) (4,5)
	    (1)(2) (3,4)(5)
	    (1)(2) (3,5)(4)
	    (1) (2,3)(4)(5)
	    (1) (2,3) (4,5)
	    (1) (2,4)(3)(5)
	    (1) (2,4) (3,5)
	    (1) (2,5)(4)(3)
	    (1) (2,5) (4,3)
	    (1,2)(3)(4)(5)
	    (1,2)(3) (4,5)
	    (1,2) (3,4)(5)
	    (1,2) (3,5)(4)
	    (1,3)(2)(4)(5)
	    (1,3)(2) (4,5)
	    (1,3) (2,4)(5)
	    (1,3) (2,5)(4)
	    (1,4)(3)(2)(5)
	    (1,4)(3) (2,5)
	    (1,4) (3,2)(5)
	    (1,4) (3,5)(2)
	    (1,5)(3)(4)(2)
	    (1,5)(3) (4,2)
	    (1,5) (3,4)(2)
	    (1,5) (3,2)(4)
	*/
	task.firendPairing(n);
	n = 3;
	/*
	    n = 3
	    ------------
	    1 : (1)(2)(3)
	    2 : (1) (2,3)
	    3 :  (1,2)(3)
	    4 :  (1,3)(2)
	*/
	task.firendPairing(n);
}

Output

 Given N : 5
 Number of groups : 26
 Given N : 3
 Number of groups : 4


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved