This is a problem that requires simulation. But one thing needs to be carefully taken care off, that is we can’t use a dictionary to keep frequencies.
I made a mistake of using a dictionary and whenever the current frequency is not 1 (which means it’s not unique), I updated the next key with freq[current] - 1
and updated the current frequency.
You might have noticed the flaw in this - if I have consecutive numbers this would have worked. But since I don’t I mustn’t update the next key, rather the next number must be added with freq[current] - 1
.
This way, we are making sure that we are accurately counting the number of steps to make array unique - and most importantly - not counting the steps multiple times.
Code:
Java
Big O Analysis
-
Runtime
The runtime complexity here is since we are visiting all elements in the array only once.
-
Memory The memory usage is since we use array to store frequencies.
— A