Train Partner Codechef Solution

Hello Programmers In this post, you will know how to solve the Train Partner Codechef Solution. The Problem Code: ANKTRAIN

Train Partner Codechef Solution
Train Partner Codechef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.


Rahul and Rashi are off to the wedding of a close relative. This time they have to travel without their guardians. Rahul got very interested in the arrangement of seats inside the train coach.

The entire coach could be viewed as an arrangement of consecutive blocks of size 8.

Berth Number   	Compartment
 1 -  8               1
 9 - 16               2
17 - 24               3
... and so on

Each of these size-8 blocks are further arranged as:

 1LB,  2MB,  3UB,  4LB,  5MB,  6UB,  7SL,  8SU
 9LB, 10MB, ...

Here LB denotes lower berth, MB middle berth and UB upper berth.

The following berths are called Train-Partners:

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

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

Rahul and Rashi are playing this game of finding the train partner of each berth. Can you write a program to do the same?


The first line of input contains a single integer T, denoting the number of test cases to follow.

Each of the next T lines contain a single integer N, the berth number whose neighbor is to be found out.


The output should contain exactly T lines each containing the berth of the neighbor of the corresponding seat.



Subtask #1 (50 points):

  • 1 ≤ T ≤ 8
  • 1 ≤ N ≤ 8

Subtask #2 (50 points):

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 500

Sample Input 1 


Sample Output 1 


Train Partner CodeChef Solution in JAVA

import java.util.Scanner;
public class Main {
	static final int[] PARTNER_BERT_NUMBER = { 3, 4, 5, 0, 1, 2, 7, 6 };
	static final String[] PARTNER_BERT_CODE = { "LB", "MB", "UB", "LB", "MB", "UB", "SU", "SL" };
	public static void main(String[] args) {
		Scanner sc = new Scanner(;
		int T = sc.nextInt();
		for (int tc = 0; tc < T; tc++) {
			int N = sc.nextInt();
	static String solve(int N) {
		int compartment = (N - 1) / 8;
		int berthNumber = (N - 1) % 8;
		return String.format("%d%s", compartment * 8 + PARTNER_BERT_NUMBER[berthNumber] + 1,

Disclaimer: The above Problem (Train Partner ) is generated by CodeChef but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: Longest AND Subarray Codechef Solution

Leave a Reply

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