Codathon Questions
Codathon Questions
Problem Statement :
Sahil takes out his Snakes and Ladders game, stares at the board and wonders: "If I can always
roll the die to whatever number I want, what would be the least number of rolls to reach the
destination?"
1. Starting from square 1 , land on square 100 with the exact roll of the die. If moving the
number rolled would place the player beyond square 100, no move is made.
2. If a player lands at the base of a ladder, the player must climb the ladder. Ladders go up
only.
3. If a player lands at the mouth of a snake, the player must go down the snake and come
out through the tail. Snakes go down only.
Your Task is to find the minimum number of moves required to reach at 100.
Input :
● ladders: a 2D integer array where each ladder[i] contains the start and end cell numbers
of a ladder
● snakes: a 2D integer array where each snakes[i] contains the start and end cell numbers
of a snake
- Each of the next n lines contains two space-separated integers, the start and end of a ladder.
- Each of the next m lines contains two space-separated integers, the start and end of a snake.
Output :
For each of the t test cases, print the least number of rolls to move from start to finish on a
separate line. If there is no solution, print -1.
Sample Input:
2
3
32 62
42 68
12 98
7
95 13
97 25
93 37
79 27
75 19
49 47
67 17
4
8 52
6 80
26 42
2 72
9
51 19
39 11
37 29
81 3
59 5
79 23
53 7
43 33
77 21
Sample Output:
3
5
Problem 2 - Medium
Problem Statement :
Pradyumn loves to keep his encyclopedia sorted by numbers. But currently, he does not have
enough time to sort his encyclopedia. So he decided to keep it Almost sorted in intervals. The
Encyclopedia would be almost sorted if there is a consecutive subsequence in a sequence that
satisfies the following property:
Please help him count the number of almost sorted encyclopedias in this permutation.
Note: Two intervals are different if at least one of the starting or ending indices are different.
Input :
The input consists of 2 lines. The first line contains an integer N while the second line contains a
permutation from 1 to N.
Output :
Output the number of almost sorted intervals
Sample Input:
5
4 1 2 5 3
Sample Output:
8
Explanation:
[4] , [1] , [1,2] , [1,2,5] , [2] , [2,5] , [5] , [3] are possible sorted combinations of encyclopedia
Problem 3 - Hard
Problem Statement:
Input :
The input consists of a single test case. The first line contains two integers n and d (1≤n≤100
and 1≤d≤10 000), where n is the number of sensors available and d is the maximum distance
between sensors that can communicate directly. Sensors are numbered 1 to n. Each of the next
n lines contains two integers x and y (−10 000≤x, y≤10 000) indicating the sensor coordinates,
Output :
Display a maximum subset of sensors in which each pair of sensors can communicate directly.
The first line of output should be the size of the subset. The second line of output should be the
(one-based) indices of the sensors in the subset. If there are multiple such subsets, any one of
them will be accepted.
Sample Input:
5 20
0 0
0 2
100 100
100 110
100 120
Sample Output:
3
4 3 5
Problem 4 - Hard
Title: Wires
Problem Statement:
Moore’s Law states that the number of transistors on a chip will double every two years.
Amazingly, this law has held true for over half a century. Whenever current technology no
longer allowed more growth, researchers have come up with new manufacturing technologies to
pack circuits even denser. In the near future, this might mean that chips are constructed in three
dimensions instead of two.
But for this problem, two dimensions will be enough. A problem common to all two-dimensional
hardware designs (for example chips, graphics cards, motherboards, and so on) is wire
placement. Whenever wires are routed on the hardware, it is problematic if they have to cross
each other. When a crossing occurs special gadgets have to be used to allow two electrical
wires to pass over each other, and this makes manufacturing more expensive.
Our problem is the following: you are given a hardware design with several wires already in
place (all of them straight line segments). You are also given the start and endpoints for a new
wire connection to be added. You will have to determine the minimum number of existing wires
that have to be crossed in order to connect the start and endpoints. This connection need not be
a straight line. The only requirement is that it cannot cross at a point where two or more wires
already meet or intersect.
The figure shows the first sample input. Eight existing wires form 4 blocks. The start and
endpoints of the new connection are in the leftmost and rightmost squares, respectively. The
purple dashed line shows that a direct connection would cross three wires, whereas the optimal
solution crosses only two wires (the curved blue line).
Input:
The input consists of a single test case. The first line contains five integers m, x0, y0, x1, y1,
which are the number of pre-existing wires (m≤100) and the start and endpoints that need to be
connected. This is followed by m lines, each containing four integers xa, ya, xb, yb describing
an existing wire of non-zero length from (xa, ya) to (xb, yb). The absolute value of each input
coordinate is less than 105. Each pair of wires has at most one point in common, that is, wires
do not overlap. The start and endpoints for the new wire do not lie on a pre-existing wire.
Output:
Display the minimum number of wires that have to be crossed to connect the start and
endpoints.
Sample Input:
8 3 3 19 3
0 1 22 1
0 5 22 5
1 0 1 6
5 0 5 6
9 0 9 6
13 0 13 6
17 0 17 6
21 0 21 6
Sample Output:
2
Problem 5 - Medium
Problem Statement.
Ravi owns a pizza shop. A pizza at Ravi’s costs only ₹5. Ravi also has some policies
which are always withheld:
There are N people (numbered 1 through N) standing in a queue to buy pizza. For
each i, the i- th person has one coin with value a[i]. Initially, Ravi has zero coins. Your
task is to find if Ravi can serve all the people in the queue or not.
Input:
The first line of the input contains a single integer T denoting the number of test cases. The
description of T test cases follows.
Output:
For each test case print Yes if all people can be served else print No.
Problem 6 - Medium
Problem Statement.
There are N houses for sale. The i-th house costs Ai dollars to buy. You have a
budget of B
dollars to spend.
What is the maximum number of houses you can buy?
Input:
The first line of the input contains a single integer T denoting the number of test cases. The
description of T test cases follows.
The first line of each test case contains two space-separated integers N and B.
The second line of each test case contains N space-separated integers in which the i-th
integer denotes the cost of i-th house (Ai).
Output:
For each test case, output a single integer S. Where S is the maximum number of houses
you can buy.