2401. Longest Nice Subarray Medium
1/**
2 * [2401] Longest Nice Subarray
3 *
4 * You are given an array nums consisting of positive integers.
5 * We call a subarray of nums nice if the bitwise AND of every pair of elements that are in different positions in the subarray is equal to 0.
6 * Return the length of the longest nice subarray.
7 * A subarray is a contiguous part of an array.
8 * Note that subarrays of length 1 are always considered nice.
9 *
10 * Example 1:
11 *
12 * Input: nums = [1,3,8,48,10]
13 * Output: 3
14 * Explanation: The longest nice subarray is [3,8,48]. This subarray satisfies the conditions:
15 * - 3 AND 8 = 0.
16 * - 3 AND 48 = 0.
17 * - 8 AND 48 = 0.
18 * It can be proven that no longer nice subarray can be obtained, so we return 3.
19 * Example 2:
20 *
21 * Input: nums = [3,1,5,11,13]
22 * Output: 1
23 * Explanation: The length of the longest nice subarray is 1. Any subarray of length 1 can be chosen.
24 *
25 *
26 * Constraints:
27 *
28 * 1 <= nums.length <= 10^5
29 * 1 <= nums[i] <= 10^9
30 *
31 */
32pub struct Solution {}
33
34// problem: https://leetcode.com/problems/longest-nice-subarray/
35// discuss: https://leetcode.com/problems/longest-nice-subarray/discuss/?currentPage=1&orderBy=most_votes&query=
36
37// submission codes start here
38
39impl Solution {
40 pub fn longest_nice_subarray(nums: Vec<i32>) -> i32 {
41 0
42 }
43}
44
45// submission codes end
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 #[test]
52 fn test_2401() {
53 }
54}
55
Back
© 2025 bowen.ge All Rights Reserved.