Count Special Triplets - Solution & Explanation
Problem Statement
You are given an integer array nums.
A special triplet is defined as a triplet of indices (i, j, k) such that:
0 <= i < j < k < n, wheren = nums.lengthnums[i] == nums[j] * 2nums[k] == nums[j] * 2
Return the total number of special triplets in the array.
Since the answer may be large, return it modulo 109 + 7.
Example 1:
Input: nums = [6,3,6]
Output: 1
Explanation:
The only special triplet is (i, j, k) = (0, 1, 2), where:
nums[0] = 6,nums[1] = 3,nums[2] = 6nums[0] = nums[1] * 2 = 3 * 2 = 6nums[2] = nums[1] * 2 = 3 * 2 = 6
Example 2:
Input: nums = [0,1,0,0]
Output: 1
Explanation:
The only special triplet is (i, j, k) = (0, 2, 3), where:
nums[0] = 0,nums[2] = 0,nums[3] = 0nums[0] = nums[2] * 2 = 0 * 2 = 0nums[3] = nums[2] * 2 = 0 * 2 = 0
Example 3:
Input: nums = [8,4,2,8,4]
Output: 2
Explanation:
There are exactly two special triplets:
(i, j, k) = (0, 1, 3)nums[0] = 8,nums[1] = 4,nums[3] = 8nums[0] = nums[1] * 2 = 4 * 2 = 8nums[3] = nums[1] * 2 = 4 * 2 = 8
(i, j, k) = (1, 2, 4)nums[1] = 4,nums[2] = 2,nums[4] = 4nums[1] = nums[2] * 2 = 2 * 2 = 4nums[4] = nums[2] * 2 = 2 * 2 = 4
Constraints:
3 <= n == nums.length <= 1050 <= nums[i] <= 105
Approach Overview
Problem Overview: You need to count triplets of indices (i, j, k) such that i < j < k and the three values satisfy a specific “special” numeric relationship defined in the problem. The challenge is avoiding the obvious cubic scan of all triplets and instead exploiting the fact that the middle index j uniquely determines what values must appear on the left and right.
Approach 1: Brute Force Triplet Enumeration (O(n^3) time, O(1) space)
The direct approach checks every possible triple of indices. Use three nested loops: the outer loop picks i, the second picks j, and the third picks k. For each triple, evaluate the special relation defined by the problem. This guarantees correctness but performs O(n^3) checks, which becomes too slow once the array grows beyond a few thousand elements. The method is mainly useful for verifying logic or constructing small test cases.
Approach 2: Enumerate Middle Number + Hash Table (O(n) time, O(n) space)
The key observation: once the middle index j is fixed, the required values for nums[i] and nums[k] become deterministic based on nums[j] and the special relation. Instead of scanning both sides repeatedly, maintain frequency counts of elements on the left and right using a hash map. As you iterate through the array, treat the current element as the middle. The left map stores counts of values that appear before j, while the right map tracks remaining elements after j.
For each j, compute the value(s) that must appear on the left and right to form a valid triplet with nums[j]. A constant‑time hash lookup gives the number of candidates on each side. Multiply those counts to determine how many triplets use this middle element. Then update the maps as the pointer moves forward. This technique converts repeated scans into O(1) lookups, reducing the total complexity to linear time.
The pattern of fixing a center element and counting compatible values on both sides appears frequently in problems involving arrays, frequency tracking with hash tables, and combinational counting. Once you recognize that the middle value determines the relationship, the optimization becomes straightforward.
Recommended for interviews: Start by explaining the O(n^3) brute force idea to show you understand the triplet constraint i < j < k. Then move to the optimized solution where you enumerate the middle element and use hash maps to count valid partners on each side. Interviewers typically expect this reasoning because it reduces redundant scans and demonstrates familiarity with frequency maps and counting techniques.
Solution
We can enumerate the middle number nums[j], and use two hash tables, left and right, to record the occurrence counts of numbers to the left and right of nums[j], respectively.
First, we add all numbers to right. Then, we traverse each number nums[j] from left to right. During the traversal:
- Remove
nums[j]fromright. - Count the occurrences of the number
nums[i] = nums[j] * 2to the left ofnums[j], denoted asleft[nums[j] * 2]. - Count the occurrences of the number
nums[k] = nums[j] * 2to the right ofnums[j], denoted asright[nums[j] * 2]. - Multiply
left[nums[j] * 2]andright[nums[j] * 2]to get the number of special triplets withnums[j]as the middle number, and add the result to the answer. - Add
nums[j]toleft.
Finally, return the answer.
The time complexity is O(n), and the space complexity is O(n), where n is the length of the array nums.
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Triplet Enumeration | O(n^3) | O(1) | Small inputs or verifying correctness during initial reasoning |
| Enumerate Middle + Hash Table | O(n) | O(n) | General case; optimal interview solution using frequency counting |
Video Solution
Count Special Triplets | LeetCode 3583 | Complete Intuition Explained • Sanyam IIT Guwahati • 1,875 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Count Special Triplets easy or hard?
Count Special Triplets Python/Java solution
How to solve Count Special Triplets in O(n)?
What is the best approach for Count Special Triplets?
Is Count Special Triplets asked at Google/Amazon/Meta?
What data structure is used in Count Special Triplets?
What is the time complexity of Count Special Triplets?
Ready to solve this problem?
Practice Count Special Triplets with our built-in code editor and test cases.
Practice on FleetCodeTable of Contents
Practice this problem
Open in Editor