Tap here to switch tabs
Problem
Submissions

Problem: Find a Value of a Mysterious Function Closest to Target

Find a Value of a Mysterious Function Closest to Target asks you to examine every contiguous subarray, bitwise AND its values, and return the smallest absolute difference from the target.

Statement

Given an integer array arr and an integer target.

Consider a function that calculates the bitwise AND of all elements in a contiguous subarray from index l to index r (inclusive), where 0≤l≤r<0 \leq l \leq r < arr.length.

Return the minimum absolute difference between the bitwise AND value of any contiguous subarray and target. In other words, find the minimum value of |( ext{arr}[l] ext{ \\& } ext{arr}[l + 1] ext{ \\& } \dots ext{ \\& } ext{arr}[r]) - ext{target}| across all valid pairs (l,r)(l, r).

Constraints:

  • 1≤1 \leq arr.length ≤105\leq 10^5

  • 1≤1 \leq arr[i] ≤106\leq 10^6

  • 0≤0 \leq target ≤107\leq 10^7

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Find a Value of a Mysterious Function Closest to Target

Find a Value of a Mysterious Function Closest to Target asks you to examine every contiguous subarray, bitwise AND its values, and return the smallest absolute difference from the target.

Statement

Given an integer array arr and an integer target.

Consider a function that calculates the bitwise AND of all elements in a contiguous subarray from index l to index r (inclusive), where 0≤l≤r<0 \leq l \leq r < arr.length.

Return the minimum absolute difference between the bitwise AND value of any contiguous subarray and target. In other words, find the minimum value of |( ext{arr}[l] ext{ \\& } ext{arr}[l + 1] ext{ \\& } \dots ext{ \\& } ext{arr}[r]) - ext{target}| across all valid pairs (l,r)(l, r).

Constraints:

  • 1≤1 \leq arr.length ≤105\leq 10^5

  • 1≤1 \leq arr[i] ≤106\leq 10^6

  • 0≤0 \leq target ≤107\leq 10^7