KJCP04 - Editorial

PROBLEM LINK:

Contest

Editorialist: Aditya Prajapati

DIFFICULTY:

EASY

PROBLEM:

You’ve got a set of instructions by which you can add an element to a list (insert k), remove all occurences of an element from the list (delete k), and get the sum of the minimum and maximum elements of the list (sum). You have to print the sum of the minimum and maximum elements of the list when you encounter a sum instruction.

EXPLANATION:

We have to maintain a list and add numbers to it when an insert instruction is found. So, if we have

insert 2 

our list will be [2]. Now, if similarly 3, 5 and 2 are added again, the list will be

[2, 3, 5, 2]

Now, whenever a delete instruction is found, we will remove all occurences of that number from the list. Example:

delete 2 

So, the list will be:

[3, 5]

When a sum instruction is found, we need to print the sum of the minimum and maximum elements of the list.

Solution

Maintain a python set.

Add k to set when insert k is encountered.

Remove k from set when delete k is encountered.

When sum is encountered, cast set to list and use the max and min properties of the list to get maximum and minumum values, add these and print them.

Time Complexity:

O(n)

Editorialists’ solution can be found here.

1 Like