Longest Nice Subarray - Complete Solution Guide
Longest Nice Subarray is LeetCode problem 2401, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an array nums consisting of positive integers. We call a subarray of nums nice if the bitwise AND of every pair of elements that are in different positions in the subarray is equal to 0 . Return the length of the longest nice subarray . A subarray is a contiguous part of an array. Note that subarrays of length 1 are always considered nice. Example 1: Input: nums = [1,3,8,48,10] Output: 3 Explanation: The longest nice subarray is [3,8,48]. This subarray satisfies the conditions: - 3
Detailed Explanation
The problem asks us to find the longest subarray within a given array of positive integers such that the bitwise AND of any two distinct elements within that subarray is equal to 0. A subarray is a contiguous sequence of elements within the original array. A 'nice' subarray is one that satisfies the aforementioned bitwise AND condition. A subarray of length 1 is always considered 'nice'.
Solution Approach
The solution utilizes a sliding window approach. We maintain a window defined by 'left' and 'right' pointers, representing the start and end of the current subarray. We also maintain a variable 'current_or' which holds the bitwise OR of all elements within the current window. As we iterate through the array with the 'right' pointer, we check if adding the current element at 'nums[right]' to the window violates the 'nice' property. If it does (i.e., '(current_or & nums[right]) != 0'), we shrink the window from the left by removing elements until the 'nice' property is restored. We then add 'nums[right]' to the window (by updating 'current_or') and update the maximum length of a 'nice' subarray encountered so far.
Step-by-Step Algorithm
- Step 1: Initialize 'left' to 0, 'current_or' to 0, and 'max_length' to 0. These variables will store the left boundary of the window, the bitwise OR of elements in the window, and the maximum length of a 'nice' subarray, respectively.
- Step 2: Iterate through the input array 'nums' using the 'right' pointer, starting from 0.
- Step 3: While the bitwise AND of 'current_or' and 'nums[right]' is not 0, it means that adding 'nums[right]' to the current subarray would violate the 'nice' property.
- Step 4: If the 'nice' property is violated, shrink the window from the left. Remove the leftmost element 'nums[left]' from the current subarray by XORing it with 'current_or' (current_or ^= nums[left]). Increment the 'left' pointer.
- Step 5: After shrinking the window as necessary, add the current element 'nums[right]' to the current subarray by performing a bitwise OR: 'current_or |= nums[right]'.
- Step 6: Update 'max_length' to the maximum of its current value and the length of the current subarray ('right - left + 1').
- Step 7: After iterating through all elements in 'nums', return 'max_length'.
Key Insights
- Insight 1: The bitwise AND condition implies that no two numbers in a 'nice' subarray can have the same bit set to 1.
- Insight 2: Using a sliding window technique is efficient to maintain and check subarrays for the 'nice' property as we iterate through the input array.
- Insight 3: The bitwise OR operation can be used to keep track of the bits that are set to 1 within the current subarray's elements. We can efficiently determine if adding a new element would violate the 'nice' property by checking if the bitwise AND of the current OR value and the new element is 0.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Bit Manipulation, Sliding Window.
Companies
Asked at: Paytm.