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++
#include <bits/stdc++.h>
using namespace std;
int minimumKnapsacks( int A[], int N, int W)
{
int ans = 0;
int L = 0, R = N - 1;
sort(A, A + N);
while (L <= R) {
if (A[L] + A[R] <= W) {
ans++;
R--;
L++;
}
else {
R--;
ans++;
}
}
return ans;
}
int main()
{
int W = 300;
int arr[] = { 130, 140, 150, 160 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << minimumKnapsacks(arr, N, W);
}
|
Java
import java.util.*;
public class GFG
{
static int minimumKnapsacks( int A[], int N, int W)
{
int ans = 0 ;
int L = 0 , R = N - 1 ;
Arrays.sort(A);
while (L <= R) {
if (A[L] + A[R] <= W) {
ans++;
R--;
L++;
}
else {
R--;
ans++;
}
}
return ans;
}
public static void main (String args[])
{
int W = 300 ;
int arr[] = { 130 , 140 , 150 , 160 };
int N = arr.length;
System.out.println(minimumKnapsacks(arr, N, W));
}
}
|
Python3
def minimumKnapsacks(A, N, W):
ans = 0 ;
L = 0
R = N - 1 ;
A.sort();
while (L < = R):
if (A[L] + A[R] < = W):
ans + = 1
R - = 1
L + = 1
else :
R - = 1
ans + = 1
return ans;
W = 300 ;
arr = [ 130 , 140 , 150 , 160 ]
N = len (arr);
print (minimumKnapsacks(arr, N, W))
|
C#
using System;
public class GFG
{
static int minimumKnapsacks( int []A, int N, int W)
{
int ans = 0;
int L = 0, R = N - 1;
Array.Sort(A);
while (L <= R) {
if (A[L] + A[R] <= W) {
ans++;
R--;
L++;
}
else {
R--;
ans++;
}
}
return ans;
}
public static void Main ()
{
int W = 300;
int []arr = { 130, 140, 150, 160 };
int N = arr.Length;
Console.Write(minimumKnapsacks(arr, N, W));
}
}
|
Javascript
<script>
const minimumKnapsacks = (A, N, W) => {
let ans = 0;
let L = 0, R = N - 1;
A.sort();
while (L <= R) {
if (A[L] + A[R] <= W) {
ans++;
R--;
L++;
}
else
{
R--;
ans++;
}
}
return ans;
}
let W = 300;
let arr = [130, 140, 150, 160];
let N = arr.length;
document.write(minimumKnapsacks(arr, N, W));
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...