Seating arrangement of N boys sitting around a round table such that two particular boys sit together
Last Updated :
09 Jun, 2022
There are N boys which are to be seated around a round table. The task is to find the number of ways in which N boys can sit around a round table such that two particular boys sit together.
Examples:
Input: N = 5
Output: 12
2 boy can be arranged in 2! ways and other boys
can be arranged in (5 – 2)! (2 is subtracted because the
previously selected two boys will be considered as a single boy now and No. of ways to arrange boys around a round table = (n-1)!)
So, total ways are 2! * (n-2)!) = 2! * 3! = 12
Input: N = 9
Output: 10080
Approach:
- First, 2 boys can be arranged in 2! ways.
- No. of ways to arrange remaining boys and the previous two boy pair is (n – 2)!.
- So, Total ways = 2! * (n – 2)!.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Total_Ways( int n)
{
int fac = 1;
for ( int i = 2; i <= n - 2; i++) {
fac = fac * i;
}
return (fac * 2);
}
int main()
{
int n = 5;
cout << Total_Ways(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int Total_Ways( int n)
{
int fac = 1 ;
for ( int i = 2 ; i <= n - 2 ; i++)
{
fac = fac * i;
}
return (fac * 2 );
}
public static void main (String[] args)
{
int n = 5 ;
System.out.println (Total_Ways(n));
}
}
|
Python3
def Total_Ways(n) :
fac = 1 ;
for i in range ( 2 , n - 1 ) :
fac = fac * i;
return (fac * 2 );
if __name__ = = "__main__" :
n = 5 ;
print (Total_Ways(n));
|
C#
using System;
class GFG
{
static int Total_Ways( int n)
{
int fac = 1;
for ( int i = 2; i <= n - 2; i++)
{
fac = fac * i;
}
return (fac * 2);
}
static public void Main ()
{
int n = 5;
Console.Write(Total_Ways(n));
}
}
|
Javascript
<script>
function Total_Ways(n)
{
var fac = 1;
for (i = 2; i <= n - 2; i++)
{
fac = fac * i;
}
return (fac * 2);
}
var n = 5;
document.write(Total_Ways(n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...