Mike and hash trick problem

Code is giving error

Hey @jha.aparna17
You dont have to code the same function
In this question you are given a hash function which give you a integer corresponding to an array
using its algorithm mentioned in question

but the problem is that algorithm has high time complexity

now you have to code a algorithm with less time complexity
first observe the pattern and then code the algorithm in another way so it becomes efficient

Can you give me a hint for solving the problem because i cant think of anything else

hello @jha.aparna17

refer this->
We know everytime a number repeats, it is given a new hash value, which is equal to the number of distinct numbers occurred till now. So we can just store the last occurrences of all the numbers and assign the values incrementally, in increasing order of the positions of their last occurrences.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

The code is giving TLE for last test case in Mike and Hashtrick problem

Hey @jha.aparna17 use unordered map instead of map, you will pass all test cases.

yes, now the program is working but why are we using unordered map only?

That’s because unordered map works as a hashtable, thus inserting , deleting and searching of element in O(1) time.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.