diff --git a/src/main/java/com/github/dkoval/leetcode/challenge/CountNumberOfMaximumBitwiseORSubsets.java b/src/main/java/com/github/dkoval/leetcode/challenge/CountNumberOfMaximumBitwiseORSubsets.java new file mode 100644 index 00000000..fb38fcc5 --- /dev/null +++ b/src/main/java/com/github/dkoval/leetcode/challenge/CountNumberOfMaximumBitwiseORSubsets.java @@ -0,0 +1,50 @@ +package com.github.dkoval.leetcode.challenge; + +/** + * Count Number of Maximum Bitwise-OR Subsets + *

+ * Given an integer array nums, find the maximum possible bitwise OR of a subset of nums and return the number of different + * non-empty subsets with the maximum bitwise OR. + *

+ * An array a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b. + * Two subsets are considered different if the indices of the elements chosen are different. + *

+ * The bitwise OR of an array a is equal to a[0] OR a[1] OR ... OR a[a.length - 1] (0-indexed). + *

+ * Constraints: + *

+ */ +public interface CountNumberOfMaximumBitwiseORSubsets { + + int countMaxOrSubsets(int[] nums); + + class CountNumberOfMaximumBitwiseORSubsetsRev1 implements CountNumberOfMaximumBitwiseORSubsets { + + @Override + public int countMaxOrSubsets(int[] nums) { + // max bitwise-or + int target = 0; + for (int x : nums) { + target |= x; + } + // DP top-down; caching is not needed due to light constraints + return calc(nums, 0, 0, target); + } + + private int calc(int[] nums, int index, int current, int target) { + if (index == nums.length) { + return (current == target) ? 1 : 0; + } + + int count = 0; + // option #1: take nums[index] + count += calc(nums, index + 1, current | nums[index], target); + // option #2: skip nums[index] + count += calc(nums, index + 1, current, target); + return count; + } + } +} diff --git a/src/test/kotlin/com/github/dkoval/leetcode/challenge/CountNumberOfMaximumBitwiseORSubsetsTest.kt b/src/test/kotlin/com/github/dkoval/leetcode/challenge/CountNumberOfMaximumBitwiseORSubsetsTest.kt new file mode 100644 index 00000000..4f650cc0 --- /dev/null +++ b/src/test/kotlin/com/github/dkoval/leetcode/challenge/CountNumberOfMaximumBitwiseORSubsetsTest.kt @@ -0,0 +1,50 @@ +package com.github.dkoval.leetcode.challenge + +import com.github.dkoval.leetcode.challenge.CountNumberOfMaximumBitwiseORSubsets.CountNumberOfMaximumBitwiseORSubsetsRev1 +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.extension.ExtensionContext +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.Arguments +import org.junit.jupiter.params.provider.ArgumentsProvider +import org.junit.jupiter.params.provider.ArgumentsSource +import java.util.stream.Stream + +internal class CountNumberOfMaximumBitwiseORSubsetsTest { + + class InputArgumentsProvider : ArgumentsProvider { + + override fun provideArguments(context: ExtensionContext): Stream = Stream.of( + Arguments.of( + intArrayOf(3, 1), + 2 + ), + Arguments.of( + intArrayOf(2, 2, 2), + 7 + ), + Arguments.of( + intArrayOf(3, 2, 1, 5), + 6 + ) + ) + } + + @Nested + inner class CountNumberOfMaximumBitwiseORSubsetsRev1Test { + + @ParameterizedTest + @ArgumentsSource(InputArgumentsProvider::class) + fun `should return the number of different non-empty subsets with the maximum bitwise OR`( + nums: IntArray, + expected: Int + ) { + CountNumberOfMaximumBitwiseORSubsetsRev1().test(nums, expected) + } + } +} + +private fun CountNumberOfMaximumBitwiseORSubsets.test(nums: IntArray, expected: Int) { + val actual = countMaxOrSubsets(nums) + assertEquals(expected, actual) +}