Minimum operations to make product of adjacent element pair of prefix sum negative | Function to find minimum operations needed to make the product of any two adjacent elements in prefix sum array negative ; Stores the minimum operations ; Stores the prefix sum and number of operations ; Traverse the array ; Update the value of sum ; Check if i + r is odd ; Check if prefix sum is not positive ; Update the value of ans and sum ; Check if prefix sum is not negative ; Update the value of ans and sum ; Update the value of res ; Print the value of res ; Driver code
def minOperations ( a ) : NEW_LINE INDENT res = 100000000000 NEW_LINE N = len ( a ) NEW_LINE for r in range ( 0 , 2 ) : NEW_LINE INDENT sum = 0 NEW_LINE ans = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE if ( ( i + r ) % 2 ) : NEW_LINE INDENT if ( sum <= 0 ) : NEW_LINE INDENT ans += - sum + 1 NEW_LINE sum = 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( sum >= 0 ) : NEW_LINE INDENT ans += sum + 1 ; NEW_LINE sum = - 1 ; NEW_LINE DEDENT DEDENT DEDENT res = min ( res , ans ) NEW_LINE DEDENT print ( res ) NEW_LINE DEDENT a = [ 1 , - 3 , 1 , 0 ] NEW_LINE minOperations ( a ) ; NEW_LINE