hardArrayBinary SearchBit ManipulationSegment Tree 0 views

Number of Subarrays With AND Value of K

Given an array of integers nums and an integer k, return the number of subarrays of nums where the bitwise AND of the elements of the subarray equals k.

Given an array of integers nums and an integer k, return the number of subarrays of nums where the bitwise AND of the elements of the subarray equals k.

Number of Subarrays With AND Value of K diagram

Example 1

Input: nums = [1,1,1], k = 1

Output: 6

Explanation: All subarrays contain only 1's.

Example 2

Input: nums = [1,1,2], k = 1

Output: 3

Explanation: Subarrays having an AND value of 1 are: [ 1 ,1,2] , [1, 1 ,2] , [ 1,1 ,2] .

Example 3

Input: nums = [1,2,3], k = 2

Output: 2

Explanation: Subarrays having an AND value of 2 are: [1, 2 ,3] , [1, 2,3 ] .

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i], k <= 10^9

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

No test cases yet.