Similar Problems

Similar Problems not available

Number Of Subarrays With Lcm Equal To K - Leetcode Solution

Companies:

LeetCode:  Number Of Subarrays With Lcm Equal To K Leetcode Solution

Difficulty: Medium

Topics: math array  

Problem Description:

Given an array arr of positive integers, find the number of subarrays with the product of the elements equal to a given value k.

Solution Approach:

We can solve this problem using sliding window technique. Let’s denote the product of elements of any subarray as lcm. We can use the fact that lcm(arr[i], ….. arr[j]) = lcm(lcm(arr[i].. arr[Mid]), lcm(arr[mid+1], … arr[j])), where mid = (i+j)/2 . This tells us that we can divide our subarray in two disjoint subarrays. Since all elements in our array are positive, lcm of a subarray only increase as we add elements to it. We can start with an empty array and slide our window till we reach the end. As soon as we increase the size of our subarray, our lcm can either increase or remain the same. We can keep track of the lcm and as soon as it becomes equal to k we can count the number of subarrays which satisfy the given condition.

Let’s take an example to illustrate the approach.

Consider the array {2, 3, 4, 5, 6} and let’s assume the product k is 60.

  • First, we start with an empty subarray. The lcm of the subarray is initialized to 1. Then we start adding elements one by one. At i=0, we have arr[0]=2, so the lcm of our subarray now becomes 2. Since 60%2 != 0, we don’t increment our counter.
  • Next, we add the element arr[1]=3 to our subarray. Now the lcm is 6. Since 60%6 !=0, we don’t increment our counter.
  • Then we add arr[2]=4, now the lcm of our subarray becomes 12. Since 60%12 != 0, we don’t increment our counter.
  • Next, we add arr[3]=5, now the lcm of our subarray becomes 60. Since 60%60 = 0, we increment our counter by the number of subarrays ending at arr[3], which is 4. Total count now becomes 4.
  • Finally, we add the last element arr[4]=6, now the lcm of our subarray becomes lcm(12,6)=12. Since 60%12!=0, we don’t increment our counter anymore.

The solution to the problem is 4.

Algorithm:

  • Initialize counter to 0.
  • Initialize left pointer and right pointer to 0.
  • Initialize product to 1.
  • While right pointer < n
  • Multiply arr[right] into product.
  • While product > k, keep removing arr[left] from product and increment left pointer.
  • If product == k, increment counter by number of subarrays ending at right pointer and increment right pointer.
  • Else, increment right pointer.
  • Return counter.

Code Implementation:

class Solution { public: int numSubarrayProductLessThanK(vector<int>& nums, int k) { int n = nums.size(); int prod = 1; int l = 0; int count = 0;

    for (int r = 0; r < n; r++) {
        prod *= nums[r];
        while (prod >= k && l <= r) {
            prod /= nums[l++];
        }
        if (prod < k) {
            count += r-l+1;
        }
    }

    return count;
}

};

Number Of Subarrays With Lcm Equal To K Solution Code

1