📌  相关文章
📜  最小化使给定数组之和等于0所需的翻转次数

📅  最后修改于: 2021-04-18 02:38:50             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是最小化需要乘以-1的元素的数量,以使数组元素的总和为0 。如果不可能使总和为0 ,则打印“ -1”

例子:

天真的方法:最简单的方法是以各种可能的方式将数组分为两个子集。对于每个除法,请检查它们的子集和的差是否为0 。如果发现为0 ,则结果是较小子集的长度。
时间复杂度: O(2 N )
辅助空间: O(N)

高效方法:为了优化上述方法,其思想是使用动态编程。请按照以下步骤解决问题:

  • 为了使所有数组元素的总和等于0 ,请将给定的数组元素划分为两个具有相等总和的子集。
  • 在给定数组的所有可能子集中,选择大小最小的子集。
  • 如果给定数组的总和为奇数,则没有子集可以使总和为0 ,因此返回-1
  • 否则,尝试数组的所有可能的子集和,并检查子集的和是否等于sum / 2总和是数组所有元素的总和。
  • dp []的递归关系为:
  • 使用以上重复出现,打印dp(0,sum / 2)作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Initialze dp[][]
int dp[2001][2001];
 
// Function to find the nimimum number
// of operations to make sum of A[] 0
int solve(vector& A, int i,
          int sum, int N)
{
    // Initialize answer
    int res = 2001;
 
    // Base case
    if (sum < 0 or (i == N and sum != 0)) {
        return 2001;
    }
 
    // Otherwise, return 0
    if (sum == 0 or i >= N) {
        return dp[i][sum] = 0;
    }
 
    // Pre-computed subproblem
    if (dp[i][sum] != -1) {
        return dp[i][sum];
    }
 
    // Recurrence relation for finding
    // the minimum of the sum of subsets
    res = min(solve(A, i + 1, sum - A[i], N) + 1,
              solve(A, i + 1, sum, N));
 
    // Return the result
    return dp[i][sum] = res;
}
 
// Function to find the minimum number
// of elements required to be flipped
// to amke sum the array equal to 0
void minOp(vector& A, int N)
{
    int sum = 0;
 
    // Find the sum of array
    for (auto it : A) {
        sum += it;
    }
 
    if (sum % 2 == 0) {
 
        // Initialise dp[][]  with -1
        memset(dp, -1, sizeof(dp));
 
        int ans = solve(A, 0, sum / 2, N);
 
        // No solution exists
        if (ans < 0 || ans > N) {
            cout << "-1" << endl;
        }
 
        // Otherwise
        else {
            cout << ans << endl;
        }
    }
 
    // If sum is odd, no
    // subset is possible
    else {
        cout << "-1" << endl;
    }
}
 
// Driver Code
int main()
{
    vector A = { 2, 3, 1, 4 };
    int N = A.size();
 
    // Function Call
    minOp(A, N);
 
    return 0;
}


