Permutations to arrange N persons around a circular table
Last Updated :
31 Aug, 2022
Given N, the number of persons. The task is to arrange N person around a circular table.
Examples:
Input: N = 4
Output: 6
Input: N = 5
Output: 24
Approach: It is the concept of Circular permutation i.e. there is no specific starting point in the arrangement, any element can be considered as the start of the arrangement.
For N = 4, Arrangements will be:
Below is the formula to find Circular permutations:
Circular Permutations = (N - 1)!
Below is the implementation of above idea:
C++
#include <bits/stdc++.h>
using namespace std;
int Circular( int n)
{
int Result = 1;
while (n > 0) {
Result = Result * n;
n--;
}
return Result;
}
int main()
{
int n = 4;
cout << Circular(n - 1);
}
|
Java
import java.io.*;
class GFG
{
static int Circular( int n)
{
int Result = 1 ;
while (n > 0 )
{
Result = Result * n;
n--;
}
return Result;
}
public static void main(String[] args)
{
int n = 4 ;
System.out.println(Circular(n - 1 ));
}
}
|
Python 3
def Circular(n) :
Result = 1
while n > 0 :
Result = Result * n
n - = 1
return Result
if __name__ = = "__main__" :
n = 4
print (Circular(n - 1 ))
|
C#
using System;
public class GFG {
static int Circular( int n)
{
int Result = 1;
while (n > 0)
{
Result = Result * n;
n--;
}
return Result;
}
public static void Main()
{
int n = 4;
Console.Write(Circular(n - 1));
}
}
|
PHP
<?php
function Circular( $n )
{
$Result = 1;
while ( $n > 0)
{
$Result = $Result * $n ;
$n --;
}
return $Result ;
}
$n = 4;
echo Circular( $n - 1);
?>
|
Javascript
<script>
function Circular(n)
{
var Result = 1;
while (n > 0)
{
Result = Result * n;
n--;
}
return Result;
}
var n = 4;
document.write(Circular(n - 1));
</script>
|
Time Complexity: O(N) where N is the number of persons.
Auxiliary Space: O(1), since no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...