Data Structures & AlgorithmsHard
Why does the OR of a subarray change at most about log(maximum value) times as its right end extends from a fixed start?
- A. OR is always equal to the maximum element of the array.
- B. Each change sets at least one new bit that was not set before, and a value has only a bounded number of bits.
- C. OR over a growing window decreases each time.
- D. OR is undefined for more than two numbers.
Correct answer: B. Each change sets at least one new bit that was not set before, and a value has only a bounded number of bits.
OR can only add bits as the window grows, so the value changes at most once per newly set bit. That bounds the number of distinct OR values ending at any position logarithmically.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Why is the OR of a subarray always at least the maximum element of that subarray?
- When counting triangles from sorted stick lengths with a two-pointer scan, why does fixing the largest side k and moving the pointers i <…
- Why is the inequality test for a non-degenerate triangle with sorted sides written as ai + aj > ak for the largest side ak?
- To count subarrays with sum divisible by m, why does the equal-residue method fail for a subarray count that depends on the MAXIMUM element?
- In the 'longest window with equal numbers of high and low values' problem, what turns the condition into a standard prefix-sum problem?
- When two consecutive windows of length 3 are compared, why does equal sums reduce to a single element comparison?