Maximum possible sum of non | Function to find the maximum sum not exceeding K possible by selecting a subset of non - adjacent elements ; Base Case ; Not selecting current element ; If selecting current element is possible ; Return answer ; Given array arr [ ] ; Given K ; Function Call
function maxSum ( a , n , k ) { if ( n <= 0 ) return 0 ; let option = maxSum ( a , n - 1 , k ) ; if ( k >= a [ n - 1 ] ) option = Math . max ( option , a [ n - 1 ] + maxSum ( a , n - 2 , k - a [ n - 1 ] ) ) ; return option ; } let arr = [ 50 , 10 , 20 , 30 , 40 ] ; let N = arr . length ; let K = 100 ; document . write ( maxSum ( arr , N , K ) ) ;