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++
#include <iostream>
using namespace std;
unsigned int factorial(unsigned int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main()
{
int N = 4;
cout << factorial(N - 1);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int factorial( int n)
{
if (n == 0 )
return 1 ;
return n * factorial(n - 1 );
}
public static void main (String[] args)
{
int N = 4 ;
System.out.print(factorial(N - 1 ));
}
}
|
Python3
def factorial(n):
if n = = 0 :
return 1
return n * factorial(n - 1 )
N = 4
print (factorial(N - 1 ))
|
C#
using System;
class GFG{
static int factorial( int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
public static void Main(String[] args)
{
int N = 4;
Console.Write(factorial(N - 1));
}
}
|
Javascript
<script>
function factorial(n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
let N = 4;
document.write(factorial(N - 1));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N) for call stack, because it is using recursion
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!