Description
Description
Editorial
Editorial
Solutions
Solutions
Submissions
Submissions
Hard

You are given an integer array nums.

A is called consistent if the bitwise AND of all its elements is equal to the minimum value of the subarray.

Return the number of consistent subarrays in nums.

 

Example 1:

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

Output: 6

Explanation:

The following subarrays are consistent:

SubarrayValuesBitwise ANDMinimum Value
nums[0..0][5]55
nums[1..1][3]33
nums[2..2][1]11
nums[3..3][2]22
nums[1..2][3, 1]3 AND 1 = 11
nums[0..2][5, 3, 1]5 AND 3 AND 1 = 11

Therefore, the number of consistent subarrays is 6.

Example 2:

Input: nums = [1,1,1]

Output: 6

Explanation:

For every subarray, both the bitwise AND and the minimum value are equal to 1. Therefore, the answer is 6.

Example 3:

Input: nums = [0]

Output: 1

Explanation:

The only subarray is [0], whose bitwise AND and minimum value are both 0. Therefore, the answer is 1.

 

Constraints:

  • 1 <= nums.length <= 2 * 10​​​​​​​5
  • 0 <= nums[i] <= 106
 
Code
Code
Testcase
Testcase
Test Result
Test Result