📌  相关文章
📜  使数组乘积等于1的最小步骤

📅  最后修改于: 2021-04-22 09:26:59             🧑  作者: Mango

给定一个包含N个整数的数组arr [] 。第一步,可以将数组的任何元素增加或减少一个。任务是找到所需的最小步骤,以使数组元素的乘积变为1

例子:

方法:

  1. 当数组中只有1和-1s且-1s的计数为偶数时,数组元素的乘积只能等于1
  2. 现在,所有正数都可以减少为1,因为它们更接近1而不是接近-1
  3. 同样,所有负数都可以更新为-1
  4. 如果数组中存在0 s,则可以根据情况将其减少为1-1 (-1s的数量必须为偶数)。
  5. 如果-ve个数的偶数为偶数,则它们总是将产生-1
  6. 但是,如果有-ve个数的奇数,则它们将产生-1s的奇数。要解决此问题,有两种可能性:
    • 首先尝试在数组中找到计数0 ,因为它将1的运算取为-1
    • 如果数组中没有零,则只需在答案中加2 ,因为它将分两个步骤将1设为-1

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the minimum
// steps requried
int MinStep(int a[], int n)
{
  
    // To store the count of 0s, positive
    // and negative numbers
    int positive = 0,
        negative = 0,
        zero = 0;
  
    // To store the ans
    int step = 0;
  
    for (int i = 0; i < n; i++) {
  
        // If array element is
        // equal to 0
        if (a[i] == 0) {
            zero++;
        }
  
        // If array element is
        // a negative number
        else if (a[i] < 0) {
            negative++;
  
            // Extra cost needed
            // to make it -1
            step = step + (-1 - a[i]);
        }
  
        // If array element is
        // a positive number
        else {
            positive++;
  
            // Extra cost needed
            // to make it 1
            step = step + (a[i] - 1);
        }
    }
  
    // Now the array will
    // have -1, 0 and 1 only
    if (negative % 2 == 0) {
  
        // As count of negative is even
        // so we will change all 0 to 1
        // total cost here will be
        // count of 0s
        step = step + zero;
    }
    else {
  
        // If there are zeroes present
        // in the array
        if (zero > 0) {
  
            // Change one zero to -1
            // and rest of them to 1
            // Total cost here will
            // be count of '0'
            step = step + zero;
        }
  
        // If there are no zeros in the array
        else {
  
            // As no 0s are availabe so we
            // have to change one -1 to 1
            // which will cost 2 to
            // change -1 to 1
            step = step + 2;
        }
    }
  
    return step;
}
  
// Driver code
int main()
{
    int a[] = { 0, -2, -1, -3, 4 };
    int n = sizeof(a) / sizeof(a[0]);
  
    cout << MinStep(a, n);
  
    return 0;
}


Java
// Java implementation of the approach 
class GFG
{
      
    // Function to return the minimum 
    // steps requried 
    static int MinStep(int a[], int n) 
    { 
      
        // To store the count of 0s, positive 
        // and negative numbers 
        int positive = 0, 
            negative = 0, 
            zero = 0; 
      
        // To store the ans 
        int step = 0; 
      
        for (int i = 0; i < n; i++) 
        { 
      
            // If array element is 
            // equal to 0 
            if (a[i] == 0) 
            { 
                zero++; 
            } 
      
            // If array element is 
            // a negative number 
            else if (a[i] < 0) 
            { 
                negative++; 
      
                // Extra cost needed 
                // to make it -1 
                step = step + (-1 - a[i]); 
            } 
      
            // If array element is 
            // a positive number 
            else 
            { 
                positive++; 
      
                // Extra cost needed 
                // to make it 1 
                step = step + (a[i] - 1); 
            } 
        } 
      
        // Now the array will 
        // have -1, 0 and 1 only 
        if (negative % 2 == 0) 
        { 
      
            // As count of negative is even 
            // so we will change all 0 to 1 
            // total cost here will be 
            // count of 0s 
            step = step + zero; 
        } 
        else 
        { 
      
            // If there are zeroes present 
            // in the array 
            if (zero > 0)
            { 
      
                // Change one zero to -1 
                // and rest of them to 1 
                // Total cost here will 
                // be count of '0' 
                step = step + zero; 
            } 
      
            // If there are no zeros in the array 
            else 
            { 
      
                // As no 0s are availabe so we 
                // have to change one -1 to 1 
                // which will cost 2 to 
                // change -1 to 1 
                step = step + 2; 
            } 
        } 
      
        return step; 
    } 
      
