|
| 1 | +# Longest Subarray with Maximum Bitwise AND - Problem #2412 |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | +You are given an array `nums` of non-negative integers. |
| 5 | + |
| 6 | +We define the bitwise AND of a subarray as the bitwise AND of all the numbers in the subarray. |
| 7 | + |
| 8 | +For example, the bitwise AND of the subarray `[3,5,2,1]` is equal to `3 & 5 & 2 & 1 = 0`. |
| 9 | + |
| 10 | +A subarray is a contiguous part of an array. |
| 11 | + |
| 12 | +Return the length of the longest subarray with bitwise AND equal to `k`. |
| 13 | + |
| 14 | +Note that subarrays of length 1 have a bitwise AND equal to the single element. |
| 15 | + |
| 16 | +## Examples |
| 17 | +``` |
| 18 | +Input: nums = [1,2,3,3,2,2], k = 2 |
| 19 | +Output: 3 |
| 20 | +Explanation: The subarray [3,3,2] has bitwise AND equal to 2. |
| 21 | +
|
| 22 | +Input: nums = [1,2,3,4], k = 1 |
| 23 | +Output: 1 |
| 24 | +Explanation: The subarray [1] has bitwise AND equal to 1. |
| 25 | +
|
| 26 | +Input: nums = [1,2,3,4], k = 0 |
| 27 | +Output: 4 |
| 28 | +Explanation: The bitwise AND of the entire array [1,2,3,4] is 0. |
| 29 | +``` |
| 30 | + |
| 31 | +## Approach |
| 32 | +**Key Insight**: The bitwise AND of a subarray can never be greater than the minimum element in that subarray. This is because bitwise AND preserves the minimum value among all elements. |
| 33 | + |
| 34 | +**Algorithm**: |
| 35 | +1. For each starting position in the array, compute the bitwise AND of all possible subarrays starting from that position |
| 36 | +2. Keep track of the longest subarray where the bitwise AND equals `k` |
| 37 | +3. Use a sliding window approach to efficiently compute bitwise AND values |
| 38 | + |
| 39 | +**Why this works**: |
| 40 | +- We need to find the longest contiguous sequence where the bitwise AND of all elements equals `k` |
| 41 | +- We can use a sliding window approach where we maintain the current bitwise AND value |
| 42 | +- When we add a new element, we update the bitwise AND by performing AND with the new element |
| 43 | + |
| 44 | +## Complexity Analysis |
| 45 | +- **Time Complexity**: O(n²) - We need to check all possible subarrays |
| 46 | +- **Space Complexity**: O(1) - Only using a few variables |
| 47 | + |
| 48 | +## Key Insights |
| 49 | +- Bitwise AND of a subarray ≤ minimum element in that subarray |
| 50 | +- We need to actually compute the bitwise AND of subarrays, not just check element values |
| 51 | +- The longest subarray with bitwise AND = k will be the longest contiguous sequence where the computed bitwise AND equals k |
| 52 | + |
| 53 | +## Alternative Approaches |
| 54 | +1. **Brute Force**: Check all possible subarrays - O(n²) time |
| 55 | +2. **Optimized Sliding Window**: Can be optimized further but still O(n²) in worst case |
| 56 | + |
| 57 | +## Solutions in Different Languages |
| 58 | + |
| 59 | +### Java |
| 60 | +```java |
| 61 | +// See solution.java |
| 62 | +class Solution { |
| 63 | + public int longestSubarray(int[] nums, int k) { |
| 64 | + int maxLength = 0; |
| 65 | + |
| 66 | + for (int i = 0; i < nums.length; i++) { |
| 67 | + int currentAnd = nums[i]; |
| 68 | + if (currentAnd == k) { |
| 69 | + maxLength = Math.max(maxLength, 1); |
| 70 | + } |
| 71 | + |
| 72 | + for (int j = i + 1; j < nums.length; j++) { |
| 73 | + currentAnd &= nums[j]; |
| 74 | + if (currentAnd == k) { |
| 75 | + maxLength = Math.max(maxLength, j - i + 1); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return maxLength; |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### JavaScript |
| 86 | +```javascript |
| 87 | +// See solution.js |
| 88 | +/** |
| 89 | + * @param {number[]} nums |
| 90 | + * @param {number} k |
| 91 | + * @return {number} |
| 92 | + */ |
| 93 | +var longestSubarray = function(nums, k) { |
| 94 | + let maxLength = 0; |
| 95 | + |
| 96 | + for (let i = 0; i < nums.length; i++) { |
| 97 | + let currentAnd = nums[i]; |
| 98 | + if (currentAnd === k) { |
| 99 | + maxLength = Math.max(maxLength, 1); |
| 100 | + } |
| 101 | + |
| 102 | + for (let j = i + 1; j < nums.length; j++) { |
| 103 | + currentAnd &= nums[j]; |
| 104 | + if (currentAnd === k) { |
| 105 | + maxLength = Math.max(maxLength, j - i + 1); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return maxLength; |
| 111 | +}; |
| 112 | +``` |
0 commit comments