Data-Structure-and-Algorithms-II-Mid-Solution
Data-Structure-and-Algorithms-II-Mid-Solution
DATA STRUCTURE
AND ALGORITHMS II
CSE 2217
SOLUTION BY
NURUL ALAM ADOR
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!)
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
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
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 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
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:
if (left == right) {
if(arr[left] % 10 == 1){
return 1;
}
Base Case
else {
return 0;
}
}
else {
mid = (left + right) / 2;
}
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
nurulalamador.github.io/UIUQuestionBank 6
∴ Time complexity, 𝑇(𝑛) = 𝑂(𝑛log𝑏 𝑎 )
= 𝑂(𝑛log2 3 )
= 𝑂(𝑛1.58 )
3. b) Consider the following function which takes two arrays (A and B) and their lengths
(n and m) as inputs respectively.
int sumB = 0;
for(int i=0; i<m; i++) {
sumB = sumB + B[i];
}
int count = 0;
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 𝑛
A[ ] = 1 1 1 1 B[ ] = 10 10 10 10 n=4 m=4
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]
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. 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
Sugar, 𝑠𝑖 35 20 40 30 25
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
𝑫
𝒊 Flavor 𝒔𝒊 𝒅𝒊 𝑫 𝒙𝒊 = min (𝟏, ) 𝑫′ = 𝑫 − 𝒅𝒊 𝒙𝒊
𝒅𝒊
3 Pineapple 40 240 750 1 750-240 = 510
∴ Maximum Sugar = 𝑠3 𝑥3 + 𝑠4 𝑥4 + 𝑠1 𝑥1
= 40 × 1 + 30 × 1 + 35 × 0.96875
= 103.90 𝑔
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
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 )
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 2 -3 -2
1 3 2 -3
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
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
∴ 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:
Break the problem into independent Break the problem into overlapping sub-
sub-problems. problems.
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
But if we use any other approach, like Dynamic Programming, then we can pay
$25 in only 2 coins ($16, $9).
3. a) Derive the best-case and the worst-case running time equations for the following
function specialTask and represent using Asymptotic Notation.
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:
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
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.
Solution:
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
[ P.T.O ]
nurulalamador.github.io/UIUQuestionBank 16
Item, 𝒊𝒊 𝒊𝟏 𝒊𝟐 𝒊𝟑 𝒊𝟒 𝒊𝟓
Value, 𝑣𝑖 50 30 35 60 25
Weight, 𝑤𝑖 2 2 1 3 2
𝑖1 𝑖2 𝑖3 𝑖4 𝑖5
𝑣𝑖
25 15 35 20 12.5
𝑤𝑖
𝑣𝑖
Sorting item based on :
𝑤𝑖
𝑖3 > 𝑖1 > 𝑖4 > 𝑖2 > 𝑖5
𝑾
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
∴ 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) }
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.
Solution:
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. b) Derive the exact-cost equation for the running time of the following function and
find the time complexity in big-oh notation.
Solution:
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 𝑛
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. 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
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.
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
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
Rice Saffron
Item Salt Sugar
Grain Powder
Weight (kg), 𝑤 12 10 8 5
[ P.T.O]
nurulalamador.github.io/UIUQuestionBank 25
𝑣𝑖 70 87 250 100
𝑣𝑖
Sorting item based on :
𝑤𝑖
Saffron Powder > Sugar > Salt > Rice Grain
Thief 1:
𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Saffron
2000 8 9 1 9-8 = 1
Powder
Thief 2:
𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Sugar 400 4 9 1 9-4 = 5
Thief 3:
𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Sugar 435 5 9 1 9-5 = 4
Rice
840 12 4 0.33 4-(12×0.33) = 0
Grain
Thief 4:
𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Rice
560 8 9 1 9-8 = 1
Grain
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
l 44 t 49 n 55 s 57 o 73 a 74 e 105
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
l 44 t 49 n 55 s 57 o 73 a 74
nurulalamador.github.io/UIUQuestionBank 27
259
n 55 s 57 o 73 a 74 93 e 105
l 44 t 49
457
0 1
198 259
0 1
0 1
l 44 t 49 n 55 s 57 o 73 a 74
ii.
nurulalamador.github.io/UIUQuestionBank 28
Spring 2023
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
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
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
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
30 -30 -5
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.
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:
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
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
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.
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.
1 log 2 𝑛 + 1
2 log 2 𝑛
4 log 2 𝑛 (log 2 𝑛) + 1
5 log 2 𝑛 (log 2 𝑛)
1 √𝑛 + 1
2 √𝑛
1 log 2 𝑛 + 1
log 2 𝑛 (log 2 𝑛 + 1)
2
2
log 2 𝑛 (log 2 𝑛 + 1)
3 +1
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
Item, 𝒊𝒊 𝒊𝟏 𝒊𝟐 𝒊𝟑 𝒊𝟒
Value, 𝑣𝑖 50 30 35 60
Weight, 𝑤𝑖 2 2 1 3
𝑖1 𝑖2 𝑖3 𝑖4
𝑣𝑖
25 15 35 20
𝑤𝑖
𝑣𝑖
Sorting item based on :
𝑤𝑖
𝑖3 > 𝑖1 > 𝑖4 > 𝑖2
𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
𝑖3 35 1 5 1 5-1 = 1
𝑖1 50 2 4 1 4-2 = 2
nurulalamador.github.io/UIUQuestionBank 35
∴ Total Profit = 𝑣3 𝑖3 + 𝑣1 𝑖1 + 𝑣4 𝑖4
= 35 × 1 + 50 × 1 + 60 × 0.667
= 125
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
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
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 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
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
Weight (kg), 𝑤 10 10 8 4
[ P.T.O]
𝑣𝑖 80 89 250 125
𝑣𝑖
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
[ 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
Thief 4:
𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Rice
640 8 8 1 8-8 = 0
Grain
Character A B C D F T _
Frequency 40 23 8 10 4 12 3
_ 3 F 4 C 8 D 10 T 12 B 23 A 40
_ 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
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
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
4. a) Derive the best-case and the worst-case running time equations for the following
function calculate and represent using Asymptotic Notation.
Solution:
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
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:
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