Java
// Java program for the above approach
class GFG
{
 
// Initialze dp[][]
static int [][]dp = new int[2001][2001];
 
// Function to find the nimimum number
// of operations to make sum of A[] 0
static int solve(int []A, int i,
          int sum, int N)
{
   
    // Initialize answer
    int res = 2001;
 
    // Base case
    if (sum < 0 || (i == N && sum != 0))
    {
        return 2001;
    }
 
    // Otherwise, return 0
    if (sum == 0 || i >= N)
    {
        return dp[i][sum] = 0;
    }
 
    // Pre-computed subproblem
    if (dp[i][sum] != -1)
    {
        return dp[i][sum];
    }
 
    // Recurrence relation for finding
    // the minimum of the sum of subsets
    res = Math.min(solve(A, i + 1, sum - A[i], N) + 1,
              solve(A, i + 1, sum, N));
 
    // Return the result
    return dp[i][sum] = res;
}
 
// Function to find the minimum number
// of elements required to be flipped
// to amke sum the array equal to 0
static void minOp(int []A, int N)
{
    int sum = 0;
 
    // Find the sum of array
    for (int it : A)
    {
        sum += it;
    }
 
    if (sum % 2 == 0)
    {
 
        // Initialise dp[][]  with -1
        for(int i = 0; i < 2001; i++)
        {
            for (int j = 0; j < 2001; j++)
            {
                dp[i][j] = -1;
            }
        }
 
        int ans = solve(A, 0, sum / 2, N);
 
        // No solution exists
        if (ans < 0 || ans > N)
        {
            System.out.print("-1" +"\n");
        }
 
        // Otherwise
        else
        {
            System.out.print(ans +"\n");
        }
    }
 
    // If sum is odd, no
    // subset is possible
    else
    {
        System.out.print("-1" +"\n");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int []A = { 2, 3, 1, 4 };
    int N = A.length;
 
    // Function Call
    minOp(A, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program for the above approach
 
# Initialze dp[][]
dp = [[-1 for i in range(2001)] for j in range(2001)]
 
# Function to find the nimimum number
# of operations to make sum of A[] 0
def solve(A, i, sum, N):
   
    # Initialize answer
    res = 2001
 
    # Base case
    if (sum < 0 or (i == N and sum != 0)):
        return 2001
 
    # Otherwise, return 0
    if (sum == 0 or i >= N):
        dp[i][sum] = 0
        return 0
 
    # Pre-computed subproblem
    if (dp[i][sum] != -1):
        return dp[i][sum]
 
    # Recurrence relation for finding
    # the minimum of the sum of subsets
    res = min(solve(A, i + 1, sum - A[i], N) + 1,
              solve(A, i + 1, sum, N))
 
    # Return the result
    dp[i][sum] = res
    return res
 
# Function to find the minimum number
# of elements required to be flipped
# to amke sum the array equal to 0
def minOp(A, N):
    sum = 0
 
    # Find the sum of array
    for it in A:
        sum += it   
    if (sum % 2 == 0):
       
        # Initialise dp[][]  with -1
        dp = [[-1 for i in range(2001)] for j in range(2001)]
        ans = solve(A, 0, sum // 2, N)
 
        # No solution exists
        if (ans < 0 or ans > N):
            print("-1")
         
        # Otherwise
        else:
            print(ans)
 
    # If sum is odd, no
    # subset is possible
    else:
        print(-1)
 
# Driver Code
A = [ 2, 3, 1, 4 ]
N = len(A)
 
# Function Call
minOp(A, N)
 
# This code is contributed by rohitsingh07052.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
 
// Initialze [,]dp
static int [,]dp = new int[2001,2001];
 
// Function to find the nimimum number
// of operations to make sum of []A 0
static int solve(int []A, int i,
          int sum, int N)
{
   
    // Initialize answer
    int res = 2001;
 
    // Base case
    if (sum < 0 || (i == N && sum != 0))
    {
        return 2001;
    }
 
    // Otherwise, return 0
    if (sum == 0 || i >= N)
    {
        return dp[i, sum] = 0;
    }
 
    // Pre-computed subproblem
    if (dp[i, sum] != -1)
    {
        return dp[i, sum];
    }
 
    // Recurrence relation for finding
    // the minimum of the sum of subsets
    res = Math.Min(solve(A, i + 1, sum - A[i], N) + 1,
              solve(A, i + 1, sum, N));
 
    // Return the result
    return dp[i, sum] = res;
}
 
// Function to find the minimum number
// of elements required to be flipped
// to amke sum the array equal to 0
static void minOp(int []A, int N)
{
    int sum = 0;
 
    // Find the sum of array
    foreach (int it in A)
    {
        sum += it;
    }
 
    if (sum % 2 == 0)
    {
 
        // Initialise [,]dp  with -1
        for(int i = 0; i < 2001; i++)
        {
            for (int j = 0; j < 2001; j++)
            {
                dp[i, j] = -1;
            }
        }
 
        int ans = solve(A, 0, sum / 2, N);
 
        // No solution exists
        if (ans < 0 || ans > N)
        {
            Console.Write("-1" +"\n");
        }
 
        // Otherwise
        else
        {
            Console.Write(ans +"\n");
        }
    }
 
    // If sum is odd, no
    // subset is possible
    else
    {
        Console.Write("-1" +"\n");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int []A = { 2, 3, 1, 4 };
    int N = A.Length;
 
    // Function Call
    minOp(A, N);
}
}
 
// This code is contributed by 29AjayKumar


输出:
2

时间复杂度: O(S * N),其中S是给定数组的总和。
辅助空间: O(S * N)