Open In App

Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides

Improve
Improve
Like Article
Like
Save
Share
Report

Given three integers N, X and Y. The task is to find the number of ways to arrange 2*N persons along two sides of a table with N number of chairs on each side such that X persons are on one side and Y persons are on the opposite side. 
Note: Both X and Y are less than or equals to N.
Examples: 

Input : N = 5, X = 4, Y = 2 
Output : 57600 
Explanation : 
The total number of person 10. X men on one side and Y on other side, then 10 – 4 – 2 = 4 persons are left. We can choose 5 – 4 = 1 of them on one side in 4\choose 1               ways and the remaining persons will automatically sit on the other side. On each side arrangement is done in 5! ways. The number of ways is 4\choose 1               .5!5!

Input : N = 3, X = 1, Y = 2 
Output : 108 


Approach : 
The total number of person 2*N. Let call both the sides as A and B. X men on side A and Y on side B, then 2*N – X – Y persons are left. We can choose N-X of them for side A in 2*N - X - Y\choose N-X               ways and the remaining persons will automatically sit on the other side B. On each side arrangement is done in N! ways. The number of ways to arrange 2*N persons along two sides of a table is 2*N - X - Y\choose N-X               .N!N!
Below is the implementation of the above approach : 
 

C++

#include <bits/stdc++.h>
using namespace std;
 
// Function to find factorial of a number
int factorial(int n)
{
    if (n <= 1)
        return 1;
    return n * factorial(n - 1);
}
 
// Function to find nCr
int nCr(int n, int r)
{
    return factorial(n) / (factorial(n - r) * factorial(r));
}
 
 
// Function to find the number of ways to arrange 2*N persons
int NumberOfWays(int n, int x, int y)
{
    return nCr(2*n-x-y, n-x) * factorial(n) * factorial(n);
}
 
 
// Driver code
int main()
{
    int n = 5, x = 4, y = 2;
     
    // Function call
    cout << NumberOfWays(n, x, y);
     
    return 0;
}

                    

Java

// Java implementation for the above approach
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG
{
     
    // Function to returns factorial of n
    static int factorial(int n)
    {
        if (n <= 1)
            return 1;
        return n * factorial(n - 1);
    }
     
    // Function to find nCr
    static int nCr(int n, int r)
    {
        return factorial(n) / (factorial(n - r) *
                               factorial(r));
    }
     
    // Function to find the number of ways
    // to arrange 2*N persons
    static int NumberOfWays(int n, int x, int y)
    {
        return nCr(2 * n - x - y, n - x) *
               factorial(n) * factorial(n);
    }
     
    // Driver code
    public static void main (String[] args)
                  throws java.lang.Exception
    {
        int n = 5, x = 4, y = 2;
         
        // Function call
        System.out.println(NumberOfWays(n, x, y));        
    }
}
 
// This code is contributed by Nidhiva

                    

Python3

# Python3 implementation for the above approach
 
# Function to find factorial of a number
def factorial(n):
 
    if (n <= 1):
        return 1;
    return n * factorial(n - 1);
 
# Function to find nCr
def nCr(n, r):
 
    return (factorial(n) /
           (factorial(n - r) * factorial(r)));
 
# Function to find the number of ways
# to arrange 2*N persons
def NumberOfWays(n, x, y):
 
    return (nCr(2 * n - x - y, n - x) *
            factorial(n) * factorial(n));
 
# Driver code
n, x, y = 5, 4, 2;
 
# Function call
print(int(NumberOfWays(n, x, y)));
 
# This code is contributed by PrinciRaj1992

                    

C#

// C# implementation for the above approach
using System;
     
class GFG
{
     
    // Function to returns factorial of n
    static int factorial(int n)
    {
        if (n <= 1)
            return 1;
        return n * factorial(n - 1);
    }
     
    // Function to find nCr
    static int nCr(int n, int r)
    {
        return factorial(n) / (factorial(n - r) *
                               factorial(r));
    }
     
    // Function to find the number of ways
    // to arrange 2*N persons
    static int NumberOfWays(int n, int x, int y)
    {
        return nCr(2 * n - x - y, n - x) *
            factorial(n) * factorial(n);
    }
     
    // Driver code
    public static void Main(String[] args)
    {
        int n = 5, x = 4, y = 2;
         
        // Function call
        Console.WriteLine(NumberOfWays(n, x, y));        
    }
}
 
// This code is contributed by Princi Singh

                    

PHP

<?php
// PHP implementation for the above approach
 
// Function to find factorial of a number
function factorial($n)
{
    if ($n <= 1)
        return 1;
    return $n * factorial($n - 1);
}
 
// Function to find nCr
function nCr($n, $r)
{
    return factorial($n) / (factorial($n - $r) *
                            factorial($r));
}
 
// Function to find the number of ways
// to arrange 2*N persons
function NumberOfWays($n, $x, $y)
{
    return nCr(2 * $n - $x - $y, $n - $x) *
           factorial($n) * factorial($n);
}
 
