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 list ; Return the final list of subarrays ; Driver Code
function maxSubArray ( arr , n , K ) { let solution = [ ] ; let multi = 1 ; let start = 0 ; if ( n <= 1 K < 0 ) { return solution ; } for ( let i = 0 ; i < n ; i ++ ) { multi = multi * arr [ i ] ; while ( multi > K ) { multi = Math . floor ( multi / arr [ start ] ) ; start ++ ; } let list = [ ] ; for ( let j = i ; j >= start ; j -- ) { list . unshift ( arr [ j ] ) ; solution . push ( list ) ; } } return solution ; } let arr = [ 2 , 7 , 1 , 4 ] ; let n = arr . length ; let K = 7 ; let v = maxSubArray ( arr , n , K ) ; document . write ( " " ) ; let first = true ; for ( let x = 0 ; x < v . length ; x ++ ) { if ( ! first ) { document . write ( " " ) ; } else { first = false ; } document . write ( " " ) ; let ff = true ; for ( let y = 0 ; y < v [ x ] . length ; y ++ ) { if ( ! ff ) { document . write ( " " ) ; } else { ff = false ; } document . write ( v [ x ] [ y ] ) ; } document . write ( " " ) ; } document . write ( " " ) ;