# Count ways to arrange N distinct objects if all clockwise arrangements are considered the same

Given **N** distinct objects, the task is to find the number of distinct arrangements of **N** objects if all clockwise arrangements are considered the same.

If A, B, and C are three distinct objects, then arrangements {A, B, C}, {C, A, B}, and {B, C, A} are considered the same as all the arrangements are clockwise to each other.

**Examples:**

Input:N = 3Output:2Explanation:

Permutation of 3 distinct objects can be represented as these 6 arrangements: {A, B, C}, {A, C, B}, {B, A, C}, {B, C, A}, {C, A, B} and {C, B, A}. But arrangement {A, B, C}, {C, A, B} and {B, C, A} are considered the same.

The arrangements {A, C, B}, {B, A, C} and {C, B, A} are also considered the same.

Hence, there are only 2 distinct arrangements.

Input:N = 2Output:1Explanation:

There can only 2 arrangements: {A, B} and {B, A}. Both these arrangements are same considering the clockwise arrangements.

Hence, only 1 distinct arrangement exist.

**Naive Approach:** The idea is to generate all the permutation of **N**** **distinct objects. Now, iterate all the arrangements and increment the counter if the current arrangement’s clockwise rotation is not present in previous arrangements. After checking all the arrangements, print the counter as the total distinct clockwise arrangements possible.

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

**Efficient Approach:** The idea is to use the following observation:

Given a permutation {A

_{1}, A_{2}, …, A_{N}} of N distinct objects. Then, there can be N clockwise rotated arrangements denotes as follows:{A

_{1}, A_{2}, …, A_{N}}

{A_{N}, A_{1}, …, A_{N-1}}

{A_{N-1}, A_{N}, …, A_{N-2}}

{A_{N-2}, A_{N-1}, …, A_{N-3}}

and so on.Therefore, for each permutation of length

Nthere existN clockwisearrangements. Also, for N distinct objects, there areN!arrangements without considering the clockwise arrangements same.Hence, N*X = N!, where

N the clockwise arrangements of any permutation of length N.

X is the number of permutations considering clockwise arrangements the same.

N! is the total number of permutations.Using the above equation:

X = N! / N

X = (N – 1)!

Hence, the total number of distinct permutations considering clockwise arrangements the same is **(N – 1)!**. Therefore, the task is to print the value of factorial **(N – 1)**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <iostream>` `using` `namespace` `std;` `// Function to return the factorial` `// of given number` `unsigned ` `int` `factorial(unsigned ` `int` `n)` `{` ` ` `// Base case` ` ` `if` `(n == 0)` ` ` `return` `1;` ` ` `// Recursively find the factorial` ` ` `return` `n * factorial(n - 1);` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given N objects` ` ` `int` `N = 4;` ` ` `// Function Call` ` ` `cout << factorial(N - 1);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.io.*;` `class` `GFG{` ` ` `// Function to return the factorial` `// of given number` `static` `int` `factorial(` `int` `n)` `{` ` ` ` ` `// Base case` ` ` `if` `(n == ` `0` `)` ` ` `return` `1` `;` ` ` `// Recursively find the factorial` ` ` `return` `n * factorial(n - ` `1` `);` `}` `// Driver Code` `public` `static` `void` `main (String[] args)` `{` ` ` ` ` `// Given N objects` ` ` `int` `N = ` `4` `;` ` ` `// Function Call` ` ` `System.out.print(factorial(N - ` `1` `));` `}` `}` `// This code is contributed by math_lover` |

## Python3

`# Python3 program for the above approach` `# Function to return the factorial` `# of a given number` `def` `factorial(n):` ` ` `# Base Case` ` ` `if` `n ` `=` `=` `0` `:` ` ` `return` `1` ` ` ` ` `# Recursively find the factorial` ` ` `return` `n ` `*` `factorial(n` `-` `1` `)` `# Driver Code` `# Given N objects` `N ` `=` `4` `# Function Call` `print` `(factorial(N ` `-` `1` `))` |

## C#

`// C# program for the ` `// above approach` `using` `System;` `class` `GFG{` ` ` `// Function to return ` `// the factorial` `// of given number` `static` `int` `factorial(` `int` `n)` `{ ` ` ` `// Base case` ` ` `if` `(n == 0)` ` ` `return` `1;` ` ` `// Recursively find the ` ` ` `// factorial` ` ` `return` `n * factorial(n - 1);` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{ ` ` ` `// Given N objects` ` ` `int` `N = 4;` ` ` `// Function Call` ` ` `Console.Write(factorial(N - 1));` `}` `}` `// This code is contributed by gauravrajput1` |

## Javascript

`<script>` `// Javascript program to implement` `// the above approach` `// Function to return the factorial` `// of given number` `function` `factorial(n)` `{` ` ` ` ` `// Base case` ` ` `if` `(n == 0)` ` ` `return` `1;` ` ` ` ` `// Recursively find the factorial` ` ` `return` `n * factorial(n - 1);` `}` ` ` ` ` `// Driver Code` ` ` ` ` `// Given N objects` ` ` `let N = 4;` ` ` ` ` `// Function Call` ` ` `document.write(factorial(N - 1));` ` ` `</script>` |

**Output**

6

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

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**