Ctrl K

Shortest Bright Window

Easy
You are given an array of non-negative integers lamps and an integer need. Call a contiguous non-empty stretch of the array bright if the bitwise OR of all its elements is at least need. Return the length of the shortest bright stretch, or -1 if there is none.
Example 1
lamps=[4, 1, 2, 5], need=7 → 2

No single value reaches 7, but the stretch [2, 5] gives 2 | 5 = 7, so the answer is 2.

Example 2
lamps=[1, 2, 4], need=7 → 3

Only the whole array gives 1 | 2 | 4 = 7, so the answer is 3.

Example 3
lamps=[3, 3], need=4 → -1

The bitwise OR of any elements is 3, which never reaches 4, so the answer is -1.

Constraints
  • 1 ≤ len(lamps) ≤ 50
  • 0 ≤ lamps[i] ≤ 10⁹
  • 0 ≤ need ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsXOR of a Close Pair EasyLight Every Lamp With a Window HardClash-Free Flag Window MediumShortest Full-OR Stretch From Each Start MediumLargest XOR of a Close Pair Hard
loading Python…
Input
lamps=[4, 1, 2, 5], need=7
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 112 tests on the server, hidden ones included.