Skip to content

Commit 2fae4f8

Browse files
add 2176
1 parent d336421 commit 2fae4f8

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,4 +386,29 @@ fun majorityElement(nums: IntArray): Int {
386386
}
387387

388388
return res
389+
}
390+
391+
/**
392+
* 2176. Count Equal and Divisible Pairs in an Array
393+
*/
394+
395+
fun countPairs(nums: IntArray, k: Int): Int {
396+
if (nums.size < 2) return 0
397+
398+
val store = mutableMapOf<Int, MutableList<Int>>()
399+
var count = 0
400+
401+
for (i in nums.indices) {
402+
val num = nums[i]
403+
if (store.contains(num)) {
404+
val indexed = store.getOrDefault(num, mutableListOf())
405+
for (index in indexed) {
406+
if ((index * i) % k == 0) count++
407+
}
408+
indexed.add(i)
409+
store[num] = indexed
410+
} else store[num] = mutableListOf(i)
411+
}
412+
413+
return count
389414
}

0 commit comments

Comments
 (0)