Skip to content

Commit ae9b26e

Browse files
authored
Create number-of-subarrays-with-and-value-of-k.cpp
1 parent b7f71d7 commit ae9b26e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Time: O(nlogr)
2+
// Space: O(logr)
3+
4+
// dp, lc3171
5+
class Solution {
6+
public:
7+
long long countSubarrays(vector<int>& nums, int k) {
8+
int64_t result = 0;
9+
unordered_map<int, int> dp;
10+
for (const auto& x : nums) {
11+
unordered_map<int, int> new_dp;
12+
if ((x & k) == k) {
13+
++new_dp[x];
14+
for (const auto& [y, c] : dp) {
15+
new_dp[y & x] += c;
16+
}
17+
if (new_dp.count(k)) {
18+
result += new_dp[k];
19+
}
20+
}
21+
dp = move(new_dp);
22+
}
23+
return result;
24+
}
25+
};

0 commit comments

Comments
 (0)