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 ; Retrieving old value and storing with the new one ; Retrieving new value ; A utility function to pr contents of arr [ 0. . n - 1 ] ; Driver code
def rearrange ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT arr [ arr [ i ] % n ] += i * n NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] //= n NEW_LINE DEDENT DEDENT def prArray ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT arr = [ 2 , 0 , 1 , 4 , 5 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Given ▁ array ▁ is ▁ : ▁ " ) NEW_LINE prArray ( arr , n ) NEW_LINE rearrange ( arr , n ) NEW_LINE print ( " Modified ▁ array ▁ is ▁ : " ) NEW_LINE prArray ( arr , n ) NEW_LINE