i - nums[i]
value.goodPairs
to count the number of good pairs.n * (n - 1) / 2
, where n
is the length of the array.nums
:
a. For each index i
, calculate difference = i - nums[i]
.
b. If difference
is already in the hashmap, increment goodPairs
by the frequency of difference
.
c. Increment the frequency of difference
in the hashmap.goodPairs
from the total number of pairs to get the count of bad pairs.