#2447

Number of Subarrays With GCD Equal to K

specialist · 845 · lc medium +31 · verified · 52.4% accepted · 464 likes · top 42%

Description

Given an integer array nums and an integer k, count the number of subarrays of nums whose GCD equals exactly k.

Return that count.

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.

Code

1
2
3