4

Given an array of integers, I'm trying to find the longest subset (powerset) with sum equal to k using the lease possible time complexity. e.g. if inputArr= [1, 2, 8, 1, 1, 7] and k= 10, then the output should be 4 since the longest subset with sum equals to 10 is [1, 1, 1, 7].

Edit: I might've forgotten an important detail; the elements of the array are all positive and non-zero.

I used this algorithm that I found on geeksforgeeks: https://www.geeksforgeeks.org/finding-all-subsets-of-a-given-set-in-java/

The code works fine, but the only problem that I have is with the execution time. I am supposed to submit this online, and when I submit it the execution terminates due to timeout.

    int maxSubLength=0;
    for (int i = 1; i < (1<<n); i++)   //n is the length of inputArr
    {
        int sum=0, length=0;

        for (int j = 0; j < n; j++)
          if ((i & (1 << j)) > 0)
          {
                sum+=inputArr[j];
                length++;
                if (sum>k)
                   break;
          }  

        if (sum==k)
            maxSubLength=Math.max(maxSubLength, length);
    }

Is there any faster algorithm? I tried a recursive one and it didn't help.

Jim
  • 133
  • 1
  • 8
  • 1
    There is no fast way to generate all subsets of a large set, because a set of size *n* has 2ⁿ supersets. Could you post the complete text of the problem you're trying to solve, including any constraints on the size of the set and/or its values? – ruakh Oct 15 '19 at 00:32
  • @ruakh I might've forgotten an important detail; the elements of the array are all positive and non-zero. Here's the full text of the problem: "Given an array of positive n non-zero integers, find the length of the longest subset of integers with sum equal to k." and 1<=n<=1000, 1<=k<=2000 .... For large value of n, the above code would cause an overflow problem. But I took care of that using BigInteger, I just didn't want to over complicate this question. – Jim Oct 15 '19 at 00:41
  • In other words what time complexity in big O notation do you need to get to? – Josh W. Oct 15 '19 at 00:41
  • @JoshW. I'm really not sure. The problem doesn't specify that, and I have no clue what's the best time complexity that this problem can be solved in. – Jim Oct 15 '19 at 00:49
  • Seems this is subset sum with the extra requirement to find the largest possible subset that sums to a given. It can be solved by slight modification in [subset sum](https://en.wikipedia.org/wiki/Subset_sum_problem) algorithm and its runtime is certainly better than calculating all subsets. – jrook Oct 15 '19 at 00:58

1 Answers1

4

We can solve this with dynamic programming in O(n*k) time and O(k) space. JavaScript code:

function f(A, K){
  let m = new Array(K + 1).fill(0)
    
  for (let a of A){
    for (let k=K; k>=a; k--)
      if (m[k - a])
        m[k] = Math.max(m[k], 1 + m[k - a])

    m[a] = Math.max(m[a], 1)
  }
  
  return m[K]
}

var A = [1, 2, 8, 1, 1, 7]
var K = 10

console.log(f(A, K))
גלעד ברקן
  • 23,602
  • 3
  • 25
  • 61