Skip to content

Commit 8568a5a

Browse files
Add solution for LeetCode #2412: Longest Subarray with Maximum Bitwise AND
1 parent 287de99 commit 8568a5a

File tree

4 files changed

+170
-2
lines changed

4 files changed

+170
-2
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Solution for LeetCode Problem #2412: LongestSubarrayWithMaximumBitwiseAnd
2+
// Date: 2025-07-30
3+
// Difficulty: Medium
4+
// Language: Java
5+
6+
class Solution {
7+
public int longestSubarray(int[] nums, int k) {
8+
int maxLength = 0;
9+
10+
for (int i = 0; i < nums.length; i++) {
11+
int currentAnd = nums[i];
12+
if (currentAnd == k) {
13+
maxLength = Math.max(maxLength, 1);
14+
}
15+
16+
for (int j = i + 1; j < nums.length; j++) {
17+
currentAnd &= nums[j];
18+
if (currentAnd == k) {
19+
maxLength = Math.max(maxLength, j - i + 1);
20+
}
21+
}
22+
}
23+
24+
return maxLength;
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Solution for LeetCode Problem #2412: LongestSubarrayWithMaximumBitwiseAnd
2+
// Date: 2025-07-30
3+
// Difficulty: Medium
4+
// Language: JavaScript
5+
6+
/**
7+
* @param {number[]} nums
8+
* @param {number} k
9+
* @return {number}
10+
*/
11+
var longestSubarray = function(nums, k) {
12+
let maxLength = 0;
13+
14+
for (let i = 0; i < nums.length; i++) {
15+
let currentAnd = nums[i];
16+
if (currentAnd === k) {
17+
maxLength = Math.max(maxLength, 1);
18+
}
19+
20+
for (let j = i + 1; j < nums.length; j++) {
21+
currentAnd &= nums[j];
22+
if (currentAnd === k) {
23+
maxLength = Math.max(maxLength, j - i + 1);
24+
}
25+
}
26+
}
27+
28+
return maxLength;
29+
};

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This repository is created for learning and educational purposes. The goal is to
1515

1616
| Date | Problem ID | Problem Title | Difficulty | Folder Link |
1717
|------|------------|---------------|------------|-------------|
18+
| 2025-07-30 | 2412 | Longest Subarray with Maximum Bitwise AND | Medium | [Link](Medium/2025-07-30-2412-LongestSubarrayWithMaximumBitwiseAnd/) |
1819
| 2025-07-29 | 2411 | Smallest Subarrays With Maximum Bitwise OR | Medium | [Link](Medium/2025-07-29-2411-SmallestSubarraysWithMaximumBitwiseOR/) |
1920
| 2025-07-29 | 2044 | Count Number of Maximum Bitwise OR Subsets | Medium | [Link](Medium/2025-07-29-2044-CountNumberOfMaximumBitwiseORSubsets/) |
2021

@@ -43,9 +44,9 @@ YYYY-MM-DD-ProblemID-Title/
4344

4445
## Statistics
4546

46-
- **Total Problems Solved**: 2
47+
- **Total Problems Solved**: 3
4748
- **Easy**: 0
48-
- **Medium**: 2
49+
- **Medium**: 3
4950
- **Hard**: 0
5051

5152
## Getting Started

0 commit comments

Comments
 (0)