Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2ee0e4b

Browse files
authoredMar 18, 2025··
2401_Longest_Nice_Subarray.java
1 parent cb22feb commit 2ee0e4b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
 

‎JAVA/2401_Longest_Nice_Subarray.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Problem No: 2401
2+
3+
// Longest Nice Subarray
4+
5+
class Solution {
6+
public int longestNiceSubarray(int[] nums) {
7+
int ans = 0;
8+
int used = 0;
9+
10+
for (int l = 0, r = 0; r < nums.length; ++r) {
11+
while ((used & nums[r]) > 0)
12+
used ^= nums[l++];
13+
used |= nums[r];
14+
ans = Math.max(ans, r - l + 1);
15+
}
16+
17+
return ans;
18+
}
19+
}

0 commit comments

Comments
 (0)
Please sign in to comment.