📌  相关文章
📜  最小更改数量,以使元素首先为负,然后为正

📅  最后修改于: 2021-04-30 03:33:58             🧑  作者: Mango

给定大小为N的数组arr [] 。任务是找到转换数组所需的最小更改数,以使对于任何索引0≤k ,数组中直到第k个索引的元素都将小于零,而在第k个索引之后,则更大大于零。

那是:

例子:

方法:首先,对于每个有效k ,在其左侧找到非负整数的数量,在右侧找到非正整数的数量。现在,为每个有效k (0≤k k个这些值的最小值是我们必需的答案。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count
// of minimum operations required
int Minimum_Operations(int a[], int n)
{
  
    // To store the count of negative integers
    // on the right of the current index (inclusive)
    int np[n + 1];
    np[n] = 0;
  
    // Find the count of negative integers
    // on the right
    for (int i = n - 1; i >= 0; i--) {
        np[i] = np[i + 1];
  
        // If current element is negative
        if (a[i] <= 0)
            np[i]++;
    }
  
    // To store the count of positive elements
    int pos = 0;
    int ans = n;
  
    // Find the positive integers
    // on the left
    for (int i = 0; i < n - 1; i++) {
  
        // If current element is positive
        if (a[i] >= 0)
            pos++;
  
        // Update the answer
        ans = min(ans, pos + np[i + 1]);
    }
  
    // Return the required answer
    return ans;
}
  
// Driver code
int main()
{
    int a[] = { -1, 0, 1, 2 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << Minimum_Operations(a, n);
  
    return 0;
}


Java
// Java implementation of the approach 
class GFG
{
      
// Function to return the count 
// of minimum operations required 
static int Minimum_Operations(int []a, int n) 
{ 
  
    // To store the count of negative integers 
    // on the right of the current index (inclusive) 
    int[] np = new int[n + 1]; 
    np[n] = 0; 
  
    // Find the count of negative integers 
    // on the right 
    for (int i = n - 1; i >= 0; i--)
    { 
        np[i] = np[i + 1]; 
  
        // If current element is negative 
        if (a[i] <= 0) 
            np[i]++; 
    } 
  
    // To store the count of positive elements 
    int pos = 0; 
    int ans = n; 
  
    // Find the positive integers 
    // on the left 
    for (int i = 0; i < n - 1; i++) 
    { 
  
        // If current element is positive 
        if (a[i] >= 0) 
            pos++; 
  
        // Update the answer 
        ans = Math.min(ans, pos + np[i + 1]); 
    } 
  
    // Return the required answer 
    return ans; 
} 
  
// Driver code 
public static void main(String args[]) 
{ 
    int []a = { -1, 0, 1, 2 }; 
    int n = a.length; 
    System.out.print(Minimum_Operations(a, n)); 
} 
}
  
// This code is contributed by Akanksha Rai


Python3
# Python3 implementation of the approach
  
# Function to return the count
# of minimum operations required
def Minimum_Operations(a, n):
  
    # To store the count of negative integers
    # on the right of the current index (inclusive)
    np = [0 for i in range(n + 1)]
  
    # Find the count of negative integers
    # on the right
    for i in range(n - 1, -1, -1):
        np[i] = np[i + 1]
  
        # If current element is negative
        if (a[i] <= 0):
            np[i] += 1
  
    # To store the count of positive elements
    pos = 0
    ans = n
  
    # Find the positive integers
    # on the left
    for i in range(n - 1):
  
        # If current element is positive
        if (a[i] >= 0):
            pos += 1
  
        # Update the answer
        ans = min(ans, pos + np[i + 1])
  
    # Return the required answer
    return ans
  
# Driver code
a = [-1, 0, 1, 2]
n = len(a)
print(Minimum_Operations(a, n))
  
# This code is contributed by mohit kumar


C#
// C# implementation of the approach 
using System; 
  
class GFG
{
      
// Function to return the count 
// of minimum operations required 
static int Minimum_Operations(int []a, int n) 
{ 
  
    // To store the count of negative integers 
    // on the right of the current index (inclusive) 
    int[] np = new int[n + 1]; 
    np[n] = 0; 
  
    // Find the count of negative integers 
    // on the right 
    for (int i = n - 1; i >= 0; i--)
    { 
        np[i] = np[i + 1]; 
  
        // If current element is negative 
        if (a[i] <= 0) 
            np[i]++; 
    } 
  
    // To store the count of positive elements 
    int pos = 0; 
    int ans = n; 
  
    // Find the positive integers 
    // on the left 
    for (int i = 0; i < n - 1; i++) 
    { 
  
        // If current element is positive 
        if (a[i] >= 0) 
            pos++; 
  
        // Update the answer 
        ans = Math.Min(ans, pos + np[i + 1]); 
    } 
  
    // Return the required answer 
    return ans; 
} 
  
// Driver code 
static void Main() 
{ 
    int []a = { -1, 0, 1, 2 }; 
    int n = a.Length; 
    Console.WriteLine(Minimum_Operations(a, n)); 
} 
}
  
// This code is contributed by mits


PHP
= 0; $i--) 
    { 
        $np[$i] = $np[$i + 1]; 
  
        // If current element is negative 
        if ($a[$i] <= 0) 
            $np[$i]++; 
    } 
  
    // To store the count of positive elements 
    $pos = 0; 
    $ans = $n; 
  
    // Find the positive integers 
    // on the left 
    for ($i = 0; $i < $n - 1; $i++)
    { 
  
        // If current element is positive 
        if ($a[$i] >= 0) 
            $pos++; 
  
        // Update the answer 
        $ans = min($ans, $pos + $np[$i + 1]); 
    } 
  
    // Return the required answer 
    return $ans; 
} 
  
// Driver code 
$a = array( -1, 0, 1, 2 ); 
$n = count($a) ;
  
echo Minimum_Operations($a, $n); 
  
// This code is contributed by Ryuga
?>


输出:
1