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

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 = 2Output:1Explanation:

The only possible way to make two necklaces is that {1 | 2}.

N = 4Input:Output:3Explanation:

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 <bits/stdc++.h>` `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

`<script>` `// Javascript program for the above approach` `// Function to calculate factorial` `function` `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` `function` `numOfNecklace(N)` `{` ` ` `// Number of ways to choose N/2 beads` ` ` `// from N beads` ` ` `var` `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 Cod` ` ` `// Given Input` ` ` `var` `N = 4;` ` ` `// Function Call` ` ` `document.write(numOfNecklace(N));` `// This code is contributed by SURENDRA_GANGWAR.` `</script>` |

**Output**

3

**Time Complexity: **O(N)**Auxiliary Space: **O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.