Number of Single Divisor Triplets - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [4,7,9,6]
Output: 1
💡 Note: Only triplet (0,1,3) with values [4,7,6] has sum 17. Check: 17%4≠0, 17%7≠0, 17%6≠0... Actually, let me recalculate: 4+7+6=17, and 17%4=1, 17%7=3, 17%6=5, so no divisors. Let me try (0,1,2): 4+7+9=20, 20%4=0 (✓), 20%7=6 (✗), 20%9=2 (✗). Exactly 1 divisor!
Example 2 — Multiple Valid Triplets
$ Input: nums = [1,2,3]
Output: 0
💡 Note: Only one triplet (0,1,2): sum = 1+2+3=6. Check divisibility: 6%1=0 (✓), 6%2=0 (✓), 6%3=0 (✓). Has 3 divisors, not exactly 1, so invalid.
Example 3 — Larger Array
$ Input: nums = [1,1,1,1,1]
Output: 0
💡 Note: All triplets have sum 3, and 3%1=0, so each triplet has 3 divisors (all three 1's divide 3). None have exactly 1 divisor.

Constraints

  • 3 ≤ nums.length ≤ 100
  • 1 ≤ nums[i] ≤ 100

Visualization

Tap to expand
Single Divisor Triplets INPUT Array: nums 4 i=0 7 i=1 9 i=2 6 i=3 All Triplet Combinations (0,1,2): 4+7+9 = 20 (0,1,3): 4+7+6 = 17 (0,2,3): 4+9+6 = 19 (1,2,3): 7+9+6 = 22 Input: nums = [4, 7, 9, 6] ALGORITHM STEPS 1 Generate Triplets Use 3 nested loops 2 Calculate Sum sum = nums[i]+nums[j]+nums[k] 3 Check Divisibility Count divisors among i,j,k 4 Count if Exactly One Increment if divisorCount==1 Checking (0,2,3): sum=19 19 % 4 = 3 (NO) 19 % 9 = 1 (NO) 19 % 6 = 1 (NO) Divisor count: 0 Not a single divisor triplet FINAL RESULT Valid Triplet Found: (0,1,2): nums[0,1,2] 4 + 7 + 9 = 20 20 % 4 = 0 (divisible!) 20%7!=0, 20%9!=0 All Triplets Check: (0,1,2): 20 div by 4 only OK (0,1,3): 17 div by none (0,2,3): 19 div by none (1,2,3): 22 div by none Output: 1 Key Insight: A "single divisor triplet" requires the sum to be divisible by EXACTLY ONE of the three elements. Brute force: O(n^3) - check all triplet combinations. For each, count how many elements divide the sum. If exactly one element divides the sum, it's a valid single divisor triplet. Count and return total. TutorialsPoint - Number of Single Divisor Triplets | Optimized Brute Force Approach
Asked in
Google 15 Microsoft 12 Amazon 8
8.2K Views
Medium Frequency
~15 min Avg. Time
245 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