Friday, June 24, 2022
HomeSoftware DevelopmentDepend preparations of N individuals round round desk such that Okay individuals...

Depend preparations of N individuals round round desk such that Okay individuals all the time sit collectively


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. 

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

Time Complexity: O(N)
Auxiliary Space: O(1)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments