📌  相关文章
📜  删除Array的K个元素后最小化子集和差

📅  最后修改于: 2022-05-13 01:56:05.034000             🧑  作者: Mango

删除Array的K个元素后最小化子集和差

给定一个包含N个整数和一个整数K的数组arr[] ,任务是在从给定数组中删除K个元素后最小化子集和差异。

注意:子集必须是非空的。

例子:

方法:可以基于以下思想使用递归来解决问题:

请按照提到的步骤解决问题:

  • 计算所有数组元素的总和。
  • 对每个数组元素递归迭代:
    • 如果K元素尚未删除,则删除它并继续下一个索引。
    • 将其视为一个子集的一部分。
    • 如果遍历整个数组,请检查两个子集和之间的差异。
  • 最小的差异是所需的答案。

以下是上述方法的实现:

C++
// C++ code to implement the above approach
#include 
using namespace std;
 
int mini = INT_MAX;
 
// Function to form all possible subsets
void subset(vector arr, int index, int sum, int total)
{
    if (index >= arr.size()) {
        if (sum != 0)
            mini = min(mini, sum - (total - sum));
        return;
    }
    subset(arr, index + 1, sum + arr[index], total);
    subset(arr, index + 1, sum, total);
}
 
// Function to get the minimum difference
static void print(int index, vector& arr, int total,
                  vector& ans)
{
    if (total == 0) {
        int sum = 0;
        for (int elements : ans) {
            sum += elements;
        }
 
        // Function call to form subset
        subset(ans, 0, 0, sum);
        return;
    }
    if (index >= arr.size()) {
        return;
    }
    ans.push_back(arr[index]);
    print(index + 1, arr, total - 1, ans);
    ans.pop_back();
    print(index + 1, arr, total, ans);
}
 
// Utility function to solve the problem
void solve(vector& arr, int N)
{
    int selected = arr.size() - N;
 
    if (selected <= 1) {
        cout << -1;
        return;
    }
    vector ans;
    print(0, arr, selected, ans);
}
 
// Driver code
int main()
{
    vector arr = { 7, 9, 5, 8, 1, 3 };
    int N = 2;
    solve(arr, N);
    cout << (mini);
    return 0;
}
 
// This code is contributed by rakeshsahni


Java
// Java code to implement the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    static int min = Integer.MAX_VALUE;
 
    // Function to form all possible subsets
    static void subset(List arr,
                       int index,
                       int sum, int total)
    {
        if (index >= arr.size()) {
            if (sum != 0)
                min = Math.min(min,
                               sum - (total - sum));
            return;
        }
        subset(arr, index + 1,
               sum + arr.get(index), total);
        subset(arr, index + 1, sum, total);
    }
 
    // Function to get the minimum difference
    static void print(int index, int arr[],
                      int total,
                      List ans)
    {
        if (total == 0) {
            int sum = 0;
            for (int elements : ans) {
                sum += elements;
            }
 
            // Function call to form subset
            subset(ans, 0, 0, sum);
            return;
        }
        if (index >= arr.length) {
            return;
        }
        ans.add(arr[index]);
        print(index + 1, arr, total - 1, ans);
        ans.remove(ans.size() - 1);
        print(index + 1, arr, total, ans);
    }
 
    // Utility function to solve the problem
    public static void solve(int arr[], int N)
    {
        int selected = arr.length - N;
 
        if (selected <= 1) {
            System.out.println(-1);
            return;
        }
        List ans = new ArrayList<>();
        print(0, arr, selected, ans);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 7, 9, 5, 8, 1, 3 };
        int N = 2;
 
        solve(arr, N);
        System.out.println(min);
    }
}


Python3
# Python code to implement the above approach
import sys
 
# Function to form all possible subsets
def subset(arr,index,sum,total):
    global mini
 
    if (index >= len(arr)):
        if (sum != 0):
            mini = min(mini, sum - (total - sum))
        return
 
    subset(arr, index + 1, sum + arr[index], total)
    subset(arr, index + 1, sum, total)
 
# Function to get the minimum difference
def Print(index,arr,total,ans):
 
    if (total == 0):
        sum = 0
        for elements in ans:
            sum += elements
 
        # Function call to form subset
        subset(ans, 0, 0, sum)
        return
 
    if (index >= len(arr)):
        return
 
    ans.append(arr[index])
    Print(index + 1, arr, total - 1, ans)
    ans.pop()
    Print(index + 1, arr, total, ans)
 
# Utility function to solve the problem
def solve(arr,N):
 
    selected = len(arr) - N
 
    if (selected <= 1):
        print(-1)
        return
 
    ans = []
    Print(0, arr, selected, ans)
 
# Driver code
 
if __name__=="__main__":
 
    mini = sys.maxsize
    arr = [ 7, 9, 5, 8, 1, 3 ]
    N = 2
    solve(arr, N)
    print(mini)
     
# This code is contributed by shinjanpatra


Javascript



输出
-23

时间复杂度: O(2 N )
辅助空间: O(N)