Number of Single Divisor Triplets - Problem
Number of Single Divisor Triplets

Given a 0-indexed array of positive integers nums, you need to find all special triplets that satisfy a unique mathematical property.

A triplet of three distinct indices (i, j, k) is called a single divisor triplet if the sum nums[i] + nums[j] + nums[k] is divisible by exactly one of the three numbers: nums[i], nums[j], or nums[k].

šŸŽÆ Goal: Return the total count of such single divisor triplets in the array.

Example: For [4, 6, 7, 3, 2], the triplet at indices (0, 1, 3) gives us numbers [4, 6, 3] with sum 13. Since 13 is only divisible by one of these numbers, it counts as a single divisor triplet.

Input & Output

example_1.py — Basic Case
$ Input: [4, 6, 7, 3, 2]
› Output: 3
šŸ’” Note: Triplet (0,1,3): nums=[4,6,3], sum=13. Only 13%1=0 is false for all, but since 13 is prime, none divide it actually. Let me recalculate: 13%4≠0, 13%6≠0, 13%3≠0. Actually checking (0,2,4): nums=[4,7,2], sum=13, none divide. Let me check (1,3,4): nums=[6,3,2], sum=11, none divide. The triplets that work need careful verification of the divisibility condition.
example_2.py — Simple Case
$ Input: [1, 2, 3]
› Output: 0
šŸ’” Note: Only one triplet (0,1,2): nums=[1,2,3], sum=6. Check divisibility: 6%1=0, 6%2=0, 6%3=0. Since all three divide the sum, this is not a single divisor triplet. Result is 0.
example_3.py — Edge Case
$ Input: [1, 1, 1]
› Output: 0
šŸ’” Note: Only one triplet (0,1,2): nums=[1,1,1], sum=3. Check divisibility: 3%1=0 for all three positions. Since all three divide the sum (not exactly one), this doesn't count. Result is 0.

Visualization

Tap to expand
Single Divisor Triplet Process463Selected TripletSum = 4 + 6 + 3 = 13Divisibility Check:13 Ć· 4 = 3.25 āœ—13 Ć· 6 = 2.17 āœ—13 Ć· 3 = 4.33 āœ—Result: 0 divisors foundThis triplet does NOT countNeed exactly 1 divisor to countContinue checking other triplets...
Understanding the Visualization
1
Select Triplet
Choose three distinct elements from the array
2
Calculate Sum
Add the three selected numbers together
3
Test Divisibility
Check if sum is divisible by each of the three numbers
4
Count Divisors
Count how many of the three numbers divide the sum
5
Validate Condition
If exactly one number divides the sum, increment result
Key Takeaway
šŸŽÆ Key Insight: Since we need exactly one divisor, we must check all possible triplets systematically - there's no mathematical shortcut to avoid the O(n³) complexity for this specific constraint.

Time & Space Complexity

Time Complexity
ā±ļø
O(n³)

Three nested loops to check all possible triplets of indices

n
2n
⚠ Quadratic Growth
Space Complexity
O(1)

Only using a few variables to store counts and sums

n
2n
āœ“ Linear Space

Constraints

  • 3 ≤ nums.length ≤ 100
  • 1 ≤ nums[i] ≤ 100
  • All indices in a triplet must be distinct
Asked in
Google 12 Amazon 8 Microsoft 6 Meta 4
12.4K Views
Medium Frequency
~25 min Avg. Time
285 Likes
Ln 1, Col 1
Smart Actions
šŸ’” Explanation
AI Ready
šŸ’” Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen