Skip to content

Commit ca89465

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

5 files changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Construct the Minimum Bitwise Array I (Easy)
2+
3+
**Problem ID:** 3314
4+
**Date:** 2026-01-20
5+
**Link:** https://leetcode.com/problems/construct-the-minimum-bitwise-array-i/
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 true, we can follow a systematic approach:
10+
11+
### Main Idea:
12+
The key insight is to recognize the relationship between the bitwise OR operation and the binary representation of numbers. Specifically, for a prime number `p`, the goal is to find the smallest non-negative integer `x` such that `x OR (x + 1) = p`.
13+
14+
### Steps to Solve:
15+
1. **Understanding the Condition**: The expression `x OR (x + 1)` essentially sets all bits to 1 from the least significant bit of `x` up to the first 0 bit in `x`. Therefore, to satisfy the condition, `p` must have a specific form in its binary representation.
16+
17+
2. **Iterate Through Each Prime Number**: For each prime number in the input array `nums`, determine the smallest `ans[i]`:
18+
- If `p` is even (which is only true for 2), it is impossible to find a suitable `ans[i]` since no non-negative integer can satisfy the condition. Thus, set `ans[i] = -1`.
19+
- For odd primes, we need to find the largest power of two less than or equal to `p`. This can be achieved by using bit manipulation to isolate the highest set bit.
20+
21+
3. **Calculate the Minimum Value**: For each odd prime `p`, the smallest `x` that satisfies the condition can be derived as follows:
22+
- Let `x` be the largest power of two less than `p`. This can be computed using bit manipulation: `x = p - (p & (p - 1))`.
23+
- If `x` is valid (i.e., `x < p`), then `ans[i]` can be set to `x`. Otherwise, set `ans[i] = -1`.
24+
25+
### Data Structures:
26+
- An array `ans` of the same length as `nums` to store the results.
27+
- Simple integer variables to hold intermediate calculations.
28+
29+
### Complexity:
30+
- The time complexity is O(n) where n is the length of the input array `nums`, as we need to process each element once.
31+
- The space complexity is O(n) for storing the output array `ans`.
32+
33+
By following this approach, we ensure that we efficiently construct the required output while adhering to the constraints and minimizing each value in the resultant array.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int[] constructDistancedArray(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+
int candidate = -1;
9+
for (int j = 0; j <= num; j++) {
10+
if ((j | (j + 1)) == num) {
11+
candidate = j;
12+
break;
13+
}
14+
}
15+
ans[i] = candidate;
16+
}
17+
18+
return ans;
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function constructArray(nums) {
2+
const ans = [];
3+
for (let num of nums) {
4+
let found = false;
5+
for (let i = 0; i <= num; i++) {
6+
if ((i | (i + 1)) === num) {
7+
ans.push(i);
8+
found = true;
9+
break;
10+
}
11+
}
12+
if (!found) {
13+
ans.push(-1);
14+
}
15+
}
16+
return ans;
17+
}
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
@@ -339,3 +339,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
339339
- 2026-01-17 — [Find the Largest Area of Square Inside Two Rectangles](https://leetcode.com/problems/find-the-largest-area-of-square-inside-two-rectangles/) (Medium) → `Medium/2026-01-17-3047-Find-the-Largest-Area-of-Square-Inside-Two-Rectangles`
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`
342+
- 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`

0 commit comments

Comments
 (0)