See the original problem on HackerRank.
Solutions
This simple problem can be solved by memorizing the inverse of \(p\) into a fastlookup container like a map/dictionary or even into an array (since the domain spans from \(1\) to \(N\)).
Since all the associations are unique, \(p\) is invertible.
Basically, the key of the map is the value of \(p\) and the value is the index.
Here is a C++ implementation:


We can even use a vector
instead of a map
:

