Count of ways to form 2 necklace from N beads containing N / 2 beads each | Java program for the above approach ; Function to calculate factorial ; Function to count number of ways to make 2 necklace having exactly N / 2 beads if each bead is considered different ; Number of ways to choose N / 2 beads from N beads ; Number of ways to permute N / 2 beads ; Divide ans by 2 to remove repetitions ; Return ans ; Driver Code ; Given Input ; Function Call
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 ) ) ; } }