Find all possible subarrays having product less than or equal to K | C # Program to implement the above approach ; Function to return all possible subarrays having product less than or equal to K ; Store the required subarrays ; Stores the product of current subarray ; Stores the starting index of the current subarray ; Check for empty array ; Iterate over the array ; Calculate product ; If product exceeds K ; Reduce product ; Increase starting index of current subarray ; Stores the subarray elements ; Store the subarray elements ; Add the subarrays to the list ; Return the final list of subarrays ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static List < List < int > > maxSubArray ( int [ ] arr , int K ) { List < List < int > > solution = new List < List < int > > ( ) ; int multi = 1 ; int start = 0 ; if ( arr . Length <= 1 K < 0 ) { return new List < List < int > > ( ) ; } for ( int i = 0 ; i < arr . Length ; i ++ ) { multi = multi * arr [ i ] ; while ( multi > K ) { multi = multi / arr [ start ] ; start ++ ; } List < int > list = new List < int > ( ) ; for ( int j = i ; j >= start ; j -- ) { list . Insert ( 0 , arr [ j ] ) ; solution . Add ( new List < int > ( list ) ) ; } } return solution ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 1 , 4 } ; int K = 7 ; List < List < int > > list = maxSubArray ( arr , K ) ; foreach ( List < int > i in list ) { Console . Write ( " [ " ) ; foreach ( int j in i ) { Console . Write ( j ) ; if ( i . Count > 1 ) Console . Write ( " , " ) ; } Console . Write ( " ] " ) ; } } }