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

img1 :Seating Arrangement

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.

Explanation:

Basically what the question says is
1. There are ‘N’ number of seats in a train.
2. There are 3 types of seats in the train, that are Window seat(WS), Middle seat(MS) and Aisle seat(AS).
3. You are given a seat number.
4. Now you have to find the type of that seat and the seat number of the seat facing it.


Inputs and Outputs:

The inputs are
1. ‘T’ : Number of testcases.
2. For each test cases there will be

  • 1 integer input (N) ,which is the given seat number.
  • The output is type of that seat and the seat number of the seat facing it.


    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();
            }   
        }

    Step2 : Now We have to define two methods. One for getting the type of seat and another for getting the number of seat facing the given input seat.

    1. Getting Seat type:

    The method will take only the given seat number as it's parameter.

       public static String getSeat(int num)

    Looking at the picture of train we can say that in the left side of the train the 3 seats are Window seat, middle seat and aisle seat respectively. Similarly in the right side of the train the 3 seats are aisle seat, middle seat and Window seat respectively. That means after every 3 seat the sequence of the seat type is alternating. So we can define a boolean variable which will change it's value after every 3 seats. Let for the first 3 seats it's value is true that means we are on the right side of the train. similarly for the next 3 seats it's value will be false that means we are on the right side of the train. According to it's value we can choose one of the sequences i.e. (WS-MS-AS or AS-MS-WS).

    Now we know that there are 108 number of seat. To check in which side the given seat lies we have to alternate the boolean value whenever the iterator reaches the first seat of 3 sets of seats i.e. (1,4,7..etc). we can do that by:

      
    boolean check = false;   
    for(int i = 1; i < 109; i++){
        if((i-1) % 3 == 0){
            check = !check;
        }
    }
    

    Now if the value of the boolean variable is true, then the seat is in the left , else it's in the right. We have to stop this iteration whenever the iterator "i" will be equal to the given seat Number and then we can check the value of the boolean variable if it's true or false

    As we know if it's true then the seat is in the left side and it follows the "WS-MS-AS" sequence. That means the first seat of the set of 3 is Window seat ,2nd is Middle seat and 3rd is Aisle seat. Now we just have to find out whether the given seat is 1st seat(WS) or 2nd seat(MS) or 3rd seat(AS). To do that we have to perform modulos by 3 operation with the given seat number. like if the seat number is 7, then 7 % 3 = 1 i.e it's the first seat.

    
            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";
                        }
                    }
             }
    

    Now if the value of the boolean variable is false, then the seat is in the right , we have to do the exactly same thing but this time the sequence will be "AS-MS-WS"(opposite of previous). that means if it's the first seat then it's a Aisle seat, if it's a second seat it's a Middle seat, if it's a third seat then it's a windows seat. and the first part of the output is over.

    
            if(num == i){
                    // '!' is the symbol of logical NOT i.e if check is false then !check will be true
                    if(!check){
                        if(num % 3 == 1){
                            return "WS";
                        }
                        else if(num % 3 == 2){
                            return "MS";
                        }
                        else if(num % 3 == 0){
                            return "AS";
                        }
                    }
             }
    

    Combined code of this method is given below :

    
        public static String getSeat(int num){
            boolean check = false;
            String out = "";
            for(int i = 1; i < 109; 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 "";
        }
    }
    
    

    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.

    Now look at their difference
    (1,12) - > 11 (12 - (1) - (1-1)) = > (12 -(-1) -2) = > (13 - 2) = > (13 - 2 * 1)
    (2,11) - > 9 (12 - (2) - (2-1)) = > (12 -(-1) -4) = > (13 - 4) = > (13 - 2 * 2)
    (3,10) - > 7 (12 - (3) - (3-1)) = > (12 -(-1) -6) = > (13 - 6) = > (13 - 2 * 3)
    (4,9) - > 5 (12 - (4) - (4-1)) = > (12 -(-1) -8) = > (13 - 8) = > (13 - 2 * 4)
    (5,8) - > 3 (12 - (5) - (5-1)) = > (12 -(-1) -10) = > (13 - 10) = > (13 - 2 * 5)
    (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;   
        }
    

    Step3 : Now the only thing left is getting the output and printing it.

    This can be done by calling the two functions and converting the integer value to string and then by using string addition to merge the two values and then print it.

    
    System.out.println(Integer.toString(front_seat(N)) + " " + getSeat(N));
    

    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 "";
        }
    }
    

    Leave a Reply

    Your email address will not be published. Required fields are marked *