2447. Number of Subarrays With GCD Equal to K Medium

@problem@discussion
#Array#Math#Number Theory



1/**
2 * [2447] Number of Subarrays With GCD Equal to K
3 *
4 * 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.
5 * A subarray is a contiguous non-empty sequence of elements within an array.
6 * The greatest common divisor of an array is the largest integer that evenly divides all the array elements.
7 *  
8 * <strong class="example">Example 1:
9 * 
10 * Input: nums = [9,3,1,2,6,3], k = 3
11 * Output: 4
12 * Explanation: The subarrays of nums where 3 is the greatest common divisor of all the subarray's elements are:
13 * - [9,<u>3</u>,1,2,6,3]
14 * - [9,3,1,2,6,<u>3</u>]
15 * - [<u>9,3</u>,1,2,6,3]
16 * - [9,3,1,2,<u>6,3</u>]
17 * 
18 * <strong class="example">Example 2:
19 * 
20 * Input: nums = [4], k = 7
21 * Output: 0
22 * Explanation: There are no subarrays of nums where 7 is the greatest common divisor of all the subarray's elements.
23 * 
24 *  
25 * Constraints:
26 * 
27 * 	1 <= nums.length <= 1000
28 * 	1 <= nums[i], k <= 10^9
29 * 
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/number-of-subarrays-with-gcd-equal-to-k/
34// discuss: https://leetcode.com/problems/number-of-subarrays-with-gcd-equal-to-k/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39    pub fn subarray_gcd(nums: Vec<i32>, k: i32) -> i32 {
40        0
41    }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_2447() {
52    }
53}
54


Back
© 2025 bowen.ge All Rights Reserved.