leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 4124df9850734fcc79b3473bc726a67b5b301e4f
parent 3be481987589516e81b9b589226088d7a54113e5
Author: Jack Sangdahl <jack@pngu.org>
Date:   Tue, 20 Jan 2026 17:41:34 -0700

3315. construct the minimum bitwse array 2

Diffstat:
Aconstruct_min_bitwise_arr2.c | 38++++++++++++++++++++++++++++++++++++++
Aconstruct_min_bitwise_arr2.py | 31+++++++++++++++++++++++++++++++
2 files changed, 69 insertions(+), 0 deletions(-)

diff --git a/construct_min_bitwise_arr2.c b/construct_min_bitwise_arr2.c @@ -0,0 +1,38 @@ +// 3315. Construct the Minimum Bitwise Array II +#include "leetcode.h" + +/* + * you are given an array 'nums' consisting of 'n' prime integers. you need to + * construct an array 'ans' of length 'n' such that for each index 'i', the + * bitwise or of 'ans[i]' and 'ans[i + 1]' is equal to 'nums[i]'. additionally, + * you must minimize each value 'ans[i]' in the resulting array. if it is not + * possible to find such a value for 'ans[i]' that satisfies the condition, then + * set 'ans[i] = -1'. + */ + +int *minBitwiseArray(int *nums, int numsSize, int *returnSize) { + int *ans = (int *)malloc(numsSize * sizeof(int)); + *returnSize = numsSize; + for (int i = 0; i < numsSize; i++) { + int n = nums[i]; + if (n != 2) + ans[i] = n - ((n + 1) & (-n - 1)) / 2; + else + ans[i] = -1; + } + return ans; +} + +int main() { + int n1[] = {2, 3, 5, 7}, n2[] = {11, 13, 31}; + int rs1, *mba1 = minBitwiseArray(n1, ARRAY_SIZE(n1), &rs1); + int rs2, *mba2 = minBitwiseArray(n2, ARRAY_SIZE(n2), &rs2); + for (int i = 0; i < rs1; i++) + printf("%d ", mba1[i]); // expect: -1 1 4 3 + printf("\n"); + for (int i = 0; i < rs2; i++) + printf("%d ", mba2[i]); // expect: 9 12 15 + printf("\n"); + free(mba1); + free(mba2); +} diff --git a/construct_min_bitwise_arr2.py b/construct_min_bitwise_arr2.py @@ -0,0 +1,31 @@ +# 3315. Construct the Minimum Bitwise Array II + +""" +you are given an array 'nums' consisting of 'n' prime integers. you need to +construct an array 'ans' of length 'n' such that for each index 'i', the +bitwise or of 'ans[i]' and 'ans[i + 1]' is equal to 'nums[i]'. additionally, +you must minimize each value 'ans[i]' in the resulting array. if it is not +possible to find such a value for 'ans[i]' that satisfies the condition, then +set 'ans[i] = -1'. +""" + + +class Solution(object): + def minBitwiseArray(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + ans = [] + for i in nums: + if i != 2: + ans.append(i - ((i + 1) & (-i - 1)) // 2) + else: + ans.append(-1) + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.minBitwiseArray(nums=[2, 3, 5, 7])) + print(obj.minBitwiseArray(nums=[11, 13, 31]))