Display Friends Pairing Group

Here given code implementation process.

``````/*
Java program for
Display Friends Pairing Group
*/
public class Pairing
{
public int count;
public Pairing()
{
this.count = 0;
}
public void swapElement(int[] element, int a, int b)
{
int temp = element[a];
element[a] = element[b];
element[b] = temp;
}
public void findPairs(int[] element, String output, int index, int n)
{
if (index <= n)
{
// This is work on pair of single element
findPairs(element,
output + "(" + element[index] + ")",
index + 1, n);
for (int i = index + 1; i <= n; ++i)
{
if (index + 1 == i)
{
// Consicutive element are pair
findPairs(element,
output + " (" + element[index] + "," +
element[(index + 1)] + ")",
index + 2, n);
}
else
{
swapElement(element, i, index + 1);
findPairs(element,
output + " (" + element[index] + "," +
element[(index + 1)] + ")", index + 2, n);
swapElement(element, i, index + 1);
}
}
}
else
{
// Increase the value of result counter
++this.count;
// Display pair result
System.out.println((this.count) + " : " + output);
}
}
public void printPair(int n)
{
if (n <= 0)
{
return;
}
System.out.println("\nGiven n " + n);
this.count = 0;
int[] element = new int[n + 1];
for (int i = 0; i <= n; ++i)
{
element[i] = i;
}
findPairs(element, "", 1, n);
}
public static void main(String[] args)
{
/*
n = 4
------------
(1)(2)(3)(4)
(1)(2) (3,4)
(1) (2,3)(4)
(1) (2,4)(3)
(1,2)(3)(4)
(1,2) (3,4)
(1,3)(2)(4)
(1,3) (2,4)
(1,4)(3)(2)
(1,4) (3,2)
*/
/*
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)
*/
}
}``````

Output

``````Given n 4
1 : (1)(2)(3)(4)
2 : (1)(2) (3,4)
3 : (1) (2,3)(4)
4 : (1) (2,4)(3)
5 :  (1,2)(3)(4)
6 :  (1,2) (3,4)
7 :  (1,3)(2)(4)
8 :  (1,3) (2,4)
9 :  (1,4)(3)(2)
10 :  (1,4) (3,2)

Given n 5
1 : (1)(2)(3)(4)(5)
2 : (1)(2)(3) (4,5)
3 : (1)(2) (3,4)(5)
4 : (1)(2) (3,5)(4)
5 : (1) (2,3)(4)(5)
6 : (1) (2,3) (4,5)
7 : (1) (2,4)(3)(5)
8 : (1) (2,4) (3,5)
9 : (1) (2,5)(4)(3)
10 : (1) (2,5) (4,3)
11 :  (1,2)(3)(4)(5)
12 :  (1,2)(3) (4,5)
13 :  (1,2) (3,4)(5)
14 :  (1,2) (3,5)(4)
15 :  (1,3)(2)(4)(5)
16 :  (1,3)(2) (4,5)
17 :  (1,3) (2,4)(5)
18 :  (1,3) (2,5)(4)
19 :  (1,4)(3)(2)(5)
20 :  (1,4)(3) (2,5)
21 :  (1,4) (3,2)(5)
22 :  (1,4) (3,5)(2)
23 :  (1,5)(3)(4)(2)
24 :  (1,5)(3) (4,2)
25 :  (1,5) (3,4)(2)
26 :  (1,5) (3,2)(4)``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ program for
Display Friends Pairing Group
*/
class Pairing
{
public: int count;
Pairing()
{
this->count = 0;
}
void swapElement(int element[], int a, int b)
{
int temp = element[a];
element[a] = element[b];
element[b] = temp;
}
void findPairs(int element[], string output, int index, int n)
{
if (index <= n)
{
// This is work on pair of single element
this->findPairs(element, output  +  "("
+  to_string(element[index])  +  ")",
index + 1, n);
for (int i = index + 1; i <= n; ++i)
{
if (index + 1 == i)
{
// Consicutive element are pair
this->findPairs(element, output  +  " ("
+  to_string(element[index])  +  ","
+  to_string(element[(index + 1)])  +  ")",
index + 2, n);
}
else
{
this->swapElement(element, i, index + 1);
this->findPairs(element, output  +  " ("
+  to_string(element[index])  +  ","
+  to_string(element[(index + 1)])  +  ")",
index + 2, n);
this->swapElement(element, i, index + 1);
}
}
}
else
{
// Increase the value of result counter
++this->count;
// Display pair result
cout << (this->count) << " : " << output << endl;
}
}
void printPair(int n)
{
if (n <= 0)
{
return;
}
cout << "\nGiven n " << n << endl;
this->count = 0;
int element[n + 1];
for (int i = 0; i <= n; ++i)
{
element[i] = i;
}
this->findPairs(element, "", 1, n);
}
};
int main()
{
/*
n = 4
------------
(1)(2)(3)(4)
(1)(2) (3,4)
(1) (2,3)(4)
(1) (2,4)(3)
(1,2)(3)(4)
(1,2) (3,4)
(1,3)(2)(4)
(1,3) (2,4)
(1,4)(3)(2)
(1,4) (3,2)
*/
/*
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)
*/
return 0;
}``````

Output

``````Given n 4
1 : (1)(2)(3)(4)
2 : (1)(2) (3,4)
3 : (1) (2,3)(4)
4 : (1) (2,4)(3)
5 :  (1,2)(3)(4)
6 :  (1,2) (3,4)
7 :  (1,3)(2)(4)
8 :  (1,3) (2,4)
9 :  (1,4)(3)(2)
10 :  (1,4) (3,2)

Given n 5
1 : (1)(2)(3)(4)(5)
2 : (1)(2)(3) (4,5)
3 : (1)(2) (3,4)(5)
4 : (1)(2) (3,5)(4)
5 : (1) (2,3)(4)(5)
6 : (1) (2,3) (4,5)
7 : (1) (2,4)(3)(5)
8 : (1) (2,4) (3,5)
9 : (1) (2,5)(4)(3)
10 : (1) (2,5) (4,3)
11 :  (1,2)(3)(4)(5)
12 :  (1,2)(3) (4,5)
13 :  (1,2) (3,4)(5)
14 :  (1,2) (3,5)(4)
15 :  (1,3)(2)(4)(5)
16 :  (1,3)(2) (4,5)
17 :  (1,3) (2,4)(5)
18 :  (1,3) (2,5)(4)
19 :  (1,4)(3)(2)(5)
20 :  (1,4)(3) (2,5)
21 :  (1,4) (3,2)(5)
22 :  (1,4) (3,5)(2)
23 :  (1,5)(3)(4)(2)
24 :  (1,5)(3) (4,2)
25 :  (1,5) (3,4)(2)
26 :  (1,5) (3,2)(4)``````
``````// Include namespace system
using System;
/*
Csharp program for
Display Friends Pairing Group
*/
public class Pairing
{
public int count;
public Pairing()
{
this.count = 0;
}
public void swapElement(int[] element, int a, int b)
{
int temp = element[a];
element[a] = element[b];
element[b] = temp;
}
public void findPairs(int[] element, String output, int index, int n)
{
if (index <= n)
{
// This is work on pair of single element
this.findPairs(element,
output + "(" + element[index] + ")",
index + 1, n);
for (int i = index + 1; i <= n; ++i)
{
if (index + 1 == i)
{
// Consicutive element are pair
this.findPairs(element,
output + " (" + element[index] + "," +
element[(index + 1)] + ")",
index + 2, n);
}
else
{
this.swapElement(element, i, index + 1);
this.findPairs(element,
output + " (" + element[index] + "," +
element[(index + 1)] + ")",
index + 2, n);
this.swapElement(element, i, index + 1);
}
}
}
else
{
// Increase the value of result counter
++this.count;
// Display pair result
Console.WriteLine((this.count) + " : " + output);
}
}
public void printPair(int n)
{
if (n <= 0)
{
return;
}
Console.WriteLine("\nGiven n " + n);
this.count = 0;
int[] element = new int[n + 1];
for (int i = 0; i <= n; ++i)
{
element[i] = i;
}
this.findPairs(element, "", 1, n);
}
public static void Main(String[] args)
{
/*
n = 4
------------
(1)(2)(3)(4)
(1)(2) (3,4)
(1) (2,3)(4)
(1) (2,4)(3)
(1,2)(3)(4)
(1,2) (3,4)
(1,3)(2)(4)
(1,3) (2,4)
(1,4)(3)(2)
(1,4) (3,2)
*/
/*
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)
*/
}
}``````

Output

``````Given n 4
1 : (1)(2)(3)(4)
2 : (1)(2) (3,4)
3 : (1) (2,3)(4)
4 : (1) (2,4)(3)
5 :  (1,2)(3)(4)
6 :  (1,2) (3,4)
7 :  (1,3)(2)(4)
8 :  (1,3) (2,4)
9 :  (1,4)(3)(2)
10 :  (1,4) (3,2)

