# Competitive Coding : Seating Arrangement October 18, 2020 by shekhar

### Problem Statement:

Akash and Vishal are quite fond of travelling. They mostly travel by railways. They were travelling in a train one day and they got interested in the seating arrangement of their compartment. The compartment looked something like So they got interested to know the seat number facing them and the seat type facing them. The seats are denoted as follows :

Window Seat : WS
Middle Seat : MS
Aisle Seat : AS

You will be given a seat number, find out the seat number facing you and the seat type, i.e. WS, MS or AS.

INPUT
First line of input will consist of a single integer T denoting number of test-cases. Each test-case consists of a single integer N denoting the seat-number.

OUTPUT
For each test case, print the facing seat-number and the seat-type, separated by a single space in a new line.

CONSTRAINTS
1<=T<=105 1<=N<=108
If you don’t need the explanation, just scroll down to the end of this page to get the full code for this question. If you have any doubts, you can ask me in comment section.

### Let’s Code it:

###### Step1 : Take the Inputs(explanation for each lines of code is written as comments)

``````
import java.util.Scanner;
class TestClass {

public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
//T is number of TestCases
int T = sc.nextInt();
//For each test cases we have to take inputs indivisually using for loop.
for(int i = 0; i < T; i++){
//N stores the give seat number input
int N = sc.nextInt();
}
}``````

###### 2. Getting the seat number facing the given seat :

Now it's the pure aptitude part of the question. Here we have to find the seat number facing the give seat. By looking at the diagram we can take some example and deduce the logic. The seat pairs facing each other are (1,12), (2,11), (3,10), (4,9), (5,8), (6,7) etc.

###### (6,7) - > 1 (12 - (6) - (6-1)) = > (12 -(-1) -12) = > (13 - 12) = > (13 - 2 * 6)

From this we have concluded that the difference can be given by : (13 - (2 * (seat no. % 12))) . But now the problem is for modulos(seat number % 12) value of 1 to 6 we have to add the difference and for modulos(seat number % 12) value of 7 to 11 we have to substract the difference to get the seat facing it. for ex. if the seat is "6" , then we have to add the difference "1" with it to get its pair 7, but if the seat number is 7 ,then we have to substract 1 to get 6.

To solve the issue we can use if else condition to check whether the modulos value is less than or greater than 6. If it is less than or equal to 6 then we have to add the difference else substract it from the seat number.

Now we have got another problem that is when we calculate the modulos of all seat numbers divisible by 12 , then it becomes 0 ,which gives us wrong difference value. But look at those seat numbers 12,24,36,48...and their facing seat pairs 1,13,25,37..etc and their difference value is 11 in all the cases (12,1), (24,13) etc.

This problem can be solved by taking a if else condition to check whether the seat number is divisible by 12 or not, if divisible then we have to subtract 11 from seat number to get it's facing seat pair, else we can use the above discussed method. The code for the method is given below

``````
public static int front_seat(int num){
if(num % 12 != 0){
if((num % 12) <= 6 )
return num + Math.abs(13 - (2 * (num % 12)));
else
return num - Math.abs(13 - (2 * (num % 12)));
}
else
return num - 11;
}
``````

###### Full code (Open in Laptop or use Desktop view in Smartphone) :

``````import java.util.Scanner;
class TestClass {

public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int i = 0; i < T; i++){
int N = sc.nextInt();
System.out.println(Integer.toString(front_seat(N)) + " " + getSeat(N));
}
}

public static int front_seat(int num){
if(num % 12 != 0){
if((num % 12) <= 6 )
return num + Math.abs(13 - (2 * (num % 12)));
else
return num - Math.abs(13 - (2 * (num % 12)));
}
else
return num - 11;
}

public static String getSeat(int num){
boolean check = false;
String out = "";
for(int i = 0; i < 108; i++){
if((i-1) % 3 == 0){
check = !check;
}
if(num == i){
if(check){
if(num % 3 == 1){
return "WS";
}
else if(num % 3 == 2){
return "MS";
}
else if(num % 3 == 0){
return "AS";
}
}
if(!check){
if(num % 3 == 1){
return "AS";
}
else if(num % 3 == 2){
return "MS";
}
else if(num % 3 == 0){
return "WS";
}
}
}
}
return "";
}
}
``````