Given an integer array nums
and an integer d
, find the number of index triplets (i, j, k)
such that 0 <= i < j < k < nums.length
, and the sum nums[i] + nums[j] + nums[k]
is divisible by d
.
Example 1:
Input: nums = [3,3,6,4,7,5], d = 3
Output: 4
Explanation: The following triplets have a sum divisible by 3:
(3, 3, 6): 3 + 3 + 6 = 12 is divisible by 3
(3, 6, 4): 3 + 6 + 4 = 13. It's wrong.
(3, 6, 5): 3 + 6 + 5 = 14. It's wrong.
(3, 3, 6): 3 + 3 + 6 = 12 is divisible by 3. It's duplicated.
(3, 3, 6): 3 + 3 + 6 = 12 is divisible by 3. It's duplicated.
(3, 6, 4): 3 + 6 + 4 = 13. It's wrong.
(3, 4, 5): 3 + 4 + 5 = 12 is divisible by 3
(3, 7, 5): 3 + 7 + 5 = 15 is divisible by 3
(6, 4, 5): 6 + 4 + 5 = 15 is divisible by 3
Example 2:
Input: nums = [4,2,1,5,3], d = 2
Output: 4
Explanation: The following triplets have a sum divisible by 2:
(4, 2, 1): 4 + 2 + 1 = 7 is not divisible by 2.
(4, 2, 5): 4 + 2 + 5 = 11 is not divisible by 2.
(4, 2, 3): 4 + 2 + 3 = 9 is not divisible by 2.
(4, 1, 5): 4 + 1 + 5 = 10 is divisible by 2.
(4, 1, 3): 4 + 1 + 3 = 8 is divisible by 2.
(4, 5, 3): 4 + 5 + 3 = 12 is divisible by 2.
(2, 1, 5): 2 + 1 + 5 = 8 is divisible by 2.
(2, 1, 3): 2 + 1 + 3 = 6 is divisible by 2.
(2, 5, 3): 2 + 5 + 3 = 10 is divisible by 2.
(1, 5, 3): 1 + 5 + 3 = 9 is not divisible by 2.
Constraints:
3 <= nums.length <= 1000
1 <= nums[i] <= 1000
1 <= d <= 100
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 approach to this problem is about checking every possible combination. We will try every possible group of three numbers from the input to see if their sum is divisible by a given number. If it is, we count it; if not, we move on.
Here's how the algorithm would work step-by-step:
def number_of_divisible_triplet_sums(numbers, divisor):
number_of_numbers = len(numbers)
count_of_divisible_triplets = 0
for first_index in range(number_of_numbers):
for second_index in range(first_index + 1, number_of_numbers):
for third_index in range(second_index + 1, number_of_numbers):
# Calculate sum of the current triplet
triplet_sum = numbers[first_index] + numbers[second_index] + numbers[third_index]
# Check if the sum is divisible by the divisor
if triplet_sum % divisor == 0:
count_of_divisible_triplets += 1
# Increment count if triplet sum is divisible
return count_of_divisible_triplets
The key is to avoid checking every possible group of three numbers. We can use the properties of remainders to cleverly count valid combinations, drastically reducing the work.
Here's how the algorithm would work step-by-step:
def number_of_divisible_triplet_sums(number_array, divisor): array_length = len(number_array)
remainder_counts = [0] * divisor
for number in number_array:
remainder_counts[number % divisor] += 1
triplet_count = 0
#Case 1: (0,0,0)
triplet_count += remainder_counts[0] * (remainder_counts[0] - 1) * (remainder_counts[0] - 2) // 6
#Case 2: (a,a,a), where (3 * a) % divisor == 0
for remainder in range(1, divisor):
if (3 * remainder) % divisor == 0:
triplet_count += remainder_counts[remainder] * (remainder_counts[remainder] - 1) * (remainder_counts[remainder] - 2) // 6
#Case 3: (0, a, divisor-a)
for remainder in range(1, divisor // 2 + 1):
triplet_count += remainder_counts[0] * remainder_counts[remainder] * remainder_counts[divisor - remainder]
#Case 4: (a, a, divisor - 2a)
for remainder in range(1, divisor):
if (2 * remainder) < divisor and (divisor - 2 * remainder) != remainder:
triplet_count += remainder_counts[remainder] * (remainder_counts[remainder] - 1) // 2 * remainder_counts[divisor - 2 * remainder]
#Case 5: (a, b, divisor - a - b)
for remainder_a in range(1, divisor):
for remainder_b in range(remainder_a + 1, divisor):
remainder_c = divisor - remainder_a - remainder_b
if remainder_c >= remainder_b + 1:
triplet_count += remainder_counts[remainder_a] * remainder_counts[remainder_b] * remainder_counts[remainder_c % divisor]
#The above is because the total number of triplets need to be divisible.
return triplet_count
Case | How to Handle |
---|---|
Empty input array | Return 0 since no triplets can be formed. |
Input array with fewer than 3 elements | Return 0 since a triplet cannot be formed. |
k is 0 | Handle division by zero by checking for k==0 and returning 0 if nums[i] + nums[j] + nums[l] is never 0, or iterate and count solutions if sum is ever 0. |
Large input array (performance considerations) | Optimize the solution for O(n^2) or better complexity to handle large inputs efficiently. |
Input array with negative numbers | The modulo operator (%) should be handled correctly for negative remainders by adding k if the result is negative. |
Input array with zero values | The solution should correctly count triplets involving zero values. |
Integer overflow in the sum of nums[i] + nums[j] + nums[l] | Use a larger data type (e.g., long) to store the sum to prevent integer overflow. |
All numbers in the array are the same | Carefully calculate the number of combinations nCr, where n is the size of the array and r is 3, to avoid overcounting. |