Find all possible subarrays having product less than or equal to K | 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 li ; Return the final li of subarrays ; Driver Code
def maxSubArray ( arr , n , K ) : NEW_LINE INDENT solution = [ ] NEW_LINE multi = 1 NEW_LINE start = 0 NEW_LINE if ( n <= 1 or K < 0 ) : NEW_LINE INDENT return solution NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT multi = multi * arr [ i ] NEW_LINE while ( multi > K ) : NEW_LINE INDENT multi = multi // arr [ start ] NEW_LINE start += 1 NEW_LINE DEDENT li = [ ] NEW_LINE j = i NEW_LINE while ( j >= start ) : NEW_LINE INDENT li . insert ( 0 , arr [ j ] ) NEW_LINE solution . append ( list ( li ) ) NEW_LINE j -= 1 NEW_LINE DEDENT DEDENT return solution NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 7 , 1 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE K = 7 NEW_LINE v = maxSubArray ( arr , n , K ) NEW_LINE print ( v ) NEW_LINE DEDENT