-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsolution.java
More file actions
26 lines (22 loc) · 726 Bytes
/
solution.java
File metadata and controls
26 lines (22 loc) · 726 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Solution for LeetCode Problem #2412: LongestSubarrayWithMaximumBitwiseAnd
// Date: 2025-07-30
// Difficulty: Medium
// Language: Java
class Solution {
public int longestSubarray(int[] nums, int k) {
int maxLength = 0;
for (int i = 0; i < nums.length; i++) {
int currentAnd = nums[i];
if (currentAnd == k) {
maxLength = Math.max(maxLength, 1);
}
for (int j = i + 1; j < nums.length; j++) {
currentAnd &= nums[j];
if (currentAnd == k) {
maxLength = Math.max(maxLength, j - i + 1);
}
}
}
return maxLength;
}
}