Given n 5
1 : (1)(2)(3)(4)(5)
2 : (1)(2)(3) (4,5)
3 : (1)(2) (3,4)(5)
4 : (1)(2) (3,5)(4)
5 : (1) (2,3)(4)(5)
6 : (1) (2,3) (4,5)
7 : (1) (2,4)(3)(5)
8 : (1) (2,4) (3,5)
9 : (1) (2,5)(4)(3)
10 : (1) (2,5) (4,3)
11 :  (1,2)(3)(4)(5)
12 :  (1,2)(3) (4,5)
13 :  (1,2) (3,4)(5)
14 :  (1,2) (3,5)(4)
15 :  (1,3)(2)(4)(5)
16 :  (1,3)(2) (4,5)
17 :  (1,3) (2,4)(5)
18 :  (1,3) (2,5)(4)
19 :  (1,4)(3)(2)(5)
20 :  (1,4)(3) (2,5)
21 :  (1,4) (3,2)(5)
22 :  (1,4) (3,5)(2)
23 :  (1,5)(3)(4)(2)
24 :  (1,5)(3) (4,2)
25 :  (1,5) (3,4)(2)
26 :  (1,5) (3,2)(4)``````
``````package main
import "strconv"
import "fmt"
/*
Go program for
Display Friends Pairing Group
*/
type Pairing struct {
count int
}
func getPairing() * Pairing {
var me *Pairing = &Pairing {}
me.count = 0
return me
}
func(this Pairing) swapElement(element[] int, a int, b int) {
var temp int = element[a]
element[a] = element[b]
element[b] = temp
}
func(this *Pairing) findPairs(element[] int, output string, index int, n int) {
if index <= n {
// This is work on pair of single element
this.findPairs(element, output + "(" +
strconv.Itoa(element[index]) + ")", index + 1, n)
for i := index + 1 ; i <= n ; i++ {
if index + 1 == i {
// Consicutive element are pair
this.findPairs(element, output + " (" +
strconv.Itoa(element[index]) + "," +
strconv.Itoa(element[(index + 1)]) + ")", index + 2, n)
} else {
this.swapElement(element, i, index + 1)
this.findPairs(element, output + " (" +
strconv.Itoa(element[index]) + "," +
strconv.Itoa(element[(index + 1)]) + ")", index + 2, n)
this.swapElement(element, i, index + 1)
}
}
} else {
// Increase the value of result counter
this.count++
// Display pair result
fmt.Println((this.count), " : ", output)
}
}
func(this Pairing) printPair(n int) {
if n <= 0 {
return
}
fmt.Println("\nGiven n ", n)
this.count = 0
var element = make([] int, n + 1)
for i := 0 ; i <= n ; i++ {
element[i] = i
}
this.findPairs(element, "", 1, n)
}
func main() {
var task * Pairing = getPairing()
/*
n = 4
------------
(1)(2)(3)(4)
(1)(2) (3,4)
(1) (2,3)(4)
(1) (2,4)(3)
(1,2)(3)(4)
(1,2) (3,4)
(1,3)(2)(4)
(1,3) (2,4)
(1,4)(3)(2)
(1,4) (3,2)
*/
/*
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)
*/
}``````

Output

``````Given n  4
1  :  (1)(2)(3)(4)
2  :  (1)(2) (3,4)
3  :  (1) (2,3)(4)
4  :  (1) (2,4)(3)
5  :   (1,2)(3)(4)
6  :   (1,2) (3,4)
7  :   (1,3)(2)(4)
8  :   (1,3) (2,4)
9  :   (1,4)(3)(2)
10  :   (1,4) (3,2)

Given n  5
1  :  (1)(2)(3)(4)(5)
2  :  (1)(2)(3) (4,5)
3  :  (1)(2) (3,4)(5)
4  :  (1)(2) (3,5)(4)
5  :  (1) (2,3)(4)(5)
6  :  (1) (2,3) (4,5)
7  :  (1) (2,4)(3)(5)
8  :  (1) (2,4) (3,5)
9  :  (1) (2,5)(4)(3)
10  :  (1) (2,5) (4,3)
11  :   (1,2)(3)(4)(5)
12  :   (1,2)(3) (4,5)
13  :   (1,2) (3,4)(5)
14  :   (1,2) (3,5)(4)
15  :   (1,3)(2)(4)(5)
16  :   (1,3)(2) (4,5)
17  :   (1,3) (2,4)(5)
18  :   (1,3) (2,5)(4)
19  :   (1,4)(3)(2)(5)
20  :   (1,4)(3) (2,5)
21  :   (1,4) (3,2)(5)
22  :   (1,4) (3,5)(2)
23  :   (1,5)(3)(4)(2)
24  :   (1,5)(3) (4,2)
25  :   (1,5) (3,4)(2)
26  :   (1,5) (3,2)(4)``````
``````<?php
/*
Php program for
Display Friends Pairing Group
*/
class Pairing
{
public \$count;
public	function __construct()
{
\$this->count = 0;
}
public	function swapElement(&\$element, \$a, \$b)
{
\$temp = \$element[\$a];
\$element[\$a] = \$element[\$b];
\$element[\$b] = \$temp;
}
public	function findPairs(\$element, \$output, \$index, \$n)
{
if (\$index <= \$n)
{
// This is work on pair of single element
\$this->findPairs(\$element, \$output.
"(".strval(\$element[\$index]).
")", \$index + 1, \$n);
for (\$i = \$index + 1; \$i <= \$n; ++\$i)
{
if (\$index + 1 == \$i)
{
// Consicutive element are pair
\$this->findPairs(\$element, \$output.
" (".strval(\$element[\$index]).
",".strval(\$element[(\$index + 1)]).
")", \$index + 2, \$n);
}
else
{
\$this->swapElement(\$element, \$i, \$index + 1);
\$this->findPairs(\$element, \$output.
" (".strval(\$element[\$index]).
",".strval(\$element[(\$index + 1)]).
")", \$index + 2, \$n);
\$this->swapElement(\$element, \$i, \$index + 1);
}
}
}
else
{
// Increase the value of result counter
++\$this->count;
// Display pair result
echo((\$this->count).
" : ".\$output.
"\n");
}
}
public	function printPair(\$n)
{
if (\$n <= 0)
{
return;
}
echo("\nGiven n ".\$n.
"\n");
\$this->count = 0;
\$element = array_fill(0, \$n + 1, 0);
for (\$i = 0; \$i <= \$n; ++\$i)
{
\$element[\$i] = \$i;
}
\$this->findPairs(\$element, "", 1, \$n);
}
}

function main()
{
/*
n = 4
------------
(1)(2)(3)(4)
(1)(2) (3,4)
(1) (2,3)(4)
(1) (2,4)(3)
(1,2)(3)(4)
(1,2) (3,4)
(1,3)(2)(4)
(1,3) (2,4)
(1,4)(3)(2)
(1,4) (3,2)
*/
/*
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)
*/
}
main();``````

Output

``````Given n 4
1 : (1)(2)(3)(4)
2 : (1)(2) (3,4)
3 : (1) (2,3)(4)
4 : (1) (2,4)(3)
5 :  (1,2)(3)(4)
6 :  (1,2) (3,4)
7 :  (1,3)(2)(4)
8 :  (1,3) (2,4)
9 :  (1,4)(3)(2)
10 :  (1,4) (3,2)

Given n 5
1 : (1)(2)(3)(4)(5)
2 : (1)(2)(3) (4,5)
3 : (1)(2) (3,4)(5)
4 : (1)(2) (3,5)(4)
5 : (1) (2,3)(4)(5)
6 : (1) (2,3) (4,5)
7 : (1) (2,4)(3)(5)
8 : (1) (2,4) (3,5)
9 : (1) (2,5)(4)(3)
10 : (1) (2,5) (4,3)
11 :  (1,2)(3)(4)(5)
12 :  (1,2)(3) (4,5)
13 :  (1,2) (3,4)(5)
14 :  (1,2) (3,5)(4)
15 :  (1,3)(2)(4)(5)
16 :  (1,3)(2) (4,5)
17 :  (1,3) (2,4)(5)
18 :  (1,3) (2,5)(4)
19 :  (1,4)(3)(2)(5)
20 :  (1,4)(3) (2,5)
21 :  (1,4) (3,2)(5)
22 :  (1,4) (3,5)(2)
23 :  (1,5)(3)(4)(2)
24 :  (1,5)(3) (4,2)
25 :  (1,5) (3,4)(2)
26 :  (1,5) (3,2)(4)``````
``````/*
Node JS program for
Display Friends Pairing Group
*/
class Pairing
{
constructor()
{
this.count = 0;
}
swapElement(element, a, b)
{
var temp = element[a];
element[a] = element[b];
element[b] = temp;
}
findPairs(element, output, index, n)
{
if (index <= n)
{
// This is work on pair of single element
this.findPairs(element,
output + "(" + element[index] + ")",
index + 1, n);
for (var i = index + 1; i <= n; ++i)
{
if (index + 1 == i)
{
// Consicutive element are pair
this.findPairs(element,
output + " (" + element[index] + "," +
element[(index + 1)] + ")",
index + 2, n);
}
else
{
this.swapElement(element, i, index + 1);
this.findPairs(element,
output + " (" + element[index] + "," +
element[(index + 1)] + ")",
index + 2, n);
this.swapElement(element, i, index + 1);
}
}
}
else
{
// Increase the value of result counter
++this.count;
// Display pair result
console.log((this.count) + " : " + output);
}
}
printPair(n)
{
if (n <= 0)
{
return;
}
console.log("\nGiven n " + n);
this.count = 0;
var element = Array(n + 1).fill(0);
for (var i = 0; i <= n; ++i)
{
element[i] = i;
}
this.findPairs(element, "", 1, n);
}
}

function main()
{
/*
n = 4
------------
(1)(2)(3)(4)
(1)(2) (3,4)
(1) (2,3)(4)
(1) (2,4)(3)
(1,2)(3)(4)
(1,2) (3,4)
(1,3)(2)(4)
(1,3) (2,4)
(1,4)(3)(2)
(1,4) (3,2)
*/
/*
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)
*/
}
main();``````

Output

``````Given n 4
1 : (1)(2)(3)(4)
2 : (1)(2) (3,4)
3 : (1) (2,3)(4)
4 : (1) (2,4)(3)
5 :  (1,2)(3)(4)
6 :  (1,2) (3,4)
7 :  (1,3)(2)(4)
8 :  (1,3) (2,4)
9 :  (1,4)(3)(2)
10 :  (1,4) (3,2)

Given n 5
1 : (1)(2)(3)(4)(5)
2 : (1)(2)(3) (4,5)
3 : (1)(2) (3,4)(5)
4 : (1)(2) (3,5)(4)
5 : (1) (2,3)(4)(5)
6 : (1) (2,3) (4,5)
7 : (1) (2,4)(3)(5)
8 : (1) (2,4) (3,5)
9 : (1) (2,5)(4)(3)
10 : (1) (2,5) (4,3)
11 :  (1,2)(3)(4)(5)
12 :  (1,2)(3) (4,5)
13 :  (1,2) (3,4)(5)
14 :  (1,2) (3,5)(4)
15 :  (1,3)(2)(4)(5)
16 :  (1,3)(2) (4,5)
17 :  (1,3) (2,4)(5)
18 :  (1,3) (2,5)(4)
19 :  (1,4)(3)(2)(5)
20 :  (1,4)(3) (2,5)
21 :  (1,4) (3,2)(5)
22 :  (1,4) (3,5)(2)
23 :  (1,5)(3)(4)(2)
24 :  (1,5)(3) (4,2)
25 :  (1,5) (3,4)(2)
26 :  (1,5) (3,2)(4)``````
``````#    Python 3 program for
#    Display Friends Pairing Group
class Pairing :
def __init__(self) :
self.count = 0

def swapElement(self, element, a, b) :
temp = element[a]
element[a] = element[b]
element[b] = temp

def findPairs(self, element, output, index, n) :
if (index <= n) :
#  This is work on pair of single element
self.findPairs(element, output + "("
+ str(element[index]) + ")", index + 1, n)
i = index + 1
while (i <= n) :
if (index + 1 == i) :
#  Consicutive element are pair
self.findPairs(element, output + " ("
+ str(element[index]) + ","
+ str(element[(index + 1)]) + ")", index + 2, n)
else :
self.swapElement(element, i, index + 1)
self.findPairs(element, output + " ("
+ str(element[index]) + ","
+ str(element[(index + 1)]) + ")", index + 2, n)
self.swapElement(element, i, index + 1)

i += 1

else :
#  Increase the value of result counter
self.count += 1
#  Display pair result
print((self.count) ," : ", output)

def printPair(self, n) :
if (n <= 0) :
return

print("\nGiven n ", n)
self.count = 0
element = [0] * (n + 1)
i = 0
while (i <= n) :
element[i] = i
i += 1

self.findPairs(element, "", 1, n)

def main() :
#    n = 4
#    ------------
#    (1)(2)(3)(4)
#    (1)(2) (3,4)
#    (1) (2,3)(4)
#    (1) (2,4)(3)
#    (1,2)(3)(4)
#    (1,2) (3,4)
#    (1,3)(2)(4)
#    (1,3) (2,4)
#    (1,4)(3)(2)
#    (1,4) (3,2)
#    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)

if __name__ == "__main__": main()``````

Output

``````Given n  4
1  :  (1)(2)(3)(4)
2  :  (1)(2) (3,4)
3  :  (1) (2,3)(4)
4  :  (1) (2,4)(3)
5  :   (1,2)(3)(4)
6  :   (1,2) (3,4)
7  :   (1,3)(2)(4)
8  :   (1,3) (2,4)
9  :   (1,4)(3)(2)
10  :   (1,4) (3,2)

Given n  5
1  :  (1)(2)(3)(4)(5)
2  :  (1)(2)(3) (4,5)
3  :  (1)(2) (3,4)(5)
4  :  (1)(2) (3,5)(4)
5  :  (1) (2,3)(4)(5)
6  :  (1) (2,3) (4,5)
7  :  (1) (2,4)(3)(5)
8  :  (1) (2,4) (3,5)
9  :  (1) (2,5)(4)(3)
10  :  (1) (2,5) (4,3)
11  :   (1,2)(3)(4)(5)
12  :   (1,2)(3) (4,5)
13  :   (1,2) (3,4)(5)
14  :   (1,2) (3,5)(4)
15  :   (1,3)(2)(4)(5)
16  :   (1,3)(2) (4,5)
17  :   (1,3) (2,4)(5)
18  :   (1,3) (2,5)(4)
19  :   (1,4)(3)(2)(5)
20  :   (1,4)(3) (2,5)
21  :   (1,4) (3,2)(5)
22  :   (1,4) (3,5)(2)
23  :   (1,5)(3)(4)(2)
24  :   (1,5)(3) (4,2)
25  :   (1,5) (3,4)(2)
26  :   (1,5) (3,2)(4)``````
``````#    Ruby program for
#    Display Friends Pairing Group
class Pairing
# Define the accessor and reader of class Pairing
attr_accessor :count
def initialize()
self.count = 0
end

def swapElement(element, a, b)
temp = element[a]
element[a] = element[b]
element[b] = temp
end

def findPairs(element, output, index, n)
if (index <= n)
#  This is work on pair of single element
self.findPairs(element, output + "(" +
element[index].to_s + ")", index + 1, n)
i = index + 1
while (i <= n)
if (index + 1 == i)
#  Consicutive element are pair
self.findPairs(element, output + " (" +
element[index].to_s + "," +
element[(index + 1)].to_s + ")",
index + 2, n)
else

self.swapElement(element, i, index + 1)
self.findPairs(element,
output + " (" +
element[index].to_s + "," +
element[(index + 1)].to_s + ")",
index + 2, n)
self.swapElement(element, i, index + 1)
end

i += 1
end

else

#  Increase the value of result counter
self.count += 1
#  Display pair result
print((self.count) ," : ", output, "\n")
end

end

def printPair(n)
if (n <= 0)
return
end

print("\nGiven n ", n, "\n")
self.count = 0
element = Array.new(n + 1) {0}
i = 0
while (i <= n)
element[i] = i
i += 1
end

self.findPairs(element, "", 1, n)
end

end

def main()
#    n = 4
#    ------------
#    (1)(2)(3)(4)
#    (1)(2) (3,4)
#    (1) (2,3)(4)
#    (1) (2,4)(3)
#    (1,2)(3)(4)
#    (1,2) (3,4)
#    (1,3)(2)(4)
#    (1,3) (2,4)
#    (1,4)(3)(2)
#    (1,4) (3,2)
#    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)
end

main()``````

Output

``````Given n 4
1 : (1)(2)(3)(4)
2 : (1)(2) (3,4)
3 : (1) (2,3)(4)
4 : (1) (2,4)(3)
5 :  (1,2)(3)(4)
6 :  (1,2) (3,4)
7 :  (1,3)(2)(4)
8 :  (1,3) (2,4)
9 :  (1,4)(3)(2)
10 :  (1,4) (3,2)

Given n 5
1 : (1)(2)(3)(4)(5)
2 : (1)(2)(3) (4,5)
3 : (1)(2) (3,4)(5)
4 : (1)(2) (3,5)(4)
5 : (1) (2,3)(4)(5)
6 : (1) (2,3) (4,5)
7 : (1) (2,4)(3)(5)
8 : (1) (2,4) (3,5)
9 : (1) (2,5)(4)(3)
10 : (1) (2,5) (4,3)
11 :  (1,2)(3)(4)(5)
12 :  (1,2)(3) (4,5)
13 :  (1,2) (3,4)(5)
14 :  (1,2) (3,5)(4)
15 :  (1,3)(2)(4)(5)
16 :  (1,3)(2) (4,5)
17 :  (1,3) (2,4)(5)
18 :  (1,3) (2,5)(4)
19 :  (1,4)(3)(2)(5)
20 :  (1,4)(3) (2,5)
21 :  (1,4) (3,2)(5)
22 :  (1,4) (3,5)(2)
23 :  (1,5)(3)(4)(2)
24 :  (1,5)(3) (4,2)
25 :  (1,5) (3,4)(2)
26 :  (1,5) (3,2)(4)
``````
``````/*
Scala program for
Display Friends Pairing Group
*/
class Pairing(var count: Int)
{
def this()
{
this(0);
}
def swapElement(element: Array[Int], a: Int, b: Int): Unit = {
var temp: Int = element(a);
element(a) = element(b);
element(b) = temp;
}
def findPairs(
element: Array[Int],
output: String,
index: Int,
n: Int): Unit = {
if (index <= n)
{
// This is work on pair of single element
findPairs(element,
output + "(" + element(index).toString() + ")",
index + 1, n);
var i: Int = index + 1;
while (i <= n)
{
if (index + 1 == i)
{
// Consicutive element are pair
findPairs(element,
output + " (" + element(index).toString() + "," +
element((index + 1)).toString() + ")",
index + 2, n);
}
else
{
swapElement(element, i, index + 1);
findPairs(element, output + " (" +
element(index).toString() + "," +
element((index + 1)).toString() + ")",
index + 2, n);
swapElement(element, i, index + 1);
}
i += 1;
}
}
else
{
// Increase the value of result counter
this.count += 1;
// Display pair result
println(""+(this.count) + " : " + output);
}
}
def printPair(n: Int): Unit = {
if (n <= 0)
{
return;
}
println("\nGiven n " + n);
this.count = 0;
var element: Array[Int] = Array.fill[Int](n + 1)(0);
var i: Int = 0;
while (i <= n)
{
element(i) = i;
i += 1;
}
findPairs(element, "", 1, n);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Pairing = new Pairing();
/*
n = 4
------------
(1)(2)(3)(4)
(1)(2) (3,4)
(1) (2,3)(4)
(1) (2,4)(3)
(1,2)(3)(4)
(1,2) (3,4)
(1,3)(2)(4)
(1,3) (2,4)
(1,4)(3)(2)
(1,4) (3,2)
*/
/*
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)
*/
}
}``````

