-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubarray_or.py
151 lines (124 loc) · 3 KB
/
subarray_or.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# -----------------------------------------------
# Author : Mohit Chaudhari
# Created Date : 08/07/21
# -----------------------------------------------
# ***** SUB ARRAY OR *****
# Problem Description
#
# Given an array of integers A of size N.
#
# Value of a subarray is defined as BITWISE OR of all elements in it.
#
# Return the sum of Value of all subarrays of A % 109 + 7.
#
#
#
# Problem Constraints
# 1 <= N <= 105
#
# 1 <= A[i] <= 108
#
#
#
# Input Format
# The first argument given is the integer array A.
#
#
#
# Output Format
# Return the sum of Value of all subarrays of A % 109 + 7.
#
#
#
# Example Input
# Input 1:
#
# A = [1, 2, 3, 4, 5]
# Input 2:
#
# A = [7, 8, 9, 10]
#
#
# Example Output
# Output 1:
#
# 71
# Output 2:
#
# 110
#
#
# Example Explanation
# Explanation 1:
#
# Value([1]) = 1
# Value([1, 2]) = 3
# Value([1, 2, 3]) = 3
# Value([1, 2, 3, 4]) = 7
# Value([1, 2, 3, 4, 5]) = 7
# Value([2]) = 2
# Value([2, 3]) = 3
# Value([2, 3, 4]) = 7
# Value([2, 3, 4, 5]) = 7
# Value([3]) = 3
# Value([3, 4]) = 7
# Value([3, 4, 5]) = 7
# Value([4]) = 4
# Value([4, 5]) = 5
# Value([5]) = 5
# Sum of all these values = 71
# Explanation 2:
#
# Sum of value of all sub array is 110.
from math import log2
class Solution:
# @param A : list of integers
# @return an integer
def solve(self, A):
n = len(A)
# Find max element of the array
max_element = max(A)
# Find the max bit position set in
# the array
maxBit = int(log2(max_element)) + 1
totalSubarrays = n * (n + 1) // 2
s = 0
# Traverse from 1st bit to last bit which
# can be set in any element of the array
for i in range(maxBit):
c1 = 0
# List to store indexes of the array
# with i-th bit not set
vec = []
sum = 0
# Traverse the array
for j in range(n):
# Check if ith bit is not set in A[j]
a = A[j] >> i
if (not (a & 1)):
vec.append(j)
# Variable to store count of subarrays
# whose bitwise OR will have i-th bit
# not set
cntSubarrNotSet = 0
cnt = 1
for j in range(1, len(vec)):
if (vec[j] - vec[j - 1] == 1):
cnt += 1
else:
cntSubarrNotSet += cnt * (cnt + 1) // 2
cnt = 1
# For last element of vec
cntSubarrNotSet += cnt * (cnt + 1) // 2
# If vec is empty then cntSubarrNotSet
# should be 0 and not 1
if len(vec) == 0:
cntSubarrNotSet = 0
# Variable to store count of subarrays
# whose bitwise OR will have i-th bit set
cntSubarrIthSet = totalSubarrays - cntSubarrNotSet
s += cntSubarrIthSet * pow(2, i)
return s % (10 ** 9 + 7)
s = Solution()
print(s.solve([1, 2, 3, 4, 5]))
# OUTPUT: 71