File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
contest/src/main/java/com/github/contest/hashTable Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -365,4 +365,50 @@ fun countGood(nums: IntArray, k: Int): Long {
365365private fun IntArray.hasSingle () = when {
366366 this .size == 1 -> true
367367 else -> false
368+ }
369+
370+
371+ /* *
372+ * 169. Majority Element
373+ */
374+
375+ fun majorityElement (nums : IntArray ): Int {
376+ var majority = 0
377+ var res = 0
378+ val target = nums.size / 2
379+
380+ nums.forEach {
381+ if (majority == 0 ) res = it
382+ majority + = when {
383+ it == res -> 1
384+ else -> - 1
385+ }
386+ }
387+
388+ 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
368414}
You can’t perform that action at this time.
0 commit comments