3583. Count Special Triplets Medium

@problem@discussion
#Array#Hash Table#Counting



1/**
2 * [3583] Count Special Triplets
3 *
4 * You are given an integer array nums.
5 * A special triplet is defined as a triplet of indices (i, j, k) such that:
6 * 
7 * 	0 <= i < j < k < n, where n = nums.length
8 * 	nums[i] == nums[j] * 2
9 * 	nums[k] == nums[j] * 2
10 * 
11 * Return the total number of special triplets in the array.
12 * Since the answer may be large, return it modulo 10^9 + 7.
13 *  
14 * <strong class="example">Example 1:
15 * <div class="example-block">
16 * Input: <span class="example-io">nums = [6,3,6]</span>
17 * Output: <span class="example-io">1</span>
18 * Explanation:
19 * The only special triplet is (i, j, k) = (0, 1, 2), where:
20 * 
21 * 	nums[0] = 6, nums[1] = 3, nums[2] = 6
22 * 	nums[0] = nums[1] * 2 = 3 * 2 = 6
23 * 	nums[2] = nums[1] * 2 = 3 * 2 = 6
24 * </div>
25 * <strong class="example">Example 2:
26 * <div class="example-block">
27 * Input: <span class="example-io">nums = [0,1,0,0]</span>
28 * Output: <span class="example-io">1</span>
29 * Explanation:
30 * The only special triplet is (i, j, k) = (0, 2, 3), where:
31 * 
32 * 	nums[0] = 0, nums[2] = 0, nums[3] = 0
33 * 	nums[0] = nums[2] * 2 = 0 * 2 = 0
34 * 	nums[3] = nums[2] * 2 = 0 * 2 = 0
35 * </div>
36 * <strong class="example">Example 3:
37 * <div class="example-block">
38 * Input: <span class="example-io">nums = [8,4,2,8,4]</span>
39 * Output: <span class="example-io">2</span>
40 * Explanation:
41 * There are exactly two special triplets:
42 * 
43 * 	(i, j, k) = (0, 1, 3)
44 * 	
45 * 		nums[0] = 8, nums[1] = 4, nums[3] = 8
46 * 		nums[0] = nums[1] * 2 = 4 * 2 = 8
47 * 		nums[3] = nums[1] * 2 = 4 * 2 = 8
48 * 	
49 * 	
50 * 	(i, j, k) = (1, 2, 4)
51 * 	
52 * 		nums[1] = 4, nums[2] = 2, nums[4] = 4
53 * 		nums[1] = nums[2] * 2 = 2 * 2 = 4
54 * 		nums[4] = nums[2] * 2 = 2 * 2 = 4
55 * 	
56 * 	
57 * </div>
58 *  
59 * Constraints:
60 * 
61 * 	3 <= n == nums.length <= 10^5
62 * 	0 <= nums[i] <= 10^5
63 * 
64 */
65pub struct Solution {}
66
67// problem: https://leetcode.com/problems/count-special-triplets/
68// discuss: https://leetcode.com/problems/count-special-triplets/discuss/?currentPage=1&orderBy=most_votes&query=
69
70// submission codes start here
71
72impl Solution {
73    pub fn special_triplets(nums: Vec<i32>) -> i32 {
74        0
75    }
76}
77
78// submission codes end
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn test_3583() {
86    }
87}
88

Back
© 2026 bowen.ge All Rights Reserved.