Java Program for Dining Table Seating Arrangement Problem

Java Program for Dining Table Seating Arrangement Problem

Dining Table Seating Arrangement Problem

One of the real-world problems asked in TCS CodeVita where we need to find the impactful seating arrangements for the N numbers of attendees. Here is the Java Program for Dining Table Seating Arrangement Problem, which takes user input R for no of Round tables and N for the total number of attendees.

Problem Statement

In a Conference ,attendees are invited for a dinner after the conference.The Co-ordinator,Sagar arranged around round tables for dinner and want to have an impactful seating experience for the attendees.Before finalizing the seating arrangement,he wants to analyze all the possible arrangements.These are R round tables and N attendees.In case where N is an exact multiple of R,the number of attendees must be exactly N//R,,If N is not an exact multiple of R, then the distribution of attendees must be as equal as possible.Please refer to the example section before for better understanding.
For example, R = 2 and N = 3
All possible seating arrangements are
(1,2) & (3)
(1,3) & (2)
(2,3) & (1)
Attendees are numbered from 1 to N.

Input Format:

  • The first line contains T denoting the number of test cases.
  • Each test case contains two space separated integers R and N, Where R denotes the number of round tables and N denotes the number of attendees.

Output Format:

Single Integer S denoting the number of possible unique arrangements.

Constraints:

  • 0 <= R <= 10(Integer)
  • 0 < N <= 20 (Integer)
Sample Input 1:
1
2 5
Sample Output 1:

10

Explanation:

R = 2, N = 5

(1,2,3) & (4,5)

(1,2,4) & (3,5)

(1,2,5) & (3,4)

(1,3,4) & (2,5)

(1,3,5) & (2,4)

(1,4,5) & (2,3)

(2,3,4) & (1,5)

(2,3,5) & (1,4)

(2,4,5) & (1,3)

(3,4,5) & (1,2)

Arrangements like

(1,2,3) & (4,5)

(2,1,3) & (4,5)

(2,3,1) & (4,5) etc.

But as it is a round table,all the above arrangements are same.

 

Dining Table Seating Arrangement Problem in few other Coding Languages

C

We don’t have the solution for this problem, you can contribute the answer of this code in C programming language, we post that answer on our page

C++

We don’t have the solution for this problem, you can contribute the answer of this code in C++ programming language, we post that answer on our page

Python

To see the Python solution of above problem click on the button below –

 

Python

16 comments on “Java Program for Dining Table Seating Arrangement Problem”


  • Shivansh

    import java.util.*;
    public class SeatingArrangement
    {
    //To find the factorial of the given number
    public static int fact(int n)
    {
    int y = 1;
    if(n==0||n==1)
    return y;
    else if(n>1)
    {
    for(int a = n;a>1;a–)
    y = y*a;
    return y;
    }
    else
    return -1;
    }
    public static void main(String[] args)
    {
    Scanner sc = new Scanner(System.in);
    //Number of test case
    int t = sc.nextInt();
    for(int b = 1;b0&&d=0&&r<=20)
    {
    int out = 1;
    //The combination formula (nCr = n!/r!*(n-r)!)
    out = fact(d)/(fact(r)*fact(d-r));
    System.out.println(out);
    }
    }
    }
    }


    • Shivansh

      Sorry for the little bit of type mistake in my code here is the correct version:
      import java.util.*;
      class main
      {
      //To find the factorial of the given number
      public static int fact(int n)
      {
      int y = 1;
      if(n==0||n==1)
      return y;
      else if(n>1)
      {
      for(int a = n;a>1;a–)
      y = y*a;
      return y;
      }
      else
      return -1;
      }
      public static void main(String[] args)
      {
      Scanner sc = new Scanner(System.in);
      //Number of test case
      int t = sc.nextInt();
      for(int b = 1;b0&&d=0&&r<=20)
      {
      int out = 1;
      //The combination formula (nCr = n!/r!*(n-r)!)
      out = fact(d)/(fact(r)*fact(d-r));
      System.out.println(out);
      }
      }
      }
      }


  • Ripon

    public class SeatingArrangement {

    // finding factorial
    public static long fact(int n) {
    if (n <= 1)
    return 1;
    return n * fact(n – 1);
    }

    public static long seatingArrangement(int R, int N) {
    int i = N / R;
    // finding combination
    long res = (fact(N) / (fact(N – i) * fact(i)));
    return res;
    }

    public static void main(String[] args) {
    System.out.println(seatingArrangement(2, 3));
    System.out.println(seatingArrangement(2, 5));
    }
    }


  • Jay

    import java.util.*;
    public class DiningTable{

    public static void main(String []args){
    Scanner sc= new Scanner(System.in);
    int R= sc.nextInt();
    int N= sc.nextInt();
    int pB= N/R;
    int pA= pB+1;
    int tA= N % R;
    int tB= R-tA;

    int fact[]= new int[N+2];
    fact[0]= 1;
    for(int i=1;i3) {
    ans*= (int)(Math.pow(fact[pA-1]/2,tA) * Math.pow(fact[pB-1]/2,tB));
    }
    System.out.println(ans);
    }
    }


  • Souvik

    /* This is the cpp code*/
    #include
    using namespace std;
    int fact(int n)
    {
    if(n>table;
    cin>>total;
    if(total%table==0)
    res=total/table;
    else
    res=total/table+1;
    while(total>0)
    {
    result=result*combination(total,res);
    //cout<<result<<" ";
    total-=res;
    table–;
    if(total%table==0)
    res=total/table;
    else
    res=total/table+1;
    if(total<=res)
    {
    total=0;
    }
    }
    cout<<result;
    return 0;
    }


  • Atishay

    public static void main(String[]args){
    // int[] arrange = new int[5];
    //
    // for(int i=0;i ” + Arrays.toString(arrange));
    // System.out.println(3/2);

    // int test = 0;
    // int R=0;
    // int N = 0;
    // int num =0;
    // int dum1=0;
    // int dum2=0;
    // int dum =0;
    // int ans = 0;
    // int t=0;
    // Scanner sc= new Scanner(System.in);
    // t = sc.nextInt();
    // R = sc.nextInt();
    // N = sc.nextInt();
    //
    // for(test = 0; test0; r– )
    // {
    // r= r*(r–);
    // num = r;
    // }
    //
    // for (int n = N; n>0; n–)
    // {
    // n= n*(n–);
    // dum1 = n;
    // }
    // for(int i =a; i>0;i–)
    // {
    // i= i*(i–);
    // dum2 = i;
    //
    // }
    // dum = dum1*dum2;
    // ans = num/dum;
    //
    // }
    int test, attenedees, table, division, possibleCases = 0;
    System.out.println(“Enter no. of test cases : “);
    Scanner scanner = new Scanner(System.in);
    test = scanner.nextInt();
    for(int i = 1; i ” + table);
    } else if(attenedees%table==0) {
    System.out.println(“Possible cases -> ” + attenedees/table);
    }else{
    possibleCases = (fact(attenedees) / (fact(division) * fact(attenedees – division))) * (fact(division) / fact(division));
    System.out.println(“Possible cases -> ” + possibleCases);
    }
    }
    }
    public static int fact(int n){
    int fact = 1;
    if(n==0)
    return fact = 1;
    for(int i = 1; i<=n; ++i){
    fact *=i;
    }
    return fact;
    }


    • Atishay

      public static void main(String[]args){
      int test, attenedees, table, division, possibleCases = 0;
      System.out.println(“Enter no. of test cases : “);
      Scanner scanner = new Scanner(System.in);
      test = scanner.nextInt();
      for(int i = 1; i ” + table);
      } else if(attenedees%table==0) {
      System.out.println(“Possible cases -> ” + attenedees/table);
      }else{
      possibleCases = (fact(attenedees) / (fact(division) * fact(attenedees – division))) * (fact(division) / fact(division));
      System.out.println(“Possible cases -> ” + possibleCases);
      }
      }
      }
      public static int fact(int n){
      int fact = 1;
      if(n==0)
      return fact = 1;
      for(int i = 1; i<=n; ++i){
      fact *=i;
      }
      return fact;
      }


  • prince

    public class Dining {

    public static void main(String[] args) {
    System.out.println(find(2,3));
    }
    public static int find(int r,int n)
    {
    int x = n/r;
    int y1 = n%r;
    int x1 = 0;
    int ans1 = 1;
    while(r!=0)
    {
    if(y1>0)
    {
    x1 = x+1;
    y1 = y1-1;
    }
    else
    {
    x1 = x;
    }
    ans1 = ans1*combination(n,x1);
    n = n-x1;
    r–;
    }
    return ans1;
    }
    public static int factorial(int n)
    {
    if(n==0||n==1)
    {
    return 1;
    }
    return n * factorial(n-1);
    }
    public static int combination(int n,int r)
    {
    return factorial(n)/(factorial(n-r)*factorial(r));
    }

    }


  • nikhil

    from itertools import combinations

    test = int(input())
    for te in range(test):
    r,n=map(int,input().split())
    s1=n//r
    s2=s1+1
    ar=[]
    for x in range(n):
    ar.append(x)
    com1=list(combinations(ar,s1))
    com2=list(combinations(ar,s2))
    com=com1+com2
    f=[]
    for x in com :
    l=list(x)
    l.sort()
    if l not in f:
    f.append(l)
    print(len(f)//2)


  • Sukesh

    #Hey there! I’m tried this program in python
    Test_case = int(input())
    A = [list(map(int, input().split())) for __ in range(Test_case)]
    Ans = []
    for k in range(Test_case):
    R = A[k][0]; N = A[k][1]
    if N % R != 0:
    Last_table = ((N // R) + (N % R))
    R = Last_table
    for i in range(1, Last_table):
    N *= ((A[k][1]) – i)
    R *= (Last_table – i)
    temp = N / R
    Ans.append(int(temp / ((A[k][0]) – 1)))
    elif N % R == 0:
    R = N // R
    for j in range(1, R):
    N *= ((A[k][1]) – j)
    R *= (2 – j)
    temp1 = N / R
    Ans.append(int(temp1 / (A[k][0])))
    for _ in range(len(Ans)):
    print(Ans[_], end=’ ‘)


  • KiRUBANANTHAM

    import java.util.Scanner;
    class DiningTable
    {
    public static void main(String[] args)
    {
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    int p = sc.nextInt();
    int[] arrange = new int[t];
    int q = p/t;
    int r = p%t;

    for(int i=0;i<r;i++)
    {
    arrange[i]++;
    }
    for(int i=0;i<t;i++)
    {
    arrange[i] = arrange[i]+q;

    }

    long comb = 1;int temp = p;
    for(int i=0;i<t;i++)
    {

    comb = comb * ncr(temp,arrange[i]);

    temp = temp-arrange[i];
    }
    System.out.println(comb);
    }
    public static long ncr(int n,int r)
    {
    if(n==r || r==0)
    return 1;
    else if(r==1)
    return n;
    else
    {
    long res = fact(n)/fact(r);
    res = res / fact(n-r);
    return res;
    }
    }
    public static long fact(int f)
    {
    long[] fact = new long[]{1l,1l,2l,6l,24l,120l,720l,5040l,40320l,362880l,3628800l,
    39916800l,479001600l,6227020800l,87178291200l,
    1307674368000l,20922789888000l,355687428096000l,
    6402373705728000l,121645100408832000l,2432902008176640000l};

    return fact[f];
    }
    }