3209. Number of Subarrays With AND Value of K Hard

@problem@discussion
#Array#Binary Search#Bit Manipulation#Segment Tree



1/**
2 * [3209] Number of Subarrays With AND Value of K
3 *
4 * Given an array of integers nums and an integer k, return the number of <span data-keyword="subarray-nonempty">subarrays</span> of nums where the bitwise AND of the elements of the subarray equals k.
5 *  
6 * <strong class="example">Example 1:
7 * <div class="example-block">
8 * Input: <span class="example-io">nums = [1,1,1], k = 1</span>
9 * Output: <span class="example-io">6</span>
10 * Explanation:
11 * All subarrays contain only 1's.
12 * </div>
13 * <strong class="example">Example 2:
14 * <div class="example-block">
15 * Input: <span class="example-io">nums = [1,1,2], k = 1</span>
16 * Output: <span class="example-io">3</span>
17 * Explanation:
18 * Subarrays having an AND value of 1 are: [<u>1</u>,1,2], [1,<u>1</u>,2], [<u>1,1</u>,2].
19 * </div>
20 * <strong class="example">Example 3:
21 * <div class="example-block">
22 * Input: <span class="example-io">nums = [1,2,3], k = 2</span>
23 * Output: <span class="example-io">2</span>
24 * Explanation:
25 * Subarrays having an AND value of 2 are: [1,<u>2</u>,3], [1,<u>2,3</u>].
26 * </div>
27 *  
28 * Constraints:
29 * 
30 * 	1 <= nums.length <= 10^5
31 * 	0 <= nums[i], k <= 10^9
32 * 
33 */
34pub struct Solution {}
35
36// problem: https://leetcode.com/problems/number-of-subarrays-with-and-value-of-k/
37// discuss: https://leetcode.com/problems/number-of-subarrays-with-and-value-of-k/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41impl Solution {
42    pub fn count_subarrays(nums: Vec<i32>, k: i32) -> i64 {
43        
44    }
45}
46
47// submission codes end
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_3209() {
55    }
56}
57


Back
© 2025 bowen.ge All Rights Reserved.