PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Setter: Praveen Dhinwa
Tester: Teja Vardhan Reddy
Editorialist: Taranpreet Singh
DIFFICULTY:
Cakewalk
PREREQUISITES:
Basic Data-structures. Sets/Arrays.
PROBLEM:
Given two lists T1 and D1 denoting the truth tasks and dare tasks, Ram can perform and lists T2 and D2 denoting the tasks Ram is asked to perform, Inform whether Ram can perform all tasks or not.
Note that performing a task does not cause Ram’s ability to perform that task again.
QUICK EXPLANATION
- Ram can perform all tasks if all distinct elements of T2 and D2 are also present in T1 and D1.
- Just print in yes/no terms, whether the above condition is satisfied or not.
EXPLANATION
In this problem, we know the tasks Ram can perform, and the task he will be asked to perform. So, we need to check, if for all tasks Ram will be asked to perform, Can Ram perform all of them. If he can, we print yes, otherwise, we print no.
So, checking whether T1 a task, can be done using a boolean array or using set data structure. We do the same for D1.
Using set
We insert all elements of T1 into a set, and for every element in T2, we check whether this element is present in the set. If not, we know Ram cannot perform all tasks.
Using boolean array
We make a boolean array and mark all the elements in T1 as doable. Now, for every element in T2, we can check immediately if this task is doable.
Both of these solutions takes time O(N) where N is the size of the input. The constraints of this problem were low enough to let even the slow solution pass, which is, for every task in the to-do list, iterate over all elements of the can-do list and check if todo task is present in the list or not.
Time Complexity
Time complexity is O(N) where N is the size of the input.
AUTHOR’S AND TESTER’S SOLUTIONS:
Setter’s solution
Tester’s solution
Editorialist’s solution
Feel free to Share your approach, If it differs. Suggestions are always welcomed.