Given a 0-indexed integer array nums
of length n
and an integer k
, return the number of pairs (i, j)
such that:
0 <= i < j <= n - 1
andnums[i] * nums[j]
is divisible by k
.Example 1:
Input: nums = [1,2,3,4,5], k = 2 Output: 7 Explanation: The 7 pairs of indices whose corresponding products are divisible by 2 are (0, 1), (0, 3), (1, 2), (1, 3), (1, 4), (2, 3), and (3, 4). Their products are 2, 4, 6, 8, 10, 12, and 20 respectively. Other pairs such as (0, 2) and (2, 4) have products 3 and 15 respectively, which are not divisible by 2.
Example 2:
Input: nums = [1,2,3,4], k = 5 Output: 0 Explanation: There does not exist any pair of indices whose corresponding product is divisible by 5.
Constraints:
1 <= nums.length <= 105
1 <= nums[i], k <= 105
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 problem asks us to find pairs of numbers within a group that, when multiplied together, can be perfectly divided by a specific number. The brute force way is to simply try out every possible pair and see if it fits the condition. This means checking each number against every other number.
Here's how the algorithm would work step-by-step:
def count_array_pairs_divisible_by_k(numbers, divisor):
count_divisible_pairs = 0
# Iterate through the array to choose the first number in each pair
for first_index in range(len(numbers)):
# Iterate from the next element to avoid duplicate pairs
for second_index in range(first_index + 1, len(numbers)):
# This condition avoids checking against elements already checked
product_of_numbers = numbers[first_index] * numbers[second_index]
# Check if the product is divisible by the divisor
if product_of_numbers % divisor == 0:
count_divisible_pairs += 1
return count_divisible_pairs
The efficient solution avoids checking every single pair of numbers. Instead, it focuses on finding the remainders when each number is divided by the given divisor, and cleverly uses math to figure out which pairs of remainders would result in a number divisible by the divisor.
Here's how the algorithm would work step-by-step:
def count_array_pairs_divisible_by_k(numbers, divisor):
remainders = [number % divisor for number in numbers]
remainder_counts = {}
for remainder in remainders:
remainder_counts[remainder] = remainder_counts.get(remainder, 0) + 1
pair_count = 0
for remainder in remainder_counts:
# Find the complementary remainder.
complementary_remainder = (divisor - remainder) % divisor
# If the remainder is its own complement.
if remainder == complementary_remainder:
# Use combination formula to avoid double counting.
pair_count += remainder_counts[remainder] * (remainder_counts[remainder] - 1) // 2
# Ensure each pair is counted only once.
elif remainder < complementary_remainder:
# Count pairs using complementary remainders.
if complementary_remainder in remainder_counts:
pair_count += remainder_counts[remainder] * remainder_counts[complementary_remainder]
return pair_count
Case | How to Handle |
---|---|
Empty input array nums | Return 0 immediately as no pairs can be formed. |
Input array nums has only one element | Return 0 immediately as no pairs can be formed. |
k is 1 | Every pair will be divisible by 1, so return the number of possible pairs (n * (n - 1) / 2). |
k is a large prime number and no product of elements in nums is divisible by k | Return 0 since no pairs satisfy the condition. |
nums contains very large numbers such that the product of two numbers may cause an integer overflow. | Use a data type that can handle larger numbers, like long long in C++ or convert to GCD problem. |
nums contains negative numbers | The product can be negative or positive, so the divisibility check should handle both cases (e.g., using the modulo operator correctly). |
k is zero | Division by zero error; must be handled by returning 0, or throwing an error, depending on requirements. |
Large input array nums, potentially leading to time limit exceed issues | Optimize for time complexity, such as using a HashMap based on the greatest common divisor (GCD) with K, and iterating up to the sqrt(K). |