Count Special Triplets - Problem

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, where n = nums.length
  • nums[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

example_1.py โ€” Basic Case
$ Input: [4, 2, 8, 1, 4]
โ€บ Output: 1
๐Ÿ’ก Note: There is one special triplet: indices (0, 1, 2) where nums[0] = 4, nums[1] = 2, nums[2] = 8. We have 4 = 2ร—2 and 8 = 2ร—2, but actually 8 โ‰  2ร—2, so this is incorrect. Let me reconsider: we need both nums[i] and nums[k] to equal nums[j]ร—2. So for middle element 2, we need first and third elements to be 4. We have 4 at index 0 and 4 at index 4, giving us triplet (0, 1, 4).
example_2.py โ€” Multiple Triplets
$ Input: [1, 2, 1, 2, 1]
โ€บ Output: 4
๐Ÿ’ก Note: Multiple special triplets exist: (0,1,2), (0,1,4), (0,3,4), and (2,3,4). For each middle element with value 2, we need first and third elements with value 4. Since we have 1s at positions 0,2,4 and 2s at positions 1,3, we get 2ร—2=4 valid combinations.
example_3.py โ€” No Valid Triplets
$ Input: [1, 3, 5]
โ€บ Output: 0
๐Ÿ’ก Note: No special triplets exist. For the middle element 3 at index 1, we would need elements with value 6 at indices 0 and 2, but we have 1 and 5 instead.

Visualization

Tap to expand
๐ŸŽต Special Triplet Harmony Detection ๐ŸŽต4First (i)2Middle (j)4Third (k)2 ร— 2 = 42 ร— 2 = 4Harmony Check โœ“First = Middle ร— 2Third = Middle ร— 2Order: i < j < k๐Ÿ” Optimization StrategyFor each middle element j:Left Count ร— Right Count = Total TripletsUse hash map for left counting, linear scan for right
Understanding the Visualization
1
Select Middle Element
Choose each array element as a potential middle element of the triplet
2
Count Valid First Elements
Count how many elements before the middle equal middle ร— 2
3
Count Valid Third Elements
Count how many elements after the middle equal middle ร— 2
4
Multiply and Sum
For each middle element, multiply left count ร— right count and add to total
Key Takeaway
๐ŸŽฏ Key Insight: Instead of checking all O(nยณ) triplets, we can decompose the problem by fixing the middle element and independently counting valid left and right elements, reducing complexity to O(nยฒ).

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

Single pass through array, but for each element we scan the remaining elements

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Hash map to store frequency counts of elements seen so far

n
2n
โšก Linearithmic Space

Constraints

  • 3 โ‰ค nums.length โ‰ค 3000
  • 0 โ‰ค nums[i] โ‰ค 200
  • All array elements are non-negative integers
  • Important: Return result modulo 109 + 7
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
73.6K Views
Medium-High Frequency
~18 min Avg. Time
1.8K 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