3171. Find Subarray With Bitwise OR Closest to K LeetCode Solution
In this guide, you will get 3171. Find Subarray With Bitwise OR Closest to K LeetCode Solution with the best time and space complexity. The solution to Find Subarray With Bitwise OR Closest to K problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find Subarray With Bitwise OR Closest to K solution in C++
- Find Subarray With Bitwise OR Closest to K solution in Java
- Find Subarray With Bitwise OR Closest to K solution in Python
- Additional Resources
Problem Statement of Find Subarray With Bitwise OR Closest to K
You are given an array nums and an integer k. You need to find a subarray of nums such that the absolute difference between k and the bitwise OR of the subarray elements is as small as possible. In other words, select a subarray nums[l..r] such that |k – (nums[l] OR nums[l + 1] … OR nums[r])| is minimum.
Return the minimum possible value of the absolute difference.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,2,4,5], k = 3
Output: 0
Explanation:
The subarray nums[0..1] has OR value 3, which gives the minimum absolute difference |3 – 3| = 0.
Example 2:
Input: nums = [1,3,1,3], k = 2
Output: 1
Explanation:
The subarray nums[1..1] has OR value 3, which gives the minimum absolute difference |3 – 2| = 1.
Example 3:
Input: nums = [1], k = 10
Output: 9
Explanation:
There is a single subarray with OR value 1, which gives the minimum absolute difference |10 – 1| = 9.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
1 <= k <= 109
Complexity Analysis
- Time Complexity: O(n\log\max(\texttt{nums}))
- Space Complexity: O(n)
3171. Find Subarray With Bitwise OR Closest to K LeetCode Solution in C++
class Solution {
public:
// Similar to 1521. Find a Value of a Mysterious Function Closest to Target
int minimumDifference(vector<int>& nums, int k) {
int ans = INT_MAX;
// all the values of subarrays that end in the previous number
unordered_set<int> prev;
for (const int num : nums) {
unordered_set<int> next{num};
// Extend each subarray that ends in the previous number. Due to
// monotonicity of the OR operation, the size of `next` will be at most
// num.bit_count() + 1.
for (const int val : prev)
next.insert(val | num);
for (const int val : next)
ans = min(ans, abs(k - val));
prev = std::move(next);
}
return ans;
}
};
/* code provided by PROGIEZ */
3171. Find Subarray With Bitwise OR Closest to K LeetCode Solution in Java
class Solution {
// Similar to 1521. Find a Value of a Mysterious Function Closest to Target
public int minimumDifference(int[] nums, int k) {
int ans = Integer.MAX_VALUE;
// all the values of subarrays that end in the previous number
Set<Integer> prev = new HashSet<>();
for (final int num : nums) {
HashSet<Integer> next = new HashSet<>(Arrays.asList(num));
// Extend each subarray that ends in the previous number. Due to
// monotonicity of the OR operation, the size of `next` will be at most
// Integer.bitCount(num) + 1.
for (final int val : prev)
next.add(val | num);
for (final int val : next)
ans = Math.min(ans, Math.abs(k - val));
prev = next;
}
return ans;
}
}
// code provided by PROGIEZ
3171. Find Subarray With Bitwise OR Closest to K LeetCode Solution in Python
class Solution:
# Similar to 1521. Find a Value of a Mysterious Function Closest to Target
def minimumDifference(self, nums: list[int], k: int) -> int:
ans = math.inf
dp = set() # all the values of subarrays that end in the current number
for num in nums:
# Extend each subarray that ends in the dpious number. Due to
# monotonicity of the OR operation, the size of `next_set` will be at most
# bin(num).count('1') + 1.
dp = {num} | {val | num for val in dp}
ans = min(ans, min(abs(k - val) for val in dp))
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.