kt: add «1863. Sum of All Subset XOR Totals»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
1ef3050db6
commit
928354c370
1 changed files with 18 additions and 0 deletions
18
kt/sum-of-all-subset-xor-totals.kt
Normal file
18
kt/sum-of-all-subset-xor-totals.kt
Normal file
|
@ -0,0 +1,18 @@
|
|||
class Solution {
|
||||
private fun xorSum(
|
||||
nums: IntArray,
|
||||
i: Int,
|
||||
sum: Int,
|
||||
): Int {
|
||||
if (i == nums.size) {
|
||||
return sum
|
||||
}
|
||||
|
||||
val including = xorSum(nums, i + 1, sum xor nums[i])
|
||||
val excluding = xorSum(nums, i + 1, sum)
|
||||
|
||||
return including + excluding
|
||||
}
|
||||
|
||||
fun subsetXORSum(nums: IntArray): Int = xorSum(nums, 0, 0)
|
||||
}
|
Loading…
Reference in a new issue