Rearrange an array such that ' arr [ j ] ' becomes ' i ' if ' arr [ i ] ' is ' j ' | Set 1 | A simple method to rearrange arr [ 0. . n - 1 ] ' ▁ so ▁ that ▁ ' arr [ j ] ' ▁ becomes ▁ ' i ' ▁ if ▁ ' arr [ i ] ' ▁ is ▁ ' j ; Create an auxiliary array of same size ; Store result in temp [ ] ; Copy temp back to arr [ ] ; A utility function to print contents of arr [ 0. . n - 1 ] ; Driver program
def rearrangeNaive ( arr , n ) : NEW_LINE INDENT temp = [ 0 ] * n NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT temp [ arr [ i ] ] = i NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT arr [ i ] = temp [ i ] NEW_LINE DEDENT DEDENT def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 3 , 0 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Given ▁ array ▁ is " , end = " ▁ " ) NEW_LINE printArray ( arr , n ) NEW_LINE rearrangeNaive ( arr , n ) NEW_LINE print ( " Modified array is " , ▁ end ▁ = ▁ "   " ) NEW_LINE printArray ( arr , n ) NEW_LINE