Free written solution

Count Number of Bad Pairs (LeetCode 2364): Solution and Explanation

A pair (i, j) is good when nums[i] - i equals nums[j] - j. Group indices by that key with a hash map, and every index j adds j minus the size of its bucket to the bad count.

Read the intuition, approach, code and complexity →