Count Special Triplets - Problem
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 10^9 + 7.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [2,4,8,4,8]
›
Output:
1
💡 Note:
Only one valid triplet exists: (2,3,4) where nums[2]=8, nums[3]=4, nums[4]=8. Here 8 == 4*2 and 8 == 4*2, satisfying both conditions.
Example 2 — No Valid Triplets
$
Input:
nums = [1,2,3,4,5]
›
Output:
0
💡 Note:
No valid triplets exist since no element equals another element times 2 in the required positions.
Example 3 — Multiple Matches
$
Input:
nums = [4,2,4,2,4]
›
Output:
4
💡 Note:
Multiple triplets where middle element 2 has 4's (2*2=4) on both sides, creating several valid combinations.
Constraints
- 3 ≤ nums.length ≤ 103
- 1 ≤ nums[i] ≤ 106
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code