0% found this document useful (0 votes)
96 views18 pages

Final Exam Prep Guide 2021

The document provides instructions for preparing for a final exam taking place from January 15-16, 2021. It includes a list of 15 problems (A-O) with the title, author, and editorialist for each. It also provides sample solutions and analyses for 6 of the problems (A, B, C, D, E, F) discussing the algorithms and time complexities used to solve each one. Login credentials are provided at the top for accessing the exam problems.

Uploaded by

nEZ Official
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views18 pages

Final Exam Prep Guide 2021

The document provides instructions for preparing for a final exam taking place from January 15-16, 2021. It includes a list of 15 problems (A-O) with the title, author, and editorialist for each. It also provides sample solutions and analyses for 6 of the problems (A, B, C, D, E, F) discussing the algorithms and time complexities used to solve each one. Login credentials are provided at the top for accessing the exam problems.

Uploaded by

nEZ Official
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Final Exam Preparation

15th January 2021 − 16th January 2021

Haven’t got the key yet?


Admin 1: HURTGLMQJBSL
Admin 2: VGLRBLLSWGQ
Admin 3: LGCFMJSRRBTGMLM
Admin 4: EQBEMQGURBKKSLUBJFBLQY

Title Author Editorialist


A. Literally Sorting Admin 4 Admin 4
B. Quadbonacci Admin 3 Admin 4
C. Pokemon Flag Admin 2 Admin 2
D. Thou Shalt Not Passeth Admin 4 Admin 4
E. Identical Substring Admin 4 Admin 4
F. Afterschool Candy Admin 3 Admin 1
G. Unown Apocalypse Admin 2 Admin 4
H. Pokemon Journey Continue Admin 2 Admin 2
I. Beauty Of A String Admin 2 Admin 2 and Admin 4
J. New Era Of Team Rocket Admin 2 Admin 2
Bonus Problem
K. A Trip with Friends Admin 1 Admin 2 and Admin 4
L. Late for Work Admin 4 Admin 4
M. The Path to Sunibland Admin 3 Admin 2
N. Sherbet Equation Admin 2 Admin 4
O. Angry Alchemist Admin 2 Admin 4
A. Literally Sorting
Notice that for basic sorting algorithms such as bubble sort or selection sort, your program
will have a time complexity of 𝑂(𝑇𝑁 2 ) due to multiple test cases. This means, for the
worst case, your program will run roughly 𝑇 × 𝑁 × 𝑁 = 3000 × 1000 × 1000 =
3 × 109 operations. As a rule of thumb, an online judge can run 108 operations in a
second. Thus, this kind of program will run for 30 seconds, which is far exceeding the time
limit of 1 second.

To solve this problem, we need to use an advanced sorting algorithm that has a time
complexity of 𝑂(𝑁 log 𝑁). With this, our program has a complexity of 𝑂(𝑇𝑁𝑙𝑜𝑔𝑁),
running 𝑇 × 𝑁 × log 2 𝑁 ≈ 3 × 106 operations, or about 0.03 seconds.

Time Complexity: 𝑂(𝑇𝑁𝑙𝑜𝑔𝑁)

Source Code:
https://ideone.com/UbSWSo
B. Quadbonacci
The first naïve solution is obviously recursion. Upon first observation, it can be noticed
that the formula for the 𝑁-th term of quadbonacci (𝑁 > 4) is

𝐹 (𝑁) = 𝐹 (𝑁 − 1) + 𝐹 (𝑁 − 2) + 𝐹 (𝑁 − 3) + 𝐹(𝑁 − 4).

Thus, the easiest to code solution probably looks like this:


https://ideone.com/VIJBqz

However, this code is inefficient. Look at a part of the recursion process below.

Notice that several duplicate recursions are being executed, such as 𝐹(𝑁 − 2), 𝐹(𝑁 − 5),
and more. These duplicate recursions are unneeded and causing the program to run
significantly slower.

To solve this problem, we can store our calculated value in an array. So, when we need to
find the value of 𝐹(𝑥) for some 𝑥, we can simply access the value stored in 𝑞𝑢𝑎𝑑 [𝑥 ].

Time Complexity: 𝑂(𝑁)

Source Code:
Recursive : https://ideone.com/JuytbO
Iterative : https://ideone.com/47DKTR
C. Pokemon Flag
To solve this problem, we can look at the sample given. Look at the value of 𝑁 in:
• Sample 0. If 𝑁 = 3, the number pattern printed is: [1 2 1] 3 [1 2 1]
• Sample 1. If 𝑁 = 4, the number pattern printed is: [1 2 1 3 1 2 1] 4 [1 2 1 3 1 2 1]
• Sample 2. If 𝑁 = 2, the number pattern printed is: [1] 2 [1]

It means that if we want to draw a flag with a value 𝑁 ≥ 2, it must begin with a flag with
pattern 𝑁 − 1, then a number 𝑁, then end with a flag with pattern 𝑁 − 1.

Then we can look at each pattern. If a section is numbered with 𝑋, it will make a stairs
pattern with height 𝐿. And the width of the stairs is 𝑋 times larger. As an example, if 𝑋 =
3 and 𝐿 = 3, the stairs pattern will be like this:

Stairs Pattern 𝑋 = 3, height 𝐿 = 3 with normal width:

Stairs Pattern 𝑋 = 3, height 𝐿 = 3 with 𝑋 times width:

Then, we can print the flag pattern using recursion.

Time Complexity: 𝑂(2𝑁 𝑁 2 𝐿)

Source Code:
https://ideone.com/nP2Hwp
D. Thou Shalt Not Passeth
The idea is straightforward. Given the list of participants, you only need to sort all
participants based on the largest number and then the most preferred color. To simplify
the color comparison, we can assign an integer value for each color. That way, we can
compare each person’s color easier than comparing color using strings.

Also, notice that the constraints are relatively small. Because of this, we can simply use a
basic sorting algorithm (e.g., bubble sort, selection sort).

Time Complexity: 𝑂(𝑁 2 ) or 𝑂(𝑁 log 𝑁), depending on the sorting algorithm.

Source Code:
https://ideone.com/qq0XzT
E. Identical Substring
First Method:
Iterate through the string S and count the number of consecutive identical characters. If
the current character is the same as the previous one, add 1 to the counter. Otherwise,
reset the counter back to 1. If the current counter is equal to 𝑁, print the correct index,
and stop the process.

Example:
𝑆 = “aabbccc”
𝑁=3
Position Counter
aabbccc 1
aabbccc 2
aabbccc 1
aabbccc 2
aabbccc 1
aabbccc 2
aabbccc 3 (Finished)

Alternative Method:
Check all substrings of 𝑆 with the size 𝑁. The first substring that satisfies the condition
will be the answer.

Example:
𝑆 = “aabbccc”
𝑁=3
Substring Is the answer?
aabbccc No
aabbccc No
aabbccc No
aabbccc No
aabbccc Yes (Finished)

Time Complexity:
First Method : 𝑂(|𝑆|)
Second Method : 𝑂(|𝑆|𝑁)

Source Code:
First Method : https://ideone.com/MV4jzS
Second Method : https://ideone.com/9Kh2Ea
F. Afterschool Candy
To solve this problem, we must sort all candy types based on their price. Do not forget
that when you sort the candy prices, you must also include the stock in the sorting process
so they won’t get mixed up.
After that, we just need to iterate through all candy types and find the maximum number
of candy that Jay and Max can buy. If the number of candy Jay and Max can buy is at least
𝑁, print “YES”, else print “NO”.
Do notice that we cannot manually pick candies one by one, as the constraint for 𝐵𝑖 is
109 . We can check the number of candies we can buy for the 𝑖-th type of candy by finding
𝐾
min ( , 𝑠𝑡𝑜𝑐𝑘[𝑖 ]). This formula determines whether you can buy all the stock for
𝑝𝑟𝑖𝑐𝑒[𝑖]
the current candy, or you can only buy some of it with your remaining money.

Time Complexity: 𝑂(𝑇𝑀 𝑙𝑜𝑔𝑀).


Source Code:
https://ideone.com/3Gx1tK
G. Unown Apocalypse
To solve this problem, we need to decrypt the word in the text back to normal, if the word is
not decrypted or still the same as before, then it means it is a tricky word or it means that do
not print the word.
We can make an array of int that can be used like a dictionary. This array saves the ASCII value
of a character. When we input the pairs, do not forget that there are lowercase letters too.
One of the solutions is to split the string into words by using strtok function from <string.h>
library. Each word is then decrypted back to normal and we can use a parameter that
determines whether the word has been decrypted back or stay the same as before. Once it
is done, if it has already been decrypted back, print the word.
Time Complexity: 𝑂(𝐿)
Source code:
https://ideone.com/zE72Vh
H. Pokemon Journey Continue

In this problem, we need to find how many islands there are on the map. It is said that
“Each island is not touching or adjacent to each other horizontally, vertically, or
diagonally”. This means two lands are still part of the same island if both of them touch
diagonally with each other.
As an example:

This is still called as 1 island since the lands are touching diagonally. To solve this, we can
use the flood fill algorithm. When we spot a land, it means that we found an island.
We recursively visit every adjacent cell in 8 directions and check whether that cell is part
of the same island the current cell is. After visiting a cell, change that cell into ‘.’ so that
we do not revisit the same cell twice. Do this until all cells become ‘.’.

Time Complexity: 𝑂(𝑁𝑀)

Source Code:
https://ideone.com/f1B7JR
I. Beauty of A String
To solve this problem, Initially, we make a list of numbers for the value of beauty that will
be obtained later in the process. Then we can generate all possible strings that can be
made by permutation. In each possible permutation, we calculate the beauty values and
marked them on the beauty values list. After the permutation process has been done,
then we can just search, how many beauty values has been marked and print the answer.
To understand the process of permutation, check the diagram below:

For every character that we want to add, check whether the character has been used or
not, temporarily mark the mentioned character as taken, then proceed to continue the
recursion for more characters.
Example:
Time Complexity: 𝑂(𝑁!)
Source code:
https://ideone.com/XbOVcL
J. New Era of Team Rocket
In this problem, we can solve just by doing exactly what the problem said or
implementation. We list the contestant in a struct array with the identity: name, total
vote, and total cities won. After listing the contestant, we go to the next part where it is
the votes from each city given.
In this part, we can use an array of strings to list the winner, then move it into the
contestant list. Do not forget to count the number of votes each contestant gets in each
city.
If the calculation and list of each city have been done, then we sort the contestant list
based on their total votes and total city win. We want it to be sorted by total votes in
descending order, or if the total votes are the same, then sorted by total city won in
descending order.
Then we have our winner on the first index of the list. But we need to check again whether
the first and the second index information (total votes and total cities won) are the same
or not. If same, then the election must be repeated. Otherwise, we have the winner.
Source code:
https://ideone.com/e0g08F

One of the admins reading this problem:

The creator:
K. A Trip with Friends
In this problem, we can solve it by generating all possible combinations of the group.
The method is quite similar to how we generate all permutation in the problem I, Beauty
of A String. To guarantee that the combination is sorted, we always pick an integer that
is larger than the integer we pick previously. With this, we also do not need to check
whether an integer is taken or not.
For example, check the diagram below:
Let 𝑁 = 4, 𝐾 = 3.
All possible combinations:

In each possible combination, check whether there are fail pairs (a pair of students who
do not like each other). One of the solutions is we can use a 2D matrix. If x and y are a
pair of students who do not like each other then we mark matrix[x][y] and matrix[y][x].
So that, when we check whether there are fail pairs or not, we can just look from the
marked matrix.

𝑁!
Time Complexity: 𝑂(𝐾!(𝑁−𝐾)! 𝐾 2 )

Source Code:
https://ideone.com/5OLMsN
L. Late for Work
Observe that to maximize the number of presents Santa can deliver, we need to pick
children starting with the lowest value of naughtiness first. So, the first step is to sort all
naughtiness in ascending order. Since the constraint for 𝑁 is large, we need a sorting
algorithm with 𝑂(𝑁 log 𝑁) time complexity.

The second problem is the query. You cannot count the answer simply by picking a child
while the average value is less than 𝐾𝑖 , as the time complexity would be 𝑂(𝑁𝑄) for the
worst case. A faster solution is needed.

Notice that if we try to pick 𝑋 children, then we will always pick 𝑋 children with the
smallest value. Thus, we can compute the sum of the first 𝑋 children using the prefix sum.

Prefix sum, more formally, can be expressed as the formula below.


𝑋

𝑆𝑢𝑚[𝑋] = ∑ 𝐴[𝑖]
𝑖=1
Where 𝐴[𝑖] is the 𝑖-th smallest naughtiness value.

Notice that the prefix sum is always sorted in ascending order since 𝐴[𝑖 ] > 0 for all 𝑖 and
the array is sorted. Because of this, we can do a binary search to determine the maximum
number of children to pick.

Example:
Look at the sample test case. First, sort all values in ascending order.
index 1 2 3 4 5
Arr[index] 2 3 4 7 9

Then, calculate the prefix sum.


index 0 1 2 3 4 5
Arr[index] 2 3 4 7 9
Sum[index] 0 2 5 9 16 25

Finally, do a binary search for the queries. Below is the process for 𝐾 = 3.
Left Mid Right Average Next Action
𝑆𝑢𝑚[3] 9 Average <= 𝐾, try higher
0 3 5 = =3
3 3 value.
𝑆𝑢𝑚 [4] 16 Average> 𝐾, try lower
3 4 5 = =4
4 4 value.
𝑆𝑢𝑚 [4] 16 Average> 𝐾, try lower
3 4 4 = =4
4 4 value.
3 3 3 Answer is found.

So, the maximum number of children Santa can give presents to for 𝐾 = 3 is 3.

Time Complexity: 𝑂(𝑇(𝑁𝑙𝑜𝑔𝑁 + 𝑄𝑙𝑜𝑔𝑁)) = 𝑂(𝑇𝑙𝑜𝑔𝑁(𝑁 + 𝑄 ))

Source Code:
https://ideone.com/MONQ1f
M. The Path to Sunibland
In this problem, we can use DFS or Depth-First Search algorithm (more commonly known
as floodfill) on finding the path. To start, we need to find the start point (𝑥 and 𝑦). Then
we can recursively go to 4 possible paths:

• 𝑥 – 1 and 𝑦
• 𝑥 + 1 and 𝑦
• 𝑥 and 𝑦 – 1
• 𝑥 and 𝑦 + 1
We continue each of that part again repeatedly until the point out of the border or reach
the destination point, do not forget that we need to list the movement too (U, R, D, and
L). Remember that we cannot pass the same cell twice. It means that we can use the
permutation concept, where we temporarily mark all passed points and erase those
marks when a path or answer has been found. When we reach the destination point, do
not forget to keep the string in an array of strings.
After the depth-first search has been done, then we can sort the array and output the
answer. It is recommended to sort the array in 𝑂(𝑁 log 𝑁) time complexity to avoid
getting Time Limit Exceeded verdict.

Source Code:
https://ideone.com/hzXbU0
N. Sherbet Equation
There are two things that need to be solved for an accepted solution.

The first one is calculating 2𝑋 for 𝑆(𝑋, 0). Doing this iteratively will cause your program
to run slower, as each 𝑆(𝑋, 0) will have a time complexity of 𝑂(𝑋).

The issue is, there might be several 𝑆(𝑋, 0) being executed. As an example, executing
𝑆(12, 2) will execute both 𝑆(12, 1) and 𝑆(6, 1). 𝑆(12, 1) will execute 𝑆(3, 0) and 𝑆(2, 0).
𝑆(6, 1) also executes 𝑆(3, 0) and 𝑆(2, 0). Notice that both function 𝑆(3, 0) and 𝑆(2, 0)
run twice, which is unnecessary.

To counter this problem, we can save the value of 2 𝑋 𝑚𝑜𝑑 109 + 7 in a separate array,
namely, 𝑐𝑎𝑙𝑐. Then, simply return the value of 𝑐𝑎𝑙𝑐[𝑋] whenever we call 𝑆(𝑋, 0).

The second one is the factorization process. Again, doing the factorization process in
𝑆(𝑋, 𝑏) will have a time complexity of 𝑂(𝑋). To optimize this, notice that if an integer 𝑖 is
𝑋
a factor of 𝑋, then 𝑖 is also a factor of 𝑋. With this, we can reduce the looping to only √𝑋
𝑋
instead of 𝑋, as the greatest integer 𝑖 where 𝑖 ≤ is √𝑋. Any factor beyond √𝑋 has
𝑖
already been checked when we check 𝑖, where 𝑖 ≤ √𝑋.

Source Code:
https://ideone.com/Bfy3t5

Note:
Alternatively, you can also use binary exponentation for calculating 2𝑋 .
O. Angry Alchemist
Idea 1: Brute force
Try every possible combination of 𝐴𝑖 , 𝐵𝑗 , 𝐶𝑘 , 𝐷𝑙 . If 𝐴𝑖 + 𝐵𝑗 + 𝐶𝑘 + 𝐷𝑙 = 𝐾, add the answer
by 1.
The time complexity is 𝑂(𝑁 4 ), which is not fast enough and will end up getting a Time
Limit Exceeded verdict.

Idea 2: Slightly faster brute force


Sort the array 𝐷. Try every possible combination of 𝐴𝑖 , 𝐵𝑗 , 𝐶𝑘 . Observe that if 𝐴𝑖 , +𝐵𝑗 +
𝐶𝑘 + 𝑋 = 𝐾, then 𝑋 = 𝐾 − (𝐴𝑖 + 𝐵𝑗 + 𝐶𝑘 ). Search for the value 𝑋 in array 𝐷 using lower
bound binary search.
The time complexity is 𝑂(𝑁 3 𝑙𝑜𝑔𝑁), still not enough and will end up getting a Time Limit
Exceeded verdict.

Idea 3: Even faster brute force (Expected Solution)


For every possible combination of 𝐴𝑖 and 𝐵𝑗 , store the value of 𝐴𝑖 + 𝐵𝑗 in a separate array,
namely, 𝑠𝑢𝑚𝐴𝐵. Do the similar thing for array 𝐶 and 𝐷. Store the value of 𝐶𝑖 + 𝐷𝑗 in an
array, 𝑠𝑢𝑚𝐶𝐷. Sort the array 𝑠𝑢𝑚𝐶𝐷. Now, for every value in 𝑠𝑢𝑚𝐴𝐵, find the number
of values in 𝑠𝑢𝑚𝐶𝐷 that is equal to 𝐾 − 𝑠𝑢𝑚𝐴𝐵𝑖 using lower bound binary search. Add
the number to the sum.
The time complexity is 𝑂(𝑁 2 log(𝑁 2 ))

Source Code:
https://ideone.com/oavVTI

You might also like