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 = 3
Output: 2
Explanation:
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 = 2
Output: 1
Explanation:
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 {A1, A2, …, AN} of N distinct objects. Then, there can be N clockwise rotated arrangements denotes as follows:

{A1, A2, …, AN}
{AN, A1, …, AN-1}
{AN-1, AN, …, AN-2}
{AN-2, AN-1, …, AN-3}
and so on.

Therefore, for each permutation of length N there exist N clockwise arrangements. Also, for N distinct objects, there are N! 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output

6

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.




My Personal Notes arrow_drop_up

Recommended Posts:


Active and well versed member of Competitive Programming

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : math_lover, GauravRajput1