Count how many index pairs are not “good” based on the relation between values and indices.
You are given an integer array nums.
For a pair of indices (i, j) with i < j, define the pair as good if:
All other pairs are bad.
Return the number of bad pairs in the array.
Your task is to count all index pairs and subtract the number of good pairs efficiently.
nums of length n.(i, j) such that i < j and the pair is not good.nums fit in a 32-bit signed integerExample 1
Input
nums = [4,1,3,3]
Output
5
Explanation
All pairs are: (0,1), (0,2), (0,3), (1,2), (1,3), (2,3). Good pairs satisfy j - i = nums[j] - nums[i]. Only (1,2) is good because 2 - 1 = 3 - 1 = 2 is false; wait, check carefully:
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.