Sei sulla pagina 1di 18

TCS CODE VITA QUESTIONS

1. Coast Guard
Problem Description

Of late, smuggling has increased many fold and as Chief of the Coast Guard,
you are responsible for intercepting the smuggling vessels and nullify them.
You have stationed several smart, high speed boats in the sea. The entire sea
under your control can be divided into a rectangular grid of 1 km by 1 km
squares. Due to a design flaw in the boats, they can move only in horizontal
or vertical directions (EW or NS) (they cannot move diagonally, in
particular). Of course, no two boats are stationed in the same grid square.

The boats can reach an adjacent grid square (horizontally or vertically) in


one minute. Every grid square that can be reached by one boat faster than it
can be reached by any other boat is said to be controlled by that boat. If a
grid square may be reached by at least one more boat in the same time as the
fastest boat, it is said to be uncontrolled.

It is in your interest to minimize the number of grid squares that are


uncontrolled.
In the figure above we are considering a grid of 3 rows and four columns. The
bottom left corner square is numbered (0,0), and the top right corner is
numbered (3,2). Two boats are in positions (2,0) and (0,2). The four shaded

squares can be reached in equal (minimum) time by both boats, and are
hence uncontrolled.
Given the position of the boats, write a program to identify the number of
grid squares that are uncontrolled.

Constraints

0<M,N<50, 1<k<10
Input Format

The first line will be three comma separated integers M, N and k. M and N
give the number of rows and columns of the grid, and k the number of
boats.
The next k lines will a pair of comma separated numbers giving the
coordinates of the grid square with a boat

Output

A single line containing the number of uncontrolled grid squares.


Explanation

Example 1
Input

3,4,2
2,0

0,2

Output

Explanation

M=3,N=4,k=2. There are 3 rows and 4 columns. There are 2 boats at (2,0)
and (0,2).

The position is the same as in the earlier figure. There are 4 uncontrolled
squares. Hence the result is 4.

Example 2

Input

2,4,2

0,1
2,0
Output

Explanation
M=2, N=4, k=2. There are two boats positioned as below

It can be seen that there is no grid square that is reached at the same
minimum time from the two boats. Hence the result is 0.

2. Square Free Number


Problem Description
In the theory of numbers, square free numbers have a special place. A square
free number is one that is not divisible by a perfect square (other than 1). Thus
72 is divisible by 36 (a perfect square), and is not a square free number, but 70
has factors 1, 2, 5, 7, 10, 14, 35 and 70. As none of these are perfect squares
(other than 1), 70 is a square free number.

For some algorithms, it is important to find out the square free numbers that
divide a number. Note that 1 is not considered a square free number.

In this problem, you are asked to write a program to find the number of square
free numbers that divide a given number.

Constraints
N < 10^9
Input Format
The only line of the input is a single integer N which is divisible by no prime
number larger than 19

Output
One line containing an integer that gives the number of square free numbers
(not including 1)
Explanation
Example 1
Input
20

Output
3

Explanation
N=20

If we list the numbers that divide 20, they are

1, 2, 4, 5, 10, 20

1 is not a square free number, 4 is a perfect square, and 20 is divisible by 4, a


perfect square. 2 and 5, being prime, are square free, and 10 is divisible by 1,2,5
and 10, none of which are perfect squares. Hence the square free numbers that
divide 20 are 2, 5, 10. Hence the result is 3.

Example 2
Input
72
Output
3
Explanation
N=72. The numbers that divide 72 are

1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72

1 is not considered square free. 4, 9 and 36 are perfect squares, and 8,12,18,24
and 72 are divisible by one of the. Hence only

2, 3 and 6 are square free. (It is easily seen that none of them are divisible by a
perfect square). The result is 3

3.Spiral Primes

Problem Description
The prime numbers are written in a spiral form staring at (0,0) and moving as
shown in the diagram below.

.
Constraints
N<=20
Each output prime < 1000000
-130< x,y <130
Input Format
The first line has an integer N that specifies the number of coordinates in this
test case

The next N lines each have a pair of comma separated integers, which are the x
and y coordinates of the position

Output
The output consists of N lines.
Each consists of an integer specifying the prime at the corresponding position.

Explanation
Example 1

Input
2
1,0
0,1

Output
3
7

Explanation
N=2. There are 2 sets of coordinates in this test case. The coordinates are (1,0)
and (0,1).. The corresponding primes in the spiral are 3 and 7. The output hence
has these.

Example 2

Input

1,1

-1,1

-1,0

Output

11

13

Explanation
There are 3 sets of coordinates in this test case (N=3). The coordinates are
(1,1),(-1,1) and (-1,0). The corresponding primes at these positions are 5, 11,
13. Hence the output has these in 3 lines.

4. N-pod Numbers
Problem Description
Consider the natural numbers 1, 2, 4. Using these numbers alone or in
combinations (as sums), one can realize the fo
1
2
3=1+2
4
5=1+4
6=2+4
7=1+2+4
That's all!
Let us call 1, 2 and 4 as the "seeds" and the generated numbers 1 through 7 as
the "plants".
Let us call a seed-list as an "N-pod" if its seeds are distinct and it generates
plants 1 through N.

We shall confine ourselves to natural number (positive integer) seeds.


Hence 7-Pod-1 = {1, 2, 4} generates Plant-set {1, 2, 3, 4, 5, 6, 7}.
Note that seed-list {1, 2, 2, 2}, having the seed "2" repeated 3 times, also
generates plants 1 through 7:
1
2
3=1+2
4=2+2
5=1+2+2
6=2+2+2
7=1+2+2+2
However, since the seed-list has non-distinct seeds, {1, 2, 2, 2} is not a pod.
Consider 7-Pod-2 = {1, 2, 3, 6}. We can check that this pod also generates 1
through 7:
1
2
3
4=1+3
5=2+3
6
7=1+6
Note that the number of seeds in 7-Pod-1 (3) is smaller than that (4) in 7-Pod-
2. Let us call the numbers of seeds Hence 7-Pod-1-Length = 3 < 7-Pod-2-
Length = 4.
Note also that the sum of the seeds in 7-Pod-1 (1 + 2 + 4 = 7) is smaller than
that (1 + 2 + 3 + 6 = 12) of 7-Pod-2 Let us call the sum of the seeds in a pod
as its "Sigma". Hence 7-Pod-1-Sigma = 7 < 12 = 7-Pod-2-Sigma. Consider
the following 19-Pods that each have the least lengths and sigmas possible:

19-Pod-1 = {1, 2, 3, 4, 9}
19-Pod-2 = {1, 2, 3, 5, 8}

19-Pod-3 = {1, 2, 3, 6, 7}

19-Pod-4 = {1, 2, 4, 5, 7}

Note that the largest seed ("max-seed") in both 19-Pod-3 and 19-Pod-4 is 7,
which is smaller than the max-seeds of

Focusing on the 19-Pods that have max-seeds 7. let us calculate the medians
of the seeds:

Median-19-Pod-3 = Median(1, 2, 3, 6, 7) = 3

Median-19-Pod-4 = Median(1, 2, 4, 5, 7) = 4

When number of seeds in pod is even then median has to be calculated as the
average of middle two numbers.

The Median should be considered up-to accuracy of 2 decimal places.

Thus 19-Pod-3 is the least-length, least-sigma, least-max-seed and least-


median 19-Pod. Let us call such a pod as "A Thus an Atomic N-Pod is a
least-length, least-sigma, least-max-seed and least-median pod that consists of
seeds ge Here's the problem: Accept number N and identify all Atomic N-
pods.
Constraints
1 <= N <= 35
Input Format
First line contains Integer N.
Output
Atomic N-pods separated by OR arranged in ascending order if there are
multiple such pods. Within a pod, arrange the seeds in ascending order.
Explanation

Example 1: Input: 7

Output: {1,2,4}

Example 2:

Input: 32

Output: {1,2,3,7,9,10}OR{1,2,4,6,9,10}

5.Hop Game

Problem Description
Dr Felix Kline, the Math teacher at Gauss School introduced the following
game to teach his students problem solving. He places a series of “hopping
stones” (pieces of paper) in a line with points (a positive number) marked on
each of the stones.