// Driver code
$n = 5;
$x = 4;
$y = 2;
 
// Function call
echo (NumberOfWays($n, $x, $y));
 
// This code is contributed by Naman_garg.
?>

                    

Javascript

<script>
 
// JavaScript implementation for the above approach
// Function to returns factorial of n
    function factorial(n) {
        if (n <= 1)
            return 1;
        return n * factorial(n - 1);
    }
 
    // Function to find nCr
    function nCr(n , r) {
        return factorial(n) / (factorial(n - r) * factorial(r));
    }
 
    // Function to find the number of ways
    // to arrange 2*N persons
    function NumberOfWays(n , x , y) {
        return nCr(2 * n - x - y, n - x) * factorial(n) * factorial(n);
    }
 
    // Driver code
     
        var n = 5, x = 4, y = 2;
 
        // Function call
        document.write(NumberOfWays(n, x, y));
 
// This code contributed by Rajput-Ji
 
</script>

                    

Output: 
57600

 

Time Complexity: O(N)
Auxiliary Space: O(N), for recursive stack space.

Method – 2 O(1) Space Solution

In the above solution, we implemented the factorial function in a recursive manner if we implement the factorial function in an iterative manner then we don’t need O(n) auxiliary extra stack space so this way solution becomes O(1) space complexity solution
Below is an implementation for the same   :

C++

// C++ code for above mentioned solution
 
#include <bits/stdc++.h>
using namespace std;
 
// Iterative function to find factorial of a number
int factorial(int n) {
    int fact = 1;
    for (int i = 1; i <= n; i++) {
        fact = fact * i;
    }
    return fact;
}
 
// Function to find nCr
int nCr(int n, int r) {
    return factorial(n) / (factorial(n - r) * factorial(r));
}
 
 
// Function to find the number of ways to arrange 2*N persons
int NumberOfWays(int n, int x, int y) {
    return nCr(2 * n - x - y, n - x) * factorial(n) * factorial(n);
}
 
 
// Driver code
int main() {
    int n = 5, x = 4, y = 2;
 
    // Function call
    cout << NumberOfWays(n, x, y);
 
    return 0;
}

                    

Java

// Java code for above mentioned solution
public class Main {
 
  // Iterative function to find factorial of a number
  public static int factorial(int n) {
    int fact = 1;
    for (int i = 1; i <= n; i++) {
      fact = fact * i;
    }
    return fact;
  }
 
  // Function to find nCr
  public static int nCr(int n, int r) {
    return factorial(n) / (factorial(n - r) * factorial(r));
  }
 
  // Function to find the number of ways to arrange 2*N persons
  public static int NumberOfWays(int n, int x, int y) {
    return nCr(2 * n - x - y, n - x) * factorial(n) * factorial(n);
  }
 
  // Driver code
  public static void main(String[] args) {
    int n = 5, x = 4, y = 2;
 
    // Function call
    System.out.println(NumberOfWays(n, x, y));
  }
}

                    

Python3

import math
 
# Function to find factorial of a number
def factorial(n):
    fact = 1
    for i in range(1, n+1):
        fact = fact * i
    return fact
 
# Function to find nCr
def nCr(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))
 
# Function to find the number of ways to arrange 2*N persons
def NumberOfWays(n, x, y):
    return nCr(2*n - x - y, n - x) * factorial(n) * factorial(n)
 
# Driver code
n, x, y = 5, 4, 2
 
# Function call
print(NumberOfWays(n, x, y))

                    

C#

using System;
 
public class Program {
   
  // Iterative function to find factorial of a number
  public static int Factorial(int n) {
    int fact = 1;
    for (int i = 1; i <= n; i++) {
      fact = fact * i;
    }
    return fact;
  }
 
  // Function to find nCr
  public static int nCr(int n, int r) {
    return Factorial(n) / (Factorial(n - r) * Factorial(r));
  }
 
  // Function to find the number of ways to arrange 2*N persons
  public static int NumberOfWays(int n, int x, int y) {
    return nCr(2 * n - x - y, n - x) * Factorial(n) * Factorial(n);
  }
 
  // Driver code
  public static void Main() {
    int n = 5, x = 4, y = 2;
 
    // Function call
    Console.WriteLine(NumberOfWays(n, x, y));
  }
}
 
// This code is contributed by divyansh2212

                    

Javascript

//GFG
// Recursive function to find factorial of a number
function factorial(n) {
    let fact = 1;
    for (let i = 1; i <= n; i++) {
        fact = fact * i;
    }
    return fact;
}
 
// Function to find nCr
function nCr(n, r) {
  return factorial(n) / (factorial(n - r) * factorial(r));
}
 
// Function to find the number of ways to arrange 2*N persons
function numberOfWays(n, x, y) {
  return nCr(2 * n - x - y, n - x) * factorial(n) * factorial(n);
}
 
// Driver code
const n = 5, x = 4, y = 2;
 
// Function call
console.log(numberOfWays(n, x, y));
// This code is contributed by Sundaram

                    

Output
57600

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



Last Updated : 06 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads