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?

  1. A. OR is always equal to the maximum element of the array.
  2. B. Each change sets at least one new bit that was not set before, and a value has only a bounded number of bits.
  3. C. OR over a growing window decreases each time.
  4. 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.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic