Class Solution
java.lang.Object
g2401_2500.s2447_number_of_subarrays_with_gcd_equal_to_k.Solution
2447 - Number of Subarrays With GCD Equal to K.<p>Medium</p>
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the number of <strong>subarrays</strong> of</em> <code>nums</code> <em>where the greatest common divisor of the subarray’s elements is</em> <code>k</code>.</p>
<p>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>
<p>The <strong>greatest common divisor of an array</strong> is the largest integer that evenly divides all the array elements.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [9,3,1,2,6,3], k = 3</p>
<p><strong>Output:</strong> 4</p>
<p><strong>Explanation:</strong> The subarrays of nums where 3 is the greatest common divisor of all the subarray’s elements are:</p>
<ul>
<li>
<p>[9,<ins> <strong>3</strong> </ins>,1,2,6,3]</p>
</li>
<li>
<p>[9,3,1,2,6,<ins> <strong>3</strong> </ins>]</p>
</li>
<li>
<p>[<ins> <strong>9,3</strong> </ins>,1,2,6,3]</p>
</li>
<li>
<p>[9,3,1,2,<ins> <strong>6,3</strong> </ins>]</p>
</li>
</ul>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [4], k = 7</p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong> There are no subarrays of nums where 7 is the greatest common divisor of all the subarray’s elements.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>1 <= nums[i], k <= 10<sup>9</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
subarrayGCD
public int subarrayGCD(int[] nums, int k)
-