File tree 2 files changed +38
-0
lines changed
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 849
849
2381|[ Shifting Letters II] ( ./2381-shifting-letters-ii.js ) |Medium|
850
850
2390|[ Removing Stars From a String] ( ./2390-removing-stars-from-a-string.js ) |Medium|
851
851
2396|[ Strictly Palindromic Number] ( ./2396-strictly-palindromic-number.js ) |Medium|
852
+ 2401|[ Longest Nice Subarray] ( ./2401-longest-nice-subarray.js ) |Medium|
852
853
2413|[ Smallest Even Multiple] ( ./2413-smallest-even-multiple.js ) |Easy|
853
854
2425|[ Bitwise XOR of All Pairings] ( ./2425-bitwise-xor-of-all-pairings.js ) |Medium|
854
855
2427|[ Number of Common Factors] ( ./2427-number-of-common-factors.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments