Train Partner — ANKTRAIN

Ashish Patel
Codebrace
Published in
1 min readDec 6, 2016

Problem link. Submit here

Solution:

All we have to do is to find the train partner where seats are following the pattern given in question:

3UB   |  6UB  
2MB | 5MB
1LB | 4LB
7SL | 8SU

and the pattern is repeated for every set of 8 berths.

we will only use modulus operator to find out the train partner of the given seat number.

let say partner have seat number is r then for given seat number n :

if (n%8==0) means it is the last seat of a compartment (i.e. "n SU")  so r=(n-1)SLsimilarly if(n%8=7) then r=(n+1)SUif(n%8=1) then r=(n+3)LBif(n%8=2) then r=(n+3)MBif(n%8=3) then r=(n+3)UBif(n%8=4) then r=(n-3)LBif(n%8=5) then r=(n-3)MBif(n%8=6) then r=(n-3)UB

This is valid for every compartment in the train as same pattern will be followed there.
Problem's code is very simple (we can use switch-case to make it more clear and readable ):Code-C++#include <stdio.h>
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
switch(n%8)
{
case 0:printf("%dSL\n",n-1);
break;
case 7:printf("%dSU\n",n+1);
break;
case 1:printf("%dLB\n",n+3);
break;
case 2:printf("%dMB\n",n+3);
break;
case 3:printf("%dUB\n",n+3);
break;
case 4:printf("%dLB\n",n-3);
break;
case 5:printf("%dMB\n",n-3);
break;
case 6:printf("%dUB\n",n-3);
break;
}
}
return 0;
}


#Happy_Coding #Codebrace #A_U_V
If you find something wrong or have any question about the solution then comment down below, you can also contact us using contact form

--

--

Ashish Patel
Codebrace

Big Data Engineer at Skyscanner , loves Competitive programming, Big Data.