|
| 1 | +--- |
| 2 | +id: Find-Subarray-With-Bitwise-OR-Closest-to-K |
| 3 | +title: Find Subarray With Bitwise OR Closest to K (LeetCode) |
| 4 | +level: hard |
| 5 | +sidebar_label: 3171-Find Subarray With Bitwise OR Closest to K |
| 6 | +tags: |
| 7 | + - Dynamic Programming |
| 8 | + - Bit Manipulation |
| 9 | + - Sliding Window |
| 10 | + - Hash Set |
| 11 | +description: This description is for the solution of Find Subarray With Bitwise OR Closest to K |
| 12 | +sidebar_position: 3171 |
| 13 | +--- |
| 14 | + |
| 15 | +## Problem statement: |
| 16 | + |
| 17 | +Given an array of integers `arr` and an integer `k`, return the maximum possible bitwise OR of a subarray of `arr` that is closest to `k`. |
| 18 | + |
| 19 | +The bitwise OR of an array of numbers is defined as `a1 | a2 | ... | an`, where `|` denotes the bitwise OR operator. |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | + |
| 23 | +Input: arr = [5,6,7,8,9], k = 10 |
| 24 | +Output: 10 |
| 25 | + |
| 26 | +Explanation: The subarray [6,7] has bitwise OR = 6 | 7 = 7, which is closest to 10. |
| 27 | + |
| 28 | +**Example 2:** |
| 29 | + |
| 30 | +Input: arr = [1,2,4,8,16], k = 32 |
| 31 | +Output: 31 |
| 32 | + |
| 33 | +Explanation: The subarray [1,2,4,8,16] has bitwise OR = 1 | 2 | 4 | 8 | 16 = 31, which is closest to 32. |
| 34 | + |
| 35 | +**Constraints:** |
| 36 | + |
| 37 | +- `1 <= arr.length <= 10^5` |
| 38 | +- `1 <= arr[i], k <= 10^9` |
| 39 | + |
| 40 | +## Solutions: |
| 41 | + |
| 42 | +### Approach 1: Sliding Window with Bitmasking |
| 43 | + |
| 44 | +```python |
| 45 | +class Solution: |
| 46 | + def closestToTarget(self, arr: List[int], k: int) -> int: |
| 47 | + ans = float('inf') |
| 48 | + current = set() |
| 49 | + |
| 50 | + for num in arr: |
| 51 | + current = {num & val for val in current} | {num} |
| 52 | + for val in current: |
| 53 | + ans = min(ans, abs(val - k)) |
| 54 | + |
| 55 | + return ans |
| 56 | +``` |
| 57 | + |
| 58 | +### Approach 2: Efficient Bitmasking with Set Operations |
| 59 | + |
| 60 | +```python |
| 61 | +class Solution: |
| 62 | + def closestToTarget(self, arr: List[int], k: int) -> int: |
| 63 | + ans = float('inf') |
| 64 | + current = set() |
| 65 | + next_set = set() |
| 66 | + |
| 67 | + for num in arr: |
| 68 | + next_set = {num & val for val in current} | {num} |
| 69 | + for val in next_set: |
| 70 | + ans = min(ans, abs(val - k)) |
| 71 | + current = next_set |
| 72 | + |
| 73 | + return ans |
| 74 | +``` |
| 75 | + |
| 76 | +### Approach 3: Optimized Sliding Window with Prefix Bitmasking |
| 77 | + |
| 78 | +```python |
| 79 | +class Solution: |
| 80 | + def closestToTarget(self, arr: List[int], k: int) -> int: |
| 81 | + ans = float('inf') |
| 82 | + current = set() |
| 83 | + |
| 84 | + for num in arr: |
| 85 | + new_current = {num} |
| 86 | + for val in current: |
| 87 | + new_current.add(val | num) |
| 88 | + current = new_current |
| 89 | + for val in current: |
| 90 | + ans = min(ans, abs(val - k)) |
| 91 | + |
| 92 | + return ans |
| 93 | +``` |
| 94 | +## Complexity Analysis: |
| 95 | + |
| 96 | +### Approach 1: Sliding Window with Bitmasking |
| 97 | + |
| 98 | +- **Time Complexity:** $O(n⋅log(max_element))$ |
| 99 | + - Where \( n \) is the number of elements in the array `arr`, and `max_element` is the maximum element in `arr`. |
| 100 | + - The logarithmic factor comes from the bitwise operations and set operations involved. |
| 101 | + |
| 102 | +- **Space Complexity:** $O(log(max_element))$ |
| 103 | + - Space used by the set `current` to store intermediate bitwise OR values. |
| 104 | + |
| 105 | +### Approach 2: Efficient Bitmasking with Set Operations |
| 106 | + |
| 107 | +- **Time Complexity:** $O(n⋅log(max_element))$ |
| 108 | + - Same as Approach 1, due to similar operations on sets. |
| 109 | + |
| 110 | +- **Space Complexity:** $O(log(max_element))$ |
| 111 | + - Similar to Approach 1, space used by the sets `current` and `next_set`. |
| 112 | + |
| 113 | +### Approach 3: Optimized Sliding Window with Prefix Bitmasking |
| 114 | + |
| 115 | +- **Time Complexity:** $O(n⋅log(max_element))$ |
| 116 | + - Same as Approach 1 and 2, as it involves bitwise OR operations and set updates. |
| 117 | + |
| 118 | +- **Space Complexity:** $O(log(max_element))$ |
| 119 | + - Space used by the set `current` to store intermediate bitwise OR values. |
| 120 | + |
| 121 | +Here, `max_element` refers to the maximum element present in the array `arr`, which affects the logarithmic factor in the time and space complexities due to bitwise operations and set operations involved in each approach. |
0 commit comments