nums
in ascending order.left
set to 0 and right
set to len(nums) - 1
.count
to store the number of valid pairs.left
is less than right
:
a. If nums[left] + nums[right]
is less than target
, increment count
by right - left
(all pairs between left
and right
are valid), and increment left
.
b. If the sum is greater than or equal to target
, decrement right
.count
.