# Count of ways to form 2 necklace from N beads containing N/2 beads each

• Last Updated : 07 Oct, 2022

Given a positive even integer N denoting the number of distinct beads, the task is to find the number of ways to make 2 necklaces having exactly N/2 beads.

Examples:

Input: N = 2
Output: 1
Explanation:
The only possible way to make two necklaces is that {1 | 2}.

Input: N = 4
Output: 3
Explanation:
The possible ways to make two necklaces are {(1, 2) | (3, 4)}, {(1, 3) | (2, 4)}, and {(1, 4) | (2, 3)}.

Approach: The problem can be solved by using the concept of circular permutation and combinatorics. Follow the steps below to solve the problem:

• Define a function, say factorial for calculating the factorial of a number, by following the steps below:
• Base Case: If n = 0, then return 1.
• If n != 0, then recursively call the function and return n * factorial(n-1).
• Initialize a variable, say, ans as C(N, N/2), that is the number of ways to choose N/2 beads from N beads.
• Since the necklace is circular, the number of ways to permute N/2 beads are factorial(N/2 -1), so multiply the value of ans by factorial(N/2 -1)*factorial(N/2-1) since there are two necklaces.
• Now divide the ans by 2. Because of symmetrical distribution. For eg, For N=2, the distribution {1 | 2} and {2 | 1} are considered the same.
• Finally, after completing the above steps, print the value of ans as the answer.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to calculate factorial``int` `factorial(``int` `n)``{``    ``if` `(n == 0)``        ``return` `1;``    ``return` `n * factorial(n - 1);``}` `// Function to count number of ways``// to make 2 necklace having exactly``// N/2 beads if each bead is``// considered different``long` `long` `numOfNecklace(``int` `N)``{``    ``// Number of ways to choose N/2 beads``    ``// from N beads``    ``long` `long` `ans = factorial(N)``                    ``/ (factorial(N / 2) * factorial(N / 2));` `    ``// Number of ways to permute N/2 beads``    ``ans = ans * factorial(N / 2 - 1);``    ``ans = ans * factorial(N / 2 - 1);` `    ``// Divide ans by 2 to remove repetitions``    ``ans /= 2;` `    ``// Return ans``    ``return` `ans;``}` `// Driver Code``int` `main()``{``    ``// Given Input``    ``int` `N = 4;` `    ``// Function Call``    ``cout << numOfNecklace(N) << endl;``    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;``class` `GFG``{``  ` `    ``// Function to calculate factorial``    ``static` `int` `factorial(``int` `n)``    ``{``        ``if` `(n == ``0``)``            ``return` `1``;``        ``return` `n * factorial(n - ``1``);``    ``}``  ` `    ``// Function to count number of ways``    ``// to make 2 necklace having exactly``    ``// N/2 beads if each bead is``    ``// considered different``    ``static` `long` `numOfNecklace(``int` `N)``    ``{``      ` `        ``// Number of ways to choose N/2 beads``        ``// from N beads``        ``long` `ans = factorial(N)``                   ``/ (factorial(N / ``2``) * factorial(N / ``2``));` `        ``// Number of ways to permute N/2 beads``        ``ans = ans * factorial(N / ``2` `- ``1``);``        ``ans = ans * factorial(N / ``2` `- ``1``);` `        ``// Divide ans by 2 to remove repetitions``        ``ans /= ``2``;` `        ``// Return ans``        ``return` `ans;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``      ` `        ``// Given Input``        ``int` `N = ``4``;` `        ``// Function Call``        ``System.out.println(numOfNecklace(N));` `    ``}``}` `// This code is contributed by Potta Lokesh`

## Python3

 `# Python 3 program for the above approach` `# Function to calculate factorial``def` `factorial(n):``    ``if` `(n ``=``=` `0``):``        ``return` `1``    ``return` `n ``*` `factorial(n ``-` `1``)` `# Function to count number of ways``# to make 2 necklace having exactly``# N/2 beads if each bead is``# considered different``def` `numOfNecklace(N):``  ` `    ``# Number of ways to choose N/2 beads``    ``# from N beads``    ``ans ``=` `factorial(N) ``/``/` `(factorial(N ``/``/` `2``) ``*` `factorial(N ``/``/` `2``))` `    ``# Number of ways to permute N/2 beads``    ``ans ``=` `ans ``*` `factorial(N ``/``/` `2` `-` `1``)``    ``ans ``=` `ans ``*` `factorial(N ``/``/` `2` `-` `1``)` `    ``# Divide ans by 2 to remove repetitions``    ``ans ``/``/``=` `2` `    ``# Return ans``    ``return` `ans` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``# Given Input``    ``N ``=` `4` `    ``# Function Call``    ``print``(numOfNecklace(N))``    ` `    ``# This code is contributed by ipg2016107.`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to calculate factorial``static` `int` `factorial(``int` `n)``{``    ``if` `(n == 0)``        ``return` `1;``        ` `    ``return` `n * factorial(n - 1);``}` `// Function to count number of ways``// to make 2 necklace having exactly``// N/2 beads if each bead is``// considered different``static` `long` `numOfNecklace(``int` `N)``{``    ` `    ``// Number of ways to choose N/2 beads``    ``// from N beads``    ``long` `ans = factorial(N) /``              ``(factorial(N / 2) *``               ``factorial(N / 2));` `    ``// Number of ways to permute N/2 beads``    ``ans = ans * factorial(N / 2 - 1);``    ``ans = ans * factorial(N / 2 - 1);` `    ``// Divide ans by 2 to remove repetitions``    ``ans /= 2;` `    ``// Return ans``    ``return` `ans;``}` `// Driver Code``static` `public` `void` `Main ()``{``    ` `    ``// Given Input``    ``int` `N = 4;` `    ``// Function Call``    ``Console.Write( numOfNecklace(N));``}``}` `// This code is contributed by sanjoy_62`

## Javascript

 ``

Output

`3`

Time Complexity: O(N)
Auxiliary Space: O(N), due to recursive call stack

My Personal Notes arrow_drop_up