0% found this document useful (0 votes)
59 views

TCS - CodeVita - Coding Arena

Uploaded by

Yogesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

TCS - CodeVita - Coding Arena

Uploaded by

Yogesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

7/28/2017 TCS: CodeVita - Coding Arena

TCS NextStep TCS Careers www.tcs.com Logout

Welcome DEASIONE S Home Coding Arena Compile & Run Submissions Graphs Feedback

Time Left

Coding Arena 05 57 08
Change Default Language hr min sec

A
B
C
D
E
F

Rules & Regulations


Problem : Mountain peak sequence
Launch Code Editor
Consider the first three natural numbers 1, 2, 3. These can be arranged in the following ways: 2, 3, 1 and 1, 3, 2. Inboth of
these arrangements, the numbers increase to a certain point and then decrease. A sequence with this property is called a Notifications
"mountain peak sequence".

Given an integer N, write a program to find the remainder of mountain peak arrangements that can be obtained by rearranging Status messages
the numbers 1, 2, ...., N.

Input Format:

One line containing the integer N

Output Format:

An integer m, giving the remainder of the number of mountain peak arrangements that could be obtained from 1, 2, ...., N is
divide by Mod

Constraints:

Mod = 109+7
N ≤ 109

Example 1

Input
3

Output
2

Explanation
There are two such arrangements: 1, 3, 2 and 2, 3, 1

Example 2

Input
4

Output
6

Explanation
The six arrangements are (1, 2, 4, 3), (1,3,4,2), (1,4,3,2), (2,3,4,1), (2,4,3,1), (3,4,2,1)

Note:

Please do not use package and namespace in your code. For object oriented languages your code should be written in one
class.
Note:

Participants submitting solutions in C language should not use functions from <conio.h> / <process.h> as these files do not
exist in gcc
Note:

For C and C++, return type of main() function should be int.

https://www.tcscodevita.com/CodevitaV6/problemstart.jsp 1/8
7/28/2017 TCS: CodeVita - Coding Arena

© 2017 Tata Consultancy Services Limited. All Rights Reserved.

Submit Answer

I , DEASIONE S confirm that the answer submitted is my own.


I would like to provide attribution to the following sources.

Select Language
Select File
Browse...

Rules & Regulations


Problem : Concatenating primes
Launch Code Editor
If you like numbers, you may have been fascinated by prime numbers. Sometimes we obtain by concatenating two primes. For
example, concatenating 2 and 3, we obtain the prime 23. The aim is to find all such distinct "concatenated primes" that could Notifications
be obtained by concatenating primes ≤ a given integer N.

Input Format: Status messages

Integer N

Output Format:

M, the number of distinct primes that could be obtained by concatenating two primes ≤ N

Constraints:

N ≤ 70

Example 1

Input
10

Output
4

Explanations
The primes ≤ 10 are 2, 3, 5, 7. These can be used to form the following concatenated numbers: 22, 23, 25, 27, 32, 33, 35, 37,
52, 53, 55, 57, 72, 73, 75, 77. Of these, there are four primes: 23 37 53 and 73. Hence the output is 4.

Example 2

Input
20

Output
17

Explanation
The prime numbers up to 20 are 2 3 5 7 11 13 17 and 19.

Concatenating these two at a time in all possible ways, we get the following numbers:

22 23 25 27 211 213 217 219


32 33 35 37 311 313 317 319
52 53 55 57 511 513 517 519
72 73 75 77 711 713 717 719
112 113 115 117 1111 1113 1117 1119
132 133 135 137 1311 1313 1317 1319
172 173 175 177 1711 1713 1717 1719
192 193 195 197 1911 1913 1917 1919

We have the following 17 primes numbers in this list: 23 37 53 73 113 137 173 193 197 211 311 313 317 719 1117 1319
1913 Hence the output would be 17.

Note:

Please do not use package and namespace in your code. For object oriented languages your code should be written in one
class.
Note:

Participants submitting solutions in C language should not use functions from <conio.h> / <process.h> as these files do not
exist in gcc
Note:

For C and C++, return type of main() function should be int.

https://www.tcscodevita.com/CodevitaV6/problemstart.jsp 2/8
7/28/2017 TCS: CodeVita - Coding Arena

© 2017 Tata Consultancy Services Limited. All Rights Reserved.

Submit Answer

I , DEASIONE S confirm that the answer submitted is my own.


I would like to provide attribution to the following sources.

Select Language
Select File
Browse...

Rules & Regulations


