Minimum partitions of maximum size 2 and sum limited by given value | Javascript program to count minimum number of partitions of size 2 and sum smaller than or equal to given key . ; sort the array ; if sum of ith smaller and jth larger element is less than key , then pack both numbers in a set otherwise pack the jth larger number alone in the set ; After ending of loop i will contain minimum number of sets ; Driver Code
function minimumSets ( arr , n , key ) { var i , j ; arr . sort ( ( a , b ) => a - b ) for ( i = 0 , j = n - 1 ; i <= j ; ++ i ) if ( arr [ i ] + arr [ j ] <= key ) j -- ; return i ; } var arr = [ 3 , 5 , 3 , 4 ] ; var n = arr . length ; var key = 5 ; document . write ( minimumSets ( arr , n , key ) ) ;