You are given an integer array nums. Your task is to find and count all special triplets within this array.
A special triplet is defined as a triplet of indices (i, j, k) that satisfies these conditions:
0 โค i < j < k < n, wheren = nums.lengthnums[i] == nums[j] * 2(the first element is double the second)nums[k] == nums[j] * 2(the third element is also double the second)
In other words, you're looking for triplets where the first and third elements are both exactly double the middle element, and they appear in strictly increasing order of indices.
Goal: Return the total count of such special triplets. Since the answer may be large, return it modulo 109 + 7.
Example: In array [2, 4, 8, 4], indices (0, 1, 2) form a special triplet because nums[0] = 2, nums[1] = 4, nums[2] = 8, and both 2 and 8 equal 4 ร 2.
Input & Output
Visualization
Time & Space Complexity
Single pass through array, but for each element we scan the remaining elements
Hash map to store frequency counts of elements seen so far
Constraints
- 3 โค nums.length โค 3000
- 0 โค nums[i] โค 200
- All array elements are non-negative integers
- Important: Return result modulo 109 + 7