Problem : Round Vaults in Bank
Launch Code Editor
In the strong room of ABC bank there are N vaults arranged in a circle. The amount of money inside each vault displayed on
the door. You can empty any number of vaults as long as you do not empty more than 2 out of any 5 adjacent vaults. If you Notifications
attempt to break more than 2 of any 5 adjacent vaults, an alarm sounds and the sentry a sharp shooter will kill you instantly
with his laser gun! Note that as the vaults are arranged in a circle, the last vault is adjacent to the first one.
Status messages
The output is the maximum amount of money that can be emptied without sounding the alarm

Input Format:

The first line contains an integer N which is the number of vaults. The next line has a sequence of positive integers of length N,
giving the amount of cash in its vaults in order

Output Format:

The maximum amount of money that can be looted without sounding the alarm.

Constraints:

N<=50, Amount in each vault <=50000

Example 1

Input
9
1000, 2000, 1000, 5000, 9000, 5000, 3000, 4000, 1000

Output
15000

Explanation
The vaults 1, 5, 6 are looted, giving a total loot of (1000+5000+9000)=15000

Example 2

Input
10
1000,2000,3000,5000,9000,7000,6000,4000,7000,5000

Output
26000

Explanation
There are 10 vaults arranged in a circle. The amounts in the vaults are 1000, 2000, ... 5000.

One way of getting the maximum is to loot vaults 4, 5, 9 and 10 giving a total of 26000. Hence the output is 26000. Note that
no 5 adjacent vaults have more than 2 looted.

Note:

Please do not use package and namespace in your code. For object oriented languages your code should be written in one
class.
Note:

Participants submitting solutions in C language should not use functions from <conio.h> / <process.h> as these files do not
exist in gcc
Note:

For C and C++, return type of main() function should be int.

© 2017 Tata Consultancy Services Limited. All Rights Reserved.

Submit Answer
https://www.tcscodevita.com/CodevitaV6/problemstart.jsp 3/8
7/28/2017 TCS: CodeVita - Coding Arena

I , DEASIONE S confirm that the answer submitted is my own.


I would like to provide attribution to the following sources.

Select Language
Select File
Browse...

Rules & Regulations


Problem : Colliding Cannons
Launch Code Editor
We have seen in many mythological movies, the arrows shot, by the opponents collide midair and one devour the other.
Notifications

Status messages

You wanted to simulate a similar situation for the video game you are designing. In the game, the opponents are in a tunnel
and have a gun each. They can shoot bullets in any direction (within limits). The roof and floor of the tunnel are perfect
surfaces and any object hitting them are bounced off according to the law of reflection (angle of incidence equals the angle of
reflection), with unchanged speed. For simplicity, we can assume that the tunnel is a two dimensional horizontal strip. Of
course, this being the mythological world, gravity does not exist, and the bullets travel in straight lines at constant spped until
being reflected (or they collide).

The two guns are positioned at half the height (h) of the tunnel, at a distance D apart.

The two guns fire simultaneously. The trajectories of the bullets (if extended) will meet at a maximum of one point. They are
said to collide if their trajectories meet, and the two bullets arrive at that point within 0.5 seconds of each other.

The shooting angle varies from -85 degrees to 85 degrees from the horizontal. For the left gun, the angles are measured anti-
clockwise, and for the right gun they are measured clockwise. Hence, with a positive angle for both, the left gun shoots up and
to the right, and the right gun shoots up and to the left.

Write a program to decide whether the bullets shot will collide or not. If they do, determine where will they collide. The
coordinate axes for reference have origin at the midpoint of the line joining the guns, X axis along the line joining the guns.
Hence, the left gun's coordinates are (-D/2,0) and the right gun's coordinates are (D/2,0). The equations for the top of the
tunnel is y=h/2, and of the bottom of the tunnel is y=-h/2.

Input Format:

The input has two lines.

The first line has two comma separated positive integers, h and D (the height of the tunnel and the distance between them).
The unit of distance measurement is myth units.

The second line has four comma separated numbers (with up to two decimals each) giving LA, LS, RA and RS respectively,
where LA, LS denote the angle (in degrees) and speed of firing (in myth units per second) of the left gun and RA, RS denote
the angle (in degrees) and speed of firing (in myth units per second) of the right gun.

Output Format:

If the two collide (the trajectories meet, and they arrive at the meeting point within 0.5 seconds of each other), the output is a
comma separated string of the word Yes and the coordinates of the colliding point.

Yes, x, y

Here, x and y are the coordinates of the collision point, and each must be rounded to two decimal places If they do not collide,
the output is the word No.

No

Constraints:

-85 ≤ LA, RA ≤85


0 < LS, RS <1000
0 < h, D < 10000

Example 1

Input
500,2000
30,90,40,70

Output
Yes,46.20,171.01

Explanation
The distance between the guns is 500 myth units, and the height of the tunnel is 2000 myth units. The angle of firing of the
left gun is 30 degrees and the speed of the bullet from the let gun is 90 myth units per second. The angle of the right gun is 40
degrees, and the speed of the bullet from the right gun is 70 myth units per second.

