The post Competitive Coding : Seating Arrangement appeared first on EbooksUniverse.

]]>

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.**

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

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

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.

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

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));
```

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

The post Competitive Coding : Seating Arrangement appeared first on EbooksUniverse.

]]>The post Competitive Coding : Cost of balloons appeared first on EbooksUniverse.

]]>

You are conducting a contest at your college. This contest consists of two problems and n participants. You know the problem that a candidate will solve during the contest.

You provide a balloon to a participant after he or she solves a problem. There are only green and purple-colored balloons available in a market. Each problem must have a balloon associated with it as a prize for solving that specific problem. You can distribute balloons to each participant by performing the following operation:

1. Use green-colored balloons for the first problem and purple-colored balloons for the second problem

2. Use purple-colored balloons for the first problem and green-colored balloons for the second problem

You are given the cost of each balloon and problems that each participant solve. Your task is to print the minimum price that you have to pay while purchasing balloons.

Input format

• First line: T that denotes the number of test cases (1≤T≤10)

• For each test case:

o First line: Cost of green and purple-colored balloons

o Second line: n that denotes the number of participants (1≤n≤10)

• Next n lines: Contain the status of users. For example, if the value of the jth integer in the ith row is 0, then it depicts that the ith participant has not solved the jth problem. Similarly, if the value of the jth integer in the ith row is 1, then it depicts that the ith participant has solved the jth problem.

Output format

For each test case, print the minimum cost that you have to pay to purchase balloons.

**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.**

>

We have to find the minimum cost. The selection of prizes can be done in 2 possible ways i.e

We can do that by calling the method 2 times by alternating the prices of 2 balloons(2nd and 3rd parameters of the method) and then by comparing the two values we can get the minimum cost and print that value.

```
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 k = 0; k < T ; k++ ){
int green_cost = sc.nextInt();
int purple_cost = sc.nextInt();
int n = sc.nextInt();
int[][] solve = new int[n][2];
for(int i = 0; i < n; i++){
for(int j = 0; j < 2; j++){
solve[i][j] = sc.nextInt();
}
}
int amt1 = get_amt(solve,green_cost,purple_cost,n);
int amt2 = get_amt(solve,purple_cost,green_cost,n);
if(amt1 < amt2){
System.out.println(amt1);
}
else{
System.out.println(amt2);
}
}
}
public static int get_amt(int[][] arr, int p1, int p2,int n){
int amt = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < 2; j++){
if(j == 0){
amt += (arr[i][j] * p1);
}
else{
amt += (arr[i][j] * p2);
}
}
}
return amt;
}
}
```

The post Competitive Coding : Cost of balloons appeared first on EbooksUniverse.

]]>The post Android Screen Wired Projection and Control. appeared first on EbooksUniverse.

]]>

The post Android Screen Wired Projection and Control. appeared first on EbooksUniverse.

]]>