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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for this problem involves checking every possible group of numbers within the given set. We calculate the greatest common divisor (GCD) for each group and count how many of these GCDs match our target value. It's like exhaustively trying every combination to find the ones that work.
Here's how the algorithm would work step-by-step:
def number_of_subarrays_with_gcd_equal_to_k_brute_force(numbers, target):
number_of_subarrays = 0
for start_index in range(len(numbers)):
current_gcd = 0
for end_index in range(start_index, len(numbers)):
# Need to calculate GCD of the current subarray.
current_gcd = numbers[end_index] if current_gcd == 0 else greatest_common_divisor(current_gcd, numbers[end_index])
# Check if GCD is equal to target.
if current_gcd == target:
number_of_subarrays += 1
return number_of_subarrays
def greatest_common_divisor(first_number, second_number):
#Euclid's algorithm to calculate GCD
while(second_number):
first_number, second_number = second_number, first_number % second_number
return first_number
The core idea is to find all the continuous sections within the list of numbers where the greatest common divisor (GCD) is exactly the target number. Instead of checking every possible section, we use a method that efficiently calculates the GCD of each section as we go, and only count the sections that match our target.
Here's how the algorithm would work step-by-step:
def greatest_common_divisor(first_number, second_number):
while second_number:
first_number, second_number = second_number, first_number % second_number
return first_number
def number_of_subarrays_with_gcd_equal_to_k(number_list, target_gcd):
subarray_count = 0
list_length = len(number_list)
for start_index in range(list_length):
current_gcd = 0
# Consider each element as start of subarray
for end_index in range(start_index, list_length):
if current_gcd == 0:
current_gcd = number_list[end_index]
else:
# Update GCD with current element
current_gcd = greatest_common_divisor(current_gcd, number_list[end_index])
# If GCD is less than target_gcd, break
if current_gcd < target_gcd:
break
if current_gcd == target_gcd:
# Count the subarray if GCD equals target
subarray_count += 1
return subarray_count
Case | How to Handle |
---|---|
Null or Empty Array | Return 0, as no subarrays can be formed. |
Array with a single element | Check if the single element equals k, return 1 if true, 0 otherwise. |
Array where all elements are multiples of k | The algorithm should correctly count all subarrays whose GCD reduces to k. |
Array where no subarray has GCD equal to k | The algorithm should return 0. |
Large input array (potential performance bottleneck) | Ensure GCD calculation is optimized for speed, possibly using precomputed GCDs or other optimizations if necessary, and avoid recalculating the GCD of the same range repeatedly. |
k = 0 | If k is zero, return the number of subarrays whose elements are all zero, or zero if the array contains no zeros; handle the special case where GCD(x, 0) = x. |
k > max(array) | Return 0, as GCD cannot be greater than any element in the array. |
Array contains large numbers leading to integer overflow in GCD calculation | Use appropriate data types (e.g., long) to prevent integer overflow during GCD calculations. |