Minimum number of changes such that elements are first Negative and then Positive | Function to return the count of minimum operations required ; To store the count of negative letegers on the right of the current index ( inclusive ) ; Find the count of negative letegers on the right ; If current element is negative ; To store the count of positive elements ; Find the positive letegers on the left ; If current element is positive ; Update the answer ; Return the required answer ; Driver Code
function Minimum_Operations ( a , n ) { let np = Array ( n + 1 ) . fill ( 0 ) ; np [ n ] = 0 ; for ( let i = n - 1 ; i >= 0 ; i -- ) { np [ i ] = np [ i + 1 ] ; if ( a [ i ] <= 0 ) np [ i ] ++ ; } let pos = 0 ; let ans = n ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( a [ i ] >= 0 ) pos ++ ; ans = Math . min ( ans , pos + np [ i + 1 ] ) ; } return ans ; } let a = [ - 1 , 0 , 1 , 2 ] ; let n = a . length ; document . write ( Minimum_Operations ( a , n ) ) ;