要更改的最小元素,以便对于索引 i,左侧的所有元素都是 -ve,右侧的所有元素都是 +ve
给定一个大小为n的数组arr ,任务是找到应该更改的最小元素数(元素值可以更改为任何值),以便存在索引0 ≤ i ≤ n-2 ,使得:
- 0到i (含)范围内的所有数字均< 0 。
- 在i+1到n-1 (包括)范围内的所有数字都是> 0 。
例子:
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
方法:固定i的值,我们需要进行哪些更改才能使索引i成为所需的索引?将i左侧的所有正元素更改为负,将i右侧的所有负元素更改为正。因此,所需的操作数量为:
(i 左边的正项数) + (i 右边的负数)
为了找到所需的术语,我们可以使用后缀 sum 预先计算它们。
因此,我们尝试将每个i作为所需的索引,并选择需要最小更改的索引。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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
= 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
输出:
1
时间复杂度: O(n)