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 3abfd6d

Browse files
committedMar 18, 2025
Add solution #2401
1 parent 2122809 commit 3abfd6d

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,7 @@
849849
2381|[Shifting Letters II](./2381-shifting-letters-ii.js)|Medium|
850850
2390|[Removing Stars From a String](./2390-removing-stars-from-a-string.js)|Medium|
851851
2396|[Strictly Palindromic Number](./2396-strictly-palindromic-number.js)|Medium|
852+
2401|[Longest Nice Subarray](./2401-longest-nice-subarray.js)|Medium|
852853
2413|[Smallest Even Multiple](./2413-smallest-even-multiple.js)|Easy|
853854
2425|[Bitwise XOR of All Pairings](./2425-bitwise-xor-of-all-pairings.js)|Medium|
854855
2427|[Number of Common Factors](./2427-number-of-common-factors.js)|Easy|
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 2401. Longest Nice Subarray
3+
* https://leetcode.com/problems/longest-nice-subarray/
4+
* Difficulty: Medium
5+
*
6+
* You are given an array nums consisting of positive integers.
7+
*
8+
* We call a subarray of nums nice if the bitwise AND of every pair of elements that are in
9+
* different positions in the subarray is equal to 0.
10+
*
11+
* Return the length of the longest nice subarray.
12+
*
13+
* A subarray is a contiguous part of an array.
14+
*
15+
* Note that subarrays of length 1 are always considered nice.
16+
*/
17+
18+
/**
19+
* @param {number[]} nums
20+
* @return {number}
21+
*/
22+
var longestNiceSubarray = function(nums) {
23+
let result = 1;
24+
let left = 0;
25+
let usedBits = 0;
26+
27+
for (let right = 0; right < nums.length; right++) {
28+
while ((usedBits & nums[right]) !== 0) {
29+
usedBits ^= nums[left];
30+
left++;
31+
}
32+
usedBits |= nums[right];
33+
result = Math.max(result, right - left + 1);
34+
}
35+
36+
return result;
37+
};

0 commit comments

Comments
 (0)
Please sign in to comment.