(Solution) Mid CSE221 Fall '23
(Solution) Mid CSE221 Fall '23
BRAC UNIVERSITY
Department of Computer Science and Engineering
1. a. Explain the time complexity of the following code snippet in regards of the Big-O notation: 04
CO2
1. for (i=0; i<n; i+=4) {
2. for (j=1; j<n; j*=2) {
3. for (k=0; k<30; k++) {
4. print(“Am I still not 30?!!”);
5. }
6. print(“Why, God, why? We had a Deal!”);
7. for (m=n; m>0; m-=2) {
8. print(“Could you BE more dramatic?”);
9. }
10. }
11. }
Solution: n2*log2n
index 0 1 2 3 4 5 6 7
value 9 12 15 2 4 5 7 8
Given the value of i, propose an algorithm to search a key_value in the array. Complexity of your
algorithm must be less than O(N).
a) Present your solution idea as a code/ pseudocode/ flowchart/ step-by-step instructions/ 04
logical explanations in short.
b) Write the time complexity of your presented solution. 01
c) Show a simulation of the Merge Sort algorithm to organize the whole array in increasing 05
order.
Solution:
a) Idea 1: Call binary search on arr[0: i] and arr[i: len(arr)] separately
Idea 2: if key < arr[len(arr)-1] : just search the right portion, otherwise the left portion
b) O(logn)
c)
3. CO1 Your friend gave you a binary string B (meaning each character is either 0 or 1). He wanted to
find out how to calculate the maximum number of consecutive 0s in that particular string. For
example,
You, as an algorithm enthusiast, know that this can be solved in linear time. However, your friend
asked you to propose a Divide and Conquer approach
Solution:
a. Suitable divide and conquer algorithm for this task will be the “maximum sub array sum
problem” which uses the idea of the solution existing either on the left divided
subproblems, right divided subproblems or the solutions existing in the crosspoint of those
two divided subproblems each.
b. Code Solution:
def crossmax(st,mid):
left_sum = right_sum= 0
i = mid
#calculating the left portion's maximum longest sum in
crossover point
while i>=0:
if st[i]!="0":
break
left_sum+=1
i-=1
#calculating the right portion's maximum longest sum in
crossover point
i=mid+1
while i<len(st):
if st[i]!="0":
break
right_sum+=1
i+=1
if right_sum==0 or left_sum==0:
return float("-inf")
return left_sum + right_sum
def maxZero(st):
if len(st)==1:
if st[0]=="0":
return 1
else: return float("-inf")
mid = len(st)//2
left_max = maxZero(st[:mid])
right_max = maxZero(st[mid:])
cross_max= crossmax(st,mid)
return max(left_max,right_max,cross_max)
print(maxZero("100100000111"))
4. a. You have the following adjacency matrix for a graph. However, some of the entries are missing. 06
CO1 Your job is to find these missing entries with the help of some clues. Then draw the graph.
Clues:
● E can not be reached from B
● D can be reached from A
● 2 x |E| = 3 x |V|
(twice the no. of edges is equal to thrice
the no. of vertices)
● In-degree of B is not a prime number
Solution:
(CF : 0)
(DB : 1)
(FC : 1)
(FE : 0)
1. a. Explain the time complexity of the following code snippet in regards of the Big-O notation: 04
CO2
1. for (i=0; i<n; i+=4) {
2. for (j=1; j<n; j*=2) {
3. for (k=0; k<20; k++) {
4. print(“Am I still not 30?!!”);
5. }
6. print(“Why, God, why? We had a Deal!”);
7. for (m=n; m>0; m-=4) {
8. print(“Could you BE more dramatic?”);
9. }
10. }
11. }
Solution: n2*log2n
index 0 1 2 3 4 5 6 7
value 9 12 15 20 4 5 7 8
Given the value of i, propose an algorithm to search a key_value in the array. Complexity of your
algorithm must be less than O(N).
a) Present your solution idea as a code/ pseudocode/ flowchart/ step-by-step instructions/ 04
logical explanations in short.
b) Write the time complexity of your presented solution. 01
c) Show a simulation of the Merge Sort algorithm to organize the whole array in increasing 05
order.
3. CO1 Your friend gave you a binary string B (meaning each character is either 0 or 1). He wanted to
find out how to calculate the maximum number of consecutive 0s in that particular string. For
example,
You, as an algorithm enthusiast, know that this can be solved in linear time. However, your friend
asked you to propose a Divide and Conquer approach 02
d) Name a suitable Divide and Conquer algorithm for this task. 06
e) Explain how you can apply that algorithm in this scenario. Present your idea in a
pseudocode/programmable code/Flowchart/step-by-step instructions. 02
f) Write the time complexity of your algorithm.
4. a. You have the following adjacency matrix for a graph. However, some of the entries are missing. 06
CO1 Your job is to find these missing entries with the help of some clues. Then draw the graph.
Clues:
● B can not be reached from C
● A can be reached from D
● 2 x |E| = 3 x |V|
(twice the no. of edges is equal to
thrice the no. of vertices)
● In-degree of C is not a prime number.
Solution:
(AC : 1)
(EB : 0)
(EF : 1)
(FE : 0)