Programming Geek
Rated 4.7/5 based on 1446 reviews

CodeVita 2018 Round 1 Question: Parallelograms

Parallelograms

Problem Description

A number (N) of lines (extending to infinity) in both directions are drawn on a plane. The lines are specified by the angle (positive or negative) made with the x axis (in degrees). It may be assumed that the different lines are not coincident (they do not overlap each other). The objective is to determine the number of parallelograms in the plane formed by these lines.
If the lines are given with an angle of 10, 70, 30 and 30, the figure looks like this

L1 is at 10 degrees to the x axis, L2 is at 70 degrees to the x axis, L3 and L4 are at 30 degrees to the x axis. It can be seen that there are no parallelograms formed by these lines

Constraints
N<=50
-89 <= angle for any line <=90
Input Format
The first line of the input consists of a single integer, N.
The next line consists of a series of comma separated integers (positive, zero or negative), each corresponding to a separate line, and giving the angle that it makes with the x axis (in degrees).

Output
The output is a single integer giving the number of parallelograms in the plane formed by the lines
Explanation
Example 1
Input
5
20,20,-20,-20,50
Output
1
Explanation
There are 5 lines (N=5), with angles at 20,20,-20,-20and 50 degrees with the x axis. The figure looks like this


There is one
Hence the output is 1.
Example 2
Input
6
20,20,-20,-20,50,50
Output
3
Explanation
There are 6 lines (N=6) with angles 20,20, -20,-20,50 and 50 degrees with the x axis.. The figure looks like this


There are three parallelograms formed by (L1, L2, L3, L4), (L1, L2, L5, L6) and (L3, L4, L5, L6). Hence the output is 3.

CodeVita 2018 Round 1 Question: Wander Paths

Wander Paths


Problem Description

The city of Looneywalks has a rectangular grid of roads. One of the favorite pastimes of the folks at Looneywalks is to wander around the blocks. While they could take the shortest path, more often, they retrace their steps and engage themselves in finding the number of ways of reaching the destination with a given number of steps. For example, in the figure above a block is shown with 8 blocks and roads around them. Any walk is a series of symbols U, R, L, D where by U we mean moving one block towards north, R, moving one block East, L moving one block West and D moving one block South. One such path for the above reaching the upper right corner from lower left corner is RRRRUU. This path has length 6 (the number of symbols in the path), which is the shortest pat (that do not go outside the grid)h between the two points. If we enumerate all such shortest paths of length 6, there are 15 of them.
Consider a path of length 8: RRLRRRUU. Note that while walking, one cannot move out of the grid. If all the valid paths of length 8 are enumerated, there are 252 of them. However, it soon becomes difficult to enumerate path manually, and we need to use a program
You are given the layout of the roads at Looneywalks. Given a starting point, S ending point E and the length k of the path, write a program to find the number of valid paths from S to E that have length precisely k. A valid path is one that does not go outside the grid, and two path is one
Every grid point is referred to by the X coordinate and a Y coordinate (the number of blocks east of the southwest corner, and the number of blocks north of the southwest corner). Thus the lower left corner in the above diagram is (0,0) and the top right corner is (4,2).
The size of the block is given by two numbers, M and N. M is the number of rows in the grid, and N is the number of columns (in the grid above, M is 2 and N is 4).
Constraints
M, N < 10, k < 20
Input Format
The first line of the input has three comma separated integers M, N and k. The Dimensions of the grid are M blocks in rows, and N blocks in columns. The path length is given by k.
The next line has four comma separated numbers giving the coordinates of the starting point and the ending point respectively.
Output
One integer representing the number of valid paths from the starting point to ending point that have length precisely k.
Explanation
Example 1
Input
2,4,6
0,0,4,2
Output
15
Explanation
M is 2, and N is 4. The value of k is 6, and we are counting paths of length 6. The starting point is (0,0) and the end point is (4,2).
The sketch of Loonewalks above is valid for this case. 6 is the length of the shortest path from the starting point to the ending point. Hence, we need to find the number of shortest paths. Each such path has 4 moves to the right (R) and two moves up (U). All paths having 4 Rs and 2 Us are valid, as none of them will take the walker outside the grid. This is just the number of arrangements of 4 R and 2U in any order, which can be seen to be 15. Hence the result is 15.
Example 2

Input
3,3,6
1, 1, 3, 3
Output
90
Explanation
M=3, N=3,k=6. The starting and ending points are (1,1) and (3,3) respectively.
The shortest path is of length 4, and has two R and two U in the path. An example is RRUU. As the desired path length is 6, we need to add an additional R (and the corresponding L) to all th shortest paths, or add an additional U (and the corresponding D) to all the shortest paths.
Let us first consider adding one R and one L. The shortest path will have two R, and if we look at only the horizontal part of the path, the new sequence could be one of three, LRRR, RLRR or RRLR. Note that RRRL is not valid, as it will go outside the grid. We need to add two U to each of these. In say RLRR, there are 5 ways if the two Us are together (UURLRR, RUULRR, and so on), and 10 wys if the Us are separate (URULRR, URLURR and so on), a total of 15 ways. Hence for all the three sequences, there are 45 paths if a RL pair is added.
Similar considerations show that if we add a UD pair, there are another 45 paths. The total number of paths with length exactly 6 is therefore 45 + 45=90, which is the result.

CodeVita 2018 Round 1 Question: On a Cube

On a Cube


Problem Description
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 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 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 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.
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
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
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.
Explanation
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*pi)/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.

CodeVita 2018 Round 1 Question: Supertrend

Supertrend

Problem Description
Write a program to analyze and back test stock data to find total profit and buy / sell order price using super trend indicator formula.
There is one indicator on chart data called super trend which is used in stock market to make buy and sell decisions.
If we see super trend indicator on stock chart and want to back test super trend trading strategy then we have to manually check each and every buy / sell occurrences. A program will make it easy to analyze or back test data pertaining to any number of periods.
It is calculated based on Average True Range (ATR) and a multiplier value. ATR measures the degree of volatility of the market.
Below are the mathematical formulae involved in calculation of super trend indicator:
True Range (TR) = Max of (Current High- Current Low, Current High- Previous Close, Current Low- Previous Close)
Note: TR value to be calculated starting from 2nd iteration as we are using Previous close in the formula
Average True Range (ATR) = Average of last n True Range values (Here n is by default 10. Means we have to find last 10 days average true range at every point.)
Basic Upper band = (Current High + Current Low) / 2 + Multiplier * ATR (Here multiplier is by default 3)
Basic Lower band = (Current High + Current Low) / 2 -– Multiplier * ATR
Final Upper band = IF( (Current Basic Upper band < Previous Final Upper band) or (Previous Close > Previous Final Upper band)) Then (Current Basic Upper band) ELSE Previous Final Upper band)
Final Lower band = IF( (Current Basic Lower band > Previous Final Lower band) or (Previous Close < Previous Final Lower band)) Then (Current Basic Lower band) ELSE Previous Final Lower band)
SUPERTREND = IF(Current Close <= Current Final Upper band ) Then Current Final Upper band ELSE Current Final Lower band
Buy & Sell Process
We have to buy stock on current close price when Super trend indicator value closes above the current close price and a sell stock when super trend value closes below the current closing price.
Note: Here ATR period is 10 so buy and sell must start after 10th period so that super trend values can be generated. In first initial 10 periods only true range (TR) would be calculated. Calculate total number of buy and sell transactions that were performed based on Super trend analysis.
Assumption
We are trading stock market index or stock futures contracts and not equities.
Constraints
1 <= N <= 1000
Input Format
First line contains single integer N that denotes the number of periods for which data exist
Second line contains N * 3 space delimited values which correspond to the sequence …repeated 3N times
Output
For each Buy / Sell transaction output the following
Buy at (New Position).
Sell at (Close Previous Position).
Profit
Where , and are actual values for that transaction accurate up to 2 digit after decimal point.
Explanation
Example 1
Input
14
335.4 317.3 321 336.7 319 322.75 338.1 325 325.05 333 318.8 324.2 334.3 320.45 326 331.8 323 327.45 324.95 314.65 319.9 325.9 317.1 322.1 324.35 318.5 323.7 330.05 307 315.55 329.45 310 316.8 0 0 326.05 323.5 312.2 315.45 318.95 315.75 316.85
Output
Buy at 316.8 (New Position).
Sell at 326.05 (Close Previous Position).
Profit 9.25
Sell at 326.05 (New Position).
Buy at 315.45 (Close Previous Position).
Profit 19.85
Buy at 315.45 (New Position).
Total orders 3
Example 2
Input
59
10315.3 10208.35 10301.4 10339 10248.45 10327.4 10345 10170.25 10194.7 10403 10288.45 10398.05 10420 10360 10399.3 10469.7 10410 10438.55 10482 10438.6 10468 10479.9 10411.5 10468.55 10520.95 10440.25 10507.5 10570 10501.5 10533.2 10590.55 10463.8 10585.5 10604.45 10540 10589.2 10634.05 10560.15 10577.85 10624.7 10591 10612.75 10632.6 10570 10627.6 10660.15 10589 10625.6 10670.9 10609.8 10654.35 10645.95 10568.25 10605.2 10651.6 10591.45 10639.65 10750 10660 10729.9 10796.25 10742.4 10788.9 10801.7 10720 10746.9 10745 10677 10719.45 10696.4 10654 10669.5 10754.4 10676.3 10748.6 10775 10690.1 10742.9 10795.5 10718.8 10777 10800 10724.05 10735.3 10833.65 10746.55 10827.8 10853.5 10792.35 10821.95 10955.35 10794.05 10824.25 10823.3 10731.6 10772.8 10789.7 10693.4 10709.05 10697.5 10599 10605.95 10639 10537.05 10552.35 10587 10521.2 10568 10558.45 10430.35 10442.85 10535.45 10426.05 10516.45 10650 10522.6 10616.2 10710.95 10646.05 10691.2 10714 10606.75 10628.05 10655 10556.2 10614.45 10715.4 10625.1 10702.15 10744 10669.95 10683.9 10732.2 10587.65 10598.7 10630 10552 10596.45 10701 10575 10686.25 10814.05 10725.25 10765.2 10759.4 10691 10748.1 10835 10761.25 10785.8 10849.9 10780.25 10839.05 10873.5 10831.5 10844.85 10834.05 10770.5 10813.9 10837.95 10735.5 10814.65 10817.3 10781 10789.6 10760 10704.55 10719.85 10796.9 10725.2 10790.65 10805.95 10716.65 10737.15 10849 10712.1 10833.95
Output
Buy at 10585.5 (New Position).
Sell at 10827.8 (Close Previous Position).
Profit 242.3
Sell at 10827.8 (New Position).
Buy at 10821.95 (Close Previous Position).
Profit 248.15
Buy at 10821.95 (New Position).
Sell at 10839.05 (Close Previous Position).
Profit 265.25
Sell at 10839.05 (New Position).
Buy at 10844.85 (Close Previous Position).
Profit 259.45
Buy at 10844.85 (New Position).
Total orders 5

CodeVita 2018 Round 1 Question: Date Time

Date Time

Problem Description
Arun and his sister Usha are challenging each other with some mathematical puzzles. Usha, the cleverer one, has come up with the idea of givingArun 12 distinct digits from 0 to 9, and have him form the largest date time in 2018 with them. Arun is a little nervous, and asks you to help him with a computer program.
Usha will give Arun 12 distinct digits. He needs to create a date time combination in the year 2018: the date in the MM/DD form (all four digits must be present), and the time in the format HH:MM (all four digits must be present). The date may be from 01/01 to 12/31 and the time may be from 00:00 to 23:59 (in the 24 hour format). The digits provided may be used only once in the answer that Arun gives.
If more than one date time combination may be formed, Arun needs to give the latest valid date time possible in the year 2018.
Constraints
Single digits (any of 0-9)
Input Format
A line consisting of a sequence of 12 (not necessarily distinct) single digits (any of 0-9) separated by commas. The sequence will be non-decreasing.
Output
The maximum possible valid date time in the year 2018. The output must be in the format
MM/DD HH:MM
If no date time can be constructed, the output should be 0
Explanation
Example1 :
Input
0,0,1,2,2,2,3,5,9,9,9,9
Output
12/30 22:59
Explanation
The 12 digits to be used by Arun are given.
The maximum valid date time using only the digits given, and with each digit used at most once is
12/30 22:59
This is the output.
Example 2
Input
3,3,3,3,3,3,3,3,3,3,3,3
Output
0
Explanation
As no digit less than 3 is present in the input, a valid month cannot be formed. Hence no valid Date time can be formed with the input digits.

CodeVita 2018 Round 1 Question: Codu And Sum Love

Codu And Sum Love


Problem Description
```
Scanner sc = new Scanner(System.in);
long sum = 0;
int N = sc.nextInt();
for (int i = 0; i < N; i++) {
final long x = sc.nextLong(); // read input
String str = Long.toString((long) Math.pow(1 << 1, x));
str = str.length() > 2 ? str.substring(str.length() - 2) : str;
sum += Integer.parseInt(str);
}
System.out.println(sum%100);
```
Given N number of x’'s, perform logic equivalent of the above Java code and print the output
Constraints
1<=N<=10^7 0<=x<=10^18
Input Format
First line contains an integer N
Second line will contain N numbers delimited by space
Output
Number that is the output of the given code by taking inputs as specified above
Explanation
Example 1
Input 4 8 6 7 4 Output 64
Example 2
Input
3
1 2 3
Output
14