Count the number of special triplets in an array according to the problem's validity rule.
Given an integer array nums, count how many index triplets (i, j, k) satisfy the problem's special condition.
A triplet is valid only when the required ordering of indices holds and the values at those indices match the rule implied by the problem. Your task is to return the total number of valid triplets.
Because the answer may be large, return the count as an integer.
O(n^3) triplets.nums.1 <= nums.lengthExample 1
Input
nums = [1, 2, 1, 2, 1]
Output
4
Explanation
Illustrative example: there are four valid index triplets that satisfy the special condition for this array.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.