给定 Array 的一组所有可能的子集和值包含数字 [0, K] 的 K 的最大值
给定一个包含N个整数的数组arr[] ,任务是找到集合S中存在的K的最大计数,即从 0 到 K 的连续整数,其中S包含所有可能的子集和值数组arr[] 。
例子:
Input: arr[] = {1, 3}
Output: 2
Explanation: The possible subsets are {}, {1}, {3}, {1, 3} and there respective sums are {0, 1, 3, 4}. Therefore the maximum count of consecutive integers starting from 0 in the set containing all the subset sums is 2 (i.e, {0, 1}).
Input: arr[] = {1, 1, 1, 4}
Output: 8
Explanation: The set containing all the subset sums of the given array is {0, 1, 2, 3, 4, 5, 6, 7}. Therefore the maximum count of consecutive integers starting from 0 is 8.
朴素的方法:给定的问题可以使用动态编程来解决,方法是将所有可能的子集和保存在一个数组中,并使用背包技术完成。此后,计算连续整数的最大计数。
时间复杂度: O(N*K) 其中 K 表示数组arr[]中所有元素的总和。
辅助空间: O(K)
有效方法:上述问题可以使用贪心方法来解决。假设包含数组arr[]的所有子集和的集合包含[0, X]范围内的所有整数。如果在数组中引入一个新数Y ,则[Y, X + Y]范围内的所有整数也可以作为子集和。使用此观察,可以使用以下步骤解决给定问题:
- 以非递减顺序对给定数组进行排序。
- 维护一个变量,比如X为0 ,它表示[0, X]范围内的整数可以作为给定数组arr[]的子集和。
- 为了保持连续整数的连续性, arr[i] <= X + 1必须成立。 因此,遍历[0, N)范围内每个i的给定数组,如果arr[i] <= X + 1的值,则更新X = X + arr[i]的值。否则,跳出循环。
- 完成上述步骤后,计算[0, X]范围内的整数个数,即(X + 1) 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find maximum count of
// consecutive integers from 0 in set
// S of all possible subset-sum
int maxConsecutiveCnt(vector arr)
{
// Stores the maximum possible integer
int X = 0;
// Sort the given array in
// non-decreasing order
sort(arr.begin(), arr.end());
// Iterate the given array
for (int i = 0; i < arr.size(); i++) {
// If arr[i] <= X+1, then update
// X otherwise break the loop
if (arr[i] <= (X + 1)) {
X = X + arr[i];
}
else {
break;
}
}
// Return Answer
return X + 1;
}
// Driver Code
int main()
{
vector arr = { 1, 1, 1, 4 };
cout << maxConsecutiveCnt(arr);
return 0;
}
Java
// Java program for the above approach
import java.util.Arrays;
class GFG {
// Function to find maximum count of
// consecutive integers from 0 in set
// S of all possible subset-sum
public static int maxConsecutiveCnt(int[] arr)
{
// Stores the maximum possible integer
int X = 0;
// Sort the given array in
// non-decreasing order
Arrays.sort(arr);
// Iterate the given array
for (int i = 0; i < arr.length; i++) {
// If arr[i] <= X+1, then update
// X otherwise break the loop
if (arr[i] <= (X + 1)) {
X = X + arr[i];
} else {
break;
}
}
// Return Answer
return X + 1;
}
// Driver Code
public static void main(String args[]) {
int[] arr = { 1, 1, 1, 4 };
System.out.println(maxConsecutiveCnt(arr));
}
}
// This code is contributed by gfgking.
Python3
# python program for the above approach
# Function to find maximum count of
# consecutive integers from 0 in set
# S of all possible subset-sum
def maxConsecutiveCnt(arr):
# Stores the maximum possible integer
X = 0
# Sort the given array in
# non-decreasing order
arr.sort()
# Iterate the given array
for i in range(0, len(arr)):
# If arr[i] <= X+1, then update
# X otherwise break the loop
if (arr[i] <= (X + 1)):
X = X + arr[i]
else:
break
# Return Answer
return X + 1
# Driver Code
if __name__ == "__main__":
arr = [1, 1, 1, 4]
print(maxConsecutiveCnt(arr))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find maximum count of
// consecutive integers from 0 in set
// S of all possible subset-sum
public static int maxConsecutiveCnt(int[] arr)
{
// Stores the maximum possible integer
int X = 0;
// Sort the given array in
// non-decreasing order
Array.Sort(arr);
// Iterate the given array
for (int i = 0; i < arr.Length; i++)
{
// If arr[i] <= X+1, then update
// X otherwise break the loop
if (arr[i] <= (X + 1))
{
X = X + arr[i];
}
else
{
break;
}
}
// Return Answer
return X + 1;
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 1, 1, 4 };
Console.Write(maxConsecutiveCnt(arr));
}
}
// This code is contributed by gfgking.
Javascript
// JavaScript Program to implement
// the above approach
// Function to find maximum count of
// consecutive integers from 0 in set
// S of all possible subset-sum
function maxConsecutiveCnt(arr)
{
// Stores the maximum possible integer
let X = 0;
// Sort the given array in
// non-decreasing order
arr.sort(function (a, b) { return a - b })
// Iterate the given array
for (let i = 0; i < arr.length; i++) {
// If arr[i] <= X+1, then update
// X otherwise break the loop
if (arr[i] <= (X + 1)) {
X = X + arr[i];
}
else {
break;
}
}
// Return Answer
return X + 1;
}
// Driver Code
let arr = [1, 1, 1, 4];
document.write(maxConsecutiveCnt(arr));
// This code is contributed by Potta Lokesh
8
时间复杂度: O(N*log N)
辅助空间: O(1)