Smallest subarray from a given Array with sum greater than or equal to K | Set 2 | C # program for the above approach ; Function to find the smallest subarray sum greater than or equal to target ; Base Case ; If sum of the array is less than target ; If target is equal to the sum of the array ; Required condition ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int smallSumSubset ( List < int > data , int target , int maxVal ) { int sum = 0 ; foreach ( int i in data ) sum += i ; if ( target <= 0 ) return 0 ; else if ( sum < target ) return maxVal ; else if ( sum == target ) return data . Count ; else if ( data [ 0 ] >= target ) return 1 ; else if ( data [ 0 ] < target ) { List < int > temp = new List < int > ( ) ; for ( int i = 1 ; i < data . Count ; i ++ ) temp . Add ( data [ i ] ) ; return Math . Min ( smallSumSubset ( temp , target , maxVal ) , 1 + smallSumSubset ( temp , target - data [ 0 ] , maxVal ) ) ; } return 0 ; } static void Main ( ) { List < int > data = new List < int > ( ) ; data . Add ( 3 ) ; data . Add ( 1 ) ; data . Add ( 7 ) ; data . Add ( 1 ) ; data . Add ( 2 ) ; int target = 11 ; int val = smallSumSubset ( data , target , data . Count + 1 ) ; if ( val > data . Count ) Console . Write ( - 1 ) ; else Console . Write ( val ) ; } }