Students start from one end and hop to the other end. One can step on a stone
and add the number on the stone to their cumulative score or jump over a stone
and land on the next stone. In this case, they get twice the points marked on the
stone they land but do not get the points marked on the stone they jumped over.
At most once in the journey, the student is allowed (if they choose) to do a
double jump that is, they jump over two consecutive stones - where they would
get three times the points of the stone they land on, but not the points of the
stone they jump over.

The teacher expected his students to do some thinking and come up with a plan
to get the maximum score possible. Given the numbers on the sequence of
stones, write a program to determine the maximum score possible.

Constraints

The number of stones in the sequence< 30

Input Format

The first line contains N, the number of integers (this is a positive integer)
The next line contains the N points (each a positive integer) separated by
commas. These are the points on the stones in the order the stones are placed.

Output
One integer representing the maximum score

Explanation

Example 1
Input
3
4,2,3
Output

10

Explanation

There are 3 stones (N=3), and the points (in the order laid out) are 4,2 and
3 respectively.

If we step on the first stone and skip the second to get 4 + 2 x 3 = 10. A
double jump to the third stone will get only 9. Hence the result is 10, and
the double jump is not used

Example 2

Input

4,5,6,7,4,5

Output

35

Explanation

N=6, and the sequence of points is given. One way of getting 35 is to start
with a double jump to stone 3 (3 x 6=18), go to stone 4 (7) and jump to stone
6 (10 points) for a total of 35. The double jump was used only once, and the
result is 35.
6. Sorting Boxes
Problem Description
The parcel section of the Head Post Office is in a mess. The parcels that
need to be loaded to the vans have been lined up in a row in an arbitr order
of weights. The Head Post Master wants them to be sorted in the increasing
order of the weights of the parcels, with one exception. wants the heaviest
(and presumably the most valuable) parcel kept nearest his office.

You and your friend try to sort these boxes and you decide to sort them by
interchanging two boxes at a time. Such an interchange needs eff equals to
the product of the weights of the two boxes.

The objective is to reposition the boxes as required with minimum effort.

Constraints

N<=50

Weights <= 1000

Input Format

The first line consists of two space separated positive integers giving the
number of boxes (N) and the position of the Head Post Master's offi (k)
where the heaviest box must be.
The second line consists of N space separated positive integers giving the
weights of the boxes. You may assume that no two weights are equal

Output
The output is one line giving the total effort taken to get the boxes in sorted
order, and the heaviest in position

Explanation
Example 1
Input
52
20 50 30 80 70
Output
3600
Explanation

There are 5 boxes (N=5) and the heaviest box must be in position 2 (k=2).
If we look at the final order (sorted, with the heaviest at position 2) should
be 20 80 30 50 70. If we look at this, we notice that only the 50 and the 80
parcels need to be exchanged. As this takes effort of the produ of the
weights, the effort is 4000.

Further reduction can be obtained if we use the smallest package (20) as an


intermediary. If we exchange 20 with 50 (effort 1000), then with (effort
1600) and back with 50 again (effort 1000), the effect is the same, with a
total effort of 3600 (less th an the effort obtained by the dir move)an the
effort

The results after the optimal sequence of exchanges are


50 20 30 80 70

50 80 30 20 70

20 80 30 50 70

As this takes an effort of 3600, the output is 3600.

Example 2

Input
63

30 20 40 80 70 60

Output

7600

Explanation

There are 6 parcels, and the heaviest should be at position 3. Hence the final order
needs to be 20 30 80 40 60 70. If we look at the initial positi we see that 20 and
30 need to be exchanged (effort 600), 40 and 80 need to be exchanged (effort
3200) and 60 and 70 need to be exchang (effort 4200). Hence the total effort is
600+3200+4200=8000.

If we use the same approach as in Example 1, we get the following efforts


(600) 20 30 40 80 70 60

(3200) 20 30 80 40 70 60

(1200) 60 30 80 40 70 20

(1400) 60 30 80 40 20 70

(1200) 20 30 80 40 60 70

A total effort of 7600 is obtained rather than an effort of 8000, which is the
output.

Potrebbero piacerti anche