Problem statement
In this problem, we are given a weight 'W,' and an array consisting of some weights, and the value of all the weights in the Array is more than 'W/3' the final goal is to minimize the total number of knapsacks required to store the elements of the Array.
Example 1
Input:
Let the input array be like weights = [ 80,120,100,150 ] and W = 200
Output:
3
Explanation:
[150], [80,100], [120], These 3 Knapsacks will be used to store the entire Array of weights.
As for the first knapsack total weight is 150 <= 200
For the second knapsack total weight is 180 <= 200
For the third knapsack total weight is 120 <= 200
Example 2
Input:
Let the input array be like weights = [ 150,180, 200,150] and W = 400
Output:
2
Explanation:
[150,200],[150,180], These 2 Knapsacks will be used to store the entire Array of weights
Approach
To minimize the number of Knapsacks to store the entire Array, we can use the concept of two pointers. We can sort the Array and can iterate through the Array from both the ends and try to include value with maximum value and value with minimum weight in a single knapsack. This will solve the problem for us in a greedy approach.