In this post, we will see the solution of the Python Coding Question which is earlier asked in the TCS NQT Exam.
Problem Statement: An international conference will be held in India. Presidents from all over the world representing their respective countries will be attending the conference. The task is to find the number of ways possible(P) to make the N members sit around the circular table such that the president of India and prime minister of India will always sit next to each other.
Example 1: Input: 5 -> Value of N (Number of members) Output: 48 -> Possible ways of seating the members Explanation: 2 members should always sit together i.e. sit next to each other. So, 2 members can be in 2! (2 factorial) ways Rest of the members can be arranged in (5-1)! (4 factorial) ways. (1 is subtracted because the earlier selected two members will be considered as single members now). So total possible ways 5 members can be seated around the circular table 2*24= 48. Hence, output is 48. Example 2: Input: 6 -> Value of N (Number of members) Output: 240 -> Possible ways of seating the members Explanation: 2 members should always sit together i.e. sit next to each other. So, 2 members can be in 2! (2 factorial) ways Rest of the members can be arranged in (6-1)! (5 factorial) Ways. (1 is subtracted because the earlier selected two members will be considered as a single member now). So, total possible ways 6 members can be seated around a circular table is 2*120 = 240 ways. Hence, output is 240. The input format for testing: The candidate has to write the code to accept one input First input: Accept value of number of N (Positive integer) The output format for testing: The output should be a positive integer or print the message (if any) given in the problem statement (Check the output in example 1, example2) Constraints: 2<=N<=50
Solution:
# define a function to find the # factorial of a positive integral number def factorial(n): fact = 1 # iterating from n to 2 for i in range(n, 1, -1): fact *= i return fact # taking inputs N = int(input()) # calculating num of ways to # arrange on a circular table num_ways = factorial(N-1) * 2 print(num_ways)
Output:
6 240
Time Complexity: O(N)
Space Complexity: O(1)