Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
For a fixed index l, try to find the minimum value of index r, such that the subarray is not good
When a number is added to a subarray, it increases the number of pairs by its previous appearances.
When a number is removed from the subarray, it decreases the number of pairs by its remaining appearances.
Maintain 2-pointers l and r such that we can keep in account the number of equal pairs.