You are on page 1of 9

Problem solving Set III

1. The Block Game


The citizens of Byteland regularly play a game. They have blocks each denoting some integer from 0
to 9. These are arranged together in a random manner without seeing to form different numbers
keeping in mind that the first block is never a 0. Once they form a number they read in the reverse
order to check if the number and its reverse is the same. If both are same then the player wins. We call
such numbers palindrome
Ash happens to see this game and wants to simulate the same in the computer. As the first step he
wants to take an input from the user and check if the number is palindrome and declare if the user wins
or not

Input
The first line of the input contains T, the number of test cases. This is followed by T lines containing an integer N.

Output
For each input output "wins" if the number is a palindrome and "losses" if not.

Constraints
1<=T<=20
1<=N<=10000
Input:
3
331
666
343

Output:
losses
wins
wins

2. Cutting Recipes
The chef has a recipe he wishes to use for his guests, but the recipe will make far more food than he
can serve to the guests. The chef therefore would like to make a reduced version of the recipe which
has the same ratios of ingredients, but makes less food. The chef, however, does not like fractions. The
original recipe contains only whole numbers of ingredients, and the chef wants the reduced recipe to
only contain whole numbers of ingredients as well. Help the chef determine how much of each
ingredient to use in order to make as little food as possible.
Input
Input will begin with an integer T, the number of test cases. Each test case consists of a single line. The line begins with a
positive integer N, the number of ingredients. N integers follow, each indicating the quantity of a particular ingredient that
is used.

Output
For each test case, output exactly N space-separated integers on a line, giving the quantity of each ingredient that the chef
should use in order to make as little food as possible.

Sample Input
3
244
3234
4 3 15 9 6

Sample Output
11
234
1532
Constraints
T≤100
2≤N≤50
All ingredient quantities are between 1 and 1000, inclusive.

3. Mahasena
Kattapa, as you all know was one of the greatest warriors of his time. The kingdom of Maahishmati
had never lost a battle under him (as army-chief), and the reason for that was their really powerful
army, also called as Mahasena.
Kattapa was known to be a very superstitious person. He believed that a soldier is "lucky" if the soldier
is holding an even number of weapons, and "unlucky" otherwise. He considered the army as
"READY FOR BATTLE" if the count of "lucky" soldiers is strictly greater than the count of "unlucky"
soldiers, and "NOT READY" otherwise.
Given the number of weapons each soldier is holding, your task is to determine whether the army
formed by all these soldiers is "READY FOR BATTLE" or "NOT READY".
Note: You can find the definition of an even number here.

Input
The first line of input consists of a single integer N denoting the number of soldiers. The second line of
input consists of N space separated integers A1, A2, ..., AN, where Ai denotes the number of weapons that
the ith soldier is holding.

Output
Generate one line output saying "READY FOR BATTLE", if the army satisfies the conditions that Kattapa
requires or "NOT READY" otherwise (quotes for clarity).

Constraints
 1 ≤ N ≤ 100
 1 ≤ Ai ≤ 100

Example 1
Input:
1
1

Output:
NOT READY

Example 2
Input:
1
2

Output:
READY FOR BATTLE

Example 3
Input:
4
11 12 13 14

Output:
NOT READY

Example 4
Input:
3
234

Output:
READY FOR BATTLE

Example 5
Input:
5
12345

Output:
NOT READY

Explanation
 Example 1: For the first example, N = 1 and the array A = [1]. There is only 1 soldier and he is holding
1 weapon, which is odd. The number of soldiers holding an even number of weapons = 0, and number
of soldiers holding an odd number of weapons = 1. Hence, the answer is "NOT READY" since the
number of soldiers holding an even number of weapons is not greater than the number of soldiers
holding an odd number of weapons.
 Example 2: For the second example, N = 1 and the array A = [2]. There is only 1 soldier and he is
holding 2 weapons, which is even. The number of soldiers holding an even number of weapons = 1,
and number of soldiers holding an odd number of weapons = 0. Hence, the answer is "READY FOR
BATTLE" since the number of soldiers holding an even number of weapons is greater than the number
of soldiers holding an odd number of weapons.
 Example 3: For the third example, N = 4 and the array A = [11, 12, 13, 14]. The 1st soldier is holding
11 weapons (which is odd), the 2nd soldier is holding 12 weapons (which is even), the 3rd soldier is
holding 13 weapons (which is odd), and the 4th soldier is holding 14 weapons (which is even). The
number of soldiers holding an even number of weapons = 2, and number of soldiers holding an odd
number of weapons = 2. Notice that we have an equal number of people holding even number of
weapons and odd number of weapons. The answer here is "NOT READY" since the number of soldiers
holding an even number of weapons is not strictly greater than the number of soldiers holding an odd
number of weapons.
 Example 4: For the fourth example, N = 3 and the array A = [2, 3, 4]. The 1st soldier is holding 2
weapons (which is even), the 2nd soldier is holding 3 weapons (which is odd), and the 3rd soldier is
holding 4 weapons (which is even). The number of soldiers holding an even number of weapons = 2,
and number of soldiers holding an odd number of weapons = 1. Hence, the answer is "READY FOR
BATTLE" since the number of soldiers holding an even number of weapons is greater than the number
of soldiers holding an odd number of weapons.
 Example 5: For the fifth example, N = 5 and the array A = [1, 2, 3, 4, 5]. The 1st soldier is holding 1
weapon (which is odd), the 2nd soldier is holding 2 weapons (which is even), the 3rd soldier is holding 3
weapons (which is odd), the 4th soldier is holding 4 weapons (which is even), and the 5th soldier is
holding 5 weapons (which is odd). The number of soldiers holding an even number of weapons = 2,
and number of soldiers holding an odd number of weapons = 3. Hence, the answer is "NOT READY"
since the number of soldiers holding an even number of weapons is not greater than the number of
soldiers holding an odd number of weapons.

4. Optimality Test
Alice and Bob are meeting after a long time. As usual they love to play some math games. This times Alice
takes the call and decides the game. The game is very simple, Alice says out an integer and Bob has to say
whether the number is prime or not. Bob as usual knows the logic but since Alice doesn't give Bob much
time to think, so Bob decides to write a computer program.
Help Bob accomplish this task by writing a computer program which will calculate whether the number is
prime or not .

Input
The first line of the input contains T test cases, T lines follow
Each of T line contains an integer N which has to be tested for primality

Output
For each test case output in a separate line, "yes" if the number is prime else "no"

Constraints
1<=T<=20 1<=N<=10000 1<=M<=10000
Input:
5
23
13
20
1000
99991

Output:
yes
yes
no
no
yes

5. Id and Ship
Write a program that takes in a letter class ID of a ship and display the equivalent string class description of
the given ID. Use the table below.
Class ID Ship Class
B or b BattleShip
C or c Cruiser
D or d Destroyer
F or f Frigate

Input
The first line contains an integer T, total number of test cases. Then follow T lines, each line contains a
character.

Output
Display the Ship Class depending on ID.

Constraints
 1 ≤ T ≤ 1000

Example
Input

3
B
c
D

Output
BattleShip
Cruiser
Destroyer
6. Fit Squares in Triangle
What is the maximum number of squares of size 2x2 that can be fit in a right angled isosceles triangle of
base B.
One side of the square must be parallel to the base of the isosceles triangle.
Base is the shortest side of the triangle

Input
First line contains T, the number of test cases.
Each of the following T lines contains 1 integer B.

Output
Output exactly T lines, each line containing the required answer.

Constraints
1 ≤ T ≤ 103
1 ≤ B ≤ 104

Sample Input
11
1
2
3
4
5
6
7
8
9
10
11

Sample Output
0
0
0
1
1
3
3
6
6
10
10

7. The Block Game


The citizens of Byteland regularly play a game. They have blocks each denoting some integer from 0 to 9.
These are arranged together in a random manner without seeing to form different numbers keeping in mind
that the first block is never a 0. Once they form a number they read in the reverse order to check if the
number and its reverse is the same. If both are same then the player wins. We call such
numbers palindrome
Ash happens to see this game and wants to simulate the same in the computer. As the first step he wants to
take an input from the user and check if the number is palindrome and declare if the user wins or not

Input
The first line of the input contains T, the number of test cases. This is followed by T lines containing an
integer N.

Output
For each input output "wins" if the number is a palindrome and "losses" if not.
Constraints
1<=T<=20
1<=N<=10000
Input:
3
331
666
343

Output:
losses
wins
wins

