Minimum Bitwise OR
You are given a 2D integer array, grid, of size m x n.
Your task is to select exactly one integer from each row of the grid such that the bitwise OR of all selected integers is minimized. Return this minimum possible bitwise OR value.
Example 1:
Input: grid = [[10, 2], [5, 4]]
Output: 6
Explanation: If we choose 10 from the first row and 4 from the second row, the bitwise OR is 14. If we choose 2 and 5, the OR is 7. To completely minimize the final result, we should strategically select 2 (binary 0010) from the first row and 4 (binary 0100) from the second row. Their bitwise OR merges flawlessly without cascading into higher bits: 0010 | 0100 = 0110, which equals exactly 6, the absolute minimum possible value.
Example 2:
Input: grid = [[1, 3], [2, 3], [4, 3]]
Output: 3
Explanation: A naive approach might be to greedily pick the smallest absolute numbers from each row: 1, 2, and 4. However, because they occupy completely different bit positions, their bitwise OR evaluates to a higher number: 1 | 2 | 4 = 7. A much more optimal strategy is to recognize that the number 3 is present in every single row. By uniformly selecting 3 (binary 0011) from the first, second, and third rows, the accumulated bitwise OR remains perfectly contained within those specific active bits: 3 | 3 | 3 = 3.
Now Your Turn!
Pick the correct output for the given inputInput: grid = [[1, 2], [1, 2], [1, 2]]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= m == grid.length <= 105
- 1 <= n == grid[i].length <= 105
- m * n <= 105
- 1 <= grid[i][j] <= 105