Sum of all differences between Maximum and Minimum of increasing Subarrays | Function to calculate and return the sum of differences of maximum and minimum of strictly increasing subarrays ; Stores the sum ; Traverse the array ; If last element of the increasing sub - array is found ; Update sum ; If the last element of the array is reached ; Update sum ; Return the sum ; Driver Code
def sum_of_differences ( arr , N ) : NEW_LINE INDENT sum = 0 NEW_LINE i = 0 NEW_LINE while ( i < N - 1 ) : NEW_LINE INDENT if arr [ i ] < arr [ i + 1 ] : NEW_LINE INDENT flag = 0 NEW_LINE for j in range ( i + 1 , N - 1 ) : NEW_LINE INDENT if arr [ j ] >= arr [ j + 1 ] : NEW_LINE INDENT sum += ( arr [ j ] - arr [ i ] ) NEW_LINE i = j NEW_LINE flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if flag == 0 and arr [ i ] < arr [ N - 1 ] : NEW_LINE INDENT sum += ( arr [ N - 1 ] - arr [ i ] ) NEW_LINE break NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT arr = [ 6 , 1 , 2 , 5 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( sum_of_differences ( arr , N ) ) NEW_LINE