Count of ways to form 2 necklace from N beads containing N/2 beads each
Last Updated :
20 Jan, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int factorial( int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
long long numOfNecklace( int N)
{
long long ans = factorial(N)
/ (factorial(N / 2) * factorial(N / 2));
ans = ans * factorial(N / 2 - 1);
ans = ans * factorial(N / 2 - 1);
ans /= 2;
return ans;
}
int main()
{
int N = 4;
cout << numOfNecklace(N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int factorial( int n)
{
if (n == 0 )
return 1 ;
return n * factorial(n - 1 );
}
static long numOfNecklace( int N)
{
long ans = factorial(N)
/ (factorial(N / 2 ) * factorial(N / 2 ));
ans = ans * factorial(N / 2 - 1 );
ans = ans * factorial(N / 2 - 1 );
ans /= 2 ;
return ans;
}
public static void main(String[] args)
{
int N = 4 ;
System.out.println(numOfNecklace(N));
}
}
|
Python3
def factorial(n):
if (n = = 0 ):
return 1
return n * factorial(n - 1 )
def numOfNecklace(N):
ans = factorial(N) / / (factorial(N / / 2 ) * factorial(N / / 2 ))
ans = ans * factorial(N / / 2 - 1 )
ans = ans * factorial(N / / 2 - 1 )
ans / / = 2
return ans
if __name__ = = '__main__' :
N = 4
print (numOfNecklace(N))
|
C#
using System;
class GFG{
static int factorial( int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
static long numOfNecklace( int N)
{
long ans = factorial(N) /
(factorial(N / 2) *
factorial(N / 2));
ans = ans * factorial(N / 2 - 1);
ans = ans * factorial(N / 2 - 1);
ans /= 2;
return ans;
}
static public void Main ()
{
int N = 4;
Console.Write( numOfNecklace(N));
}
}
|
Javascript
<script>
function factorial(n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
function numOfNecklace(N)
{
var ans = factorial(N)
/ (factorial(N / 2) * factorial(N / 2));
ans = ans * factorial(N / 2 - 1);
ans = ans * factorial(N / 2 - 1);
ans /= 2;
return ans;
}
var N = 4;
document.write(numOfNecklace(N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N), due to recursive call stack
Share your thoughts in the comments
Please Login to comment...