Minimum number of changes such that elements are first Negative and then Positive | C ++ implementation of the approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Minimum_Operations ( int a [ ] , int n ) { int np [ n + 1 ] ; np [ n ] = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { np [ i ] = np [ i + 1 ] ; if ( a [ i ] <= 0 ) np [ i ] ++ ; } int pos = 0 ; int ans = n ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( a [ i ] >= 0 ) pos ++ ; ans = min ( ans , pos + np [ i + 1 ] ) ; } return ans ; } int main ( ) { int a [ ] = { -1 , 0 , 1 , 2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << Minimum_Operations ( a , n ) ; return 0 ; }