File tree 2 files changed +27
-0
lines changed
2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change 518
518
2336|[ Smallest Number in Infinite Set] ( ./2336-smallest-number-in-infinite-set.js ) |Medium|
519
519
2349|[ Design a Number Container System] ( ./2349-design-a-number-container-system.js ) |Medium|
520
520
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|
521
522
2390|[ Removing Stars From a String] ( ./2390-removing-stars-from-a-string.js ) |Medium|
522
523
2396|[ Strictly Palindromic Number] ( ./2396-strictly-palindromic-number.js ) |Medium|
523
524
2413|[ Smallest Even Multiple] ( ./2413-smallest-even-multiple.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments