0% found this document useful (0 votes)
2 views

Data-Structure-and-Algorithms-II-Mid-Solution

This document contains mid-term question solutions for Data Structures and Algorithms II, authored by Nurul Alam Ador, updated until Spring 2024. It includes problems and solutions related to dynamic programming, divide and conquer algorithms, and time complexity analysis, covering various topics such as minimum tile coverage, maximum-sum subarrays, and algorithm efficiency. The document is structured by trimester, providing a comprehensive guide for students preparing for exams in this subject.

Uploaded by

shanto.nirob47
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Data-Structure-and-Algorithms-II-Mid-Solution

This document contains mid-term question solutions for Data Structures and Algorithms II, authored by Nurul Alam Ador, updated until Spring 2024. It includes problems and solutions related to dynamic programming, divide and conquer algorithms, and time complexity analysis, covering various topics such as minimum tile coverage, maximum-sum subarrays, and algorithm efficiency. The document is structured by trimester, providing a comprehensive guide for students preparing for exams in this subject.

Uploaded by

shanto.nirob47
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

`

MID-TERM QUESTION SOLUTIONS

DATA STRUCTURE
AND ALGORITHMS II
CSE 2217

SOLUTION BY
NURUL ALAM ADOR

UPDATED TILL SPRING 2024

nurulalamador.github.io/UIUQuestionBank
Index

Trimester Page

Spring 2024 3

Fall 2023 11

Summer 2023 19

Spring 2023 29

Fall 2022 37

Important Note
The simulation of various kinds of algorithms (such as maximum sum
subarray and minimum-maximum value) could vary by teacher. In this
solution, we follow Md. Tamzid Hossain sir's simulation technique.

nurulalamador.github.io/UIUQuestionBank 2
Spring 2024

1. a) Imagine you have a land with an area of 1x10 m2, and you want to cover it using
some tiles. There are three types of tiles available which varies in size: 1x1 m2, 1x5 m2
and 1x6 m2 (See figure). Each type has infinite amount of supplies, so you never run
out of tiles. Now, there are many ways that you can cover that land using these
tiles, but you want to use as few tiles as possible (Tiles are expensive!)

1x6 1x5 1x1

Tiles

Land 1 x 10

Using Dynamic Programming method, find the minimum number of tiles that can
cover your land. Which tiles should we use? Describe your solution with detailed
calculation.
Solution:
Given,
Total Land = 1 × 10 𝑚2 = 10 𝑚2
Available Tiles, 𝑇𝑖 = {𝑇1 , 𝑇2 , 𝑇3 }
= {1 × 6 𝑚2 , 1 × 5 𝑚2 , 1 × 1 𝑚2 }
= {6 𝑚2 , 5 𝑚2 , 1 𝑚2 }

Now,
Total Land, 𝐿 (𝑚2 )
0 1 2 3 4 5 6 7 8 9 10

1 0 1 2 3 4 5 6 7 8 9 10
Tiles, 𝑇 (𝑚2 )

5 0 1 2 3 4 1 2 3 4 5 2

6 0 1 2 3 4 1 1 2 3 4 2

∴ Minimum tiles needed = 2


Tiles should be use: 2 tiles of 1 x 5 m2

1. b) Suppose your wallet has capacity to hold only 8 grams of gold coins, and your best
friend just offered you 4 gold coins from his own collection. The weights and the
values of the coins are as follows: [5g, 4g, 6g, 3g] and [110$, 100$, 120$, 90$]. Using
Dynamic Programming, determine which coins you should take so that your total
gain is maximized. Keep in mind that you cannot carry more than 8 grams of gold
coins.

nurulalamador.github.io/UIUQuestionBank 3
Solution:
Here,
Capacity = 8 g

Coin No 1 2 3 4

Values ($) 110 100 120 90

Weight (g) 5 4 6 3

Now,
Weight (g)
0 1 2 3 4 5 6 7 8

0 0 0 0 0 0 0 0 0 0

1 0 0 0 0 0 110 110 110 110

2 0 0 0 0 100 110 110 110 110


Coins

3 0 0 0 0 100 110 120 120 120

4 0 0 0 90 100 110 120 190 200

∴ Total maximize gain = 200$


Coin should take = 1st (110$) and 4th (90$)

2. a) Consider the following array

-1 10 -1 6 -2 -4 2 2 8 -4

Find the Maximum-sum subarray using divide-and-conquer. You must show the
recursion tree and clearly mention left, right and crossing sum for each tree node.
Solution:
[ P.T.O ]

nurulalamador.github.io/UIUQuestionBank 4
(2, 9, 21)

1 2 3 4 5 6 7 8 9 10

-1 10 -1 6 -2 -4 2 2 8 -4
(2, 9, 21)
(2, 4, 15) (7, 8, 12)
1 2 3 4 5 6 7 8 9 10

-1 10 -1 6 -2 -4 2 2 8 -4
1 2 3 4 5 21 6 7 8 9 10

nurulalamador.github.io/UIUQuestionBank
-1 10 -1 6 -2 -4 2 2 8 -4
(2, 4, 15) (7, 8, 12)
1 2 3 4 5 6 7 8 9 10
(2, 2, 10) (4, 4, 6) (7, 8, 4) (9, 9, 8)
-1 10 -1 6 -2 -4 2 2 8 -4
15 12
1 2 3 4 5 6 7 8 9 10

-1 10 -1 6 -2 -4 2 2 8 -4

(2, 2, 10) (2, 3, 9) (3, 3, -1) (4, 4, 6) (4, 5, 4) (5, 5, -2) (7, 7, 2) (7, 8, 4) (8, 8, 2) (9, 9, 8) (9, 10, 4) (10, 10, -4)

1 2 1 2 3 3 4 4 5 5 6 7 6 7 8 8 9 9 10 10

-1 10 -1 10 -1 -1 6 6 -2 -2 -4 2 -4 2 2 2 8 8 -4 -4
9 4 4 4
(1, 1, -1) (1, 2, 9) (2, 2, 10) (6, 6, -4) (6, 7, -2) (7, 7, 2)

1 1 2 2 6 6 7 7
-1 -1 10 10 -4 -4 2 2
9 -2

∴ Maximum continuous subarray is:


2 3 4 5 6 7 8 9

10 -1 6 -2 -4 2 2 8 (Maximum Sum = 21)

5
2. b) Design an algorithm using the divide and conquer approach that counts the
number of elements in an array that end with the digit ‘1’. The algorithm should
make use of the modulus operator to determine if an element ends with ‘1’. Include
the pseudocode for your algorithm, clearly defining the base case and detailing all
necessary steps and calculations.
Solution:
The pseudocode has been written below:

CountEndsWithOne(arr[], left, right) {

if (left == right) {
if(arr[left] % 10 == 1){
return 1;
}
Base Case
else {
return 0;
}
}

else {
mid = (left + right) / 2;

leftCount = countEndsWithOne(arr, left, mid);


rightCount = countEndsWithOne(arr, mid + 1, right);

return leftCount + rightCount;


}

}
If it is not base case, dividing the array in two nearly
equal halves and merging their return result

3. a) Using recursion tree method or the Master Theorem find out a good asymptotic
upper bound on the following recurrence:
T(n) = 3T(n/2) + O(n)
Solution:
Given,
𝑛
𝑇(𝑛) = 3𝑇 ( ) + 𝑂(𝑛)
2
𝑛 p
Comparing with base equation of Master method 𝑇(𝑛) = 𝑎𝑇 ( ) + 𝑂(𝑛𝑘 log 𝑛),
𝑏
Here,
𝑎=3
𝑏=2
𝑘=1
𝑝=0
𝑏 𝑘 = 21 = 2

Since 𝑎 > 𝑏 𝑘 , therefore this recurrence belongs to case 1 of Master Theorem.

nurulalamador.github.io/UIUQuestionBank 6
∴ Time complexity, 𝑇(𝑛) = 𝑂(𝑛log𝑏 𝑎 )
= 𝑂(𝑛log2 3 )
= 𝑂(𝑛1.58 )

∴ Asymptotic upper bound of given recurrence is 𝑂(𝑛1.58 ).

3. b) Consider the following function which takes two arrays (A and B) and their lengths
(n and m) as inputs respectively.

void func1 (int A[], int B[], int n, int m) {


int sumA = 0;
for(int i=0; i<n; i++) {
sumA = sumA + A[i];
}

int sumB = 0;
for(int i=0; i<m; i++) {
sumB = sumB + B[i];
}

int count = 0;

for(int i=0; i<n; i++) {


for(int j=0; j<m; j++) {
if(A[i]*A[i]>B[i] || sumA>sumB) {
count++;
} else {
break;
}
}
}
}

Now determine the following:


i. The exact-cost equation for the running-time.
ii. The best case and worst case running time using the Big - Oh notation
iii. Examples of best case and worst case inputs
Solution:

Line Worst Case Best Case

2 1 1

3 𝑛+1 𝑛+1
4 𝑛 𝑛

7 1 1
8 𝑚+1 𝑚+1
9 𝑚 𝑚
12 1 1

nurulalamador.github.io/UIUQuestionBank 7
14 𝑛+1 𝑛+1
15 𝑛𝑚 + 𝑛 𝑛
16 𝑛𝑚 𝑛

17 𝑛𝑚 0

19 0 𝑛

i) Time complexity, 𝑇(𝑛) = 1 + 𝑛 + 1 + 𝑛 + 1 + 𝑚 + 1 + 𝑚 + 1 + 𝑛 + 1 + 𝑛𝑚 + 𝑛 + 2𝑛𝑚


= 3𝑛𝑚 + 4𝑛 + 2𝑚 + 6

∴ Exact-cost equation is 3𝑛𝑚 + 4𝑛 + 2𝑚 + 6

ii) Best case, 𝑇(𝑛) = 1 + 𝑛 + 1 + 𝑛 + 1 + 𝑚 + 1 + 𝑚 + 1 + 𝑛 + 1 + 𝑛 + 𝑛 + 𝑛


= 6𝑛 + 2𝑚 + 6
= 𝑂(𝑛 + 𝑚)

Worst case, 𝑇(𝑛) = 1 + 𝑛 + 1 + 𝑛 + 1 + 𝑚 + 1 + 𝑚 + 1 + 𝑛 + 1 + 𝑛𝑚 + 𝑛 + 𝑛𝑚 + 𝑛𝑚


= 3𝑛𝑚 + 4𝑛 + 2𝑚 + 6
= 𝑂(𝑛𝑚)

iii) Example of best case inputs:

A[ ] = 1 1 1 1 B[ ] = 10 10 10 10 n=4 m=4

Example of worst case:

A[ ] = 10 10 10 10 B[ ] = 1 1 1 1 n=4 m=4

4. a) Given the arrival and the departure times (in minutes) of 8 trains for a railway
platform, find out the maximum number of trains that can use the platform
without any collision, using a greedy algorithm. There must be exist at least 10
minutes of safety break between the departure of one train and arrival of the next
one.
[1000, 1030], [840, 1030], [850, 1040], [1700, 2000], [800, 835], [1300, 1800], [1500,
1650], [1200, 1380]
Explain your strategy very briefly and show detailed calculations. No need to write
pseudocode. Explain why your solution satisfies the optimal substructure
property.
Solution:
Sorting train based on departure time:

Train, 𝒕𝒊 𝒕𝟏 𝒕𝟐 𝒕𝟑 𝒕𝟒 𝒕𝟓 𝒕𝟔 𝒕𝟕 𝒕𝟖
Arrival Time 800 840 1000 850 1200 1500 1300 1700

Departure Time 835 1030 1030 1040 1380 1650 1800 2000

Now,

nurulalamador.github.io/UIUQuestionBank 8
1 [800, 835]
2 [840, 1030]

3 [1000, 1030]

4 [850, 1040]

5 [1200, 1380]
Train

6 [1500, 1650]

7 [1300, 1800]

8 [1700, 2000]

500 1000 1500 2000

Time

∴ Maximum 5 trains [Scheduled for [800, 835], [1000, 1030], [1200, 1380], [1500, 1650]
and [1700, 2000]] can use the platform without any collision.

Here, we use Activity Selection strategy. At first, we sort all the train in ascending
order on the based on departure time. Then, we select the first train and select next
train which arrival time is at least after 10 minutes from first train departure time.

Our solution satisfies the optimal substructure property because here an optimal
solution to the entire problem can be constructed from optimal solutions to its sub-
problems. By selecting the train with the earliest departure time, the remaining
sub-problems can be solved using the same approach, leading to an overall optimal
solution.

4. b) “Data encoded using Huffman coding is uniquely decodable” - is the statement


true or false? Justify your answer.
Solution:
The following statement is true. Huffman coding ensures unique decodability
through its prefix property, where no code is a prefix of any other code. This property
allows the encoded data to be parsed unambiguously from left to right without
additional delimiters. During decoding, each bit sequence can be traced in the
Huffman tree to a unique leaf node, ensuring that each sequence corresponds to a
unique symbol. Consequently, Huffman coding guarantees that the encoded data
can be uniquely and correctly decoded.

4. c) Suppose you have a cup with capacity 750 ml. The following table shows that there
are 5 flavors of drinks. For each flavor, there is only 𝑑 ml available, and each flavor
of drink contains a total of 𝑠 grams sugar. At most how many grams of sugar can
you consume if you fill up the cup using greedy approach? You need to show all
the steps of your calculation.

[ P.T.O ]

nurulalamador.github.io/UIUQuestionBank 9
Flavor Volume (𝑑 ) Total sugar (𝑠)

Apple 320 35

Orange 220 20

Pineapple 240 40

Cranberry 200 30

Strawberry 280 25

Solution:
Given,
No of juices, 𝑛 = 5
Capacity, 𝐷 = 750 𝑚𝑙

Index, 𝒊 1 2 3 4 5

Flavor Apple Orange Pineapple Cranberry Strawberry

Sugar, 𝑠𝑖 35 20 40 30 25

Volume, 𝑑𝑖 320 220 240 200 280

Finding sugar per volume:

1 2 3 4 5
Apple Orange Pineapple Cranberry Strawberry
𝑠𝑖
0.109 0.091 0.167 0.150 0.089
𝑑𝑖

𝑠𝑖
Sorting item based on :
𝑑𝑖
Pineapple > Cranberry > Apple > Orange > Strawberry

Adding item in knapsack:

𝑫
𝒊 Flavor 𝒔𝒊 𝒅𝒊 𝑫 𝒙𝒊 = min (𝟏, ) 𝑫′ = 𝑫 − 𝒅𝒊 𝒙𝒊
𝒅𝒊
3 Pineapple 40 240 750 1 750-240 = 510

4 Cranberry 30 200 510 1 510-200 = 310

1 Apple 35 320 310 0.96875 310-(320*0.96875) = 0

∴ Maximum Sugar = 𝑠3 𝑥3 + 𝑠4 𝑥4 + 𝑠1 𝑥1
= 40 × 1 + 30 × 1 + 35 × 0.96875
= 103.90 𝑔

∴ At most 103.90 grams of sugar can be consumed.

nurulalamador.github.io/UIUQuestionBank 10
1.
a)
(2, 4, 5)

Solution:
1 2 3 4 5 6

-2 3 -1 3 -4 4
(2, 2, 3) (6, 6, 4)
(2, 4, 5)

1 2 3 1 2 3 4 5 6 4 5 6

nurulalamador.github.io/UIUQuestionBank
-2 3 -1 -2 3 -1 3 -4 4 3 -4 4

5
(2, 2, 3) (2, 3, 2) (3, 3, -1) (4, 4, 3) (4, 6, 3) (6, 6, 4)

1 2 1 2 3 3 4 5 4 5 6 6

-2 3 -2 3 -1 -1 3 -4 3 -4 4 4
Fall 2023

2 3
(1, 1, -2) (1, 2, 1) (2, 2, 3) (4, 4, 3) (4, 5, -1) (5, 5, -4)

1 1 2 2 4 4 5 5
mention left, right and crossing sum for each tree node.

-2 -2 3 3 3 3 -4 -4

1 -1

∴ Maximum sub continuous subarray is:

2 3 4

3 -1 3 (Maximum Sum = 5)
Given an array A = {-2, 3, -1, 3, -4, 4}, find the maximum-sum continuous subarray
using divide-and-conquer approach. You must show the recursion tree and clearly

11
1. b) Find out a good asymptotic upper bound on the following recurrence:
𝑛
𝑇(𝑛) = 3𝑇 ( ) + 𝑂(𝑛2 )
4
You may use Recursion-tree or Master method to solve the recurrence.
Solution:
Given,
𝑛
𝑇(𝑛) = 3𝑇 ( ) + 𝑂(𝑛2 )
4
𝑛 p
Comparing with base equation of Master method 𝑇(𝑛) = 𝑎𝑇 ( ) + 𝑂(𝑛𝑘 log 𝑛),
𝑏
Here,
𝑎=3
𝑏=4
𝑘=2
𝑝=0
𝑏 𝑘 = 42 = 16

Since 𝑎 < 𝑏 𝑘 and 𝑝 ≥ 0, therefore this recurrence belongs to 1st condition of case 3.
𝑝
∴ Time complexity, 𝑇(𝑛) = 𝑂(𝑛𝑘 log 𝑛)
= 𝑂(𝑛2 log 0 𝑛)
= 𝑂(𝑛2 )

∴ Asymptotic upper bound of given recurrence is 𝑂(𝑛2 ).

1. c) Given an array of integers A = {1, 3, -5, 2, -3, -2}, find the Maximum and Minimum
using divide-and-conquer. Show the necessary steps to support your answer.
Solution:
(-5, 3)

1 3 -5 2 -3 -2

(-5, 3) (-3, 2)

1 3 -5 2 -3 -2

(1, 3) (-5, -5) (-3, 2) (-2, -2)

1 3 -5 2 -3 -2

(1, 1) (3, 3) (2, 2) (-3, -3)

1 3 2 -3

∴ Maximum value is 3 and minimum value is -5.

nurulalamador.github.io/UIUQuestionBank 12
2. a) After obtaining your BSCSE degree, you embarked on an entrepreneurial journey
and established your own thriving software company. You've been consistently
successful in securing projects from a variety of clients, ensuring a steady flow of
profits. However, suddenly a situation arises where your decision-making and
leadership skills are put to the test.

You have just received 5 project offers from different clients, but you have only 7
days to complete the projects. Your project manager prepares the following
estimates for each of the projects and presents them to you for your decision.

Net Profit
200 150 100 50 300
(In Million Dollars)

Duration
3 2 1 2 5
(In Days)

Being an adept CSE graduate, you decide to approach the problem using dynamic
programming. Determine which of the projects can be taken to maximize the net
profit.

Note that you cannot partially complete a project. Also, you are unable to work on
two projects at the same time on a particular day.
Solution:
Here,
Total times = 7 days

Project 1 2 3 4 5

Net Profit 200 150 100 50 300

Days 3 2 1 2 5

Now,
Duration
0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0

1 0 0 0 200 200 200 200 200

2 0 0 150 200 200 350 350 350


Projects

3 0 100 150 250 300 350 450 450

4 0 100 150 250 300 350 450 450

5 0 100 150 250 300 350 450 450

∴ Total maximize profit = 450 Million Dollars


Selected projects = 1, 2, 3

∴ 1st, 2nd and 3rd project can be selected for maximum profit.

nurulalamador.github.io/UIUQuestionBank 13
2. b) Both the algorithmic paradigms: Divide-and-Conquer and Dynamic Programming
solve a problem by breaking it into smaller problem instances, and by solving
them. What is the fundamental difference between these two paradigms?
Solution:
Both the algorithmic paradigms Divide-and-Conquer and Dynamic Programming
solve a problem by breaking it into smaller sub-problems but there are some
fundamental difference. These differences are:

Divide and Conquer Dynamic Programming

Break the problem into independent Break the problem into overlapping sub-
sub-problems. problems.

Solve sub-problems in a bottom-up


Solve each sub-problem recursively. approach, often store previously
encountered sub-problems to use later.

Combines the solution of all sub- Avoid solving the same sub-problems
problems to get the final solution. multiple time by reusing stored solution.

2. c) You have found a treasure containing an infinite supply of $23, $16, $9, and $1
Coins. To go back home you need to pay a total of $25. However, despite having an
infinite supply of coins you want to pay using the minimum number of coins. Give
an example where the greedy approach does not provide an optimal solution in
this matter.
Solution:
We have infinite supply of:
$23, $16, $9, $1

We need to pay $25.

In this scenario, if we use greedy method,


We will need to use these coins:
$23, $1, $1.
∴ Total 3 coin will needed in greedy method.

But if we use any other approach, like Dynamic Programming, then we can pay
$25 in only 2 coins ($16, $9).

∴ Greedy approach does not provide an optimal solution in this matter.

3. a) Derive the best-case and the worst-case running time equations for the following
function specialTask and represent using Asymptotic Notation.

1 void specialTask(int arr[], int n) {


2 for(int i=0; i<n; i++) {
3 for(int j=n-1; j>i; j--) {
4 if(arr[j]<arr[j-1]) {
5 swap(arr[j], arr[j-1]);
6 }
7 }

nurulalamador.github.io/UIUQuestionBank 14
8 printArray(arr, n);
9 bool flag = true;
10 for(int j=n-1; j>0; j--){
11 if(arr[j]<arr[j-1]) {
12 flag = false;
13 }
14 }
15 if(flag == true) {
16 return;
17 }
18 }
19 return;
20 }

Solution:

Line Worst Case Best Case

2 𝑛+1 1
𝑛(𝑛 + 1)
3 +𝑛 𝑛
2
𝑛(𝑛 + 1)
4 𝑛−1
2
𝑛(𝑛 + 1)
5 0
2
8 𝑛 1
9 𝑛 1
10 𝑛×𝑛 𝑛
11 𝑛(𝑛 − 1) 𝑛−1
12 𝑛(𝑛 − 1) 0

15 𝑛 1
16 0 1
19 1 1

∴ Best case, 𝑇(𝑛) = 1 + 𝑛 + 𝑛 − 1 + 1 + 1 + 𝑛 + 𝑛 − 1 + 1 + 1 + 1


= 4𝑛 + 4
= 𝑂(𝑛)

𝑛(𝑛 + 1) 𝑛(𝑛 + 1) 𝑛(𝑛 + 1)


∴ Worst case, 𝑇(𝑛) = 𝑛 + 1 + +𝑛+ + +𝑛+𝑛+𝑛×𝑛
2 2 2
+𝑛(𝑛 − 1) + 𝑛(𝑛 − 1) + 𝑛 + 1
𝑛(𝑛 + 1)
= 4𝑛 + 3 + 4 + 𝑛2 + 𝑛2 − 𝑛 + 𝑛2 − 𝑛 + 𝑛
2
3
= 3𝑛2 + 𝑛(𝑛 + 1) + 4𝑛 + 4
2
= 𝑂(𝑛2 )

nurulalamador.github.io/UIUQuestionBank 15
3. b) Derive the exact-cost equation for the running time of the following function
funCTION and find the time complexity in Big-Oh notation.

1 int funCTION(int n, int m) {


2 int sumUP = 0;
3 for(int i=1; i<=n; i=i+2) {
4 int r = m;
5 while(r>=1) {
6 sumUP = sumUP+r;
7 r = r/3;
8 }
9 }
10 return sumUP;
11 }

Solution:

Line Time Cost

2 1
𝑛
3 +1
2
𝑛
4
2
𝑛 𝑛
5 × log 3 𝑚 +
2 2
𝑛
6 × log 3 𝑚
2
𝑛
7 × log 3 𝑚
2
10 1

𝑛 𝑛 𝑛 𝑛 𝑛 𝑛
∴ Time complexity, 𝑇(𝑛) = 1 + + 1 + + × log3 𝑚 + + × log3 𝑚 + × log3 𝑚 + 1
2 2 2 2 2 2
3 3
= 𝑛 + 𝑛 log 3 𝑚 + 3
2 2
= 𝑂(𝑛 log 𝑚)

3 3
∴ Exact cost equation is 𝑛 + 𝑛 log3 𝑚 + 3 and time complexity is 𝑂(𝑛 log 𝑚).
2 2

4. a) Find an optimal solution to the fractional knapsack instance of n = 5, W = 7, (v1, v2,


v3, v4, v5) = (50, 30, 35, 60, 25), and (w1, w2, w3, w4) = (2, 2, 1, 3, 2). Explain why your
solution satisfies the optimal substructure property.
Solution:
Given,
𝑛=5
𝑊=7

[ P.T.O ]

nurulalamador.github.io/UIUQuestionBank 16
Item, 𝒊𝒊 𝒊𝟏 𝒊𝟐 𝒊𝟑 𝒊𝟒 𝒊𝟓
Value, 𝑣𝑖 50 30 35 60 25

Weight, 𝑤𝑖 2 2 1 3 2

Finding price per unit:

𝑖1 𝑖2 𝑖3 𝑖4 𝑖5
𝑣𝑖
25 15 35 20 12.5
𝑤𝑖

𝑣𝑖
Sorting item based on :
𝑤𝑖
𝑖3 > 𝑖1 > 𝑖4 > 𝑖2 > 𝑖5

Adding item in knapsack:

𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
𝑖3 35 1 7 1 7-1 = 6

𝑖1 50 2 6 1 6-2 = 4

𝑖4 60 3 4 1 4-3 = 1

𝑖2 30 2 1 0.5 1-(2×0.5) = 1-1 = 0

∴ Total Profit = 𝑣3 𝑥3 + 𝑣1 𝑥1 + 𝑣4 𝑥4 + 𝑣2 𝑥2
= 35 × 1 + 50 × 1 + 60 × 1 + 30 × 0.5
= 160

This solution satisfies the optimal substructure property. Because if the knapsack
capacity was 1, then 35 × 1 = 35 was the highest profit, if knapsack size was 3. than
35 × 1 + 50 × 1 = 85 was the highest profit and like that, every sub-problem in here
are the optimal solution for that stage.

4. b) What is activity selection problem? Is it true that the activity selection problem
has one unique optimal solution? Justify your answer.
Solution:
Activity selection is a problem where various activities are given along with their
starting and finishing time. We need to find the out maximum how many activities
we can select in a specific time period.

It is false that Activity Selection problem has one unique solution. If there are three
activities such that {[7,12), [8,12), [13,16)} then some algorithm will give solution
{[7, 12), [13,16)} and other algorithm may give solution {[8,12), [13,16)}. But number
of maximum selected activities will be same.

4. c) You are given the arrival and the departure times of eight trains for a railway
platform, and each one is in the format: [arrival time, departure time). Only one

nurulalamador.github.io/UIUQuestionBank 17
train can use the platform at a time. Suppose that you have got the following train-
use requests for the next day.
{ [8, 12), [6, 9), [11, 14), [2, 7), [1, 7), [12, 20), [7, 12) , [13, 19) }
Find the maximum number of trains that can use the platform without any
collision by using earliest departure time.
Solution:
Given,
Following train requested for use platform:
{ [8, 12), [6, 9), [11, 14), [2, 7), [1, 7), [12, 20), [7, 12) , [13, 19) }

Sorting train based on departure time:

Train, 𝒕𝒊 𝒕𝟏 𝒕𝟐 𝒕𝟑 𝒕𝟒 𝒕𝟓 𝒕𝟔 𝒕𝟕 𝒕𝟖
Arrival Time 1 2 6 7 8 11 13 12

Departure Time 7 7 9 12 12 14 19 20

Now,

1 [1, 7)

2 [2, 7)

3 [6, 9)

4 [7, 12)
5 [8, 12)
Train

6 [11, 14)

7 [13, 19)
8 [12, 20)

5 10 15 20

Time

∴ Maximum 3 trains [Scheduled for [1, 7), [7, 12) and [13, 19)] can use the platform
without any collision.

nurulalamador.github.io/UIUQuestionBank 18
Summer 2023

1. a) Derive the best-case, and the worst-case running time equations for the following
function connectDots and represent using Asymptotic Notation.

1 bool connectDots(int arr[], int n){


2 int numberOfDots = 0;
3 for(int i=0; i<n; i++){
4 if(arr[i]&1){
5 for(int j=i+1; j<=n-1; j++){
6 numberOfDots++; 7
7 }
8 for(int j=0;j<=i; j++){
9 numberOfDots *= 2;
10 }
11 }
12 int j = 1; 13
13 while(j<=n){
14 numberOfDots+=j;
15 j = j * 2;
16 }
17 }
18 return (numberOfDots&1);
19 }

Solution:

Line Worst Case Best Case

2 1 1

3 𝑛+1 𝑛+1

4 𝑛 𝑛

𝑛(𝑛 + 1)
5 +𝑛 0
2
𝑛(𝑛 + 1)
6 0
2
𝑛(𝑛 + 1)
8 +𝑛 0
2
𝑛(𝑛 + 1)
9 0
2
12 𝑛 𝑛
13 𝑛 log 2 𝑛 + 𝑛 𝑛 log 2 𝑛 + 𝑛
14 𝑛 log 2 𝑛 𝑛 log 2 𝑛
15 𝑛 log 2 𝑛 𝑛 log 2 𝑛
18 1 1

nurulalamador.github.io/UIUQuestionBank 19
∴ Best case, 𝑇(𝑛) = 1 + 𝑛 + 1 + 𝑛 + 𝑛 + 𝑛 log2 𝑛 + 𝑛 + 𝑛 log2 𝑛 + 𝑛 log2 𝑛 + 1
= 3𝑛 log 2 𝑛 + 4𝑛 + 3
= 𝑂(𝑛 log 𝑛)

𝑛(𝑛 + 1) 𝑛(𝑛 + 1) 𝑛(𝑛 + 1) 𝑛(𝑛 + 1)


∴ Worst case, 𝑇(𝑛) = 1 + 𝑛 + 1 + +𝑛+ + +𝑛+ +𝑛
2 2 2 2
+𝑛 log 2 𝑛 + 𝑛 + 𝑛 log 2 𝑛 + 𝑛 log 2 𝑛 + 1
𝑛2 + 𝑛
=4 + 3𝑛 log 2 𝑛 + 5𝑛 + 3
2
= 2𝑛2 + 7𝑛 + 3𝑛 log 2 𝑛 + 3
= 𝑂(𝑛2 )

1. b) Derive the exact-cost equation for the running time of the following function and
find the time complexity in big-oh notation.

1 for (int i = 1; i <= n; i = i * 2){


2 for (int j = 1; j <= i; j++){
3 for (int k = n; k >= i; k--){
4 printf("%d ", k);
5 }
6 printf("\n");
7 }
8 printf("\n");
9 }

Solution:

Line Time Cost

1 log 2 𝑛 + 1
log 2 𝑛 (log 2 𝑛 + 1)
2 + log 2 𝑛
2
log 2 𝑛 (log 2 𝑛 + 1) log 2 𝑛 (log 2 𝑛 + 1) log 2 𝑛 (log 2 𝑛 + 1)
3 × +
2 2 2
log 2 𝑛 (log 2 𝑛 + 1) log 2 𝑛 (log 2 𝑛 + 1)
4 ×
2 2
log 2 𝑛 (log 2 𝑛 + 1)
6
2
8 log 2 𝑛

log2 𝑛 (log2 𝑛 + 1) log 𝑛 (log2 𝑛 + 1)


∴ Time complexity, 𝑇(𝑛) = log2 𝑛 + 1 + + log2 𝑛 + 2
2 2
log 2 𝑛 (log 2 𝑛 + 1) log 2 𝑛 (log 2 𝑛 + 1) log 2 𝑛 (log 2 𝑛 + 1)
× + +
2 2 2
log 2 𝑛 (log 2 𝑛 + 1) log 2 𝑛 (log 2 𝑛 + 1)
× + + log 2 𝑛
2 2
log 2 𝑛 (log 2 𝑛 + 1) × log 2 𝑛 (log 2 𝑛 + 1) log 𝑛 (log 2 𝑛 + 1)
= 2[ ]+3
4 2
+3 log 2 𝑛 + 1

nurulalamador.github.io/UIUQuestionBank 20
1 3
= (log 24 𝑛 + 2 log 23 𝑛 + log 22 𝑛) + (log 22 𝑛 + log 2 𝑛) + 3 log 2 𝑛 + 1
2 2
1 1 3 3
= log 24 𝑛 + log 23 𝑛 + log 22 𝑛 + log 22 𝑛 + log 2 𝑛 + 3 log 2 𝑛 + 1
2 2 2 2
1 5
= log 24 𝑛 + log 23 𝑛 + 2 log 22 𝑛 + log 2 𝑛 + 1
2 2
= 𝑂(log 4 𝑛)

1 5
∴ Exact cost equation is log24 𝑛 + log23 𝑛 + 2 log22 𝑛 + log2 𝑛 + 1 and time
2 2
4
complexity is 𝑂(log 𝑛) .

2. a) Solve the following recurrence equation, where 𝑇(1) = 𝑂(1).


𝑛
𝑇(𝑛) = 4𝑇 ( ) + 𝑂(𝑛)
2
Solution:
Given,
𝑛
𝑇(𝑛) = 4𝑇 ( ) + 𝑂(𝑛)
2
𝑛 p
Comparing with base equation of Master method 𝑇(𝑛) = 𝑎𝑇 ( ) + 𝑂(𝑛𝑘 log 𝑛),
𝑏
Here,
𝑎=4
𝑏=2
𝑘=1
𝑝=0
𝑏 𝑘 = 21 = 2

Since 𝑎 > 𝑏 𝑘 , therefore this recurrence belongs case 1 of Master Theorem.

∴ Time complexity, 𝑇(𝑛) = 𝑂(𝑛log𝑏 𝑎 )


= 𝑂(𝑛log2 4 )
= 𝑂(𝑛2 )

∴ The solution of recurrence equation is 𝑂(𝑛2 ).

2. b) You are given an array of integers A = {1, -3, 2, 1, -1, 4, -2, 3, -1, 2, -3, 4}, find the
maximum sum subarray using divide-and-conquer approach. You must show the
recursion tree and clearly mention left, right and crossing sum for each tree node.
Solution:
[ P.T.O ]

nurulalamador.github.io/UIUQuestionBank 21
(3, 12, 9)

1 2 3 4 5 6 7 8 9 10 11 12

1 -3 2 1 -1 4 -2 3 -1 2 -3 4
(3, 12, 9)
1 2 3 4 5 6 7 8 9 10 11 12
(3, 6, 6) (8, 12, 5)
1 -3 2 1 -1 4 -2 3 -1 2 -3 4
9
1 2 3 4 5 6 7 8 9 10 11 12

nurulalamador.github.io/UIUQuestionBank
1 -3 2 1 -1 4 -2 3 -1 2 -3 4
(3, 6, 6) (8, 12, 5)
1 2 3 4 5 6 7 8 9 10 11 12
(3, 3, 2) (6, 6, 4) (8, 8, 3) (12, 12, 4)
1 -3 2 1 -1 4 -2 3 -1 2 -3 4
6 5
1 2 3 4 5 6 7 8 9 10 11 12

1 -3 2 1 -1 4 -2 3 -1 2 -3 4

(1, 1, 1) (1, 3, 0) (3, 3, 2) (4, 4, 1) (4, 6, 4) (6, 6, 4) (8, 8, 3) (8, 9, 2) (9, 9, -1) (10, 10, 2) (10, 12, 3) (12, 12, 4)

1 2 1 2 3 3 4 5 4 5 6 6 7 8 7 8 9 9 10 11 10 11 12 12
1 -3 1 -3 2 2 1 -1 1 -1 4 4 -2 3 -2 3 -1 -1 2 -3 2 -3 4 4
0 4 2 3
(1, 1, 1) (1, 2, -3) (2, 2, 3) (4, 4, 1) (4, 5, 0) (5, 5, -1) (7, 7, -2) (7, 8, 1) (8, 8, 3) (10, 10, 2) (10, 11, -3) (11, 11, -3)

1 1 2 2 4 4 5 5 7 7 8 8 10 10 11 11

1 1 -3 -3 1 1 -1 -1 -2 -2 3 3 2 2 -3 -3
-2 0 1 -1

∴ Maximum continuous subarray is:


3 4 5 6 7 8 9 10 11 12

2 1 -1 4 -2 3 -1 2 -3 4 (Maximum Sum = 9)

22
2. c) Suppose we have two sorted sub-arrays: L: 1, 5, 7, 8, 10, 12 and R: 4, 6, 7, 9, 13, 14.
Perform the procedure Merge on L and R to find the final sorted array A. Show
each step of your answer and the number of comparisons required in each step.
Solution:
Let,
𝑖=1
𝑗=1
𝑘=1

Now,
1 2 3 4 5 6 1 2 3 4 5 6

L 1 5 7 8 10 12 R 4 6 7 9 13 14
i j

1 2 3 4 5 6 7 8 9 10 11 12

A 1 4 5 6 7 7 8 9 10 12 13 14
k

𝑖 = 1, 𝑗 = 1, 𝑘 = 1
1.
𝐿[1] ≤ 𝑅[1]; ∴ 𝐴[1] = 𝐿[1] = 1; 𝑖++; 𝑘++;
𝑖 = 2, 𝑗 = 1, 𝑘 = 2
2.
𝐿[2] ≥ 𝑅[1]; ∴ 𝐴[2] = 𝑅[1] = 4; 𝑗++; 𝑘++;
𝑖 = 2, 𝑗 = 2, 𝑘 = 3
3.
𝐿[2] ≤ 𝑅[2]; ∴ 𝐴[3] = 𝐿[2] = 5; 𝑖++; 𝑘++;
𝑖 = 3, 𝑗 = 2, 𝑘 = 4
4.
𝐿[3] ≥ 𝑅[2]; ∴ 𝐴[4] = 𝑅[2] = 6; 𝑗++; 𝑘++;
𝑖 = 3, 𝑗 = 3, 𝑘 = 5
5.
𝐿[3] ≥ 𝑅[3]; ∴ 𝐴[5] = 𝑅[3] = 7; 𝑗++; 𝑘++;
𝑖 = 3, 𝑗 = 4, 𝑘 = 6
6.
𝐿[3] ≤ 𝑅[4]; ∴ 𝐴[6] = 𝐿[3] = 7; 𝑖++; 𝑘++;
𝑖 = 4, 𝑗 = 4, 𝑘 = 7
7.
𝐿[4] ≤ 𝑅[4]; ∴ 𝐴[7] = 𝐿[4] = 8; 𝑖++; 𝑘++;
𝑖 = 5, 𝑗 = 4, 𝑘 = 8
8.
𝐿[5] ≥ 𝑅[4]; ∴ 𝐴[8] = 𝑅[4] = 9; 𝑗++; 𝑘++;
𝑖 = 5, 𝑗 = 5, 𝑘 = 9
9.
𝐿[5] ≤ 𝑅[5]; ∴ 𝐴[9] = 𝐿[5] = 10; 𝑖++; 𝑘++;
𝑖 = 6, 𝑗 = 5, 𝑘 = 10
10.
𝐿[6] ≤ 𝑅[5]; ∴ 𝐴[10] = 𝐿[6] = 12; 𝑖++; 𝑘++;
𝑖 = 6, 𝑗 = 5, 𝑘 = 11
𝐴[11] = 𝑅[5] = 13; 𝑗++; 𝑘++;
𝑖 = 6, 𝑗 = 6, 𝑘 = 12
𝐴[12] = 𝑅[6] = 14; 𝑗++; 𝑘++;

∴ We need 10 element comparisons. After that, there will be only one array (R) left
and that will not need comparisons for further steps.

nurulalamador.github.io/UIUQuestionBank 23
3. a) What is optimal substructure property? Write down the optimal substructure
property of the coin change problem.
Solution:
The optimal substructure property is a key concept of Dynamic Programming. It
essentially states that an optimal solution of a problem can be constructed from
optimal solutions to its sub-problems.

In this concept, every sub-problem should be a ultimate optimal solution at that


sub-problem’s current stage. For example, we have a coin change problem with
total coins 𝐶 = {1,2,3,5} and we will need to make $10. Now in a sub-problems of that
problem where we have only coins 𝐶 = {1,2}, then here we will got the ultimate
optimal solution for making $10 with only coins 𝐶 = {1,2}.

2. b) Demonstrate why the recursive approach to calculate a Fibonacci number is


inefficient, by calculating the Fibonacci number 𝐹5 . How does the dynamic
programming approach for the same solve this inefficiency? (Consider 𝐹0 =0, 𝐹1 =1)
Solution:
Recursive approach for finding 𝐹5 :

F5
Dynamic programming approach
F4 F3 for finding 𝐹5 :

F3 F2 F2 F1
F0 F1 F2 F3 F4 F5
F2 F1 F1 F0 F1 F0

F1 F0

In recursive approach, we are solving same repeated sub-problems for multiple


time such as 𝐹3 , 𝐹2 for calculating Fibonacci number 𝐹5 . One the other hand, in
dynamic programming approach, we store the sub-problem after solving it and
reuse it later instead of solving same overlapping sub-problem multiple times. In
this way, dynamic approach solve the inefficiency to calculate a Fibonacci number.

3. c) A smuggler enters a warehouse to find the items listed in the following table. He
has a bag to carry the smuggled goods, but it can carry only 8 kg weight at best.
The smuggler wants to leave with the items that will result in a maximum profit for
him. Note that he cannot take an item partially; he either will take the item, or will
not.

Using dynamic programming, calculate the maximum profit the smuggler can
earn.

Item no. 1 2 3 4

Weight 3 5 4 6

Profit 10 30 25 50

nurulalamador.github.io/UIUQuestionBank 24
Solution:
Here,
Weight capacity = 8 kg

Item 1 2 3 4

Weight 3 5 4 6

Profit 10 30 25 50

Now,
Weight (kg)
0 1 2 3 4 5 6 7 8

0 0 0 0 0 0 0 0 0 0

1 0 0 0 10 10 10 10 10 10

2 0 0 0 10 10 30 30 30 40
Items

3 0 0 0 10 25 30 30 35 40

4 0 0 0 10 25 30 50 50 50

∴ Total maximize profit = 50


Selected item: 4th item

4. a) Following items are available in a grocery shop:


 12 kilogram rice grain which costs 840 taka
 10 kilogram salt which costs 870 taka
 8 kilogram saffron powder which costs 2000 taka and
 5 kilogram sugar which costs 500 taka
A group of thieves (Thief 1, Thief 2, … Thief M) have come to steal from that shop,
each with a knapsack of capacity 9 kg. The thieves are entering in serial, Thief 2
enters after Thief 1 is done with stealing, Thief 3 enters after Thief 2 is done with
stealing and so on. Since each thief wants to maximize his/her profit, how many
thieves will be needed in the group to empty the grocery shop and what are the
items that each of those thieves carry? Show details of the calculation.
Solution:
Given,
Capacity of each thief, 𝑊 = 9 𝑘𝑔

Rice Saffron
Item Salt Sugar
Grain Powder

Value (taka), 𝑣 840 870 2000 500

Weight (kg), 𝑤 12 10 8 5

Finding price per unit:

[ P.T.O]

nurulalamador.github.io/UIUQuestionBank 25
𝑣𝑖 70 87 250 100

𝑤𝑖 Rice Salt Saffron Sugar


Grain Powder

𝑣𝑖
Sorting item based on :
𝑤𝑖
Saffron Powder > Sugar > Salt > Rice Grain

Thief 1:

𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Saffron
2000 8 9 1 9-8 = 1
Powder

Sugar 500 5 1 0.2 1-(5×0.2) = 0

Now, Sugar new weight, 𝑤𝑖′ = 𝑤𝑖 − 𝑤𝑖 𝑥𝑖 = 5 − 5 × 0.2 = 4


Sugar new value, 𝑣𝑖′ = 𝑣𝑖 − 𝑣𝑖 𝑥𝑖 = 500 − 500 × 0.2 = 400

Thief 2:

𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Sugar 400 4 9 1 9-4 = 5

Salt 870 10 5 0.5 5-(10×0.5) = 0

Now, Salt new weight, 𝑤𝑖′ = 𝑤𝑖 − 𝑤𝑖 𝑥𝑖 = 10 − 10 × 0.5 = 5


Salt new value, 𝑣𝑖′ = 𝑣𝑖 − 𝑣𝑖 𝑥𝑖 = 870 − 870 × 0.5 = 435

Thief 3:

𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Sugar 435 5 9 1 9-5 = 4

Rice
840 12 4 0.33 4-(12×0.33) = 0
Grain

Now, Rice Grain new weight, 𝑤𝑖′ = 𝑤𝑖 − 𝑤𝑖 𝑥𝑖 = 12 − 12 × 0.33 = 8


Rice Grain new value, 𝑣𝑖′ = 𝑣𝑖 − 𝑣𝑖 𝑥𝑖 = 840 − 840 × 0.33 = 560

Thief 4:

𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Rice
560 8 9 1 9-8 = 1
Grain

Here, all items in the grocery shop have been stolen.


∴ Total 4 thieves will be needed in the group to empty the grocery shop.

nurulalamador.github.io/UIUQuestionBank 26
4. b) A document to be transmitted over the internet contains the following characters
with their associated frequencies as shown in the following table:

Character a e l n o s t

Frequency 74 105 44 55 73 57 49

Use Huffman technique to answer the following questions:


i. Build the Huffman code tree for the message and find the codeword for
each character. Encode “stolen” using the codewords.
ii. What is the percentage saving if the data is sent with fixed-length code
values without compression?
Solution:
i. Sorting character based on frequency:

l 44 t 49 n 55 s 57 o 73 a 74 e 105

Building Huffman code tree:

93

l 44 t 49 n 55 s 57 o 73 a 74 e 105

112

n 55 s 57 o 73 a 74 93 e 105

l 44 t 49

147

o 73 a 74 93 e 105 112

l 44 t 49 n 55 s 57

198

93 e 105 112 147

l 44 t 49 n 55 s 57 o 73 a 74

nurulalamador.github.io/UIUQuestionBank 27
259

112 147 198

n 55 s 57 o 73 a 74 93 e 105

l 44 t 49

457
0 1

198 259
0 1
0 1

93 e 105 112 147


0 1 0 1 0 1

l 44 t 49 n 55 s 57 o 73 a 74

This is our final Huffman coding tree.

Finding codeword for each character from Huffman tree:


e: 01 a: 111
o: 110 s: 101
n: 100 t: 001
l: 000
∴ Encode of “stolen” = 10100111000001100

ii.

nurulalamador.github.io/UIUQuestionBank 28
Spring 2023

1. a) Consider a modified version of the Merge sort algorithm as follows:


If the array size is less than or equal to 2, then it sorts the array at constant time.
Otherwise, it divides the array of size n into 3 subarrays, each with a size of n/3. This
division takes 𝑶(𝒏𝟐 ) time. Then the algorithm sorts the subarrays recursively, and
then merges their solutions in time 𝑶(𝒏 𝐥𝐨𝐠 𝒏). Write a recurrence relation for the
running time 𝑻(𝒏) of this algorithm.
Solution:
Recurrence relation for the running time 𝑇(𝑛) of this algorithm is:
𝑂(1) if 𝑛 ≤ 2
𝑇(𝑛) = { 𝑛
3𝑇 ( ) + 𝑂(𝑛2 ) + 𝑂(𝑛 log 𝑛) if 𝑛 > 2
3

In the recursive relation, the time complexity includes:


𝑛
 3𝑇 (3) for sorting 3 subarrays recursively
 𝑂(𝑛2 ) for the time taken for the division into 3 subarrays
 𝑂(𝑛 log 𝑛) for merging the sorted subarrays

1. b) Prove that the divide and conquer method will sort an array in 𝑶(𝒏 𝐥𝐨𝐠 𝒏) time
when n>1.
Solution:
In divide and conquer method, for sorting an array, we divide the array into two
halves and solve them recursively.
So we have,
𝑛 𝑛 𝑛
𝑇 ( ) + 𝑇 ( ) = 2𝑇 ( )
2 2 2

Finally we merge these subarray, which take 𝑂(𝑛) times.


∴ Here our recursive relation is,
𝑛
𝑇(𝑛) = 2𝑇 ( ) + 𝑂(𝑛)
2
𝑛 p
Comparing with base equation of Master method 𝑇(𝑛) = 𝑎𝑇 ( ) + 𝑂(𝑛𝑘 log 𝑛),
𝑏
Here,
𝑎=2
𝑏=2
𝑘=1
𝑝=0
𝑏 𝑘 = 21 = 2

Since 𝑎 = 𝑏 𝑘 and 𝑝 > -1, therefore this recurrence belongs to 1st condition of case 2 of
Master Theorem.

nurulalamador.github.io/UIUQuestionBank 29
𝑝+1
∴ Time complexity, 𝑇(𝑛) = 𝑂 (𝑛log𝑏 𝑎 log 𝑛)
= 𝑂(𝑛log2 2 log 0+1 𝑛)
= 𝑂(𝑛 log 𝑛)

∴ Divide and conquer method will sort an array in 𝑂(𝑛 log 𝑛). (Proved)

1. c) Given an array of integers A = {70, -40, 20, -50, 10, -15, 20}, find the
Maximum-sum Continuous Subarray using divide-and-conquer. You must show
the recursion tree and clearly mention left, right and crossing sum for each tree
node.
Solution:

[ P.T.O ]

nurulalamador.github.io/UIUQuestionBank 30
(1, 1, 70)

1 2 3 4 5 6 7

70 -40 20 -50 10 -15 20


(1, 1, 70) (7, 7, 20)
(1, 7, 15)

1 2 3 4 5 6 7
1 2 3 4 5 6 7

nurulalamador.github.io/UIUQuestionBank
70 -40 20 -50 10 -15 20
70 -40 20 -50 10 -15 20
15

(1, 1, 70) (1, 3, 50) (3, 3, 20) (5, 5, 10) (5, 7, 15) (7, 7, 20)

1 2 1 2 3 4 3 4 5 6 5 6 7 7

70 -40 70 -40 20 -50 20 -50 10 -15 10 -15 20 20

50 15
(1, 1, 70) (1, 2, 30) (2, 2, -40) (3, 3, 20) (3, 4, -30) (4, 4, -50) (5, 5, 10) (5, 6, -5) (6, 6, -15)

1 1 2 2 3 3 4 4 5 5 6 6

70 70 -40 -40 20 20 -50 -50 10 10 -15 -15

30 -30 -5

∴ Maximum sub continuous subarray is:

70

31
2. a) What is the main difference between Dynamic Programming and Divide-and-
Conquer algorithms? When should we try to solve a problem using Dynamic
Programming?
Solution:
Divide-and-Conquer algorithm divide a problem into independent sub-problems
and solve each sub-problem and combines the solutions recursively. On the other
hand, Dynamic Programming algorithm break the problem into overlapping sub-
problems and avoid solving same repeated sub-problems multiple time by store
and reusing the solution of sub-problems. This is the main difference between
Dynamic Programming and Divide-and-Conquer.

We should try to solve a problem using Dynamic Programming when we have


more chance to having repeated overlapping sub-problems to remove inefficiency.
For example, calculating Fibonacci number.

2. b) Suppose you are a motorcycle dealer buying cars wholesale and then selling them
at retail price for a profit. You have a budget of 7 lac Tk. You went to the wholesale
market, and you saw the following items on sale:

Yamaha Harley Apache Honda CBR


Car Name
R15 Davidson RTR 160 150R R
Wholesale price (in lac
3 6 1 4
Taka)
Retail Price (in lac Taka) 5 14 2 6

You want to get the maximum profit from selling all these motorcycles, but your
budget restricts you from buying all of them. Find the maximum profit you can
obtain by buying some of these motorcycles and then selling them, provided the
total wholesale cost of the motorcycles you selected do not exceed your budget
of 7 lac Tk. Note that you can only buy one of these motorcycles at a time, so if
you purchased a Yamaha R15 from the wholesale market, you cannot purchase it
again. Find the solution to this problem by using dynamic programming and
creating a lookup table.
Solution:
Here,
Total budget = 7 Lac Tk

Index 1 2 3 4

Motorcycle Yamaha Harley Apache Honda CBR


Name R15 Davidson RTR 160 150R R

Wholesale
3 6 1 4
Price, 𝒘𝒊

Retail Price 5 14 2 6

Profit, 𝒗𝒊 2 8 1 2

Now,

[ P.T.O ]

nurulalamador.github.io/UIUQuestionBank 32
Budget (Lac Tk)
0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0

1 0 0 0 2 2 2 2 2

Motorcycles
2 0 0 0 2 2 2 8 8

3 0 1 1 2 3 3 8 9

4 0 1 1 2 3 3 8 9

∴ Total maximize profit = 9 Lac Tk


Selected motorcycles : 2 (Harley Davidson), 3 (Apache RTR 160)

2. c) Suppose now you have an infinite supply of motorcycles in the wholesale market,
which means that if you purchased a Yamaha R15 from the wholesale market, you
could purchase it again (if you have the required amount of money). What
modification should you make to the algorithm you used in question (b) to make
it work for an infinite supply of motorcycles? (You do not need to show any lookup
tables here).
Solution:
We use 0/1 knapsack algorithm in question (b). In the 0/1 knapsack, the algorithm is
based on choosing an item or not. For the infinity supply of item, we must consider
the possibility of taking the same item multiple times. Normally we use 2D array in
0/1 knapsack, but for this case we should reduce the 2D array to a 1D array since we
are no longer concerned with distinct items being selected only once. Let, our
capacity is 𝑗 and our array is 𝑑𝑝[𝑗]. For each capacity 𝑑𝑝[0], 𝑑𝑝[1] … 𝑑𝑝[𝑗], we have to
check all items to improves the value. This loop effectively considers each item
multiple times. Then we will get our final answer in last index of array, 𝑑𝑝[𝑗].

3. a) Calculate the time complexity (Best Case and Worst Case) of the following code
snippets.

(i) for (int i =1; i<n; i=i*2) {


p++;
}
for(int j=1; j<p; j=j*2){
printf(“hello”);
}

(ii) for(int i =1; i*i<n; i++){


printf(“hello”);
}

(iii) for(int i =1; i<n; i=i*2){


for(int j=1; j<i; j++){
printf(“hello”);

nurulalamador.github.io/UIUQuestionBank 33
}
}

Solution:
Since there are no use of condition, return, break, continue; therefore best the best
case and worst case time complexity will be same for all of these code snippets.

i) Line Time Cost

1 log 2 𝑛 + 1
2 log 2 𝑛
4 log 2 𝑛 (log 2 𝑛) + 1
5 log 2 𝑛 (log 2 𝑛)

∴ Time complexity, 𝑇(𝑛) = log2 𝑛 + 1 + log2 𝑛 + log2 𝑛 (log2 𝑛) + 1 + log2 𝑛 (log2 𝑛)


= 2 log 2 𝑛 + 2 log 2 𝑛 (log 2 𝑛) + 2
= 𝑂(log 𝑛)

ii) Line Time Cost

1 √𝑛 + 1
2 √𝑛

∴ Time complexity, 𝑇(𝑛) = √𝑛 + 1 + √𝑛


= 𝑂(√𝑛)

iii) Line Time Cost

1 log 2 𝑛 + 1
log 2 𝑛 (log 2 𝑛 + 1)
2
2
log 2 𝑛 (log 2 𝑛 + 1)
3 +1
2

log2 𝑛 (log2 𝑛 + 1) log 𝑛 (log2 𝑛 + 1)


∴ Time complexity, 𝑇(𝑛) = log2 𝑛 + 1 + +1+ 2
2 2
2
log 2 𝑛 + log 2 𝑛
= log 2 𝑛 + 2 ( )+2
2
= log 22 𝑛 + 2 log 2 𝑛 + 2
= 𝑂(log 2 𝑛)

3. b) Given 𝑓(𝑛) = 5𝑛3 + 6𝑛2 + 3𝑛 + 9 ; 𝑔(𝑛) = 𝑛4 ; find the values of 𝒄 and 𝒏𝟎 such that
when 𝑛 > 𝑛0 , 𝒇(𝒏) = 𝑶(𝒈(𝒏))
Solution:

nurulalamador.github.io/UIUQuestionBank 34
Given,
𝑓(𝑛) = 5𝑛3 + 6𝑛2 + 3𝑛 + 9
𝑔(𝑛) = 𝑛4

Proving 𝑓(𝑛) ≤ 𝑐 ⋅ 𝑔(𝑛) for any value of 𝑐 and 𝑛0 such that 𝑛 > 𝑛0 is enough to prove
𝑓(𝑛) = 𝑂(𝑔(𝑛)).

Now,
𝑓(𝑛) ≤ 𝑐 ⋅ 𝑔(𝑛)
or, 5𝑛3 + 6𝑛2 + 3𝑛 + 9 ≤ 𝑐 ⋅ 𝑛4
5 6 3 9
or, + 2 + 3 + 4 ≤ 𝑐
𝑛 𝑛 𝑛 𝑛

∴ Big-Oh notation 𝑓(𝑛) = 𝑂(𝑔(𝑛)) holds for any value for 𝑛0 such that 1 > 𝑛0 and 𝑐 =
5 6 3 9
23. [ + 2 + 3 + 4 = 23]
1 1 1 1

4. a) Find an optimal solution to the fractional knapsack instance of n = 4, W = 5, (v1, v2,


v3, v4) = (50, 30, 35, 60), and (w1, w2, w3, w4) = (2, 2, 1, 3).
Solution:
Given,
𝑛=5
𝑊=7

Item, 𝒊𝒊 𝒊𝟏 𝒊𝟐 𝒊𝟑 𝒊𝟒
Value, 𝑣𝑖 50 30 35 60

Weight, 𝑤𝑖 2 2 1 3

Finding price per unit:

𝑖1 𝑖2 𝑖3 𝑖4
𝑣𝑖
25 15 35 20
𝑤𝑖

𝑣𝑖
Sorting item based on :
𝑤𝑖
𝑖3 > 𝑖1 > 𝑖4 > 𝑖2

Adding item in knapsack:

𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
𝑖3 35 1 5 1 5-1 = 1

𝑖1 50 2 4 1 4-2 = 2

𝑖4 60 3 2 0.667 2-(3×0.667) = 2-2 = 0

nurulalamador.github.io/UIUQuestionBank 35
∴ Total Profit = 𝑣3 𝑖3 + 𝑣1 𝑖1 + 𝑣4 𝑖4
= 35 × 1 + 50 × 1 + 60 × 0.667
= 125

4. b) Suppose we want to encode the symbols {A, B, C, D} in binary. Is the following a


valid Huffman code?
{𝐴: 0; 𝐵: 10; 𝐶: 110; 𝐷: 111}
If it is, build the code tree; if not, explain why you can't.
Solution:
Given,
{𝐴: 0; 𝐵: 10; 𝐶: 110; 𝐷: 111}

If we can make Huffman tree for these given Huffman code without any collision,
then the given Huffman code is valid.

0 1

A
0 1

B
0 1

C D

Analyzing the Huffman code tree, we can see that every character is accessable and
there are not any overlapping issue.
∴ This is a valid Huffman code.

4. c) You are given the arrival and the departure times of eight trains for a railway
platform, and each one is in the format: [arrival time, departure time). Only one
train can use the platform at a time. Suppose that you have got the following train-
use requests for the next day.
{ [8, 12), [6, 9), [11, 14), [2, 7), [1, 7), [12, 20), [7, 12) , [13, 19) }
Find the maximum number of trains that can use the platform without any
collision by using earliest departure time.
Solution:
Repeat of Fall 2023 Question 4(c)

nurulalamador.github.io/UIUQuestionBank 36
Fall 2022

1. a) Suppose, A problem X of size n can be divided into three subproblems each of size
n/4, each of the problem can be solved recursively in time T(n/4) respectively. The
cost of dividing the problem and combining the results of the subproblems is
𝑂(𝑛𝑙𝑜𝑔𝑛). Formulate the recurrence relation assuming, 𝑇(1) = 𝑂(1).
Solution:
Recurrence relation for the given problem is:
𝑛
𝑇(𝑛) = 3𝑇 ( ) + 𝑂(𝑛 log 𝑛)
4

In the recursive relation, the time complexity includes:


𝑛
 4𝑇 (4) for solving 4 sub-problems size of 𝑛/4 recursively
 𝑂(𝑛 log 𝑛) for merging the solved sub-problems

1. b) Solve the following recurrence equation: 𝑇(𝑛) = 3𝑇(𝑛/3) + 𝑂(1), where 𝑇(1) = 𝑂(1).
Solution:
Given,
𝑛
𝑇(𝑛) = 3𝑇 ( ) + 𝑂(1)
3
𝑛 p
Comparing with base equation of Master method 𝑇(𝑛) = 𝑎𝑇 ( ) + 𝑂(𝑛𝑘 log 𝑛),
𝑏
Here,
𝑎=3
𝑏=3
𝑘=0
𝑝=0
𝑏 𝑘 = 30 = 1

Since 𝑎 > 𝑏 𝑘 , therefore this recurrence belongs to case 1 of Master Theorem.

∴ Time complexity, 𝑇(𝑛) = 𝑂(𝑛log𝑏 𝑎 )


= 𝑂(𝑛log3 3 )
= 𝑂(𝑛)

∴ Solution of following recurrence equation is 𝑂(𝑛).

1. c) Given an array of integers A = {2, -3, 2, -4, 1, -3, -2}, find the Maximum-
sum Continuous Subarray using divide-and-conquer. You must show the
recursion tree and clearly mention left, right and crossing sum for each tree
node.
Solution:

nurulalamador.github.io/UIUQuestionBank 37
(1, 1, 2)

1 2 3 4 5 6 7

2 -3 2 -4 1 -3 -2
(1, 1, 2) (5, 5, 1)
(3, 5, -1)

1 2 3 4 5 6 7
1 2 3 4 5 6 7

nurulalamador.github.io/UIUQuestionBank
2 -3 2 -4 1 -3 -2
2 -3 2 -4 1 -3 -2
-1

(1, 1, 2) (1, 3, 1) (3, 3, 2) (5, 5, 1) (5, 7, -4) (7, 7, -2)

1 2 1 2 3 4 3 4 5 6 5 6 7 7

2 -3 2 -3 2 -4 2 -4 1 -3 1 -3 -2 -2

1 -4
(1, 1, 2) (1, 2, -1) (2, 2, -3) (3, 3, 2) (3, 4, -2) (4, 4, -4) (5, 5, 1) (5, 6, -2) (6, 6, -3)

1 1 2 2 3 3 4 4 5 5 6 6

2 2 -3 -3 2 2 -4 -4 1 1 -3 -3

-1 -2 -2

∴ Maximum sub continuous subarray is:

38
4. a) Following items are available in a grocery shop:
 10 kilogram rice grain which costs 800 taka
 10 kilogram salt which costs 890 taka
 8 kilogram saffron powder which costs 2000 taka and
 4 kilogram sugar which costs 500 taka
A group of thieves (Thief 1, Thief 2, … Thief M) have come to steal from that shop,
each with a knapsack of capacity 8 kg. The thieves are entering in serial, Thief 2
enters after Thief 1 is done with stealing, Thief 3 enters after Thief 2 is done with
stealing and so on. Since each thief wants to maximize his/her profit, how many
thieves will be needed in the group to empty the grocery shop and what are the
items that each of those thieves carry? Show details of the calculation.
Solution:
Given,
Capacity of each thief, 𝑊 = 8 𝑘𝑔

Rice Saffron
Item Salt Sugar
Grain Powder

Value (taka), 𝑣 800 890 2000 500

Weight (kg), 𝑤 10 10 8 4

Finding price per unit:

[ P.T.O]
𝑣𝑖 80 89 250 125

𝑤𝑖 Rice Salt Saffron Sugar


Grain Powder

𝑣𝑖
Sorting item based on :
𝑤𝑖
Saffron Powder > Sugar > Salt > Rice Grain

Thief 1:

𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Saffron
2000 8 8 1 8-8 = 0
Powder

Thief 2:

𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Sugar 500 4 8 1 8-4 =4

Salt 890 10 4 0.4 4-(10×0.4) = 4-4 = 0

Now, Salt new weight, 𝑤𝑖′ = 𝑤𝑖 − 𝑤𝑖 𝑥𝑖 = 10 − 10 × 0.4 = 6


Salt new value, 𝑣𝑖′ = 𝑣𝑖 − 𝑣𝑖 𝑥𝑖 = 890 − 890 × 0.4 = 534

[ P.T.O ]

nurulalamador.github.io/UIUQuestionBank 39
Thief 3:

𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Sugar 534 6 8 1 8-6 = 2

Rice
800 10 2 0.2 2-(10×0.2) = 2-2 = 0
Grain

Now, Rice Grain new weight, 𝑤𝑖′ = 𝑤𝑖 − 𝑤𝑖 𝑥𝑖 = 10 − 10 × 0.2 = 8


Rice Grain new value, 𝑣𝑖′ = 𝑣𝑖 − 𝑣𝑖 𝑥𝑖 = 800 − 800 × 0.2 = 640

Thief 4:

𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Rice
640 8 8 1 8-8 = 0
Grain

Here, all items in the grocery shop have been stolen.


∴ Total 4 thieves will be needed in the group to empty the grocery shop.

2. b) A document to be transmitted over the internet contains the following characters


with their associated frequencies as shown in the following table:

Character A B C D F T _

Frequency 40 23 8 10 4 12 3

There are a total 1000 characters in the document.


i. Build the Huffman code tree for the message and find the codeword for
each character.
ii. Decode “0110001111” using codewords generated in (i).
Solution:
i. Sorting character based on frequency:

_ 3 F 4 C 8 D 10 T 12 B 23 A 40

Building Huffman code tree:

_ 3 F 4 C 8 D 10 T 12 B 23 A 40

[ P.T.O ]

nurulalamador.github.io/UIUQuestionBank 40
15

7 C 8 D 10 T 12 B 23 A 40

_ 3 F 4

22

D 10 T 12 15 B 23 A 40

7 C 8

_ 3 F 4

37

15 22 B 23 A 40

7 C 8 D 10 T 12

_ 3 F 4

60

B 23 37 A 40

15 22

7 C 8 D 10 T 12

_ 3 F 4

[ P.T.O ]

nurulalamador.github.io/UIUQuestionBank 41
100
0 1

A 40 60
0 1

B 23 37
0 1

15 22
0 1 0 1

7 C 8 D 10 T 12
0 1

_ 3 F 4

This is our final Huffman coding tree.

Finding codeword for each character from Huffman tree:


A: 0 B: 10
C: 1101 D: 1110
F: 11001 T: 1111
_: 11000

ii. Decoding “0110001111” using codewords from (i):


0110001111 = A_T

3. a) Suppose you have computed a Fibonacci series using dynamic programming.


Justify the following statements with an example:
I. Overlapping Subproblems property has been satisfied in your computation.
II. Dynamic programming gives you a more efficient solution than an obvious
recursive algorithm.
Solution:
The overlapping sub-problems property means that a problem can be broken down
into sub-problems that are reused multiple times. When compute a Fibonacci
number, we get same sub-problems for solving repeatedly.
For example, for computing 𝐹(5),
𝐹(5) requires 𝐹(4) and 𝐹(3)
𝐹(4) requires 𝐹(3) and 𝐹(2)
𝐹(3) requires 𝐹(2) and 𝐹(1)
𝐹(2) requires 𝐹(1) and 𝐹(0)
As a result, 𝐹(3) is computed twice, 𝐹(2) is computed multiple times, and so on. This
repetition indicates overlapping sub-problems. In recursive algorithm, we solve and
merge solution of each sub-problems with overlapping sub-problems. But in
dynamic programming, we store the results of these sub-problems and reuse them
and reduce the inefficiency which created on recursive algorithm.

nurulalamador.github.io/UIUQuestionBank 42
Recursive approach for calculating 𝐹5 :

F5
Dynamic programming approach
F4 F3 for calculating 𝐹5 :

F3 F2 F2 F1
F0 F1 F2 F3 F4 F5
F2 F1 F1 F0 F1 F0

F1 F0

3. b) What is ‘Optimal Substructure’ property? How does Dynamic Programming differ


from Divide-and-Conquer problems in terms of handling subproblems?
Solution:
A problem is said to have optimal substructure if an optimal solution can be
constructed from optimal solutions of its sub-problems. In other words, a problem
exhibits optimal substructure if an optimal solution to the problem contains within
it optimal solutions to its sub-problems.

A problem can have some repeated sub-problems, which are known as overlapping
sub-problems. In Divide-and-Conquer algorithm, it solve each of the sub-problems,
including repeated overlapping sub-problems. It create inefficiency for many
problems, such as Fibonacci number calculating. In Fibonacci number problem, we
have so many repeated sub-problems multiple time. Recursive algorithm solve and
merge each of repeated sub-problems. But Dynamic Programming solve and store
sub-problems solution and use it later and reduce inefficiency. It is the main
difference in terms of handling sub-problems between Divide-and-Conquer and
Dynamic Programming algorithm.

3. b) Suppose, CoffeeLand Coffee Shop charges 50 BDT (Bangladesh Taka) for each cup
of small Americano with an additional vat of 3%. You bought 2 cups of small
Americano and gave the cashier 110 taka. The cashier has got a huge supply of the
following types of coins: 1 taka, 2 taka, and 5 taka in the cashbox. You don’t want
to carry many coins, so you asked the cashier to return the change using a
minimum number of coins. Determine the number and type of coins the cashier
should return in this scenario by applying the Dynamic Programming Approach.
Solution:
Given,
Total charges = (50 × 2) + [(50 × 2) × 3%]
= 100 + (100 × 3%)
= 100 + 3
= 103
Given cash = 110
∴ Total Changes, 𝑀 = 110 − 103
=7
Coins, 𝐶𝑖 = {𝐶1 , 𝐶2 , 𝐶3 } = {1,2,5}

nurulalamador.github.io/UIUQuestionBank 43
Now,
Total Changes, M
0 1 2 3 4 5 6 7

1 0 1 2 3 4 5 6 7

2 0 1 1 2 2 3 3 4

Coins 5 0 1 1 2 2 1 2 2

∴ Minimum coins needed = 2


Selected coins = 1 coins of 2 Taka, 1 coins of 5 Taka

4. a) Derive the best-case and the worst-case running time equations for the following
function calculate and represent using Asymptotic Notation.

1 void calculate(int n, int p, int A[]) {


2 int prod = 0;
3 for(int i = 1; i<=n; i++) {
4 for(int j = 1; j <= i*i; j++) {
5 prod *= pow(i,j)
6 }
7 }
8
9 for(int m = 2; m <= P; m++) {
10 if(A[m] < 100 ) {
11 break;
12 }
13
14 prod = prod * A[m];
15 }
16
17 cout<<prod<<endl
18 }

Solution:

Line Worst Case Best Case

2 1 1

3 𝑛+1 𝑛+1

4 𝑛3 + 𝑛 𝑛3 + 𝑛
5 𝑛3 𝑛3
9 𝑝 1
10 0 1
11 0 1
14 𝑝−1 0

nurulalamador.github.io/UIUQuestionBank 44
17 1 1

∴ Best case, 𝑇(𝑛) = 1 + 𝑛 + 1 + 𝑛3 + 𝑛 + 𝑛3 + 1 + 1 + 1 + 1


= 2𝑛3 + 2𝑛 + 6
= 𝑂(𝑛3 )

∴ Worst case, 𝑇(𝑛) = 1 + 𝑛 + 1 + 𝑛3 + 𝑛 + 𝑛3 + 𝑝 + 𝑝 − 1 + 1


= 2𝑛3 + 2𝑛 + 2𝑝 + 2
= 𝑂(𝑛3 ) [Considering 𝑛3 > 𝑝]

4. b) Derive the exact-cost equation for the running time of the following function and
show that time complexity in 𝑂(𝑛 log 𝑛 log 5 𝑛):

1 int funFunction(int n)
2 {
3 int sum = 0;
4 for (int k = 0; k < n; k*=2){
5 for (int j = 2/n; j <= n; j++) {
6 for (int i = n; i >= 1; i=i/5) {
7 sum += (i+j+k);
8 }
9 }
10 }
11
12 cout<<sum<<endl;
13
14 }

Solution:

Line Time Cost

3 1
4 log 2 𝑛 + 1
𝑛
5 log 2 𝑛 × + log 2 𝑛
2
𝑛 𝑛
6 log 2 𝑛 × × log 5 𝑛 + log 2 𝑛 ×
2 2
𝑛
7 log 2 𝑛 × × log 5 𝑛
2
10 1

𝑛 𝑛
∴ Time complexity, 𝑇(𝑛) = 1 + log2 𝑛 + 1 + log2 𝑛 × + log2 𝑛 + log2 𝑛 × × log5 𝑛
2 2
𝑛 𝑛
+ log 2 𝑛 × + log 2 𝑛 × × log 5 𝑛 + 1
2 2
1 1
= 1 + log 2 𝑛 + 1 + 𝑛 log 2 𝑛 + log 2 𝑛 + 𝑛 log 2 𝑛 log 5 𝑛
2 2
1 1
+ 𝑛 log 2 𝑛 + 𝑛 log 2 𝑛 log 5 𝑛 + 1
2 2

nurulalamador.github.io/UIUQuestionBank 45
= 2 log 2 𝑛 + 𝑛 log 2 𝑛 + 𝑛 log 2 𝑛 log 5 𝑛 + 3
= 𝑂(𝑛 log 𝑛 log 5 𝑛)

∴ Exact cost equation is 2 log2 𝑛 + 𝑛 log2 𝑛 + 𝑛 log2 𝑛 log5 𝑛 + 3 and time complexity is
𝑂(𝑛 log 𝑛 log 5 𝑛). (Showed)

nurulalamador.github.io/UIUQuestionBank 46

You might also like