Skip to content

Commit ebd6bc3

Browse files
chore: add LeetCode daily solution
1 parent ca89465 commit ebd6bc3

5 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Construct the Minimum Bitwise Array II (Medium)
2+
3+
**Problem ID:** 3315
4+
**Date:** 2026-01-21
5+
**Link:** https://leetcode.com/problems/construct-the-minimum-bitwise-array-ii/
6+
7+
## Approach
8+
9+
To solve the problem of constructing the minimum bitwise array `ans` such that for each index `i`, the condition `ans[i] OR (ans[i] + 1) == nums[i]` holds, we can follow a systematic approach:
10+
11+
### Main Idea:
12+
The key observation is to understand the properties of the bitwise OR operation. For two consecutive integers `x` and `x + 1`, the result of `x OR (x + 1)` will have all bits set to 1 up to the highest set bit in `x`. Therefore, the goal is to find the smallest integer `ans[i]` such that this condition is satisfied for each prime number `nums[i]`.
13+
14+
### Steps to Approach:
15+
1. **Identify the Highest Set Bit**: For each prime number `nums[i]`, determine the highest bit position that is set to 1. This can be done using bit manipulation techniques, such as repeatedly shifting right until the number becomes zero.
16+
17+
2. **Construct `ans[i]`**: Once the highest set bit position is identified, the smallest `ans[i]` can be constructed. The value of `ans[i]` should be:
18+
- `2^k - 1` where `k` is the position of the highest set bit in `nums[i]`. This ensures that `ans[i] OR (ans[i] + 1)` will yield the desired `nums[i]`.
19+
20+
3. **Check Validity**: If the calculated `ans[i]` does not satisfy the condition `ans[i] OR (ans[i] + 1) == nums[i]`, then set `ans[i]` to `-1`.
21+
22+
4. **Iterate for All Elements**: Repeat the above steps for all elements in the `nums` array to construct the final `ans` array.
23+
24+
### Data Structures:
25+
- An array `ans` of the same length as `nums` to store the results.
26+
- Basic integer variables to hold intermediate results (e.g., for bit manipulation).
27+
28+
### Complexity:
29+
- **Time Complexity**: O(n * log(max(nums))) where `n` is the length of the `nums` array and `log(max(nums))` is due to the bit manipulation needed to find the highest set bit.
30+
- **Space Complexity**: O(n) for the output array `ans`.
31+
32+
This approach efficiently constructs the required array while ensuring that the values are minimized, adhering to the constraints of the problem.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int[] constructArray(int[] nums) {
3+
int n = nums.length;
4+
int[] ans = new int[n];
5+
6+
for (int i = 0; i < n; i++) {
7+
int num = nums[i];
8+
ans[i] = -1; // Default value
9+
10+
// Check if we can find a valid ans[i]
11+
for (int j = 0; j <= num; j++) {
12+
if ((j | (j + 1)) == num) {
13+
ans[i] = j;
14+
break;
15+
}
16+
}
17+
}
18+
19+
return ans;
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function constructArray(nums) {
2+
const ans = [];
3+
4+
for (const num of nums) {
5+
let found = false;
6+
for (let i = 0; i <= num; i++) {
7+
if ((i | (i + 1)) === num) {
8+
ans.push(i);
9+
found = true;
10+
break;
11+
}
12+
}
13+
if (!found) {
14+
ans.push(-1);
15+
}
16+
}
17+
18+
return ans;
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def minimumBitwiseArray(self, nums: List[int]) -> List[int]:
3+
ans = []
4+
for num in nums:
5+
found = False
6+
for x in range(num):
7+
if x | (x + 1) == num:
8+
ans.append(x)
9+
found = True
10+
break
11+
if not found:
12+
ans.append(-1)
13+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
340340
- 2026-01-18 — [Largest Magic Square](https://leetcode.com/problems/largest-magic-square/) (Medium) → `Medium/2026-01-18-1895-Largest-Magic-Square`
341341
- 2026-01-19 — [Maximum Side Length of a Square with Sum Less than or Equal to Threshold](https://leetcode.com/problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold/) (Medium) → `Medium/2026-01-19-1292-Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold`
342342
- 2026-01-20 — [Construct the Minimum Bitwise Array I](https://leetcode.com/problems/construct-the-minimum-bitwise-array-i/) (Easy) → `Easy/2026-01-20-3314-Construct-the-Minimum-Bitwise-Array-I`
343+
- 2026-01-21 — [Construct the Minimum Bitwise Array II](https://leetcode.com/problems/construct-the-minimum-bitwise-array-ii/) (Medium) → `Medium/2026-01-21-3315-Construct-the-Minimum-Bitwise-Array-II`

0 commit comments

Comments
 (0)