3825. Longest Strictly Increasing Subsequence With Non-Zero Bitwise AND
Description
You are given an integer array nums.
Return the length of the longest strictly increasing subsequence in nums whose bitwise AND is non-zero. If no such subsequence exists, return 0.
Example 1:
Input: nums = [5,4,7]
Output: 2
Explanation:
One longest strictly increasing subsequence is [5, 7]. The bitwise AND is 5 AND 7 = 5, which is non-zero.
Example 2:
Input: nums = [2,3,6]
Output: 3
Explanation:
The longest strictly increasing subsequence is [2, 3, 6]. The bitwise AND is 2 AND 3 AND 6 = 2, which is non-zero.
Example 3:
Input: nums = [0,1]
Output: 1
Explanation:
One longest strictly increasing subsequence is [1]. The bitwise AND is 1, which is non-zero.
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 109
Solutions
Solution 1: Enumeration + Longest Increasing Subsequence
Thinking
We want the longest strictly increasing subsequence whose AND is nonzero. \(n \le 10^5\), and a plain LIS ignores the AND.
A nonzero AND means some bit is \(1\) in every chosen value.
Enumerate that bit, keep numbers with the bit set, and run LIS on the filtered sequence.
About \(30\) bits, each an \(O(n \log n)\) LIS, and we take the maximum.
A non-zero bitwise AND result means that all numbers in the subsequence have a \(1\) at a certain bit position. We can enumerate that bit position, then find the longest strictly increasing subsequence among all numbers that have a \(1\) at that bit position, and take the maximum value across all enumerations as the answer.
The time complexity is \(O(\log M \times n \times \log n)\), and the space complexity is \(O(n)\). Here, \(n\) and \(M\) are the length of the array and the maximum value in the array, respectively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |