text: Maximum size subset with given sum using Backtracking | C # program to implement the above approach ; Initialise maximum possible length of subsequence ; Store elements to compare max_length with its size and change the value of max_length accordingly ; Store the elements of the longest subsequence ; Function to find the length of longest subsequence ; Update max_length ; Store the subsequence elements ; Recursively proceed with obtained sum ; poping elements from back of vector store ; If sum > 0 then we don 't  required thatsubsequence  so return and continue  with earlier elements ; Sort the given array ; Traverse the array ; If max_length is already greater than or equal than remaining length ; Driver code
code: using System ; using System . Collections . Generic ; class GFG { static int max_length = 0 ; static List < int > store = new List < int > ( ) ; static List < int > ans = new List < int > ( ) ; static void find_max_length ( int [ ] arr , int index , int sum , int k ) { sum = sum + arr [ index ] ; store . Add ( arr [ index ] ) ; if ( sum == k ) { if ( max_length < store . Count ) { max_length = store . Count ; ans = store ; } } for ( int i = index + 1 ; i < arr . Length ; i ++ ) { if ( sum + arr [ i ] <= k ) { find_max_length ( arr , i , sum , k ) ; store . RemoveAt ( store . Count - 1 ) ; } else return ; } return ; } static int longestSubsequence ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( max_length >= n - i ) break ; store . Clear ( ) ; find_max_length ( arr , i , 0 , k ) ; } return max_length ; } public static void Main ( String [ ] args ) { int [ ] arr = { - 3 , 0 , 1 , 1 , 2 } ; int n = arr . Length ; int k = 1 ; Console . Write ( longestSubsequence ( arr , n , k ) ) ; } }