Minimum boxes required to carry all gifts
Given an array containing weights of gifts, and an integer K representing maximum weight a box can contain (All boxes are uniform). Each box carries at most 2 gifts at the same time, provided the sum of the weight of those gifts is at most limit of box. The task is to find the minimum number of boxes required to carry all gifts.
Note: It is guaranteed each gift can be carried by a box.
Examples:
Input: A = [3, 2, 2, 1], K = 3
Output: 3
Explanation: 3 boxes with weights (1, 2), (2) and (3)
Input: A = [3, 5, 3, 4], K = 5
Output: 4
Explanation: 4 boxes with weights (3), (3), (4), (5)
Approach: If the heaviest gift can share a box with the lightest gift, then do so. Otherwise, the heaviest gift can’t pair with anyone, so it get an individual box.
The reason this works is because if the lightest gift can pair with anyone, it might as well pair with the heaviest gift.
Let A[i] be the currently lightest gift, and A[j] to the heaviest.
Then, if the heaviest gift can share a box with the lightest gift (if A[j] + A[i] <= K) then do so otherwise, the heaviest gift get an individual box.
Below is the implementation of above approach:
C++
// CPP implementation of above approach #include <bits/stdc++.h> using namespace std; // Function to return number of boxes int numBoxes(int A[], int n, int K) { // Sort the boxes in ascending order sort(A, A + n); // Try to fit smallest box with // current heaviest box (from right // side) int i = 0, j = n - 1; int ans = 0; while (i <= j) { ans++; if (A[i] + A[j] <= K) i++; j--; } return ans; } // Driver program int main() { int A[] = { 3, 2, 2, 1 }, K = 3; int n = sizeof(A) / sizeof(A[0]); cout << numBoxes(A, n, K); return 0; } // This code is written by Sanjit_Prasad |
Java
// Java implementation of above approach import java.util.*; class solution { // Function to return number of boxes static int numBoxes(int A[], int n, int K) { // Sort the boxes in ascending order Arrays.sort(A); // Try to fit smallest box with // current heaviest box (from right // side) int i = 0, j = n - 1; int ans = 0; while (i <= j) { ans++; if (A[i] + A[j] <= K) i++; j--; } return ans; } // Driver program public static void main(String args[]) { int A[] = { 3, 2, 2, 1 }, K = 3; int n = A.length; System.out.println(numBoxes(A, n, K)); } } //THis code is contributed by // Surendra_Gangwar |
Python3
# Python3 implementation of # above approach # Function to return number of boxes def numBoxex(A,n,K): # Sort the boxes in ascending order A.sort() # Try to fit smallest box with current # heaviest box (from right side) i =0 j = n-1 ans=0 while i<=j: ans +=1 if A[i]+A[j] <=K: i+=1 j-=1 return ans # Driver code if __name__=='__main__': A = [3, 2, 2, 1] K= 3 n = len(A) print(numBoxex(A,n,K)) # This code is contributed by # Shrikant13 |
C#
// C# implementation of above approach using System; class GFG { // Function to return number of boxes static int numBoxes(int []A, int n, int K) { // Sort the boxes in ascending order Array.Sort(A); // Try to fit smallest box with // current heaviest box (from right // side) int i = 0, j = (n - 1); int ans = 0; while (i <= j) { ans++; if (A[i] + A[j] <= K) i++; j--; } return ans; } // Driver Code static public void Main () { int []A = { 3, 2, 2, 1 }; int K = 3; int n = A.Length; Console.WriteLine(numBoxes(A, n, K)); } } // This code is contributed by ajit |
PHP
<?php //PHP implementation of above approach // Function to return number of boxes function numBoxes($A, $n, $K) { // Sort the boxes in ascending order sort($A); // Try to fit smallest box with // current heaviest box (from right // side) $i = 0; $j = $n - 1; $ans = 0; while ($i <= $j) { $ans++; if ($A[$i] + $A[$j] <= $K) $i++; $j--; } return $ans; } // Driver program $A = array (3, 2, 2, 1 ); $K = 3; $n = sizeof($A) / sizeof($A[0]); echo numBoxes($A, $n, $K); // This code is written by ajit ?> |
Output:
3
Time Complexity: O(N*log(N)), where N is the length of array.
Recommended Posts:
- Minimum time required to transport all the boxes from source to the destination under the given constraints
- Minimum number of stacks possible using boxes of given capacities
- Color all boxes in line such that every M consecutive boxes are unique
- Minimum Swaps required to group all 1's together
- Minimum number of changes required to make the given array an AP
- Minimum number of operations required to reduce N to 1
- Minimum halls required for class scheduling
- Minimum operations required to remove an array
- Minimum operations required to change the array such that |arr[i] - M| <= 1
- Minimum changes required to make two arrays identical
- Minimum changes required such that the string satisfies the given condition
- Minimum swaps required to bring all elements less than or equal to k together
- Minimum number of swaps required to sort an array | Set 2
- Minimum deletions required to make GCD of the array equal to 1
- Find minimum changes required in an array for it to contain k distinct elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



