While the complexity in the worst case is O(N), this is very rare on random input. And unordered_map will fail, only if we study the hashing function and intentionally provide such an input, that’ll increase collisions, blowing it up
Thus it’s safe to assume the complexity is O(1).
Since O(1) is faster than O(logN) map gives TLE, but unorderedmap doesn’t
O(nlgn) will not give AC. It was luck that unordered map gave AC as there were no collisions. Now a days, setters deliberately put some TC where unordered map will give TLE ( O(n) ).
Thanks For Replying,
But its not everytime that we can assume unordered map will be O(1) , Most Of the time it’s Reverse,map gives AC while unordered map gives TLE.
That’s what i am saying that we can’t trust unordered map as it’s worst case is O(n) but we can trust a map as in every situation it gives O(Log n).
and Unordered map giving AC means Test Cases are weak.Right?
I’ve never used auxiliary array in practice.
Unordered_map has always worked for me, but if you’re concerned about getting hacked, this shows how to avoid getting hacked
I’m just saying that, when we don’t need long long, we could just use simple int, it speeds up execution and saves us from TLE in some cases, like solving Multiset from CF, using BIT. Using long long gets TLE here, but int does not.
Exactly, idk who uses it, unordered containers have always worked perfectly fine for me, if you know a problem where it doesn’t, please provide the link.
Thank You So Much @dardev , You mean to say that collisions in unordered map happens when there are more than one key value pair in a particular bucket.
Right?