返回介绍

solution / 2400-2499 / 2447.Number of Subarrays With GCD Equal to K / README_EN

发布于 2024-06-17 01:03:05 字数 4451 浏览 0 评论 0 收藏 0

2447. Number of Subarrays With GCD Equal to K

中文文档

Description

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

Solutions

Solution 1: Direct Enumeration

We can enumerate $nums[i]$ as the left endpoint of the subarray, and then enumerate $nums[j]$ as the right endpoint of the subarray, where $i \le j$. During the enumeration of the right endpoint, we can use a variable $g$ to maintain the greatest common divisor of the current subarray. Each time we enumerate a new right endpoint, we update the greatest common divisor $g = \gcd(g, nums[j])$. If $g=k$, then the greatest common divisor of the current subarray equals $k$, and we increase the answer by $1$.

After the enumeration ends, return the answer.

The time complexity is $O(n \times (n + \log M))$, where $n$ and $M$ are the length of the array $nums$ and the maximum value in the array $nums$, respectively.

class Solution:
  def subarrayGCD(self, nums: List[int], k: int) -> int:
    ans = 0
    for i in range(len(nums)):
      g = 0
      for x in nums[i:]:
        g = gcd(g, x)
        ans += g == k
    return ans
class Solution {
  public int subarrayGCD(int[] nums, int k) {
    int n = nums.length;
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      int g = 0;
      for (int j = i; j < n; ++j) {
        g = gcd(g, nums[j]);
        if (g == k) {
          ++ans;
        }
      }
    }
    return ans;
  }

  private int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
  }
}
class Solution {
public:
  int subarrayGCD(vector<int>& nums, int k) {
    int n = nums.size();
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      int g = 0;
      for (int j = i; j < n; ++j) {
        g = gcd(g, nums[j]);
        ans += g == k;
      }
    }
    return ans;
  }
};
func subarrayGCD(nums []int, k int) (ans int) {
  for i := range nums {
    g := 0
    for _, x := range nums[i:] {
      g = gcd(g, x)
      if g == k {
        ans++
      }
    }
  }
  return
}

func gcd(a, b int) int {
  if b == 0 {
    return a
  }
  return gcd(b, a%b)
}
function subarrayGCD(nums: number[], k: number): number {
  let ans = 0;
  const n = nums.length;
  for (let i = 0; i < n; ++i) {
    let g = 0;
    for (let j = i; j < n; ++j) {
      g = gcd(g, nums[j]);
      if (g === k) {
        ++ans;
      }
    }
  }
  return ans;
}

function gcd(a: number, b: number): number {
  return b === 0 ? a : gcd(b, a % b);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文