From f29fb46aa0e7431a7d5a51e57d79000a118fa959 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Thu, 7 Nov 2024 22:11:06 +0100 Subject: [PATCH] =?UTF-8?q?kt:=20add=20=C2=AB2275.=20Largest=20Combination?= =?UTF-8?q?=20With=20Bitwise=20AND=20Greater=20Than=20Zero=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/ Signed-off-by: Matej Focko --- ...tion-with-bitwise-and-greater-than-zero.kt | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 kt/largest-combination-with-bitwise-and-greater-than-zero.kt diff --git a/kt/largest-combination-with-bitwise-and-greater-than-zero.kt b/kt/largest-combination-with-bitwise-and-greater-than-zero.kt new file mode 100644 index 0000000..1239b22 --- /dev/null +++ b/kt/largest-combination-with-bitwise-and-greater-than-zero.kt @@ -0,0 +1,22 @@ +class Solution { + private val maxBits = 24 + + private data class State(val counters: IntArray) { + fun update(num: Int): State { + counters.indices.fold(num) { num, i -> + if (num % 2 != 0) { + counters[i] += 1 + } + + num.shr(1) + } + + return this + } + } + + fun largestCombination(candidates: IntArray): Int = + candidates.fold(State(IntArray(maxBits))) { acc, it -> + acc.update(it) + }.counters.max() +}