798. Number of Subarrays with Or less than K

Given an integer array nums and an integer k, return the number of subarrays whose bitwise OR of elements is less than or equal to k.

Example 1:

Input: nums = [2, 3, 3, 2, 9, 2, 5], k = 4

Output: 11

Explanation: The satisfying subarrays are: [2], [2, 3], [2, 3, 3], [2, 3, 3, 2], [3], [3, 3], [3, 3, 2], [3], [3, 2], [2], [2].

Note that the subarrays all have distinct left and right boundary indexes.

Example 2:

Input: nums = [9, 2, 2, 5, 8, 6], k = 3

Output: 3

Explanation: The satisfying subarrays are: [2], [2, 2], [2].

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 104
  • 0 <= nums[i] <= 104
  • 0 <= k <= 104
0
class Solution {
public:
long long SubarrayOrLessThanKCount(vector<int>& nums, int k) {
 
}
};
Test Case

Input:

K
Nums