Minimize number of Knapsacks with total weigh W required to store Array containing elements greater than W/3

• Last Updated : 17 Jan, 2022

Given an array, arr[] and weight W. The task is to minimize the number of Knapsacks required to store all elements of the array. A single knapsack can store a maximum total weight of W
NOTE: Each integer of the array is greater than (W/3).

Examples:

Input: arr[] = {150, 150, 150, 150, 150},  W = 300
Output: 3
Explanation: Minimum of 3 Knapsacks are required to store all elements
Knapsack 1 – {150, 150}, Knapsack 2 – {150, 150}, Knapsack 3 – {150}. The weight of each knapsack is <= W.

Input: arr[] = {130, 140, 150, 160}, W = 300
Output: 2
Explanation: The knapsacks can be filled as {130, 150}, {140, 160}.

Approach: This problem can be solved by using the Two-Pointer approach and Sorting. Follow the steps below to solve the given problem.

• Sort the array in non-decreasing order.
• As the array contains elements with values greater than W/3 so no knapsack can contain more than two elements.
• Maintain two pointers L and R. Initially L = 0, R = N-1.
• Maintain a while loop for values L <= R.
• For each L and R check the value arr[L] + A[R] <= W. If this is true then it is possible to put these blocks in the same knapsack. Increment L by 1 and decrease R by 1.
• Otherwise, the knapsack will have a single element of value arr[i]. Decrease R by 1.
• Increment answer for each valid step.

Below is the implementation of the above approach:

C++

 // C++ implementation for the above approach#include using namespace std; // Function to calculate// minimum knapsacks required toint minimumKnapsacks(int A[], int N, int W){    // Variable to store    // the knapsacks required    int ans = 0;     // Maintain two pointers L and R    int L = 0, R = N - 1;     // Sort the array in    // non-decreasing order    sort(A, A + N);     // Maintain a while loop    while (L <= R) {         // Check if there two elements        // can be stored in a        // single knapsack        if (A[L] + A[R] <= W) {             // Increment the answer            ans++;             // Decrease the right pointer            R--;             // Increase the left pointer            L++;        }        else {            // A single knapsack will be required            // to store the Right element            R--;            ans++;        }    }    return ans;} // Driver Codeint main(){    int W = 300;    int arr[] = { 130, 140, 150, 160 };     // To store the size of arr[]    int N = sizeof(arr) / sizeof(arr);     // Print the answer    cout << minimumKnapsacks(arr, N, W);}

Java

 // Java program for the above approachimport java.util.*;public class GFG{ // Function to calculate// minimum knapsacks required tostatic int minimumKnapsacks(int A[], int N, int W){       // Variable to store    // the knapsacks required    int ans = 0;     // Maintain two pointers L and R    int L = 0, R = N - 1;     // Sort the array in    // non-decreasing order    Arrays.sort(A);     // Maintain a while loop    while (L <= R) {         // Check if there two elements        // can be stored in a        // single knapsack        if (A[L] + A[R] <= W) {             // Increment the answer            ans++;             // Decrease the right pointer            R--;             // Increase the left pointer            L++;        }        else {            // A single knapsack will be required            // to store the Right element            R--;            ans++;        }    }    return ans;} // Driver codepublic static void main (String args[]){       int W = 300;    int arr[] = { 130, 140, 150, 160 };     // To store the size of arr[]    int N = arr.length;     // Print the answer    System.out.println(minimumKnapsacks(arr, N, W));}} // This code is contributed by Samim Hossain Mondal.

Python3

 # Python implementation for the above approach # Function to calculate# minimum knapsacks required todef minimumKnapsacks(A, N, W):    # Variable to store    # the knapsacks required    ans = 0;     # Maintain two pointers L and R    L = 0    R = N - 1;     # Sort the array in    # non-decreasing order    A.sort();     # Maintain a while loop    while (L <= R):         # Check if there two elements        # can be stored in a        # single knapsack        if (A[L] + A[R] <= W):             # Increment the answer            ans += 1             # Decrease the right pointer            R -= 1             # Increase the left pointer            L += 1        else:            # A single knapsack will be required            # to store the Right element            R -= 1            ans += 1    return ans;  # Driver Code W = 300;arr = [ 130, 140, 150, 160 ] # To store the size of arr[]N = len(arr); # Print the answerprint(minimumKnapsacks(arr, N, W)) # This code is contributed by saurabh_jaiswal.

C#

 // C# program for the above approachusing System;public class GFG{ // Function to calculate// minimum knapsacks required tostatic int minimumKnapsacks(int []A, int N, int W){       // Variable to store    // the knapsacks required    int ans = 0;     // Maintain two pointers L and R    int L = 0, R = N - 1;     // Sort the array in    // non-decreasing order    Array.Sort(A);     // Maintain a while loop    while (L <= R) {         // Check if there two elements        // can be stored in a        // single knapsack        if (A[L] + A[R] <= W) {             // Increment the answer            ans++;             // Decrease the right pointer            R--;             // Increase the left pointer            L++;        }        else {            // A single knapsack will be required            // to store the Right element            R--;            ans++;        }    }    return ans;} // Driver codepublic static void Main (){       int W = 300;    int []arr = { 130, 140, 150, 160 };     // To store the size of arr[]    int N = arr.Length;     // Print the answer    Console.Write(minimumKnapsacks(arr, N, W));}} // This code is contributed by Samim Hossain Mondal.

Javascript


Output
2

Time Complexity: O(N*log(N))
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up