Minimum boxes required to carry all gifts
Last Updated :
16 Oct, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int numBoxes( int A[], int n, int K)
{
sort(A, A + n);
int i = 0, j = n - 1;
int ans = 0;
while (i <= j) {
ans++;
if (A[i] + A[j] <= K)
i++;
j--;
}
return ans;
}
int main()
{
int A[] = { 3, 2, 2, 1 }, K = 3;
int n = sizeof (A) / sizeof (A[0]);
cout << numBoxes(A, n, K);
return 0;
}
|
Java
import java.util.*;
class solution
{
static int numBoxes( int A[], int n, int K)
{
Arrays.sort(A);
int i = 0 , j = n - 1 ;
int ans = 0 ;
while (i <= j) {
ans++;
if (A[i] + A[j] <= K)
i++;
j--;
}
return ans;
}
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));
}
}
|
Python3
def numBoxex(A,n,K):
A.sort()
i = 0
j = n - 1
ans = 0
while i< = j:
ans + = 1
if A[i] + A[j] < = K:
i + = 1
j - = 1
return ans
if __name__ = = '__main__' :
A = [ 3 , 2 , 2 , 1 ]
K = 3
n = len (A)
print (numBoxex(A,n,K))
|
C#
using System;
class GFG
{
static int numBoxes( int []A, int n, int K)
{
Array.Sort(A);
int i = 0, j = (n - 1);
int ans = 0;
while (i <= j)
{
ans++;
if (A[i] + A[j] <= K)
i++;
j--;
}
return ans;
}
static public void Main ()
{
int []A = { 3, 2, 2, 1 };
int K = 3;
int n = A.Length;
Console.WriteLine(numBoxes(A, n, K));
}
}
|
PHP
<?php
function numBoxes( $A , $n , $K )
{
sort( $A );
$i = 0;
$j = $n - 1;
$ans = 0;
while ( $i <= $j ) {
$ans ++;
if ( $A [ $i ] + $A [ $j ] <= $K )
$i ++;
$j --;
}
return $ans ;
}
$A = array (3, 2, 2, 1 );
$K = 3;
$n = sizeof( $A ) / sizeof( $A [0]);
echo numBoxes( $A , $n , $K );
?>
|
Javascript
<script>
function numBoxes(A, n, K)
{
A.sort( function (a, b){ return a - b});
let i = 0, j = (n - 1);
let ans = 0;
while (i <= j)
{
ans++;
if (A[i] + A[j] <= K)
i++;
j--;
}
return ans;
}
let A = [ 3, 2, 2, 1 ];
let K = 3;
let n = A.length;
document.write(numBoxes(A, n, K));
</script>
|
Time Complexity: O(N*log(N)), where N is the length of the array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...