PYTHON Program to find the number of subarrays with product less than a value K
Naive Approach :
Key Idea:First we check for each value if it is less than K.
Then we will check for all other subarray multiplications with that value.If it is less than K then we increase counter by 1
else we break as every next product will be even more.
Program :
Output :
In the above scenario the list is [2,1,3,4] and k is 10. So, the subarray count with product less than k is 7 { [2], [1], [3], [4], [2,1], [2,1,3], [1,3] }
(Note: This post will be soon updated with an optimized approach using sliding window technique which can reduce the time complexity to O(n). We are trying to make the best pedagogy for that.)
Comments
Post a Comment