Count arrangements of N people around circular table such that K people always sit together
Last Updated :
11 Jun, 2022
Given integers N and K, the task is to find the number of possible arrangements of N people around a circular table such that K people always sit together.
Note: As the answer can be very large return it modulo 109 + 7
Examples:
Input: N = 4, K = 3
Output: 6
Explanation: If 3 people always sit together (say 1, 2 and 3) then the possible arrangements can be
{1, 2, 3, 4}, {1, 3, 2, 4}, {2, 1, 3, 4}, {2, 3, 1, 4}, {3, 2, 1, 4}, {3, 1, 2, 4}.
As there is no start or end in a circular arrangement and
the arrangements are based on relative positions, so we can consider
4 is always fixed in the last position.
Input: N = 8, K = 3
Output: 720
Approach: This problem can be solved based on the following mathematical idea:
In a circular arrangement there is no starting or ending and the arrangements are based on relative positions.
So it can be considered that any one of the person is always sitting in a fixed position and all other positions are relative to that position.
So total possible arrangements of N people around a circular table is (N-1)!
As K people will always sit together, they can be considered a group or as a single unit.
So total unit now X = (N – K + 1). Total possible arrangements of these many people are:
(X – 1)! = (N – K)!
The people of this single group can be arranged in K! ways for each of the possible arrangements.
therefore total possible arrangements = K! * (N – K)!
Follow the below steps to implement the above approach.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
const int mod = 1000000007;
int fac( int f) {
int fac = 1;
for ( int i = 1; i <= f; i++)
fac = (fac * i) % mod;
return fac;
}
int Ways( int n, int k)
{
return (fac(n - k) * fac(k)) % mod;
}
int main() {
int N = 8;
int K = 3;
cout << Ways(N, K);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int mod = 1000000007 ;
static int fac( int f)
{
int fac = 1 ;
for ( int i = 1 ; i <= f; i++)
fac = (fac * i) % mod;
return fac;
}
static int Ways( int n, int k)
{
return (fac(n - k) * fac(k)) % mod;
}
public static void main(String[] args)
{
int N = 8 ;
int K = 3 ;
System.out.println(Ways(N, K));
}
}
|
Python3
mod = 1000000007
def fac(f):
fac = 1
for i in range ( 2 , f + 1 ):
fac = (fac * i) % mod
return fac
def Ways(n, k):
return (fac(n - k) * fac(k)) % mod
if __name__ = = '__main__' :
N = 8
K = 3
print (Ways(N, K));
|
C#
using System;
public class GFG
{
static int mod = 1000000007;
static int fac( int f) {
int fac = 1;
for ( int i = 1; i <= f; i++)
fac = (fac * i) % mod;
return fac;
}
static int Ways( int n, int k)
{
return (fac(n - k) * fac(k)) % mod;
}
static public void Main (){
int N = 8;
int K = 3;
Console.WriteLine(Ways(N, K));
}
}
|
Javascript
<script>
let mod = 1000000007;
function fac(f) {
let fac = 1;
for (let i = 1; i <= f; i++)
fac = (fac * i) % mod;
return fac;
}
function Ways(n, k) {
return (fac(n - k) * fac(k)) % mod;
}
let N = 8;
let K = 3;
document.write(Ways(N, K))
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...