Rearrange an Array such that Sum of same | Function to rearrange the array such that no same - indexed subset have sum equal to that in the original array ; Initialize a vector ; Iterate the array ; Sort the vector ; Shift of elements to the index of its next cyclic element ; Print the answer ; Driver Code
function printNewArray ( a , n ) { var v = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { v . push ( [ a [ i ] , i ] ) ; } v . sort ( ) ; var ans = Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { ans [ v [ ( i + 1 ) % n ] [ 1 ] ] = v [ i ] [ 0 ] ; } for ( var i = 0 ; i < n ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } var a = [ 4 , 1 , 2 , 5 , 3 ] ; var n = a . length ; printNewArray ( a , n ) ;