2

Given a list of numbers, lst = [1, 1, 2, 4, 3, 2, 1, 1, 1, 2, 1, 4, 3, 1] how would I find an ideal number of lists which are less than or equal to 4?

There are many possibilities here. The goal is to minimize the number of possible lists. The program would need to do create subset lists like the following: {4}, {4}, {3, 1}, ... , {1, 1}.

Notice how the last list subset does not equal to four, but it less. This problem is difficult for the following reasons:

  • The program must be able to find subset-sums which are less than or equal to a sum
  • The program will needs to begin by removing values out of the original list into subset lists by first looking at the largest values
GeneralCode
  • 794
  • 1
  • 11
  • 26
  • What have you tried so far? – MoonMist Dec 13 '19 at 04:22
  • For now I have just thought it up. My education is Engineering, so I was hoping to come across a CS on here who knows a bit more about graph theory, or something. – GeneralCode Dec 13 '19 at 04:25
  • The subset contains two elements at max? Is `{1,1,1}` one of the possible outcomes? – Wander3r Dec 13 '19 at 04:33
  • `{1, 1, 1}` is a possible outcome, yes. I did not know a subset contains a maximum of two elements. – GeneralCode Dec 13 '19 at 04:41
  • @GeneralCode Didn't mean to take a dig at you. My algorithm doesn't have {1,1} in the solutions, hence asked. Could you post some sample input and output if you have to validate the algo? – Wander3r Dec 13 '19 at 05:07
  • @Wander3r, I have thought about a solution. I will code it tomorrow, it is recursion. The solution: reorder the list from largest to smallest, remove, go through remainder of list recursively to find values that sum to less than equal to N. If you think up a solution which is not recursion, I'd like to hear it. – GeneralCode Dec 13 '19 at 05:13

1 Answers1

1

Here's my attempt. The general idea is to sort the list, and go from left and right and picking the biggest subset every iteration greedily. The time complexity is O(n).

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    std::vector<int> lst{1, 1, 2, 4, 3, 2, 1, 1, 1, 2, 1, 4, 3, 1};
    std::sort(lst.begin(),lst.end()); //sort the list
    int target = 4;

    int left = 0;
    int right = lst.size()-1;

    std::vector<std::vector<int>> solutions;
    while (left<right ){
        if(lst[left] > target)   // break if no solutions
            break;

        if(lst[right] > target) // ignore larger right values
            right--;


        if(lst[right]<=target){   // while the total sum is less than target, keep adding the elements
            std::vector<int> subset;
            subset.push_back(lst[right]);
            int sum = lst[right];
            while(left<right && lst[left]+sum<=target){
                sum+=lst[left];
                subset.push_back({lst[left]});
                left++;
            }
            solutions.push_back(subset);
            right--;
        }

    }

    for(auto& ss : solutions){
        std::cout<<'{';
        for(auto n:ss){
            std::cout<<n<<',';
        }
        std::cout<<"\b}";
        std::cout<<std::endl;
    }

}
Wander3r
  • 1,801
  • 17
  • 27