Watch 2 video solutions for Number of Single Divisor Triplets, a medium level problem involving Math. This walkthrough by Programming Live with Larry has 109 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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].
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.
Example 3:
Input: nums = [1,1,1] Output: 0 Explanation: There are no single divisor triplets. Note that (0, 1, 2) is not a single divisor triplet because nums[0] + nums[1] + nums[2] = 3 and 3 is divisible by nums[0], nums[1], and nums[2].
Constraints:
3 <= nums.length <= 1051 <= nums[i] <= 100Problem Overview: You receive an integer array nums. Count triplets (i, j, k) such that exactly one of nums[i], nums[j], or nums[k] divides the sum nums[i] + nums[j] + nums[k]. The challenge is checking the divisor condition while efficiently counting all valid index permutations.
Approach 1: Brute Force Enumeration (O(n^3) time, O(1) space)
Iterate over every triplet of indices using three nested loops. For each combination, compute sum = a + b + c. Check the divisibility conditions sum % a, sum % b, and sum % c. Count the triplet only if exactly one of these values equals zero. This approach directly follows the problem definition and is useful for validating logic on small inputs. The downside is the cubic time complexity, which becomes impractical when n grows large.
Approach 2: Counting + Enumeration (O(U^3) time, O(U) space)
Instead of iterating over indices, count how many times each value appears using a frequency array or hash map. Let U be the number of distinct values (bounded and much smaller than n in typical constraints). Enumerate all value triples (a, b, c) with non‑zero frequencies and compute s = a + b + c. Check how many of a, b, and c divide s. If exactly one does, multiply by the number of index permutations derived from their frequencies. Handle cases separately where values are equal (a == b, b == c, etc.) using combinatorics. This converts expensive index enumeration into value-based counting.
The key insight is that divisibility depends only on the values, not the positions. By grouping identical numbers first, you avoid repeatedly evaluating the same triplet pattern. This technique is common in counting problems and math-based enumeration, where you trade direct iteration for frequency aggregation and combinatorial counting. The divisibility check itself is simple arithmetic, often seen in number theory style interview questions.
Recommended for interviews: Start by describing the brute force O(n^3) approach to demonstrate understanding of the condition. Then transition to the counting + enumeration strategy. Interviewers typically expect the optimized solution because it removes redundant work and shows comfort with frequency maps and combinatorics.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Triplet Enumeration | O(n^3) | O(1) | Useful for understanding the condition or validating correctness on small inputs |
| Counting + Enumeration | O(U^3) | O(U) | Best practical solution when many values repeat and the value range is small |