Minimum number of changes such that elements are first Negative and then Positive
Last Updated :
09 Jun, 2022
Given an array arr[] of size N. The task is to find the minimum number of changes required to convert the array such that for any index 0 ? k < N, the elements in the array upto k-th index will be less than zero and after k-th index will be greater than zero.
That is:
arr[0] < 0, arr[1] < 0, …, arr[k] < 0 and arr[k + 1] > 0, arr[k + 2] > 0, …, arr[N – 1] > 0.
Examples:
Input: arr[] = { -1, 1, 2, -1}
Output: 1
Replace last -1 with any positive integer.
Input: arr[] = { -1, 0, 1, 2 }
Output: 1
Replace 0 with any negative integer.
Approach: First, find for each valid k, the number of non-negative integers to the left of it and the number of non-positive integers to the right. Now, run a loop for each valid k (0 ? k <n) and find the sum of the number of non-negative integers left to it and number of non-positive integers right to it, and the minimum of these values for every k is our required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
class GFG
{
static int Minimum_Operations( int []a, int n)
{
int [] np = new int [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 = Math.min(ans, pos + np[i + 1 ]);
}
return ans;
}
public static void main(String args[])
{
int []a = { - 1 , 0 , 1 , 2 };
int n = a.length;
System.out.print(Minimum_Operations(a, n));
}
}
|
Python3
def Minimum_Operations(a, n):
np = [ 0 for i in range (n + 1 )]
for i in range (n - 1 , - 1 , - 1 ):
np[i] = np[i + 1 ]
if (a[i] < = 0 ):
np[i] + = 1
pos = 0
ans = n
for i in range (n - 1 ):
if (a[i] > = 0 ):
pos + = 1
ans = min (ans, pos + np[i + 1 ])
return ans
a = [ - 1 , 0 , 1 , 2 ]
n = len (a)
print (Minimum_Operations(a, n))
|
C#
using System;
class GFG
{
static int Minimum_Operations( int []a, int n)
{
int [] np = new int [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 = Math.Min(ans, pos + np[i + 1]);
}
return ans;
}
static void Main()
{
int []a = { -1, 0, 1, 2 };
int n = a.Length;
Console.WriteLine(Minimum_Operations(a, n));
}
}
|
PHP
<?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 );
?>
|
Javascript
<script>
function Minimum_Operations(a, n)
{
let np = Array(n+1).fill(0);
np[n] = 0;
for (let i = n - 1; i >= 0; i--)
{
np[i] = np[i + 1];
if (a[i] <= 0)
np[i]++;
}
let pos = 0;
let ans = n;
for (let i = 0; i < n - 1; i++)
{
if (a[i] >= 0)
pos++;
ans = Math.min(ans, pos + np[i + 1]);
}
return ans;
}
let a = [ -1, 0, 1, 2 ];
let n = a.length;
document.write(Minimum_Operations(a, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...