Output

``````Given n 4
1 : (1)(2)(3)(4)
2 : (1)(2) (3,4)
3 : (1) (2,3)(4)
4 : (1) (2,4)(3)
5 :  (1,2)(3)(4)
6 :  (1,2) (3,4)
7 :  (1,3)(2)(4)
8 :  (1,3) (2,4)
9 :  (1,4)(3)(2)
10 :  (1,4) (3,2)

Given n 5
1 : (1)(2)(3)(4)(5)
2 : (1)(2)(3) (4,5)
3 : (1)(2) (3,4)(5)
4 : (1)(2) (3,5)(4)
5 : (1) (2,3)(4)(5)
6 : (1) (2,3) (4,5)
7 : (1) (2,4)(3)(5)
8 : (1) (2,4) (3,5)
9 : (1) (2,5)(4)(3)
10 : (1) (2,5) (4,3)
11 :  (1,2)(3)(4)(5)
12 :  (1,2)(3) (4,5)
13 :  (1,2) (3,4)(5)
14 :  (1,2) (3,5)(4)
15 :  (1,3)(2)(4)(5)
16 :  (1,3)(2) (4,5)
17 :  (1,3) (2,4)(5)
18 :  (1,3) (2,5)(4)
19 :  (1,4)(3)(2)(5)
20 :  (1,4)(3) (2,5)
21 :  (1,4) (3,2)(5)
22 :  (1,4) (3,5)(2)
23 :  (1,5)(3)(4)(2)
24 :  (1,5)(3) (4,2)
25 :  (1,5) (3,4)(2)
26 :  (1,5) (3,2)(4)``````
``````/*
Kotlin program for
Display Friends Pairing Group
*/
class Pairing
{
var count: Int;
constructor()
{
this.count = 0;
}
fun swapElement(element: Array < Int > , a: Int, b: Int): Unit
{
val temp: Int = element[a];
element[a] = element[b];
element[b] = temp;
}
fun findPairs(element: Array < Int > ,
output: String, index: Int, n: Int): Unit
{
if (index <= n)
{
// This is work on pair of single element
this.findPairs(element,
output + "(" + element[index].toString() + ")",
index + 1, n);
var i: Int = index + 1;
while (i <= n)
{
if (index + 1 == i)
{
// Consicutive element are pair
this.findPairs(element,
output + " (" + element[index].toString() +
"," + element[(index + 1)].toString() + ")",
index + 2, n);
}
else
{
this.swapElement(element, i, index + 1);
this.findPairs(element,
output + " (" +
element[index].toString() + "," +
element[(index + 1)].toString() + ")",
index + 2, n);
this.swapElement(element, i, index + 1);
}
i += 1;
}
}
else
{
// Increase the value of result counter
this.count += 1;
// Display pair result
println(""+(this.count) + " : " + output);
}
}
fun printPair(n: Int): Unit
{
if (n <= 0)
{
return;
}
println("\nGiven n " + n);
this.count = 0;
val element: Array < Int > = Array(n + 1)
{
0
};
var i: Int = 0;
while (i <= n)
{
element[i] = i;
i += 1;
}
this.findPairs(element, "", 1, n);
}
}
fun main(args: Array < String > ): Unit
{
/*
n = 4
------------
(1)(2)(3)(4)
(1)(2) (3,4)
(1) (2,3)(4)
(1) (2,4)(3)
(1,2)(3)(4)
(1,2) (3,4)
(1,3)(2)(4)
(1,3) (2,4)
(1,4)(3)(2)
(1,4) (3,2)
*/
/*
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)
*/
}``````

Output

``````Given n 4
1 : (1)(2)(3)(4)
2 : (1)(2) (3,4)
3 : (1) (2,3)(4)
4 : (1) (2,4)(3)
5 :  (1,2)(3)(4)
6 :  (1,2) (3,4)
7 :  (1,3)(2)(4)
8 :  (1,3) (2,4)
9 :  (1,4)(3)(2)
10 :  (1,4) (3,2)

Given n 5
1 : (1)(2)(3)(4)(5)
2 : (1)(2)(3) (4,5)
3 : (1)(2) (3,4)(5)
4 : (1)(2) (3,5)(4)
5 : (1) (2,3)(4)(5)
6 : (1) (2,3) (4,5)
7 : (1) (2,4)(3)(5)
8 : (1) (2,4) (3,5)
9 : (1) (2,5)(4)(3)
10 : (1) (2,5) (4,3)
11 :  (1,2)(3)(4)(5)
12 :  (1,2)(3) (4,5)
13 :  (1,2) (3,4)(5)
14 :  (1,2) (3,5)(4)
15 :  (1,3)(2)(4)(5)
16 :  (1,3)(2) (4,5)
17 :  (1,3) (2,4)(5)
18 :  (1,3) (2,5)(4)
19 :  (1,4)(3)(2)(5)
20 :  (1,4)(3) (2,5)
21 :  (1,4) (3,2)(5)
22 :  (1,4) (3,5)(2)
23 :  (1,5)(3)(4)(2)
24 :  (1,5)(3) (4,2)
25 :  (1,5) (3,4)(2)
26 :  (1,5) (3,2)(4)``````
``````/*
Swift 4 program for
Display Friends Pairing Group
*/
class Pairing
{
var count: Int;
init()
{
self.count = 0;
}
func swapElement(_ element: inout[Int],
_ a: Int,
_ b: Int)
{
let temp: Int = element[a];
element[a] = element[b];
element[b] = temp;
}
func findPairs(_ element: inout[Int],
_ output: String, _ index: Int, _ n: Int)
{
if (index <= n)
{
// This is work on pair of single element
self.findPairs(&element, output + "("
+ String(element[index]) + ")", index + 1, n);
var i: Int = index + 1;
while (i <= n)
{
if (index + 1 == i)
{
// Consicutive element are pair
self.findPairs(&element, output + " ("
+ String(element[index]) + ","
+ String(element[(index + 1)]) + ")", index + 2, n);
}
else
{
self.swapElement(&element, i, index + 1);
self.findPairs(&element, output + " ("
+ String(element[index]) + ","
+ String(element[(index + 1)]) + ")", index + 2, n);
self.swapElement(&element, i, index + 1);
}
i += 1;
}
}
else
{
// Increase the value of result counter
self.count += 1;
// Display pair result
print((self.count) ," : ", output);
}
}
func printPair(_ n: Int)
{
if (n <= 0)
{
return;
}
print("\nGiven n ", n);
self.count = 0;
var element: [Int] = Array(repeating: 0, count: n + 1);
var i: Int = 0;
while (i <= n)
{
element[i] = i;
i += 1;
}
self.findPairs(&element, "", 1, n);
}
}
func main()
{
/*
n = 4
------------
(1)(2)(3)(4)
(1)(2) (3,4)
(1) (2,3)(4)
(1) (2,4)(3)
(1,2)(3)(4)
(1,2) (3,4)
(1,3)(2)(4)
(1,3) (2,4)
(1,4)(3)(2)
(1,4) (3,2)
*/
/*
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)
*/
}
main();``````

Output

``````Given n  4
1  :  (1)(2)(3)(4)
2  :  (1)(2) (3,4)
3  :  (1) (2,3)(4)
4  :  (1) (2,4)(3)
5  :   (1,2)(3)(4)
6  :   (1,2) (3,4)
7  :   (1,3)(2)(4)
8  :   (1,3) (2,4)
9  :   (1,4)(3)(2)
10  :   (1,4) (3,2)

Given n  5
1  :  (1)(2)(3)(4)(5)
2  :  (1)(2)(3) (4,5)
3  :  (1)(2) (3,4)(5)
4  :  (1)(2) (3,5)(4)
5  :  (1) (2,3)(4)(5)
6  :  (1) (2,3) (4,5)
7  :  (1) (2,4)(3)(5)
8  :  (1) (2,4) (3,5)
9  :  (1) (2,5)(4)(3)
10  :  (1) (2,5) (4,3)
11  :   (1,2)(3)(4)(5)
12  :   (1,2)(3) (4,5)
13  :   (1,2) (3,4)(5)
14  :   (1,2) (3,5)(4)
15  :   (1,3)(2)(4)(5)
16  :   (1,3)(2) (4,5)
17  :   (1,3) (2,4)(5)
18  :   (1,3) (2,5)(4)
19  :   (1,4)(3)(2)(5)
20  :   (1,4)(3) (2,5)
21  :   (1,4) (3,2)(5)
22  :   (1,4) (3,5)(2)
23  :   (1,5)(3)(4)(2)
24  :   (1,5)(3) (4,2)
25  :   (1,5) (3,4)(2)
26  :   (1,5) (3,2)(4)``````

Comment

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.