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
def printNewArray ( a , n ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT v . append ( ( a [ i ] , i ) ) NEW_LINE DEDENT v . sort ( ) NEW_LINE ans = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans [ v [ ( i + 1 ) % n ] [ 1 ] ] = v [ i ] [ 0 ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 4 , 1 , 2 , 5 , 3 ] NEW_LINE n = len ( a ) NEW_LINE printNewArray ( a , n ) NEW_LINE DEDENT