Bitwise OR of Adjacent Elements - Problem

Given an array nums of length n, return an array answer of length n - 1 such that answer[i] = nums[i] | nums[i + 1] where | is the bitwise OR operation.

The bitwise OR operation combines bits from two numbers: if either bit is 1, the result bit is 1; if both bits are 0, the result bit is 0.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,2,3]
Output: [3,3]
💡 Note: For adjacent pairs: 1 | 2 = 3 (binary: 01 | 10 = 11), 2 | 3 = 3 (binary: 10 | 11 = 11)
Example 2 — Different Values
$ Input: nums = [5,3,7,1]
Output: [7,7,7]
💡 Note: 5 | 3 = 7, 3 | 7 = 7, 7 | 1 = 7. All pairs result in 7.
Example 3 — Minimum Array
$ Input: nums = [8,4]
Output: [12]
💡 Note: Only one pair: 8 | 4 = 12 (binary: 1000 | 0100 = 1100)

Constraints

  • 2 ≤ nums.length ≤ 1000
  • 0 ≤ nums[i] ≤ 109

Visualization

Tap to expand
Bitwise OR Adjacent Elements OverviewInput Array1234Bitwise OR Operation1 | 2 = 32 | 3 = 33 | 4 = 7Output Array337Length reduces from n=4 to n-1=3 elements
Understanding the Visualization
1
Input
Array of integers with adjacent pairs to combine
2
Process
Apply bitwise OR operation to each adjacent pair
3
Output
New array with OR results, length n-1
Key Takeaway
🎯 Key Insight: Bitwise OR produces 1 when either bit is 1, requiring only a single pass through adjacent pairs
Asked in
Google 15 Amazon 12 Microsoft 8
12.5K Views
Medium Frequency
~8 min Avg. Time
456 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen