Data Structures & AlgorithmsVery hard
Why can the number of distinct OR values over all subarrays of an array be small even for long arrays?
- A. OR always returns zero for long arrays.
- B. OR values are always powers of two.
- C. Each new element only adds bits, so the OR values ending at a position form a short chain of growing numbers with few changes.
- D. Repeated OR values are removed automatically by the hardware.
Correct answer: C. Each new element only adds bits, so the OR values ending at a position form a short chain of growing numbers with few changes.
The distinct ORs ending at any right end form a monotone chain where each step adds bits, so there are at most about log(max value) of them. The total number of distinct OR values stays small.
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
- In the array [2, 5, 5, 3, 5, 2] the maximum of a subarray is taken. How many contiguous subarrays have their maximum value occurring…
- What is the sum of the minimum over all contiguous subarrays of [3, 1, 2, 4] that have length at least 2?
- In a grid, 2 marks a rotten orange, 1 a fresh one and 0 an empty cell. Each minute every fresh orange adjacent (4 directions) to a rotten…
- The 0/1 grid [[1,1,0],[0,1,0],[1,0,1]] is given. Two 1-cells belong to the same island if they are adjacent horizontally or vertically…
- Two numbers are stored as linked lists of digits, least significant digit first: 9 -> 9 -> 9 and 1. Their sum is added digit by digit with…
- How many index pairs i < j in [3, 4, 2, 6, 1, 2, 12, 1] have a[i] a[j] = 12?