MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / countBadPairs

Method countBadPairs

CountNumberOfBadPairs.java:2–14  ·  view source on GitHub ↗
(int[] nums)

Source from the content-addressed store, hash-verified

1class Solution {
2 public long countBadPairs(int[] nums) {
3 long n = nums.length;
4 long goodPairs=0;
5 long totalPairs = n*(n-1)/2;
6 HashMap<Integer,Integer> map = new HashMap<>();
7 for(int i=0;i<n;i++){
8 int val = i - nums[i];
9 int prevCount = map.getOrDefault(val,0);
10 goodPairs += prevCount;
11 map.put(val, map.getOrDefault(val,0)+1);
12 }
13 return totalPairs - goodPairs;
14 }
15}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected