Skip to content

2401. Longest Nice Subarray #1448

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to find the longest contiguous subarray (nice subarray) where the bitwise AND of every pair of elements in different positions is zero. This means that each element in the subarray must not share any common set bits with any other element in the subarray.

Approach

  1. Bitwise Constraints: For a subarray to be nice, each bit in the binary representation of the elements can be set in at most one element. This ensures that the bitwise AND of any two elements in the subarray is zero.
  2. Sliding Window Technique: We use a sliding window approach to maintain the longest valid subarray. The window is defined by two pointers, left and right, which represent the current subarray being checked.
  3. T…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Mar 18, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Mar 18, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants