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 integers on the right of the current index ( inclusive ) ; Find the count of negative integers on the right ; If current element is negative ; To store the count of positive elements ; Find the positive integers on the left ; If current element is positive ; Update the answer ; Return the required answer ; Driver code
< ? php function Minimum_Operations ( $ a , $ n ) { $ np = array ( ) ; $ np [ $ n ] = 0 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { $ np [ $ i ] = $ np [ $ i + 1 ] ; if ( $ a [ $ i ] <= 0 ) $ np [ $ i ] ++ ; } $ pos = 0 ; $ ans = $ n ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ a [ $ i ] >= 0 ) $ pos ++ ; $ ans = min ( $ ans , $ pos + $ np [ $ i + 1 ] ) ; } return $ ans ; } $ a = array ( -1 , 0 , 1 , 2 ) ; $ n = count ( $ a ) ; echo Minimum_Operations ( $ a , $ n ) ; ? >