Question 1Single Lane Highway[ Accepted ]
This is actually a math problem. Apart from N, inputs don't matter !
We can solve this using
Linearity of Sieve.
Rephrasing original problem, it states find average number of expected groups! SimplerNow?
Now we can solve using linearty of expectation we get,
required probability = 1 + 1/2 + 1/3 + ... + 1/n.
Just make sure you print upto 6 decimal places.
Here is the simple C++ implementation:
https://ideone.com/fRzy1Y This gave me
Acceptedstatus!
Question 2Path Through Graph[ Accepted ]
There can be atmost O(log(N)) factors of a number, and finding an edge of a number-node takes atmost O(Sqrt(N)) time using usual factorization method.
This gives us O(log N x sqrt(N)) solution. This gave me
Acceptedstatus!
Question 3 Fill the Cube[ Accepted ]
Note that N<100, so N
2 algorithm will work. Try each rotation, and find the maximum wall that can be fitted in each rotation, the maximum of this will be the answer.
First create a new matrix for each rotation like this
- Let's say the original wall matrix is "wall" and wall[i][j] is the brick in row i and column j. Let's call the rotated matrix wall_rotated, wall_rotated[i][j] is the brick at row i, colun j.
- Left side at bottom - wall_rotated[j][n-i-1] = wall[i][j]
- Right side at bottom - wall_rotated[j][i] = wall[i][j]
- Top side at bottom - wall_rotated[n-i-1][j] = wall[i][j]
- Bottom side at bottom - wall_rotated[i][j] = wall[i][j]
For each roation we calculate the maximum wall that can be fitted in N
2- In an array, store the gap created in each column of wall when all 'D' type bricks melt. Gap in column i = No of bricks of type 'D' in column i.
- For each pair of i,j check the maximum square gap with end points at ith and jth columns. This is equal to = min(j-i+1,k)). k is the minimum gap in any column between i and j inclusive. Eg. Lets say there are three columns with gaps(calculated in step 1) = [3,2,5], then with i=0,j=2, k=min(3,2,5)=2.
- The maximum value calculated in step 2 for each pair, is the maximum size of square wall that can be inserted in this rotation
Question 4 Factor of 3[ Accepted ]
Idea is simple, replace every number by modulo 3, so every number now will be 0, 1 or 2.
If a 0 and 0 are adjacent or if 1 and 2 are adjacent only then will their sum be divisible by 3, so we want to avoid it.We can avoid it by:-
- If there are no '0' numbers and '1' and '2' are present then at some place we will get adjacent '1', '2'.
- if only '0' is present then too we get 0 and a 0 adjacent forcibly
- if number of 0s > number of 1s and 2s combined then again at some place two 0s will come together
Here is implementation of the
C++ code
Question 5 4-Particles[ Accepted ]
Note, constraints are integers all within 100. So one can simply bruteforce for each configuration and compute sum of two 3d triangles.
You can compute 3d Traingle's area using 3d_areaPolygon() in this site
http://geomalgorithms.com/a01-_area.html Code :
C++ code
Question 6 Tennis Score
Simple bruteforce with tons of if-else.
Code will take time to cook.