Ctrl K

Shortest Full-OR Stretch From Each Start

Medium
You are given an array values of n non-negative integers. For every index i, consider the stretches that start at i and run to the right: values[i..j]. Their bitwise OR never decreases as j grows and peaks once the stretch reaches the end of the array, but that same peak is often reached earlier. Return an array of length n whose entry i is the length of the shortest non-empty stretch starting at i whose bitwise OR equals the largest OR of any stretch starting at i.
Example 1
values=[4, 1, 0, 2, 6] → [4, 4, 3, 2, 1]

From index 0 the OR to the end is 7, and all three bits are only collected by the element 2, so the length is 4. From index 1 the bit 4 only shows up in the last element, so 4 again. Then [0, 2, 6] gives 3, [2, 6] gives 2 and [6] gives 1.

Example 2
values=[0, 0] → [1, 1]

The best OR from any start is 0, which a single element already gives.

Example 3
values=[5, 3] → [2, 1]
Constraints
  • 1 ≤ n ≤ 10⁵
  • 0 ≤ values[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShortest Stretch to the Goal MediumSpot the Extra Copy MediumNearest k Values MediumWindows With a Small Product MediumLongest Shared Run Medium
loading Python…
Input
values=[4, 1, 0, 2, 6]
Expected
[4, 4, 3, 2, 1]

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