Minimum boxes required to carry all gifts | 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 program
< ? 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 ) ; ? >