https://www.tcscodevita.com/CodevitaV6/problemstart.jsp 4/8
7/28/2017 TCS: CodeVita - Coding Arena

The coordinates of the intersection point of the trajectories is (to two decimal places) (46.20,171.01). The time for the left
bullet to reach this point is (to two places) 3.80 seconds, and for the right bullet (to two seconds) is 3.80 seconds. As this is
within 0.5 seconds of each other, the collision is assumed to have taken place. Hence the output is Yes,46.20,171.01

Example 2

Input
500,2000
80,70,85,70

Output
Yes,84.19,104.74

Explanation
The distance between the guns is 500 myth units and the height of the tunnel is 2000 myth units. The left gun angle is 80
degrees, and the left gun speed is 70 myth units per second. The right gun angle is 85 degrees and the speed is 70 myth units
per second also.

The guns shoot, and reflect off the ceiling (at H and G respectively), and the trajectories meet at F. The coordinates of F are
(84.19, 104.74). The time taken for the left bullet is 27.49 seconds, and the time for the second bullet is 27.17 seconds. As
they arrive within 0.5 seconds of each other, this is considered a collision. Hence the output is Yes,84.19,104.74

Example 3

Input
500,2000
30,170,50,160

Output
No

Explanation
The two trajectories meet at (86.82,194.47), but the left bullet takes 2.29 seconds, and the right bullet takes 1.59 seconds.
Hence, they do not pass within 0.5 seconds of each other, and there is no collision.

Note:

Please do not use package and namespace in your code. For object oriented languages your code should be written in one
class.
Note:

Participants submitting solutions in C language should not use functions from <conio.h> / <process.h> as these files do not
exist in gcc
Note:

For C and C++, return type of main() function should be int.

© 2017 Tata Consultancy Services Limited. All Rights Reserved.

Submit Answer

I , DEASIONE S confirm that the answer submitted is my own.


I would like to provide attribution to the following sources.

Select Language
Select File
Browse...

Rules & Regulations


Problem : On a cube
Launch Code Editor
A solid cube of 10 cm x 10cm x 10 cm rests on the ground. It has a beetle on it, and some sweet honey spots at various
locations on the surface of the cube. The beetle starts at a point on the surface of the cube, and goes to the honey spots in Notifications
order along the surface of the cube.

1. If it goes from a point to another point on the same face (say X to Y), it goes in an arc of a circle that subtends an angle of Status messages
60 degrees at the centre of the circle
2. If it goes from one point to another on a different face, it goes by the shortest path on the surface of the cube, except that it
https://www.tcscodevita.com/CodevitaV6/problemstart.jsp 5/8
7/28/2017 TCS: CodeVita - Coding Arena

never travels along the bottom of the cube

The beetle is a student of Cartesian geometry, and knows the coordinates (x, y, z) of all the points it needs to go to. The origin
of coordinates it uses is one corner of the cube on the ground, and the z axis points up. Hence, the bottom surface (on which it
does not crawl) is z=0, and the top surface is z=10. The beetle keeps track of all the distances travelled, and rounds the
distance travelled to two decimal places once it reaches the next spot, so that the final distance is a sum of the rounded
distances from spot to spot.

Input Format:

The first line gives an integer N, the total number of points (including the starting point) the beetle visits

The second line is a set of 3N comma separated non-negative numbers, with up to two decimal places each. These are to be
interpreted in groups of three as the x, y, z coordinates of the points the beetle needs to visit in the given order

Output Format:

One line with a number giving the total distance travelled by the beetle accurate to two decimal places. Even if the distance
travelled is an integer, the output should have two decimal places.

Constraints:

None of the points the beetle visits is on the bottom face (z=0) or on any of the edges of the cube (the lines where two faces
meet)

2≤N≤10

Example 1

Input
3
1,1,10,2,1,10,0,1,9

Output
4.05

Explanation
There are three points visited by the beetle (N=3). The beetle starts on the top face of the cube (z=10) at point (1,1,10) and
goes to another point on the same face (2,1,10). Though the straight line distance is 1, it travels on the arc of a circle
subtending an angle of 60 degrees at the centre of the circle, and hence travels (2*π)/6 or 1.05 (note that it rounds the
distance at each leg of the journey). It then travels from (2,1,10) on the face z=10 to (0,1,9) on the face x=0 along the
surface of the cube. This is a distance of 3. The total distance travelled is 1.05+3=4.05. The output is 4.05

Example 2

Input
3
1,1,10,2,1,10,0,5,9

Output
6.05

