2447. Number of Subarrays With GCD Equal to K LeetCode Solution

In this guide, you will get 2447. Number of Subarrays With GCD Equal to K LeetCode Solution with the best time and space complexity. The solution to Number of Subarrays With GCD Equal to K problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Number of Subarrays With GCD Equal to K solution in C++
  4. Number of Subarrays With GCD Equal to K solution in Java
  5. Number of Subarrays With GCD Equal to K solution in Python
  6. Additional Resources
2447. Number of Subarrays With GCD Equal to K LeetCode Solution image

Problem Statement of Number of Subarrays With GCD Equal to K

Given an integer array nums and an integer k, return the number of subarrays of nums where the greatest common divisor of the subarray’s elements is k.
A subarray is a contiguous non-empty sequence of elements within an array.
The greatest common divisor of an array is the largest integer that evenly divides all the array elements.

Example 1:

Input: nums = [9,3,1,2,6,3], k = 3
Output: 4
Explanation: The subarrays of nums where 3 is the greatest common divisor of all the subarray’s elements are:
– [9,3,1,2,6,3]
– [9,3,1,2,6,3]
– [9,3,1,2,6,3]
– [9,3,1,2,6,3]

Example 2:

Input: nums = [4], k = 7
Output: 0
Explanation: There are no subarrays of nums where 7 is the greatest common divisor of all the subarray’s elements.

Constraints:

1 <= nums.length <= 1000
1 <= nums[i], k <= 109

Complexity Analysis

  • Time Complexity: O(n\log(\min(\texttt{nums})))
  • Space Complexity: O(n)

2447. Number of Subarrays With GCD Equal to K LeetCode Solution in C++

class Solution {
 public:
  int subarrayGCD(vector<int>& nums, int k) {
    int ans = 0;
    unordered_map<int, int> gcds;

    for (const int num : nums)
      if (num % k == 0) {
        unordered_map<int, int> nextGcds{{num, 1}};
        for (const auto& [prevGcd, count] : gcds)
          nextGcds[gcd(prevGcd, num)] += count;
        ans += nextGcds[k];
        gcds = std::move(nextGcds);
      } else {
        // The GCD streak stops, so fresh start from the next number.
        gcds.clear();
      }

    return ans;
  }
};
/* code provided by PROGIEZ */

2447. Number of Subarrays With GCD Equal to K LeetCode Solution in Java

class Solution {
  public int subarrayGCD(int[] nums, int k) {
    int ans = 0;
    Map<Integer, Integer> gcds = new HashMap<>();

    for (final int num : nums)
      if (num % k == 0) {
        Map<Integer, Integer> nextGcds = new HashMap<>();
        nextGcds.put(num, 1);
        for (Map.Entry<Integer, Integer> entry : gcds.entrySet()) {
          final int prevGcd = entry.getKey();
          final int count = entry.getValue();
          nextGcds.merge(gcd(prevGcd, num), count, Integer::sum);
        }
        ans += nextGcds.getOrDefault(k, 0);
        gcds = nextGcds;
      } else {
        // The GCD streak stops, so fresh start from the next number.
        gcds.clear();
      }

    return ans;
  }

  private int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
  }
}
// code provided by PROGIEZ

2447. Number of Subarrays With GCD Equal to K LeetCode Solution in Python

class Solution:
  def subarrayGCD(self, nums: list[int], k: int) -> int:
    ans = 0
    gcds = collections.Counter()

    for num in nums:
      if num % k == 0:
        nextGcds = collections.defaultdict(int)
        nextGcds[num] += 1
        for prevGcd, count in gcds.items():
          nextGcds[math.gcd(prevGcd, num)] += count
        ans += nextGcds.get(k, 0)
        gcds = nextGcds
      else:
        # The GCD streak stops, so fresh start from the next number.
        gcds.clear()

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.