0

Given a set of unique positive integers (size ranging upto 32), it is required to determine whether a required sum is possible or not.

A brute force approach is:

bool isPossible(long long int n, int s)
{
    if(n<0)
        return false;
    if(n == 0)
        return true;
    for(int i=s;i<32;++i)
        if(isPossible(n-allNumbers[i], i+1))
            return true;
    return false;
}

isPossible(n,0) returns the boolean value corresponding to whether the number n is possible to be expressed as sum of unique numbers from the set allNumbers[]. But the complexity of this solution is O(2^32). Is it possible to get a better run time for this problem?

yobro97
  • 1,125
  • 8
  • 23

0 Answers0