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
Last Updated :
09 Sep, 2022
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++
#include <bits/stdc++.h>
using namespace std;
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]++;
}
int pos = 0;
int mn = n;
for (i = 0; i < n - 1; i++) {
if (a[i] >= 0)
pos++;
mn = min(mn, pos + sf[i + 1]);
}
return mn;
}
int main()
{
int a[] = { -1, -2, -3, 3, -5, 3, 4 };
int n = sizeof (a) / sizeof (a[0]);
cout << minimumChanges(n, a);
}
|
Java
import java.io.*;
class GFG {
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]++;
}
int pos = 0 ;
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;
}
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));
}
}
|
Python 3
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
pos = 0
mn = n
for i in range (n - 1 ) :
if (a[i] > = 0 ):
pos + = 1
mn = min (mn, pos + sf[i + 1 ])
return mn
if __name__ = = "__main__" :
a = [ - 1 , - 2 , - 3 , 3 , - 5 , 3 , 4 ]
n = len (a)
print (minimumChanges(n, a))
|
C#
using System;
public class GFG
{
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]++;
}
}
int pos = 0;
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;
}
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));
}
}
|
PHP
<?php
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 ]++;
}
$pos = 0;
$mn = $n ;
for ( $i = 0; $i < $n - 1; $i ++)
{
if ( $a [ $i ] >= 0)
$pos ++;
$mn = min( $mn , $pos + $sf [ $i + 1]);
}
return $mn ;
}
$a = array (-1, -2, -3, 3, -5, 3, 4 );
$n = sizeof( $a );
echo minimumChanges( $n , $a );
?>
|
Javascript
<script>
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]++;
}
}
let pos = 0;
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>
|
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.
Share your thoughts in the comments
Please Login to comment...