Skip to content

Commit 1470bc9

Browse files
committedFeb 9, 2025
Add solution #2364
1 parent a261027 commit 1470bc9

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@
518518
2336|[Smallest Number in Infinite Set](./2336-smallest-number-in-infinite-set.js)|Medium|
519519
2349|[Design a Number Container System](./2349-design-a-number-container-system.js)|Medium|
520520
2352|[Equal Row and Column Pairs](./2352-equal-row-and-column-pairs.js)|Medium|
521+
2364|[Count Number of Bad Pairs](./2364-count-number-of-bad-pairs.js)|Medium|
521522
2390|[Removing Stars From a String](./2390-removing-stars-from-a-string.js)|Medium|
522523
2396|[Strictly Palindromic Number](./2396-strictly-palindromic-number.js)|Medium|
523524
2413|[Smallest Even Multiple](./2413-smallest-even-multiple.js)|Easy|
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 2364. Count Number of Bad Pairs
3+
* https://leetcode.com/problems/count-number-of-bad-pairs/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 0-indexed integer array nums. A pair of indices (i, j) is a bad pair
7+
* if i < j and j - i != nums[j] - nums[i].
8+
*
9+
* Return the total number of bad pairs in nums.
10+
*/
11+
12+
/**
13+
* @param {number[]} nums
14+
* @return {number}
15+
*/
16+
var countBadPairs = function(nums) {
17+
const map = new Map();
18+
let result = nums.length * (nums.length - 1) / 2;
19+
20+
nums.forEach((n, i) => {
21+
result -= map.get(n - i) ?? 0;
22+
map.set(n - i, (map.get(n - i) ?? 0) + 1);
23+
});
24+
25+
return result;
26+
};

0 commit comments

Comments
 (0)
Please sign in to comment.