Explanation
There are three points visited by the beetle (N=3). The beetle starts on the top face of the cube (z=10) at point (1,1,10) and
goes to another point on the same face (2,1,10). As before. This distance is 1.05. It then travels from (2,1,10) on the face
z=10 to (0,5,9) on the face x=0 along the surface of the cube. The shortest distance on the surface of the cube between these
points is 5. The total distance travelled is 1.05+5=6.05. The output is 6.05

Note:

Please do not use package and namespace in your code. For object oriented languages your code should be written in one
class.
Note:

Participants submitting solutions in C language should not use functions from <conio.h> / <process.h> as these files do not
exist in gcc
Note:

For C and C++, return type of main() function should be int.

© 2017 Tata Consultancy Services Limited. All Rights Reserved.

Submit Answer

I , DEASIONE S confirm that the answer submitted is my own.


I would like to provide attribution to the following sources.

Select Language
Select File
Browse...

Rules & Regulations


https://www.tcscodevita.com/CodevitaV6/problemstart.jsp 6/8
7/28/2017 TCS: CodeVita - Coding Arena
Rules & Regulations
Problem : Collating Sequence
Launch Code Editor
You are writing a book on Physics. Your niece, a Codevita winner, secretly hacked into your computer, and changed the collating
sequence of your alphabet, and then protected the collating sequence with a password. When you create an index for the book,
you find a strange order, instead of the standard dictionary order.
Notifications

After a lot of pleading, she says she will reset the sequence if you show you are smart enough to determine the sequence from Status messages
some information she will give you. She says she will give a set of strings in the letters of the alphabet (in upper case), each of
which is in ascending order in the changed collating sequence. You need to use this to determine the order of all the letters in
the alphabet in the changed sequence.

You know that your niece is tricky, and may not give complete information. In that case, you know she would expect you to
give all possible orders of the letters in the changed sequence which are consistent with the information given to you. In such a
case, you know she will give enough information to ensure that there are no more than two possibilities for the collating
sequence.

Input Format:

The first line gives the number of strings, N, she will give you

The next N lines give a set of strings, in ascending order in the changed collating sequence.

Output Format:

If the set of inputs is enough to determine the sequence for the full alphabet, the output is one line with that sequence.

If multiple sequences satisfy the input given, all the sequences must be given, one per line, sorted in normal dictionary order.
When this occurs, you may assume that there are no more than two possible sequences.

Constraints:

3<N<15
Length of each string ≤ 20

Example 1

Input
4
DCEFGHIPQRSTUVWXYZ
IJKLMNOP
CABEZ
DBQR

Output
DCABEFGHIJKLMNOPQRSTUVWXYZ

Explanation
From string 1, EFGHI and PQRSTUVWXYZ are in normal alphabetical order. From string 2, we know that JKLMNO come between
I and P. Hence the order for the last 22 letters of the alphabets is EFGHIJKLMNOPQRSTUVWXYZ. From strings 1 and 3, C,D,A
and B come before E, and must be the first four letters in the collating sequence. From string 1, D comes before C, and from
string 3 C comes before A and A comes before B. Hence the first four letters must be ordered DCAB. Hence the full collating
sequence is DCABEFGHIJKLMNOPQRSTUVWXYZ, which is the output.

Example 2

Input
4
DCEFGHIPQRSTUVWXYZ
IJKLMNOP
CAEZ
DCBEQR

Output
DCABEFGHIJKLMNOPQRSTUVWXYZ
DCBAEFGHIJKLMNOPQRSTUVWXYZ

Explanation
As before the last 22 letters are ordered EFGHIJKLMNOPQRSTUVWXYZ. Using strings 1,3 and 4, D, C, A, B all occur before E.
Hence they must be the first four. From string 1 and string 3, D, C and A are in that order. From string 4, D, C and B are in that
order. But no information is available about whether A comes before or after B in the collating sequence. Hence, we must
consider both orders as possible. The first four letters in order are DCAB or DCBA. Combining this with the last 22 letters gives
the two lines in the output. They are in that order as that is the dictionary order for the two strings.

Note:

Please do not use package and namespace in your code. For object oriented languages your code should be written in one
class.
Note:

Participants submitting solutions in C language should not use functions from <conio.h> / <process.h> as these files do not
exist in gcc
Note:

For C and C++, return type of main() function should be int.

© 2017 Tata Consultancy Services Limited. All Rights Reserved.

https://www.tcscodevita.com/CodevitaV6/problemstart.jsp 7/8
7/28/2017 TCS: CodeVita - Coding Arena

Submit Answer

I , DEASIONE S confirm that the answer submitted is my own.


I would like to provide attribution to the following sources.

Select Language
Select File
Browse...

© 2017 Tata Consultancy Services Limited. All Rights Reserved. In Association with | Privacy Policy

https://www.tcscodevita.com/CodevitaV6/problemstart.jsp 8/8

You might also like