Skip to content

Practice Find a Value of a Mysterious Function Closest to Target

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

Hard#1521
LeetCode Problem

Find a Value of a Mysterious Function Closest to Target

Problem Statement

For valid indices l and r, func(arr, l, r) is the bitwise AND of every entry from arr[l] through arr[r] when l <= r. If r < l, the supplied function returns -1,000,000,000. Given arr and target, return the smallest absolute difference |func(arr, l, r) - target| over choices with 0 <= l, r < arr.length.

Source: Find a Value of a Mysterious Function Closest to Target on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
arr = [9,12,3,7,15], target = 5
Output
2

# Example 2

Input
arr = [1000000,1000000,1000000], target = 1
Output
999999

# Example 3

Input
arr = [1,2,4,8,16], target = 0
Output
0

Constraints

1 <= arr.length <= 10^5
1 <= arr[i] <= 10^6
0 <= target <= 10^7

Related LeetCode Problems

Practice more problems similar to Find a Value of a Mysterious Function Closest to Target.

Review your practice with PhantomCodeAI

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