#3171

Find Subarray With Bitwise OR Closest to K

hard · 30.9% accepted · 205 likes · top 8%

array · binary search · bit manipulation · segment tree

⊣ practice⊣ open on leetcode ↗

Description

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.

Solution