Skip to content

Practice Find Subarray With Bitwise OR Closest to K

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3171
LeetCode Problem

Find Subarray With Bitwise OR Closest to K

Problem Statement

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.

Source: Find Subarray With Bitwise OR Closest to K on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,2,4,5], k = 3
Output
0

# Example 2

Input
nums = [1,3,1,3], k = 2
Output
1

# Example 3

Input
nums = [1], k = 10
Output
9

Constraints

1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= 10^9

Related LeetCode Problems

Practice more problems similar to Find Subarray With Bitwise OR Closest to K.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.