Algorithm · IBM · Medium
You are given an integer array nums and an integer d. Determine how many valid triplets the array contains. A triplet is any choice of three different positions i, j, k with i < j < k. It counts as valid when the gap between the largest and smallest of the three chosen values does not exceed d, that is: $$\max(nums[i], nums[j], nums[k]) - \min(nums[i], nums[j], nums[k]) \le d$$ Triplets are distinguished by their indices, so positions holding equal values still produce…