Problem

Given an integer array nums and an integer k, return _the number ofsubarrays 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.

Examples

Example 1

1
2
3
4
5
6
7
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

1
2
3
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 <= 10^9

Solution

Method 1 – Brute Force with GCD

Intuition

Since n is small (≤ 1000), we can check all subarrays and compute their GCD. If the GCD equals k, count it.

Approach

  1. For each start index i, initialize g = 0.
  2. For each end index j ≥ i, update g = gcd(g, nums[j]).
  3. If g == k, increment answer. If g < k, break early.
  4. Return the answer.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <numeric>
class Solution {
public:
    int subarrayGCD(vector<int>& nums, int k) {
        int n = nums.size(), ans = 0;
        for (int i = 0; i < n; ++i) {
            int g = 0;
            for (int j = i; j < n; ++j) {
                g = std::gcd(g, nums[j]);
                if (g == k) ans++;
                if (g < k) break;
            }
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
func gcd(a, b int) int {
    for b != 0 {
        a, b = b, a%b
    }
    return a
}
func subarrayGCD(nums []int, k int) int {
    n, ans := len(nums), 0
    for i := 0; i < n; i++ {
        g := 0
        for j := i; j < n; j++ {
            g = gcd(g, nums[j])
            if g == k { ans++ }
            if g < k { break }
        }
    }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
    public int subarrayGCD(int[] nums, int k) {
        int n = nums.length, 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++;
                if (g < k) break;
            }
        }
        return ans;
    }
    private int gcd(int a, int b) {
        while (b != 0) {
            int t = b; b = a % b; a = t;
        }
        return a;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
    fun subarrayGCD(nums: IntArray, k: Int): Int {
        fun gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
        var ans = 0
        for (i in nums.indices) {
            var g = 0
            for (j in i until nums.size) {
                g = gcd(g, nums[j])
                if (g == k) ans++
                if (g < k) break
            }
        }
        return ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from math import gcd
class Solution:
    def subarrayGCD(self, nums: list[int], k: int) -> int:
        n, ans = len(nums), 0
        for i in range(n):
            g = 0
            for j in range(i, n):
                g = gcd(g, nums[j])
                if g == k:
                    ans += 1
                if g < k:
                    break
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
impl Solution {
    pub fn subarray_gcd(nums: Vec<i32>, k: i32) -> i32 {
        fn gcd(mut a: i32, mut b: i32) -> i32 {
            while b != 0 {
                let t = b; b = a % b; a = t;
            }
            a
        }
        let n = nums.len();
        let mut ans = 0;
        for i in 0..n {
            let mut g = 0;
            for j in i..n {
                g = gcd(g, nums[j]);
                if g == k { ans += 1; }
                if g < k { break; }
            }
        }
        ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
    subarrayGCD(nums: number[], k: number): number {
        function gcd(a: number, b: number): number {
            while (b !== 0) { [a, b] = [b, a % b] }
            return a
        }
        let ans = 0, 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++
                if (g < k) break
            }
        }
        return ans
    }
}

Complexity

  • ⏰ Time complexity: O(n^2 log k)
  • 🧺 Space complexity: O(1)