Open In App

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

Last Updated : 17 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 <bits/stdc++.h>
using namespace std;
 
// Function to calculate
// minimum knapsacks required to
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
    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 Code
int main()
{
    int W = 300;
    int arr[] = { 130, 140, 150, 160 };
 
    // To store the size of arr[]
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Print the answer
    cout << minimumKnapsacks(arr, N, W);
}


Java




// Java program for the above approach
import java.util.*;
public class GFG
{
 
// Function to calculate
// minimum knapsacks required to
static 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 code
public 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 to
def 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 answer
print(minimumKnapsacks(arr, N, W))
 
# This code is contributed by saurabh_jaiswal.


C#




// C# program for the above approach
using System;
public class GFG
{
 
// Function to calculate
// minimum knapsacks required to
static 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 code
public 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




<script>
    // JavaScript implementation for the above approach
 
    // Function to calculate
    // minimum knapsacks required to
    const minimumKnapsacks = (A, N, W) => {
     
        // Variable to store
        // the knapsacks required
        let ans = 0;
 
        // Maintain two pointers L and R
        let 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++;
 
                // 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 Code
    let W = 300;
    let arr = [130, 140, 150, 160];
 
    // To store the size of arr[]
    let N = arr.length;
 
    // Print the answer
    document.write(minimumKnapsacks(arr, N, W));
 
    // This code is contributed by rakeshsahni
 
</script>


Output

2

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads