Ctrl K

Merge Neighbouring Flags

Easy
Each number in the list flags is a set of enabled settings packed into bits: a one in the binary form means the matching setting is on. For every pair of neighbouring items, build the merged set, where a setting is on if it is on in at least one of the two neighbours. That is the bitwise OR (|). Return a list of length len(flags) - 1 whose item at position i is flags[i] | flags[i + 1].
Example 1
flags=[2, 5, 8] → [7, 13]

2 | 5 = 0b010 | 0b101 = 0b111 = 7, and 5 | 8 = 0b0101 | 0b1000 = 0b1101 = 13.

Example 2
flags=[6, 6] → [6]

One pair of neighbours, and 6 | 6 = 6.

Example 3
flags=[0, 9, 0, 1] → [9, 9, 1]

0 | 9 = 9, 9 | 0 = 9, 0 | 1 = 1.

Constraints
  • 2 ≤ len(flags) ≤ 100
  • 0 ≤ flags[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsThe Unpaired Value EasyThe Lost Ticket EasyDoubled Tag, Lost Tag EasyMirror and Negate a Bitmap EasyMultiple of Five at Every Step Easy
loading Python…
Input
flags=[2, 5, 8]
Expected
[7, 13]

Run checks the first 3 tests in your browser. Submit checks all 107 tests on the server, hidden ones included.