845. Number of Single Divisor Triplets

You are given a 0-indexed array of positive integers nums. A triplet of three distinct indices (i, j, k) is called a single divisor triplet of nums if nums[i] + nums[j] + nums[k] is divisible by exactly one of nums[i], nums[j], or nums[k].

Return the number of single divisor triplets of nums.

Example 1:

Input: nums = [4,6,7,3,2]

Output: 12

Explanation:

The triplets (0, 3, 4), (0, 4, 3), (3, 0, 4), (3, 4, 0), (4, 0, 3), and (4, 3, 0) have the values of [4, 3, 2] (or a permutation of [4, 3, 2]).

4 + 3 + 2 = 9 which is only divisible by 3, so all such triplets are single divisor triplets.

The triplets (0, 2, 3), (0, 3, 2), (2, 0, 3), (2, 3, 0), (3, 0, 2), and (3, 2, 0) have the values of [4, 7, 3] (or a permutation of [4, 7, 3]).

4 + 7 + 3 = 14 which is only divisible by 7, so all such triplets are single divisor triplets.

There are 12 single divisor triplets in total.

Example 2:

Input: nums = [1,2,2]

Output: 6

Explanation:

The triplets (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), and (2, 1, 0) have the values of [1, 2, 2] (or a permutation of [1, 2, 2]).

1 + 2 + 2 = 5 which is only divisible by 1, so all such triplets are single divisor triplets.

There are 6 single divisor triplets in total.

Now Your Turn!

Pick the correct output for the given input

Consider the array nums = [3, 5, 7, 2, 6]. Which of the following triplets form a single divisor triplet?

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 3 <= nums.length <= 105
  • 1 <= nums[i] <= 100

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
long long singleDivisorTriplets(vector<int>& nums) {
// Your code goes here
}
};
Test Case

Input:

Nums