human
Rearrange an array so that arr [ i ] becomes arr [ arr [ i ] ] with O ( 1 ) extra space | The function to rearrange an array in - place so that arr [ i ] becomes arr [ arr [ i ] ] ..
First step : Increase all values by ( arr [ arr [ i ] ] % n ) * n.
Second Step : Divide all values by n.
A utility function to print an array of size n.
Driver program"
How can the above be solved in Python?
assistant
#Here is code to rearrange an array so that arr [ i ] becomes arr [ arr [ i ] ] with O ( 1 ) extra space and the other steps required.
 def rearrange ( arr , n ) :
	for i in range ( 0 , n ) :
		arr [ i ] += ( arr [ arr [ i ] ] % n ) * n
	for i in range ( 0 , n ) :
		arr [ i ] = int ( arr [ i ] / n )
def printArr ( arr , n ) :
	for i in range ( 0 , n ) :
		print ( arr [ i ] , end = " ▁ " )
	print ( " " )
arr = [ 3 , 2 , 0 , 1 ]
n = len ( arr )
print ( " Given ▁ array ▁ is " )
printArr ( arr , n )
rearrange ( arr , n ) ;
print ( " Modified ▁ array ▁ is " )
printArr ( arr , n )