nums
.answer
to store the count of fair pairs.i
.nums[i]
, calculate the required minimum (minRequired
) and maximum (maxRequired
) values that the other element of the pair must have to form a fair pair.leftIndex
where nums[leftIndex]
is greater than or equal to minRequired
.rightIndex
where nums[rightIndex]
is less than or equal to maxRequired
.leftIndex
is not greater than rightIndex
, add the number of elements in the range [leftIndex, rightIndex]
to answer
.answer
.