aboutsummaryrefslogtreecommitdiff
path: root/num_bad_pairs.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-02-08 18:00:46 -0700
committerJack Sangdahl <[email protected]>2025-02-08 18:00:46 -0700
commit38628b7a294223f7e1caa129f55753183a536fa6 (patch)
tree5939376ce83a3558c0b76bc8c995f7223809edc5 /num_bad_pairs.cpp
parentf72076c568e27c480a1154b527433077e3884aad (diff)
downloadleetcode-38628b7a294223f7e1caa129f55753183a536fa6.tar.gz
leetcode-38628b7a294223f7e1caa129f55753183a536fa6.zip
2364. count number of bad pairs
Diffstat (limited to 'num_bad_pairs.cpp')
-rw-r--r--num_bad_pairs.cpp19
1 files changed, 10 insertions, 9 deletions
diff --git a/num_bad_pairs.cpp b/num_bad_pairs.cpp
index 75ea430..144e6ef 100644
--- a/num_bad_pairs.cpp
+++ b/num_bad_pairs.cpp
@@ -1,19 +1,20 @@
+// 2364. Count Number of Bad Pairs
#include "leetcode.h"
/*
- * given: 0-indexed integer array
- * bad pair: (i, j) if
- * i < j && j - i != nums[j] - nums[i]
+ * you are given a 0-indexed integer array 'nums'. a pair of indices '(i, j)' is
+ * a bad pair if 'i < j' and 'j - i != nums[j] - nums[i]'. return the total
+ * number of bad pairs in 'nums'.
*/
class Solution {
public:
long long countBadPairs(vector<int> &nums) {
long long ans = 0;
- unordered_map<int, int> um;
+ unordered_map<int, int> mp;
for (int i = 0; i < nums.size(); ++i) {
- ans += (long long)i - um[nums[i] - i];
- ++um[nums[i] - i];
+ ans += i - mp[nums[i] - i];
+ mp[nums[i] - i]++;
}
return ans;
}
@@ -21,7 +22,7 @@ public:
int main() {
Solution obj;
- vector<int> nums1 = {4, 1, 3, 3}, nums2 = {1, 2, 3, 4, 5};
- cout << obj.countBadPairs(nums1) << endl; // expect: 5
- cout << obj.countBadPairs(nums2) << endl; // expect: 0
+ vector<int> n1 = {4, 1, 3, 3}, n2 = {1, 2, 3, 4, 5};
+ printf("%lld\n", obj.countBadPairs(n1)); // expect: 5
+ printf("%lld\n", obj.countBadPairs(n2)); // expect: 0
}