Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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, where n = nums.lengthnums[i] == nums[j] * 2nums[k] == nums[j] * 2Return 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 = 6Example 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 = 0Example 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 = 4Constraints:
3 <= n == nums.length <= 1050 <= nums[i] <= 105Problem summary: 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, where n = nums.length nums[i] == nums[j] * 2 nums[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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[6,3,6]
[0,1,0,0]
[8,4,2,8,4]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3583: Count Special Triplets
class Solution {
public int specialTriplets(int[] nums) {
Map<Integer, Integer> left = new HashMap<>();
Map<Integer, Integer> right = new HashMap<>();
for (int x : nums) {
right.merge(x, 1, Integer::sum);
}
long ans = 0;
final int mod = (int) 1e9 + 7;
for (int x : nums) {
right.merge(x, -1, Integer::sum);
ans = (ans + 1L * left.getOrDefault(x * 2, 0) * right.getOrDefault(x * 2, 0) % mod)
% mod;
left.merge(x, 1, Integer::sum);
}
return (int) ans;
}
}
// Accepted solution for LeetCode #3583: Count Special Triplets
func specialTriplets(nums []int) int {
left := make(map[int]int)
right := make(map[int]int)
for _, x := range nums {
right[x]++
}
ans := int64(0)
mod := int64(1e9 + 7)
for _, x := range nums {
right[x]--
ans = (ans + int64(left[x*2])*int64(right[x*2])%mod) % mod
left[x]++
}
return int(ans)
}
# Accepted solution for LeetCode #3583: Count Special Triplets
class Solution:
def specialTriplets(self, nums: List[int]) -> int:
left = Counter()
right = Counter(nums)
ans = 0
mod = 10**9 + 7
for x in nums:
right[x] -= 1
ans = (ans + left[x * 2] * right[x * 2] % mod) % mod
left[x] += 1
return ans
// Accepted solution for LeetCode #3583: Count Special Triplets
use std::collections::HashMap;
impl Solution {
pub fn special_triplets(nums: Vec<i32>) -> i32 {
let mut left: HashMap<i32, i64> = HashMap::new();
let mut right: HashMap<i32, i64> = HashMap::new();
for &x in &nums {
*right.entry(x).or_insert(0) += 1;
}
let modulo: i64 = 1_000_000_007;
let mut ans: i64 = 0;
for &x in &nums {
if let Some(v) = right.get_mut(&x) {
*v -= 1;
}
let t = x * 2;
let l = *left.get(&t).unwrap_or(&0);
let r = *right.get(&t).unwrap_or(&0);
ans = (ans + (l * r) % modulo) % modulo;
*left.entry(x).or_insert(0) += 1;
}
ans as i32
}
}
// Accepted solution for LeetCode #3583: Count Special Triplets
function specialTriplets(nums: number[]): number {
const left = new Map<number, number>();
const right = new Map<number, number>();
for (const x of nums) {
right.set(x, (right.get(x) || 0) + 1);
}
let ans = 0;
const mod = 1e9 + 7;
for (const x of nums) {
right.set(x, (right.get(x) || 0) - 1);
const lx = left.get(x * 2) || 0;
const rx = right.get(x * 2) || 0;
ans = (ans + ((lx * rx) % mod)) % mod;
left.set(x, (left.get(x) || 0) + 1);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.