Bitwise OR of Adjacent Elements - Problem
Given an array nums of integers with length n, your task is to create a new array that captures the bitwise relationships between adjacent elements.
Return an array answer of length n - 1 where each element answer[i] represents the bitwise OR operation between nums[i] and nums[i + 1].
The bitwise OR operation (|) combines bits from both numbers, resulting in 1 if either bit is 1, and 0 only when both bits are 0.
Example: If nums = [1, 2, 3], then:
• answer[0] = 1 | 2 = 3 (binary: 01 | 10 = 11)
• answer[1] = 2 | 3 = 3 (binary: 10 | 11 = 11)
• Result: [3, 3]
Input & Output
example_1.py — Basic Case
$
Input:
nums = [1, 2, 3]
›
Output:
[3, 3]
💡 Note:
1 | 2 = 3 (binary: 01 | 10 = 11), 2 | 3 = 3 (binary: 10 | 11 = 11)
example_2.py — Powers of 2
$
Input:
nums = [4, 8, 16]
›
Output:
[12, 24]
💡 Note:
4 | 8 = 12 (binary: 100 | 1000 = 1100), 8 | 16 = 24 (binary: 1000 | 10000 = 11000)
example_3.py — Minimum Length
$
Input:
nums = [5, 7]
›
Output:
[7]
💡 Note:
Only one pair possible: 5 | 7 = 7 (binary: 101 | 111 = 111)
Constraints
- 2 ≤ nums.length ≤ 105
- 0 ≤ nums[i] ≤ 109
- Array must have at least 2 elements
Visualization
Tap to expand
Understanding the Visualization
1
Initialize
Create result array of size n-1 and start with first pair
2
Process Pairs
For each adjacent pair, apply bitwise OR operation
3
Store Result
Add the OR result to the answer array
4
Continue
Move to next adjacent pair until all are processed
Key Takeaway
🎯 Key Insight: This is a simple linear scan problem - just iterate once through the array and apply the built-in OR operator to each adjacent pair. No complex algorithms needed!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code