Skip to content

Commit 19c7bf9

Browse files
committed
Add solution #2176
1 parent c044644 commit 19c7bf9

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,335 LeetCode solutions in JavaScript
1+
# 1,336 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1222,6 +1222,7 @@
12221222
2140|[Solving Questions With Brainpower](./solutions/2140-solving-questions-with-brainpower.js)|Medium|
12231223
2154|[Keep Multiplying Found Values by Two](./solutions/2154-keep-multiplying-found-values-by-two.js)|Easy|
12241224
2161|[Partition Array According to Given Pivot](./solutions/2161-partition-array-according-to-given-pivot.js)|Medium|
1225+
2176|[Count Equal and Divisible Pairs in an Array](./solutions/2176-count-equal-and-divisible-pairs-in-an-array.js)|Easy|
12251226
2179|[Count Good Triplets in an Array](./solutions/2179-count-good-triplets-in-an-array.js)|Hard|
12261227
2185|[Counting Words With a Given Prefix](./solutions/2185-counting-words-with-a-given-prefix.js)|Easy|
12271228
2206|[Divide Array Into Equal Pairs](./solutions/2206-divide-array-into-equal-pairs.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 2176. Count Equal and Divisible Pairs in an Array
3+
* https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/
4+
* Difficulty: Easy
5+
*
6+
* Given a 0-indexed integer array nums of length n and an integer k, return the number of
7+
* pairs (i, j) where 0 <= i < j < n, such that nums[i] == nums[j] and (i * j) is divisible by k.
8+
*/
9+
10+
/**
11+
* @param {number[]} nums
12+
* @param {number} k
13+
* @return {number}
14+
*/
15+
var countPairs = function(nums, k) {
16+
const frequencyMap = new Map();
17+
let result = 0;
18+
19+
nums.forEach((num, index) => {
20+
if (frequencyMap.has(num)) {
21+
const values = frequencyMap.get(num);
22+
values.forEach(prevIndex => {
23+
if ((prevIndex * index) % k === 0) {
24+
result++;
25+
}
26+
});
27+
values.push(index);
28+
} else {
29+
frequencyMap.set(num, [index]);
30+
}
31+
});
32+
33+
return result;
34+
};

0 commit comments

Comments
 (0)