2419. Longest Subarray With Maximum Bitwise AND Medium
1/**
2 * [2419] Longest Subarray With Maximum Bitwise AND
3 *
4 * You are given an integer array nums of size n.
5 * Consider a non-empty subarray from nums that has the maximum possible bitwise AND.
6 *
7 * In other words, let k be the maximum value of the bitwise AND of any subarray of nums. Then, only subarrays with a bitwise AND equal to k should be considered.
8 *
9 * Return the length of the longest such subarray.
10 * The bitwise AND of an array is the bitwise AND of all the numbers in it.
11 * A subarray is a contiguous sequence of elements within an array.
12 *
13 * Example 1:
14 *
15 * Input: nums = [1,2,3,3,2,2]
16 * Output: 2
17 * Explanation:
18 * The maximum possible bitwise AND of a subarray is 3.
19 * The longest subarray with that value is [3,3], so we return 2.
20 *
21 * Example 2:
22 *
23 * Input: nums = [1,2,3,4]
24 * Output: 1
25 * Explanation:
26 * The maximum possible bitwise AND of a subarray is 4.
27 * The longest subarray with that value is [4], so we return 1.
28 *
29 *
30 * Constraints:
31 *
32 * 1 <= nums.length <= 10^5
33 * 1 <= nums[i] <= 10^6
34 *
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/longest-subarray-with-maximum-bitwise-and/
39// discuss: https://leetcode.com/problems/longest-subarray-with-maximum-bitwise-and/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44 pub fn longest_subarray(nums: Vec<i32>) -> i32 {
45 0
46 }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_2419() {
57 }
58}
59
Back
© 2025 bowen.ge All Rights Reserved.