    // Driver code 
    public static void main (String[] args) 
    { 
        int a[] = { 0, -2, -1, -3, 4 }; 
        int n = a.length; 
      
        System.out.println(MinStep(a, n)); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
  
# Function to return the minimum
# steps requried
def MinStep(a, n):
      
    # To store the count of 0s, positive
    # and negative numbers
    positive = 0;
    negative = 0;
    zero = 0;
  
    # To store the ans
    step = 0;
  
    for i in range(n):
          
        # If array element is
        # equal to 0
        if (a[i] == 0):
            zero += 1;
              
        # If array element is
        # a negative number
        elif (a[i] < 0):
  
            negative += 1;
  
            # Extra cost needed
            # to make it -1
            step = step + (-1 - a[i]);
  
        # If array element is
        # a positive number
        else:
            positive += 1;
  
            # Extra cost needed
            # to make it 1
            step = step + (a[i] - 1);
  
    # Now the array will
    # have -1, 0 and 1 only
    if (negative % 2 == 0):
  
        # As count of negative is even
        # so we will change all 0 to 1
        # total cost here will be
        # count of 0s
        step = step + zero;
  
    else:
  
        # If there are zeroes present
        # in the array
        if (zero > 0):
  
            # Change one zero to -1
            # and rest of them to 1
            # Total cost here will
            # be count of '0'
            step = step + zero;
  
        # If there are no zeros in the array
        else:
  
            # As no 0s are availabe so we
            # have to change one -1 to 1
            # which will cost 2 to
            # change -1 to 1
            step = step + 2;
    return step;
  
# Driver code
if __name__ == '__main__':
    a = [0, -2, -1, -3, 4];
    n = len(a);
  
    print(MinStep(a, n));
  
# This code is contributed by PrinciRaj1992


C#
// C# implementation of the approach 
using System;
  
class GFG
{
          
    // Function to return the minimum 
    // steps requried 
    static int MinStep(int []a, int n) 
    { 
      
        // To store the count of 0s, 
        // positive and negative numbers 
        int positive = 0, 
            negative = 0, 
            zero = 0; 
      
        // To store the ans 
        int step = 0; 
      
        for (int i = 0; i < n; i++) 
        { 
      
            // If array element is 
            // equal to 0 
            if (a[i] == 0) 
            { 
                zero++; 
            } 
      
            // If array element is 
            // a negative number 
            else if (a[i] < 0) 
            { 
                negative++; 
      
                // Extra cost needed 
                // to make it -1 
                step = step + (-1 - a[i]); 
            } 
      
            // If array element is 
            // a positive number 
            else
            { 
                positive++; 
      
                // Extra cost needed 
                // to make it 1 
                step = step + (a[i] - 1); 
            } 
        } 
      
        // Now the array will 
        // have -1, 0 and 1 only 
        if (negative % 2 == 0) 
        { 
      
            // As count of negative is even 
            // so we will change all 0 to 1 
            // total cost here will be 
            // count of 0s 
            step = step + zero; 
        } 
        else
        { 
      
            // If there are zeroes present 
            // in the array 
            if (zero > 0)
            { 
      
                // Change one zero to -1 
                // and rest of them to 1 
                // Total cost here will 
                // be count of '0' 
                step = step + zero; 
            } 
      
            // If there are no zeros in the array 
            else
            { 
      
                // As no 0s are availabe so we 
                // have to change one -1 to 1 
                // which will cost 2 to 
                // change -1 to 1 
                step = step + 2; 
            } 
        } 
      
        return step; 
    } 
      
    // Driver code 
    static public void Main ()
    {
        int []a = { 0, -2, -1, -3, 4 }; 
        int n = a.Length; 
      
        Console.Write(MinStep(a, n)); 
    } 
}
  
// This code is contributed by ajit.


输出:
7

时间复杂度: O(N)