Find all possible subarrays having product less than or equal to K | Java 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
import java . io . * ; import java . util . * ; class GFG { public static List < List < Integer > > maxSubArray ( int [ ] arr , int K ) { List < List < Integer > > solution = new ArrayList < > ( ) ; int multi = 1 ; int start = 0 ; if ( arr . length <= 1 K < 0 ) { return new ArrayList < > ( ) ; } for ( int i = 0 ; i < arr . length ; i ++ ) { multi = multi * arr [ i ] ; while ( multi > K ) { multi = multi / arr [ start ] ; start ++ ; } List < Integer > list = new ArrayList < > ( ) ; for ( int j = i ; j >= start ; j -- ) { list . add ( 0 , arr [ j ] ) ; solution . add ( new ArrayList < > ( list ) ) ; } } return solution ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 1 , 4 } ; int K = 7 ; System . out . println ( maxSubArray ( arr , K ) ) ; } }