Tcs Codevita Season-8 Round-2 Questions
Tcs Codevita Season-8 Round-2 Questions
1.Restaurants
Problem Description
Looking for a place to rent in New York, Codu wants to stay in a place close to as
many restaurants as possible and walk as little as possible to reach them. As he is a
vegetarian, and likes only some types of food, there are only a few restaurants that
meet his criteria.
For convenience he models the roads as rectangular grid roads and plots the
restaurant locations at the intersection of two roads there. To simplify further, he
approximates the distance as the number of blocks between two places. Given the
maximum distance he wants to walk, he wants to find the most optimal location
(from which he can reach the maximum number of restaurants within that distance).
The grid starts at (0,0) at the southwest corner, and goes to (N,0) at the south east
corner, and to (N,M) at the north east corner. If two locations the same (maximum)
number of restaurants he can walk to, he would prefer to have the southernmost
location (lowest Y coordinate). If two locations lie on the same southernmost road
and have the same (maximum) number of restaurants he can walk to, he would
prefer the westernmost location (lowest X coordinate).
Constraints
1 < N,M,K <= 1000
Input Format
First line contains two integers N, M the number of horizontal and vertical blocks
(intervals between roads). The grid is given the coordinates starting with (0,0) on the
lower left corner and (N,M) the upper right corner.
The next line gives the distance he wants to walk (in blocks)
Output
Three integers, first two giving the coordinates of the optimal location and the next
integer giving the number of restaurants reachable from that location.
Test Case
Explanation
Example 1
Input
44
11
12
33
44
24
Output
315
Explanation
Smaller dots (5 nos) denote the location of the 5 restaurants. The larger dot is the
location (3, 1) where Codu would want to stay where he can access 5 restaurants
without walking more than 4 blocks. This also meets his preference of staying in
most south-east corner of the city
2.Circle Game
Problem Description
In a kids game two players have to bring down a small steel ball from circular
obstacles with minimum movement of the circles.
Player Blue gets the first turn to move the innermost circle. He can see the openings
on the current circle from which he is supposed to move the ring and the next
(orange) circle, which is responsibility of his opponent. When a player moves his
circle in a direction, all his circles (of same color) move same degrees in that
direction and all his opponents’ circles move that many degrees in opposite
direction. Since the aim of the game is to have minimum movement of circles to win
the game. Players calculate the degrees they require to move the ring from current
circle and that required by the opponent in the next circle and then make a decision
to move the circle clock wise or anti clock wise.
In each turn, every player has to ensure that the ball falls to the next ring. A player
cannot choose an angle such that the ball remains at the same level.
If advantages of moving clock wise or anti clock wise are same. Player moves the
circle clockwise.
You will be given the number of openings in each circle and you will need to provide
the total movement (in degrees) done by the winner.
Constraints
· Number of openings in each circle are greater than one
Test Case
Explanation
Example 1
Input
23
Output
60
Explanation
Blue player gets the first turn. He can move the circle 90 degrees clock wise or anti
clock wise to make the ball fall. However, if he moves the circle anti clockwise,
orange circle will move 90 degrees clockwise and his opponent will not need to move
the circle. Therefore, he moves the blue circle 90 degrees clockwise.
In the next turn, orange circle would have moved 90 degrees anti clockwise.
Therefore, this player will have to move the circle 60 degrees clockwise. Since he
moved circles 'less', he will win.
Example 2
Input
2346
Output
60
First player will have to move the first circle 90 degrees clockwise. His opponents
second circle openings move by 90 degrees each in anti-clock wise directing.
Therefore, the openings on second circle now are 0 degree 120 degree and 240
degree. Third circle is at 90 180 270 and 0. Therefore, the player has equal
advantage on turning the circle either side so he will turn it in clockwise direction by
60 degree. Now the third circle will be at 60 150 210 and 270 degrees. Fourth at 30
90 150 210 270 330. Moving circle clockwise will make both third and fourth circle
opening at 180 degree
Therefore, the second player will win by moving the circle by 60 degrees
3.Roads Required
Problem Description
President Chew Barka rules over puppy Land. Elections are near, and he has not
done much. The President wants to be reelected again.
He knows some states where he most definitely will lose. He needs to do something
for these people. A state is a group of cities, which is connected with any other city.
Now, commutation is a serious problem for citizens, and there is only one road for
transportation between any two cities in a state. He knows if he connects all cities
with each other in a state by roads, the citizens of state will be very happy and vote
him again.
He sends Sherlock Bones to figure out how many roads they have to build in every
state for gaining the trust of the people. Help Sherlock Bones to find out the count of
required roads.
Constraints
N <= 10 ^ 5
M <= N(N–1)/2
Input Format
N: Number of Cities in the country
Output
Number of roads required to be laid, to win all states
Test Case
Explanation
Example 1
75
12
13
24
56
57
Output
Explanation
There are 7 cities and 5 roads. Cities 1,2,3,4 form one state and cities 5,6,7 forms
another state, thus there are 2 states. State 1 is connected by 3 roads. State 2 is
connected by 2 roads. State 1 requires 3 more roads and State 2 requires 1 more
road so that all the cities in the state are connected.
4.Sub Arrays
Problem Description
Two arrays, of same size, and two sets of low and high indices of each of the array
are provided. The low to high indices of each array creates two sub-arrays. Find the
unique number of elements from both arrays which are not part of both sub-arrays.
Constraints
0 < N <= 5*10^3 (Integer)
Input Format
First line contains a single integer N (the size of arrays)
Next two lines contain N space delimited integers (elements of each array)
where
L1 and H1 denote low and high indices of array 1 i.e sub-part of array 1
L2 and H2 denote low and high indices of array 2 i.e sub-part of array 2
Output
For each query, print the unique number of elements from both arrays which are not
part of both sub-arrays.
Test Case
Explanation
Example 1
Input
10
1 2 3 4 5 6 7 8 9 10
2 3 4 6 7 9 10 12 23 24
1515
Output
Explanation
We can see that there is only one query so we have to get 1st to 5th elements from
the first array, which is 1 2 3 4 5. Similarly, 1st to 5th elements from the second
array, which is 2 3 4 6 7.
So now the remaining elements, which are not a part of sub-arrays are :
6 7 8 9 10 9 10 12 23 24
But we can see that elements 9 10 are appearing twice in the list. So we count them
only once. Also, elements 6 and 7 are a part of sub-array 2, hence they too should be
eliminated from the final count.
8 9 10 12 23 24
So the count of unique number of elements from both arrays which are not part of
both sub-arrays is 6.
Example 2
Input
10
1 2 3 4 5 6 7 8 9 10
6 7 8 9 10 11 12 13 15 12
1369
7 10 2 4
Output
10
Explanation
For 1st query viz. 1 3 6 9 we have to get 1st to 3rd elements from the first array,
which are 1 2 3. Similarly, 6th to 9th elements from the second array, which is 11 12
13 15.
4 5 6 7 8 9 10 6 7 8 9 10 12
But we can see that elements 6 7 8 9 10 are appearing twice in the list. So we count
them only once. Also, element 12 appears in sub-array 2. Hence we have to
eliminate 12 from the list.
4 5 6 7 8 9 10
So the count of unique number of elements from both arrays which are not part of
both sub-arrays is 7.
5.Two Circles
Problem Description
There are 2 circles, of possibly varying sizes, which rotate in clock-wise direction. A is
a point of interest on first circle and B is a point of interest on second circle. Radius
of first circle is always less than or equal to radius of second circle. The two circles
are initially, D distance apart. After rotations, their distance becomes D*. On second
circle, C is a point diametrically opposite to point B. Given this geometry, find angle
between AB and AC, that is angle BAC.
Please note the images are not to scale. They are intended to depict only the
geometry.
Constraints
1 <= R1 <= 10^5
Input Format
First line contains 5 space delimited values denoted by R1 R2 D X1 X2
where,
D is the initial distance between the two circles and is a real number
Output
Angle between AB and AC, that is Angle BAC accurate upto 6 digits after decimal
point. Angle should be in degrees.
Test Case
Explanation
Example 1
Input
1 10 5 4 3
Output
5.436806
Explanation
Position after four 90-degree rotations of circle one and three 90-degree rotations of
circle two
C is the diametrically opposite of B, depicted by dotted line. Find angle between AB
and BC, that is angle BAC
Example 2
Input
2 2800 70 79 12
Output
6.077228
Explanation
6.Cross Words
Problem Description
A crossword puzzle is a square grid with black and blank squares, containing clue
numbers (according to a set of rules) on some of the squares. The puzzle is solved by
obtaining the solutions to a set of clues corresponding to the clue numbers.
The solved puzzle has one letter in each of the blank square, which represent a
sequence of letters (consisting of one or more words in English or occasionally other
languages) running along the rows (called “Across”, or “A”) or along the columns
(called “Down” or “D”). Each numbered square is the beginning of an Across solution
or a Down solution. Some of the across and down solutions will intersect at a blank
square, and if the solutions are consistent, both of them will have the same letter at
the intersecting square.
In this problem, you will be given the specifications of the grid, and the solutions in
some random order. The problem is to number the grid appropriately, and associate
the answers consistently with the clue numbers on the grid, both as Across solutions
and as Down solutions, so that the intersecting blank squares have the same letter in
both solutions.
The clue numbers are given sequentially going row wise (Row 1 first, and then row2
and so on)
It has a blank square to its right, and it has no blank square to its left (it has a black
square to its left, or it is in the first column). This is the beginning of an Across
solution with that number
It has a blank square below it, and no blank square above it (it has a black square
above it or it is in the first row). This is the beginning of a Down solution with that
number
Constraints
5<=N<=15
5<=M<=50
Input Format
The input consists of two parts, the grid part and the solution part
The first line of the grid part consists of a number, N, the size of the grid (the overall
grid is N x N) squares. The next N lines correspond to the N rows of the grid. Each
line is comma separated, and has number of pairs of numbers, the first giving the
position (column) of the beginning of a black square block, and the next giving the
length of the block. If there are no black squares in a row, the pair “0,0” will be
specified. For example, if a line contains “2,3,7,1,14,2”, columns 2,3,4 (a block of 3
starting with 2), 7 (a block of 1 starting with 7) and 14,15 (a block of 2 starting with
14) are black in the corresponding row.
The solution part of the input appears after the grid part. The first line of the solution
part contains M, the number of solutions. The M subsequent lines consist of a
sequence of letters corresponding to a solution for one of the Across and Down clues.
All solutions will be in upper case (Capital letters)
Output
The output is a set of M comma separated lines. Each line corresponds to a solution,
and consists of three parts, the clue number, the letter A or D (corresponding to
Across or Down) and the solution in to that clue (in upper case)
The output must be in increasing clue number order. Ifa clue number has both an
Across and a Down solution, they must come in separate lines, with the Across
solution coming before the Down solution.
Test Case
Explanation
Example 1
Input
5,1
1,1,3,1,5,1
0,0
1,1,3,1,5,1
1,1
EVEN
ACNE
CALVE
PLEAS
EVADE
Output
1,A,ACNE
2,D,CALVE
3,D,EVADE
4,A,PLEAS
5,A,EVEN
Explanation
N is 5, and the disposition of the black squares are given in the next 5 (N) lines. The
grid looks like this
The solutions are fitted to the grid so that they are consistent, and the result is
shown below. Note that this is consistent, because the letter at each intersecting
blank square in the Across solution and the Down solution.
Based on this the output is given in clue number order. 1 Across is ACNE, and hence
the first line of the output is 1,A,ACNE. The same logic gives all the remaining
solutions.
Example 2
Input
1,1
1,1,3,2
0,0
1,1,3,2
0,0
ASIAN
RISEN
FEAR
CLAWS
FALLS
Output
1,A,FEAR
1,D,FALLS
2,D,RISEN
3,A,CLAWS
4,A,ASIAN
Explanation
The output can be easily given from this. Note that clue number 1 has both an Across
solution (FEAR) and a DOWN solution (FALLS). The Across solution must precede the
Down solution in the output.
7.Glass Piece
Problem Description
A square glass sheet of size 'S' cm fell down and broken down into N+1 pieces.
A man collected the broken pieces and he is trying to arrange the pieces to get the
original shape. He found that exactly one piece is missing. To find the exact position
of the missing piece, he noted down all (x,y) coordinates of each broken piece.
Please help him to find the coordinates of the missing piece.
Note 1 : The input order of corners of known glass pieces are in the clockwise
direction starting from the corner having least X value. If more than one corners
having the same least X value, then start from the corner having least X and least Y
value.
Note 2 : The corners of missing glass piece should output in the clockwise direction
starting from the corner having least X value. If more than one corners have same
least X value, then start from the corner having least X and least Y value.
Constraints
1 < S <= 1000.
Input Format
First line contains an integer, S, size of Glass Sheet.
Next N lines contain set of space separated integers indicates the details of N known
glass pieces as follows:
Output
Coordinates of missing piece in (X,Y) format separated by spaces.
Test Case
Explanation
Example 1
Input
400
Explanation
The size of the square glass plate is 400 cm * 400 cm. The glass plate broke into 4
pieces and he got 3 pieces.
Figure. 1
Figure. 2
Figure. 4
Lastly, display the coordinates of missing glass piece in the clockwise direction
starting from the corner (0,250), as output.
8.Home Lighting
Problem Description
You are moving to a new apartment and want to plan your total lighting cost for the
next 2 years which includes the cost of bulb procurement and cost of electricity
consumption. Three types of bulbs are available in the market.
1. Regular bulbs, which cost C1, have a warranty of M1 hours, and have a running
cost of R1 per hour 2. CFL bulbs, which cost C2, warranty M2 hours, running, cost R2
per hour 3. LED bulbs, which cost C3, warranty M3 hours, running cost R3 per hour
C1 < C2 < C3 and R1 > R2 > R3, nothing can be said about M1, M2, and M3. You
have a plan for 1 bulb in the kitchen at H1 hours per day, 2 bulbs per room across 3
rooms at H2, H3, H4 hours per day per room per bulb, 1 bulb for bathroom at H5
hours per day. When you prepare your budget, assume that it will last only until the
warranty period. Given the above requirements, come up with a bulb procurement
plan that incurs least cost over 2 years - where cost includes procurement and
running cost. In case warranty expires, in your model assume that the bulb will need
to be replaced. Also, assume that you will move out after 2 years, and so you are not
concerned about stretching the life of bulbs beyond that time. Assume that there are
365 days in each year.
Constraints
C1 < C2 < C3
R1 > R2 > R3
0<= Warranty<=30000
0<= H1,H2,H3,H4,H5<=24
Input Format
The first three lines contains three integers denoting Fixed cost, Warranty and
running cost of regular bulb, CFL and LED respectively.
The next five lines contain one integer each denoting usage hours of rooms (H1, H2,
H3, H4, H5)
Output
Minimum cost to light the house.
Test Case
Explanation
Example 1
Input
50 1500 16
100 8000 14
400 25000 1
14
Output
38500
Explanation
Minimum cost for meeting all the lighting requirement of house is 38500.
Example 2
Input
50 10000 16
100 8000 14
800 25000 1
10
Output
41960
Explanation
Minimum cost for meeting all the lighting requirement of house is 41960.