Skip to content

Commit 907268a

Browse files
authored
Create count-number-of-bad-pairs.cpp
1 parent 0200133 commit 907268a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

C++/count-number-of-bad-pairs.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
// freq table
5+
class Solution {
6+
public:
7+
long long countBadPairs(vector<int>& nums) {
8+
int64_t result = size(nums) * (size(nums) - 1) / 2;
9+
unordered_map<int, int> cnt;
10+
for (int i = 0; i < size(nums); ++i) {
11+
result -= cnt[nums[i] - i]++;
12+
}
13+
return result;
14+
}
15+
};

0 commit comments

Comments
 (0)