Berth Of Railway Seat Number

Easy
0/40
Average time to solve is 15m
profile
Contributed by
2 upvotes
Asked in company
Josh Technology Group

Problem statement

You are given a railway seat number as an integer, your task is to check whether it is a valid seat number or not. Also print its berth type i.e lower berth, middle berth, upper berth, side lower berth, side upper berth.

Example Of Train Berth Coach

Berth Coach

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first and the only line of input of each test case contains an integer, as described in the task.
Output format :
For each test case, the first and the only line of output prints the Birth type if valid otherwise prints "Invalid".
Birth types if valid are "Lower", "Middle", "Upper", "Side Lower" and "Side Upper".
Note:
The output of every test case is printed in a separate line.
You are not required to print anything explicitly. It has already been taken care of.
Constraints:
1 <= T <= 20
1 <= N <= 100

where N is the input berth seat number.
Sample Input 1 :
2
10
7
Sample Output 1 :
Middle
Side Lower
Explanation Of the Sample Input 1 :
As in the example figure above given 10 and 7 both are valid seat numbers, as both      
values are between 1 and 72.
10 is in the middle berth of coach and 7 is side lower berth.
Sample Input 2 :
3
100
72
1
Sample Output 2 :
Invalid
Side Upper
Lower
Explanation Of the Sample Input 2 :
As in the example figure above given 72 and 1 both are valid seat numbers, as both      
values are between 1 and 72 (both inclusive).
100 is an invalid seat number .72 is in the     
side upper berth of coach and 1 is lower berth.
Hint

Think in terms of the rotatory (modulus) property of numbers.

Approaches (1)
Berth of railway seat number

Check if seat number is valid seat number or not(i.e in range of 1 to 72).

  • If (seat_number % 8) equals 1 or 4, then berth is a lower berth
  • If (seat_number % 8) equals 2 or 5, then berth is a middle berth
  • If (seat_number % 8) equals 3 or 6, then berth is an upper berth
  • If (seat_number % 8) equals 7, then berth is a side lower berth
  • If (seat_number % 8) equals 0 then berth is a side upper berth
Time Complexity

O(1).

As you only need to check the number returned while mod with 8.  

Space Complexity

 O(1).

Only constant space required for variables.

Code Solution
(100% EXP penalty)
Berth Of Railway Seat Number
Full screen
Console