Rearrange two given arrays to maximize sum of same indexed elements | Function to find the maximum possible sum by rearranging the given array elements ; Sort the array A [ ] in ascending order ; Sort the array B [ ] in descending order ; Stores maximum possible sum by rearranging array elements ; Traverse both the arrays ; Update maxSum ; Driver Code
def MaxRearrngeSum ( A , B , N ) : NEW_LINE INDENT A . sort ( ) NEW_LINE B . sort ( reverse = True ) NEW_LINE maxSum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT maxSum += abs ( A [ i ] - B [ i ] ) NEW_LINE DEDENT return maxSum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 2 , 2 , 4 , 5 ] NEW_LINE B = [ 5 , 5 , 5 , 6 , 6 ] NEW_LINE N = len ( A ) NEW_LINE print ( MaxRearrngeSum ( A , B , N ) ) NEW_LINE DEDENT