↓Skip to main content
  1. LeetCode/

LeetCode 2401: Longest Nice Subarray

·2 mins· ·
LeetCode Medium Sliding-Window Bit-Manipulation Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-22

  • Submissions: Accepted (first try)

Approach
#

Every pair in the window ANDs to 0 ⇔ each bit is used by at most one number. So one integer used holds the OR of the window:

  • If the new number x overlaps used (used & x != 0), shrink the left until it does not.
  • When nums[left] leaves, its bits belong to it alone, so used ^= nums[left] (or &= ~) removes them.
  • Add x: used |= x.

Numbers have at most 30 bits, so a valid window has at most 30 elements.

My Solution
#

class Solution:
    def longestNiceSubarray(self, nums: List[int]) -> int:
        used = left = best = 0
        for right, x in enumerate(nums):
            while used & x:                        # bit conflict
                used ^= nums[left]                 # its bits are its own, so XOR removes them
                left += 1
            used |= x
            best = max(best, right - left + 1)
        return best

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Removing bits with -= or ^= is only correct because the window’s bits never overlap; with overlaps it would not work.

Interview Explanation
#

Pairwise AND 0 means each bit is used by at most one number, so one integer records the bits the window uses. If a new number overlaps it, I shrink the left; the leaving number’s bits are its own, so XOR removes them. Tracking the longest length gives O(n).

Related

LeetCode 930: Binary Subarrays With Sum
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 930 note: “sum exactly goal” = “sum at most goal” − “sum at most goal - 1”; or prefix sums with a hash map
LeetCode 2962: Count Subarrays Where Max Element Appears at Least K Times
·2 mins
LeetCode Medium Sliding-Window Array
LeetCode 2962 note: an “at least k” counting window: keep shrinking while the max appears k times, after which every left end in [0, left) is valid, so count += left
LeetCode 1040: Moving Stones Until Consecutive II
·3 mins
LeetCode Medium Sliding-Window Math Sorting Array
LeetCode 1040 note: max moves = total gaps minus the edge gap the first move wastes; min moves uses a length-n window holding the most stones, with a special case of 2 when n - 1 stones are already consecutive and one is far away
LeetCode 2555: Maximize Win From Two Segments
·2 mins
LeetCode Medium Sliding-Window Binary-Search Array
LeetCode 2555 note: a sliding window gives how many prizes a segment ending at each prize collects, and a running “best single segment among the first i prizes” supplies the second segment to its left
LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k
LeetCode 2271: Maximum White Tiles Covered by a Carpet
·2 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 2271 note: starting the carpet at some segment’s start is never worse; after sorting, two pointers sum the fully covered segments and count the last one partially