diff --git a/algorithms/CPlusPlus/Arrays/.cph/.subarray-product.cpp_7825d78a6905f552e3a1c581ec6241ce.prob b/algorithms/CPlusPlus/Arrays/.cph/.subarray-product.cpp_7825d78a6905f552e3a1c581ec6241ce.prob new file mode 100644 index 00000000..d2351623 --- /dev/null +++ b/algorithms/CPlusPlus/Arrays/.cph/.subarray-product.cpp_7825d78a6905f552e3a1c581ec6241ce.prob @@ -0,0 +1 @@ +{"name":"Local: subarray-product","url":"c:\\Users\\HP\\OneDrive\\Desktop\\DSA Contrib\\DSA\\algorithms\\CPlusPlus\\Arrays\\subarray-product.cpp","tests":[{"id":1688797590384,"input":"5","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\Users\\HP\\OneDrive\\Desktop\\DSA Contrib\\DSA\\algorithms\\CPlusPlus\\Arrays\\subarray-product.cpp","group":"local","local":true} \ No newline at end of file diff --git a/algorithms/CPlusPlus/Arrays/subarray-product.bin b/algorithms/CPlusPlus/Arrays/subarray-product.bin new file mode 100644 index 00000000..5dd8ea9e Binary files /dev/null and b/algorithms/CPlusPlus/Arrays/subarray-product.bin differ diff --git a/algorithms/CPlusPlus/Arrays/subarray-product.cpp b/algorithms/CPlusPlus/Arrays/subarray-product.cpp new file mode 100644 index 00000000..85b04d8c --- /dev/null +++ b/algorithms/CPlusPlus/Arrays/subarray-product.cpp @@ -0,0 +1,94 @@ +/* +A C++ (CPP) code to find how many subarrays ( contiguous part of an array) are present in an array whose product is less than a + given number 'k' + + e.g.- + Input array - arr[5]= {1, 3, 4 ,5 ,2} + Input No. - k = 5 + + Subarrays of arr having product less than (k=5) are - {1} , {3} , {4} , {2} , {1 , 3} +*/ + +/* + +Idea - Use Sliding Window Algorithm + +Procedure - + +Go in a loop ( j=0 to n) and create a sliding window each time starting from current element and increase its size (in second loop) +till both of these conditions are true ( product of all eleemnts in the window < k and iterator < last_index ) + +Proof - + If at any instance , the product of all elements in the current window (subarray) exceeds k , then all further windows's product>k + and hence we shift to the element to immediate right and do the same process . +*/ + +// Implementation + +#include +using namespace std; +#define ll long long int + +// required function + +int count_SubArray_Product_Less_Than_K(int a[], int n, int k) +{ + // Actual answer + int ans = 0; + + for (int j = 0; j < n; j++) + { + // traverse each element + int i = j; + + // initialize the product as 1 for current window . + + long long int product = 1; + + // making a window that is expanding continuously till (product < k) AND (counter variable < size of array) . + while (i < n && product < k) + { + product *= a[i]; + if (product < k) + { + ans++; + i++; + } + } + } + + return ans; +} + +int main() +{ + + // Taking INPUT from user + int n; + + cout << "Enter the no. of elements in array : \n"; + cin >> n; + + int arr[n]; + + cout << "Enter the elements : \n"; + + for (int j = 0; j < n; j++) + { + cin >> arr[j]; + } + + int k; + + cout << "Enter the number k : \n"; + cin >> k; + + // Calling the required Function + int ans = count_SubArray_Product_Less_Than_K(arr, n, k); + + // Giving Output + cout << "The Number of subarrays in given array whose product is less than " << k << "are- " << endl; + cout << ans << endl; + + return 0; +}