File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed
contest/src/main/java/com/github/contest/hashTable Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments