Assignment 02 MDMH
Assignment 02 MDMH
Course Title: Data Structure & Algorithm Lab II Course Code: CSE 2218
Trimester & Year: Fall 2023 Section: D Credit Hours: 1.0 (MdMH)
Currently, it’s Monday, and he needs to survive for the next ‘S’ days.
Find the minimum number of days on which you need to buy food from the shop so that he can
survive the next ‘S’ days or determine that it isn’t possible to survive.
Example 1:
Output: 2
Explanation: One possible solution is to buy a box on the first day (Monday), it’s sufficient to eat from
this box up to 8th day (Monday) inclusive. Now, on the 9th day (Tuesday), you buy another box and use
the chocolates in it to survive the 9th and 10th day.
Example 2:
Output: -1
Explanation: She can’t survive even if she buy food because the maximum number of units she can
buy in 1 day is less the required food for 1 day.
Primary Task: Create a function minimumDays() which takes S, N, and M as input parameters and
returns the minimum number of days Ishika needs to buy food. Otherwise, returns -1 if she cannot
survive.
Constraints:
1 ≤ N, S ≤ 50
1 ≤ M ≤ 30
2 of 7
UNITED INTERNATIONAL UNIVERSITY
Department of Computer Science and Engineering (CSE)
2. The difference between maximum number of chocolates given to a student and minimum number of
chocolates given to a student is minimum.
Example 1:
Input: N = 8, M = 5
Output: 6
Explanation: The minimum difference between maximum chocolates and minimum chocolates is 9 -
3 = 6 by choosing following M packets: {3, 4, 9, 7, 9}.
Example 2:
Input: N = 7, M = 3
Output: 2
Explanation: The minimum difference between maximum chocolates and minimum chocolates is 4 -
2 = 2 by choosing following M packets: {3, 2, 4}.
Primary Task: Create a function findMinDiff() which takes array A[ ], N and M as input parameters and returns the
minimum possible difference between maximum number of chocolates given to a student and minimum number of
chocolates given to a student.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 105
1 ≤ Ai ≤ 109
1≤M≤N
3 of 7
UNITED INTERNATIONAL UNIVERSITY
Department of Computer Science and Engineering (CSE)
Constraints:
2 <= cost.length <= 1000
0 <= cost[i] <= 999
4 of 7
UNITED INTERNATIONAL UNIVERSITY
Department of Computer Science and Engineering (CSE)
The first line of input contains an integer 'T' denoting the number of test cases.
Each test case contains 4 integers denoting 'N', 'X', 'Y', and 'Z', where 'N' is the length of the rod and 'X', 'Y', 'Z' are
the segments into which a given rod can be cut into.
Output Format:
For each test case, return the maximum number of cut segments from the given rod.
Constraint:
1 <= T <= 50
1<= X, Y, Z <= N
Sample Input 1:
2
7 5 2 2
8 3 3 3
Sample Output 1:
2
0
In the second case, there is no way to cut into segments of 3 length only as the length of the rod is
less than the given length.
Sample Input 2:
2
7322
8144
Sample Output 2:
3
8
Explanation:
In the first test case, cut it into 3 parts of 3, 2 and 2.
In the second case, cut it into 8 parts of length 1.
6 of 7
UNITED INTERNATIONAL UNIVERSITY
Department of Computer Science and Engineering (CSE)
Input format:
The first line contains a single integer 'T' denoting the number of test cases.
The first line of each test case contains a single integer 'N' denoting the number of elements in the array.
The second line contains 'N' single space-separated integers denoting the elements of the array/list.
Output format:
For each test case, print a single integer that denotes the maximum sum of the non-adjacent elements.
Constraints:
Sample Input 1:
1 2 4
2 1 4 9
Sample Output 1:
11
In test case 1, the sum of 'ARR[0]' & 'ARR[2]' is 5 which is greater than 'ARR[1]' which is 2 so the answer
is 5.
7 of 7
UNITED INTERNATIONAL UNIVERSITY
Department of Computer Science and Engineering (CSE)
In test case 2, the sum of 'ARR[0]' and 'ARR[2]' is 6, the sum of 'ARR[1]' and 'ARR[3]' is 10, and the sum
of 'ARR[0]' and 'ARR[3]' is 11. So if we take the sum of 'ARR[0]' and 'ARR[3]', it will give the maximum
sum of sequence in which no elements are adjacent in the given array/list.
Sample Input 2:
12354
123135819
Sample Output 2:
24
In test case 1, out of all the possibilities, if we take the sum of 'ARR[0]', 'ARR[2]' and 'ARR[4]', i.e. 8, it will
give the maximum sum of sequence in which no elements are adjacent in the given array/list.
In test case 2, out of all the possibilities, if we take the sum of 'ARR[0]', 'ARR[2]', 'ARR[4]', 'ARR[6]' and
'ARR[8]', i.e. 24 so, it will give the maximum sum of sequence in which no elements are adjacent in the
given array/list.