DSA Assignment
DSA Assignment
Assignment-4
Max Marks: 60
Instructions
• For the coding questions, you must ensure that your code compiles and runs otherwise
these will be awarded zero marks.
• For coding questions, please provide a brief algorithm description along with an analysis
of the time and space complexity.
• m == board.length
• n == board[i].length
• 1 ≤ m, n ≤ 200
• board[i][j] is ’F’ or ’N’.
Input:
F F F F
F N N F
F F N F
F N F F
Output:
F F F F
F F F F
F F F F
F N F F
2. (10 points) Consider an array composed of unique numbers. The objective is to identify
all ordered pairs (x, y) within this array where the remainder of the division of the
first number (x) by the second number (y) equals a specified integer, r.
Example 1:- arr[] = {4, 5, 2, 1, 3} and r = 2, the output should be (2, 3), (2, 5), (5, 3), (2, 4).
Example 2:- arr[] = {6, 1, 4, 10, 9} and r = 2. The output is the set of pairs
(6, 4), (10, 4)
The length of the array n is such that 1 ≤ n ≤ 106 .
and range of r 1 ≤ k ≤ 106 .
3. (10 points) On a 2D plane, there’s a map showcasing multiple cities pinpointed by their
coordinates, (xi , yi ), listed in an array called points. The aim is to establish flight
routes connecting these cities. The fuel needed to establish a flight route between any
two cities is contingent upon their positions on the map. The cost to travel between
cities is determined by the absolute differences in their x and y coordinates, expressed
as |xi − xj | + |yi − yj |. Return the minimum fuel required to connect all locations on the
map. All locations are connected if there is exactly one simple path between any two
locations.
Constraints:
• 1 ≤ points.length ≤ 1000
• −106 ≤ xi , yi ≤ 106
• All pairs (xi , yi ) are distinct.
Input :
locations = [[0, 0], [2, 2], [3, 10], [5, 2], [7, 0]]
Output :
20
Explanation:
We can connect the points as shown above to get the minimum cost of 20.
Input :
locations = [[3, 12], [−2, 5], [−4, 1]]
Page 2
Output :
18
4. (10 points) You need to travel from city 1 to city N across a land divided into N cities
connected by M undirected roads. Each road i (1 ≤ i ≤ M ) between cities Ui and Vi
has certain constraints, characterized by Ai entities each with Bi resistance points.
A The
travel time to cross this road at time t depends on these constraints: it takes t+1i + Bi
time units to overcome the entities and move on to city Vi , where the division is done
using integer division. You can start your journey at time 0 or any integer time unit
thereafter, and you can wait at any city for any integer amount of time. The goal is to
minimize the overall journey time. If it is not possible to reach city N , the result should
be −1. The task is to find the minimum time required to complete your journey.
Input :
Output :
Examples
Input :
2 3
1 2 2 3
1 2 2 1
1 1 1 1
Output :
By selecting the second road that links city 1 to city 2 and initiating the journey from
city 1 at time t = 0, you can arrive in city 2 with a calculated time of
1
+2=3
0+1
units, where the division is integer division. Thus, the minimal travel time required to
reach city 2 is 3 units.
Input :
Page 3
6 9
1 1 0 0
1 3 1 2
1 5 2 3
5 2 16 5
2 6 1 10
3 4 3 4
3 5 3 10
5 6 1 100
4 2 0 110
Output :
20
Hint :
j k
• ty = tx + Ai
tx +1
+ Bi
• tx is the time when you depart from city ’x’ for city ’y’
Bonus :
5. (20 points) You are about to join a Guild. There are N potions in this guild, each
with specific brewing requirements. You have been given a REQUIREMENTS list of size M ,
where REQUIREMENTS[i] consists of [’Pi ’, ’Qi ’] indicating that potion Pi must be brewed
before potion Qi can be brewed.
In one brewing cycle, you can prepare at most K potions. Thus, you must determine
the minimum number of brewing cycles required to brew all potions successfully.
Note: It is guaranteed that there is a feasible sequence to brew all the potions. The
most optimal solution is not required.
Format :
• The first line of each test case contains three single space-separated integers ’N’,
’M’ and ’K’, representing the number of potions, number of requirements, and
maximum potions that you can brew in one brewing cycle, respectively.
• The next ’M’ line of each test case contains two single space-separated integers ’P’
and ’Q’ representing that the potion ‘P‘ must be brewed before the potion ’Q’.
Constraints:
• 1 ≤ N ≤ 15
Page 4
• 0 ≤ M ≤ N · (N − 1)/2
• 1 ≤ Q, P and K ≤ N
Input :
432
21
31
14
Output :
3
Potion 1 can be brewed after potions 2 and 3 are brewed. Potion 4 can be brewed after
potion 1 is brewed.
Input :
542
21
31
41
15
Output :
4
Page 5