Maximum possible sum of non | C # program for the above approach ; 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 ; Driver Code ; Given array [ ] arr ; Given K ; Function Call
using System ; class GFG { public static int maxSum ( int [ ] a , int n , int k ) { if ( n <= 0 ) return 0 ; int 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 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 50 , 10 , 20 , 30 , 40 } ; int N = arr . Length ; int K = 100 ; Console . WriteLine ( maxSum ( arr , N , K ) ) ; } }