You are on page 1of 4

Name: Rollno:

ESO207: Data Structures and Algorithms


Programming Assignment 2
Due Date: 13th Feb, 2020

Total Number of Pages: 4 Total Points 300

Question 1. (100 points) Pizza Marathon


You and your wingies go to Inter-IIT Food Fest to take part in Pizza Marathon. The rules of competition
are as follows:

• There will be n stacks of pizzas, each containing a different number of pizzas. k persons from a
team are allowed to eat the pizzas.
• The constraint is that any participant can only eat consecutive stacks and once you start eating a
stack, only you can finish it (i.e. 2 participants can’t share a stack).
• It takes all of you to eat 1 pizza in 1 min. As soon as the time starts, all participants start eating
pizzas. The team which finishes all the stacks in the minimum time wins.

Now, you have to help your team in order to win. Be careful as the reputation of IITK is at stake.
Find the minimum time required.
Input:
Integer q - number of queries (For each query output the answer in a new line)
Integer n - number of stacks
Integer k - number of participants allowed
Integer array P containing sizes of stacks
Constraints:
1 ≤ q ≤ 1000
1 ≤ n ≤ 104
1 ≤ k ≤ 104
1 ≤ P[i] ≤ 106
Example:
Input:
1
4
2
10 20 30 40
Output:
60
Explanation: 1st participant will eat the first 3 stacks and 2nd participant will eat the last stack.

Question 2. (60 points) Alien Spaceship


Humans have reached an Alien planet through a worm hole. But as soon as they land on the planet,
the astronauts are captured by the aliens. They are facing problem in code of their spaceship and want
help from humans. You see their code and realize that the problem can be solved with a special kind of

Page 1 of 4
Name: Rollno:
stack. But aliens don’t know what a stack is. They only know about arrays. You can make it out alive
if you show them how to implement stacks using arrays. The kind of stack that is needed should be able
to get the minimum element in O(1) time.
Implement the custom stack with the following functionalities:
1. isEmpty() - return true if the stack is empty, else false
2. push(x) - pushes x on top of stack
3. get top() - return the top of stack, return -1 if stack is empty
4. pop() - removes the top element (do nothing if stack is empty)
5. get min() - returns the minimum element present in the stack, -1 if stack is empty
6. clear() - clears the stack
Input:
integer q - denoting the number of queries
Each query is one of the functions mentioned above with appropriate arguments
Output:
1. isEmpty - True or False
2. get top - top element or -1, accordingly
3. get min - min element or -1, accordingly
4. For push, pop and clear, do not print anything
Constraints:
1 ≤ q ≤ 107
1 ≤ x ≤ 109
Example:
Input:
14
push 5
push 2
push 3
push 4
get top
pop
get top
get min
isEmpty
clear
get top
get min
push 10
get top
Output:
4
3
2
False
-1
-1
10

Page 2 of 4
Name: Rollno:
Question 3. (140 points) Mind Games
Krishna and Dipanker are always in fight that who is more fast at computation and memorization.
Recently Krishna found a way of testing who is better. Krishna will ask Dipanker to learn some numbers
and then perform some operations on them. The operations are as follows:
1. learn x - Dipanker has to add this number to his dictionary
2. forget x - Dipanker has to delete this number from his dictionary. If number is not present, do
nothing.
3. decrease x n - decrease the count of x by n. If n ≥ count(x) - delete the number from his dictionary
(and do nothing if number is not present)
4. smaller nums x - find the total count of numbers smaller than x
5. larger nums x - find the total count of numbers larger than x
6. asc k - find the kth element from the start if all elements are written in ascending order (include
the count in finding this)
Since Dipanker is not so smart, he needs your help to solve this question.
Input:
Integer q - number of the queries
q lines, each containing some type of query above.
Output:
Print the following in each case:
1. smaller nums x - total count of numbers smaller than x
2. larger nums x - total count of numbers larger than x
3. asc k - kth element from start in ascending order, -1 if k is larger than total count of elements
4. For learn, forget and decrease do not print anything
Constraints:
1 ≤ q ≤ 5 · 105
1 ≤ x ≤ 109
Example:
Input:
14
learn 5
learn 2
learn 7
learn 3
learn 2
smaller nums 5
larger nums 2
asc 2
decrease 2 1
asc 2
forget 7
larger nums 2
forget 5
larger nums 2
Output:
3
3

Page 3 of 4
Name: Rollno:
2
3
2
1
Note:

• Test link: https://www.hackerrank.com/eso207a-pa2


• Please do not copy from anywhere. Plagiarism will be checked, and if found, you can get an F.
• Allowed languages: C, C++
Allowed libraries: stdio.h for C (and stdlib.h for ques 3) and iostream for C++.

• Try using scanf and printf rather than cin and cout, else you may get TLE.
• Submit your code on moodle (in .c or .cpp format only)

Page 4 of 4

You might also like