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:

  1. All the numbers in range 0 to i (inclusive) are < 0.
  2. All numbers in range i+1 to n-1 (inclusive) are > 0.

Examples:

Input: arr[] = {-1, -2, -3, 3, -5, 3, 4}
Output: 1
Change -5 to 5 and the array becomes {-1, -2, -3, 3, 5, 3, 4}



Input: arr[] = {3, -5}
Output: 2
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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>
chevron_right

Output:
1

Time Complexity : O(n)




Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :