Prime #### Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime #### Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

# 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.

## 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

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

• Atishay

public static void main(String[]args){
// int[] arrange = new int;
//
// 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;
} 0
• 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;
} 0
• HelpPrepInsta

Thanks Atishay, for contributing your code 0
• 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));
}

} 0
• HelpPrepInsta

Thanks Prince, for contributing your code 0
• 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) 0
• HelpPrepInsta

Thanks Nikhil for contributing your code 0
• 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]; N = A[k]
if N % R != 0:
Last_table = ((N // R) + (N % R))
R = Last_table
for i in range(1, Last_table):
N *= ((A[k]) – i)
R *= (Last_table – i)
temp = N / R
Ans.append(int(temp / ((A[k]) – 1)))
elif N % R == 0:
R = N // R
for j in range(1, R):
N *= ((A[k]) – j)
R *= (2 – j)
temp1 = N / R
Ans.append(int(temp1 / (A[k])))
for _ in range(len(Ans)):
print(Ans[_], end=’ ‘) 1
• HelpPrepInsta

Thanks for Contributing the code SUkesh 0
• 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];
}
} 1
• HelpPrepInsta

Thanks for contributing the code kiruba, but there is a small error in the code 0