0

Suppose you are given a number k and an array of objects having some weight. Now your task is to find the minimum number of objects that you can put in two bags such that each bag weigh at least k.
You can only take the objects as whole no breaking is allowed. Also, if an object is put in one bag it cannot be put into the other bag.

This problem seems simple to me. I have done similar problems when you need to fill just one bag. The idea I use is that you visit each object ask yourself what if I put it in the bag and what if I don't? You do this recursively until your desired weight is reached or you have no more objects. Take minimum when calling your recursive function.

However, I am not able to understand how to keep track of all the objects used up in bag 1 so that I don't include in bag 2.

Few Test cases

  1. Desired weight (k) = 4
    Number of objects (N) = 1
    [10]
    Output: -1 (Not possible)
  2. Desired weight (k) = 2
    Number of objects (N) = 3
    [2,2,2]
    Output: 2
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Dhairya Tripathi
  • 197
  • 2
  • 14
  • What's asked here is really testing basic knowledge and understanding of computer science and algorithms. If you don't know the answer, a bare code dump that implements this will not really help you to understand anything, or learn anything. Instead, the correct answer here should be to go and learn the relevant areas of computer science and algorithms which are needed to implement this. Unfortunately, stackoverflow.com is not a replacement for a [good C++ and computer science algorithms textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Jan 09 '21 at 14:51
  • I am not asking any code dumps or copy-paste solution. The thing confusing me is including objects in one and excluding them in other. Giving a simple direction would be helpful and appreciated. Also, I think this is not as simple as it looks at first. But what do I know?@SamVarshavchik – Dhairya Tripathi Jan 09 '21 at 14:57
  • What is the exact nature of your confusion? Again, this is nothing more than an algorithm. If you're not sure "how to keep track of all the objects", or "including objects in one and excluding them in other", then textbooks and other resources that explain various algorithms is what you're looking for. The C++ library itself offers many different containers whose sole reason for existence is "to keep track of the objects" they contain. Unfortunately, Stackoverflow is not a C++ tutorial site or a replacement for a textbook, but for ***specific*** questions. What is your ***specific*** question? – Sam Varshavchik Jan 09 '21 at 15:06
  • I solved the problem will add my solution soon. Don't know whats the exact name of the algorithm. But I think it's simple logic.@SamVarshavchik – Dhairya Tripathi Jan 10 '21 at 15:51

3 Answers3

1

I will focus on what you point out as your actual core problem, how to keep track of objects you used in one bag, the other bag or not at all.

Make a list (array, vector, ... whatever container you prefer) and note for each of the objects where you used it - or not.

index value meaning
0 0 not used
1 0 not used
2 0 not used
3 1 used in one bag
4 2 used in other bag

From your question it is not clear to me whether all objects have the same weight or different weights given in the input. If the weights are different, then you most likely already have a container for keeping track of the weight of each object. Modifying that container or using a second, very similar one will help you to also store the "used where" information.

I am intentionally not going into detail, because of
How do I ask and answer homework questions?

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • Weight of objects could be same also. Eg. if you have [7, 8, 19, 7, 8, 7, 10, 20] as weights of the objects and k=38, then the minimum number of objects required would be 7 – Dhairya Tripathi Jan 09 '21 at 19:02
  • That is a special case of "not all identical weight" which I do not see a problem with. Is it an obstacle for you? – Yunnosch Jan 10 '21 at 14:32
  • I don't see any problem with that. Actually, I solved this problem with an entirely different approach without using any dynamic programming will update my solution soon. Thanks. – Dhairya Tripathi Jan 10 '21 at 15:49
  • Good, make it an answer here and you can earn some reputation. – Yunnosch Jan 10 '21 at 15:52
0

I don't know if this answers your question or not, but still... You can do one thing: Initially make two empty arrays, say Bag_1 and Bag_2. As you recurse through all elements one by one, pop that element out of the array and append it to Bag_1 or Bag_2 whichever gives you the optimal solution. If the process is to be done multiple times, then creating a copy of the original array might help, if the length of the array is reasonable.

Rishi Garg
  • 21
  • 3
0

Here is the pseudo code for the program without dynamic programing.

 sort(a, a+n); // Sort the array of objects having weight
            int sum = a[n-1], count = -1; //Initialise sum and count
            unordered_set<int>log; // Create an unordered set to store logs (Unordered set will not add repetitive values in the log thus decreasing time complexity)
            log.insert(a[n-1]); // insert last element int log initially
            for(int i = n-2; i >=0; i--) {
                sum += a[i]; //increment the sum
                unordered_set<int>temp; //Create a temporary log that will be mapped to main log at the end.
                temp.insert(a[i]); //insert the sum to temp log
                for (auto it = log.begin(); it != log.end(); ++it) { //loop over all logs seen till now
                    temp.insert(*it + a[i]); // Add current sum to each of them and insert it to temp log thus creating all possible combinations.
                    if((a[i] + *it >= k) && (sum - a[i] - *it >= k)) { //Condition to check if bags have been filled with at least k weight.
                        count = n-i; // update the current count. This will be the ans.
                        break;
                    }
                    if(a[i] >= k && sum - a[i] >= k) {
                        count = n-i;
                        break;
                    }
                }
    
                if(count != -1) { //Condition to check if it's not possible to make such a combination.
                    break;
                }
                log.insert(temp.begin(), temp.end()); // add all temp to main log.
            }
            cout << count << endl; //print ans.
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dhairya Tripathi
  • 197
  • 2
  • 14