Rearrange an array such that arr [ i ] = i | Function to tranform the array ; Iterate over the array ; Check is any ar [ j ] exists such that ar [ j ] is equal to i ; Iterate over array ; If not present ; Print the output ; Driver Code ; Function Call
def fixArray ( ar , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( ar [ j ] == i ) : NEW_LINE INDENT ar [ j ] , ar [ i ] = ar [ i ] , ar [ j ] NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( ar [ i ] != i ) : NEW_LINE INDENT ar [ i ] = - 1 NEW_LINE DEDENT DEDENT print ( " Array ▁ after ▁ Rearranging " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( ar [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT ar = [ - 1 , - 1 , 6 , 1 , 9 , 3 , 2 , - 1 , 4 , - 1 ] NEW_LINE n = len ( ar ) NEW_LINE fixArray ( ar , n ) ; NEW_LINE