Count of subsets having sum of min and max element less than K | Function that return the count of subset such that Math . min ( S ) + Math . max ( S ) < K ; Sorting the array ; ans stores total number of subsets ; Add all possible subsets between i and j ; Decrease the sum ; Driver code
function get_subset_count ( arr , K , N ) { ( arr ) . sort ( function ( a , b ) { return a - b ; } ) ; let left , right ; left = 0 ; right = N - 1 ; let ans = 0 ; while ( left <= right ) { if ( arr [ left ] + arr [ right ] < K ) { ans += 1 << ( right - left ) ; left ++ ; } else { right -- ; } } return ans ; } let arr = [ 2 , 4 , 5 , 7 ] ; let K = 8 ; let N = arr . length ; document . write ( get_subset_count ( arr , K , N ) ) ;