8. Cutting Recipes
The chef has a recipe he wishes to use for his guests, but the recipe will make far more food than he can
serve to the guests. The chef therefore would like to make a reduced version of the recipe which has the
same ratios of ingredients, but makes less food. The chef, however, does not like fractions. The original
recipe contains only whole numbers of ingredients, and the chef wants the reduced recipe to only contain
whole numbers of ingredients as well. Help the chef determine how much of each ingredient to use in order
to make as little food as possible.

Input
Input will begin with an integer T, the number of test cases. Each test case consists of a single line. The line
begins with a positive integer N, the number of ingredients. N integers follow, each indicating the quantity
of a particular ingredient that is used.

Output
For each test case, output exactly N space-separated integers on a line, giving the quantity of each
ingredient that the chef should use in order to make as little food as possible.

Sample Input
3
244
3234
4 3 15 9 6

Sample Output
11
234
1532

Constraints
T≤100
2≤N≤50
All ingredient quantities are between 1 and 1000, inclusive.

9. Transform the Expression


Reverse Polish Notation (RPN) is a mathematical notation where every operator follows all of its operands. For
instance, to add three and four, one would write "3 4 +" rather than "3 + 4". If there are multiple operations, the
operator is given immediately after its second operand; so the expression written "3 − 4 + 5" would be written "3 4 −
5 +" first subtract 4 from 3, then add 5 to that.
Transform the algebraic expression with brackets into RPN form.
You can assume that for the test cases below only single letters will be used, brackets [] will not be used
and each expression has only one RPN form (no expressions like a*b*c)
Input
The first line contains t, the number of test cases (less then 100).
Followed by t lines, containing an expression to be translated to RPN form, where the length of the
expression is less then 400.

Output
The expressions in RPN form, one per line.

Example
Input:
3
(a+(b*c))
((a+b)*(z+x))
((a+t)*((b+(a+c))^(c+d)))

Output:
abc*+
ab+zx+*
at+bac++cd+^*

10. Lapindromes
Lapindrome is defined as a string which when split in the middle, gives two halves having the same
characters and same frequency of each character. If there are odd number of characters in the string, we
ignore the middle character and check for lapindrome. For example gaga is a lapindrome, since the two
halves ga and ga have the same characters with same frequency. Also, abccab, rotor and xyzxy are a few
examples of lapindromes. Note that abbaab is NOT a lapindrome. The two halves contain the same
characters but their frequencies do not match.
Your task is simple. Given a string, you need to tell if it is a lapindrome.

Input:
First line of input contains a single integer T, the number of test cases.
Each test is a single line containing a string S composed of only lowercase English alphabet.

Output:
For each test case, output on a separate line: "YES" if the string is a lapindrome and "NO" if it is not.

Constraints:
 1 ≤ T ≤ 100
 2 ≤ |S| ≤ 1000, where |S| denotes the length of S

Example:
Input:
6
gaga
abcde
rotor
xyzxy
abbaab
ababc

Output:
YES
NO
YES
YES
NO
NO

11. The Smallest Pair


You are given a sequence a1, a2, ..., aN. Find the smallest possible value of ai + aj, where 1 ≤ i < j ≤ N.

Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test
cases follows.
The first line of each description consists of a single integer N.
The second line of each description contains N space separated integers - a1, a2, ..., aN respectively.

Output
For each test case, output a single line containing a single integer - the smallest possible sum for the
corresponding test case.

Constraints
 T = 105, N = 2 : 13 points.
 T = 105, 2 ≤ N ≤ 10 : 16 points.
 T = 1000, 2 ≤ N ≤ 100 : 31 points.
 T = 10, 2 ≤ N ≤ 105 : 40 points.
 1 ≤ ai ≤ 106

Example
Input:
1
4
5134

Output:
4

Explanation
Here we pick a2 and a3. Their sum equals to 1 + 3 = 4.

12. Total Expenses


While purchasing certain items, a discount of 10% is offered if the quantity purchased is more than 1000.
If the quantity and price per item are input, write a program to calculate the total expenses.

Input
The first line contains an integer T, total number of test cases. Then follow T lines, each line contains
integers quantity and price.

Output
Output the total expenses while purchasing items.

Constraints
 1 ≤ T ≤ 1000
 1 ≤ quantity,price ≤ 100000

Example
Input

3
100 120
10 20
1200 20

Output

12000.000000
200.000000
21600.000000

You might also like