Rearrange an array such that arr [ i ] = i | C ++ program for above approach ; Function to tranform the array ; Iterate over the array ; Checf 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
#include <iostream> NEW_LINE using namespace std ; void fixArray ( int ar [ ] , int n ) { int i , j , temp ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( ar [ j ] == i ) { temp = ar [ j ] ; ar [ j ] = ar [ i ] ; ar [ i ] = temp ; break ; } } } for ( i = 0 ; i < n ; i ++ ) { if ( ar [ i ] != i ) { ar [ i ] = -1 ; } } cout << " Array ▁ after ▁ Rearranging " << endl ; for ( i = 0 ; i < n ; i ++ ) { cout << ar [ i ] << " ▁ " ; } } int main ( ) { int n , ar [ ] = { -1 , -1 , 6 , 1 , 9 , 3 , 2 , -1 , 4 , -1 } ; n = sizeof ( ar ) / sizeof ( ar [ 0 ] ) ; fixArray ( ar , n ) ; }