Bitwise ORs of Subarrays LeetCode solution in Python

1
class Solution:
2
def subarrayBitwiseORs(self, arr: List[int]) -> int:
3

4
ans = set(arr)
5

6
# each element is a subarry
7

8
one = set()
9

10
# to get the ans for the subarray of size >1
11
# starting from 0th element to the ending element
12

13
one.add(arr[0])
14

15
for i in range(1, len(arr)):
16

17
two = set()
18

19
for j in one:
20

21
two.add(j | arr[i])
22

23
# subarray from the element in one set to the current ele(i th one)
24

25
ans.add(j | arr[i])
26

27
two.add(arr[i])
28

29
# adding curr element to set two so that from next iteration we can take sub array starting from curr element
30

31
one = two
32

33
return len(ans)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0