Suppose we have an array having 0 to n-1 distinct integers
example
{2,1,4,3,5,0}
our goal is to modify this array such that a[0]
becomes a[a[0]]
means a[0]
has value 2 here so a[0]
= a[2]
and so on.
Having the following output :
{4,1,5,3,0,2}
.
Three things are to be noted here that:
- positions have range 0 to n-1 and values have range 0 to n-1 too (obviously).
- no extra array or link list or any data structure should be taken.
- should have time complexity O(n).