Minimum boxes required to carry all gifts | C # implementation of above approach ; Function to return number of boxes ; Sort the boxes in ascending order ; Try to fit smallest box with current heaviest box ( from right side ) ; Driver Code
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 ) ) ; } }