Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
Given an array arr of size n, the task is to find the minimum number of elements that should be changed (element value can be changed to anything) so that there exists an index 0 ? i ? n-2 such that:
- All the numbers in range 0 to i (inclusive) are < 0.
- All numbers in range i+1 to n-1 (inclusive) are > 0.
Examples:
Input: arr[] = {-1, -2, -3, 3, -5, 3, 4}
Output: 1
Explanation: Change -5 to 5 and the array becomes {-1, -2, -3, 3, 5, 3, 4}Input: arr[] = {3, -5}
Output: 2
Explanation: Change 3 to -3 and -5 to 5
Approach: Fix the value of i, what changes would we need to make index i the required index? Change all the positive elements on the left of i to negative and all negative elements to the right of i to positive. Hence, the number of operations required would be:
(Number of positive terms on the left of i) + (Number of negative terms on the right of i)
To find the required terms, we can pre-compute them using suffix sum.
Hence, we try each i as the required index and choose the one which needs minimum changes.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the minimum number // of required changes int minimumChanges( int n, int a[]) { int i, sf[n + 1]; sf[n] = 0; for (i = n - 1; i >= 0; i--) { sf[i] = sf[i + 1]; if (a[i] <= 0) sf[i]++; } // number of elements >=0 in prefix int pos = 0; // Minimum elements to change int mn = n; for (i = 0; i < n - 1; i++) { if (a[i] >= 0) pos++; mn = min(mn, pos + sf[i + 1]); } return mn; } // Driver Program to test above function int main() { int a[] = { -1, -2, -3, 3, -5, 3, 4 }; int n = sizeof (a) / sizeof (a[0]); cout << minimumChanges(n, a); } |
Java
// Java implementation of the approach import java.io.*; class GFG { // Function to return the minimum number // of required changes static int minimumChanges( int n, int a[]) { int i; int []sf= new int [n+ 1 ]; sf[n] = 0 ; for (i = n - 1 ; i >= 0 ; i--) { sf[i] = sf[i + 1 ]; if (a[i] <= 0 ) sf[i]++; } // number of elements >=0 in prefix int pos = 0 ; // Minimum elements to change int mn = n; for (i = 0 ; i < n - 1 ; i++) { if (a[i] >= 0 ) pos++; mn = Math.min(mn, pos + sf[i + 1 ]); } return mn; } // Driver Program to test above function public static void main (String[] args) { int []a = { - 1 , - 2 , - 3 , 3 , - 5 , 3 , 4 }; int n = a.length; System.out.println( minimumChanges(n, a)); } } // This code is contributed by inder_verma. |
Python 3
# Python 3 implementation of the approach # Function to return the minimum # number of required changes def minimumChanges(n, a): sf = [ 0 ] * (n + 1 ) sf[n] = 0 for i in range (n - 1 , - 1 , - 1 ) : sf[i] = sf[i + 1 ] if (a[i] < = 0 ): sf[i] + = 1 # number of elements >=0 in prefix pos = 0 # Minimum elements to change mn = n for i in range (n - 1 ) : if (a[i] > = 0 ): pos + = 1 mn = min (mn, pos + sf[i + 1 ]) return mn # Driver Code if __name__ = = "__main__" : a = [ - 1 , - 2 , - 3 , 3 , - 5 , 3 , 4 ] n = len (a) print (minimumChanges(n, a)) # This code is contributed # by ChitraNayal |
C#
using System; // C# implementation of the approach public class GFG { // Function to return the minimum number // of required changes public static int minimumChanges( int n, int [] a) { int i; int [] sf = new int [n + 1]; sf[n] = 0; for (i = n - 1; i >= 0; i--) { sf[i] = sf[i + 1]; if (a[i] <= 0) { sf[i]++; } } // number of elements >=0 in prefix int pos = 0; // Minimum elements to change int mn = n; for (i = 0; i < n - 1; i++) { if (a[i] >= 0) { pos++; } mn = Math.Min(mn, pos + sf[i + 1]); } return mn; } // Driver Program to test above function public static void Main( string [] args) { int [] a = new int [] {-1, -2, -3, 3, -5, 3, 4}; int n = a.Length; Console.WriteLine(minimumChanges(n, a)); } } // This code is contributed by Shrikant13 |
PHP
<?php // PHP implementation of the approach // Function to return the minimum number // of required changes function minimumChanges( $n , $a ) { $i ; $sf [ $n + 1] = array (); $sf [ $n ] = 0; for ( $i = $n - 1; $i >= 0; $i --) { $sf [ $i ] = $sf [ $i + 1]; if ( $a [ $i ] <= 0) $sf [ $i ]++; } // number of elements >=0 in prefix $pos = 0; // Minimum elements to change $mn = $n ; for ( $i = 0; $i < $n - 1; $i ++) { if ( $a [ $i ] >= 0) $pos ++; $mn = min( $mn , $pos + $sf [ $i + 1]); } return $mn ; } // Driver Code $a = array (-1, -2, -3, 3, -5, 3, 4 ); $n = sizeof( $a ); echo minimumChanges( $n , $a ); // This code is contributed by ajit ?> |
Javascript
<script> // Javascript implementation of the approach // Function to return the minimum number // of required changes function minimumChanges(n, a) { let i; let sf = new Array(n + 1); sf.fill(0); sf[n] = 0; for (i = n - 1; i >= 0; i--) { sf[i] = sf[i + 1]; if (a[i] <= 0) { sf[i]++; } } // number of elements >=0 in prefix let pos = 0; // Minimum elements to change let mn = n; for (i = 0; i < n - 1; i++) { if (a[i] >= 0) { pos++; } mn = Math.min(mn, pos + sf[i + 1]); } return mn; } let a = [-1, -2, -3, 3, -5, 3, 4]; let n = a.length; document.write(minimumChanges(n, a)); </script> |
1
Complexity Analysis:
- Time Complexity: O(N), where N is the size of the given array.
- Auxiliary Space: O(N), for creating an additional array of size N+1.
Contact Us