Skip to main content

Longest Strictly Increasing Subsequence With Non-Zero Bitwise AND - Solution & Explanation

Practice this problem

Problem Statement

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 <= 105
  • 0 <= nums[i] <= 109​​​​​​​

Approach Overview

Problem Overview: Given an integer array, find the length of the longest strictly increasing subsequence where the bitwise AND between every adjacent pair in the subsequence is non‑zero. The subsequence must preserve order and increase in value while sharing at least one common bit.

Approach 1: Dynamic Programming Enumeration (O(n^2) time, O(n) space)

Start with the classic array LIS idea. Let dp[i] represent the longest valid subsequence ending at index i. For each i, iterate all previous indices j < i. If nums[j] < nums[i] and (nums[j] & nums[i]) != 0, extend the subsequence: dp[i] = max(dp[i], dp[j] + 1). Track the maximum across all indices. The logic is straightforward and clearly enforces both constraints: increasing order and non‑zero AND. The downside is the quadratic scan across pairs, which becomes slow for large arrays.

Approach 2: Bit Enumeration + Longest Increasing Subsequence (O(32 * n log n) time, O(n) space)

The key observation: if every element in the subsequence shares at least one common bit position, then the AND between adjacent elements will always be non‑zero. Enumerate each bit from 0 to 31. For a chosen bit, filter numbers whose binary representation contains that bit. Those numbers automatically satisfy the AND constraint with each other.

Now the problem reduces to finding the Longest Increasing Subsequence among this filtered list. Use the classic patience sorting technique with binary search. Maintain a tails array where tails[k] stores the smallest ending value for an increasing subsequence of length k+1. For each number, use binary search to find its position and update the structure. This computes LIS in O(n log n) time per bit. Since there are at most 32 bits, the total complexity is O(32 * n log n).

This approach works because any valid subsequence must share at least one bit across its elements. By enumerating each bit and computing LIS independently, you guarantee that every candidate subsequence satisfies the bitwise constraint. Bit enumeration combined with LIS transforms a constraint-heavy problem into a standard subsequence optimization using bit manipulation.

Recommended for interviews: The bit enumeration + LIS approach. Starting with the quadratic DP shows you understand subsequence transitions and the AND constraint. Recognizing the shared-bit property and converting it to multiple LIS computations demonstrates strong pattern recognition and optimization skills, which is what interviewers expect for a medium-level problem.

Solution

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.

Code

Python

Java

C++

Go

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Dynamic Programming Pair CheckO(n^2)O(n)Good for understanding transitions and constraints when n is small
Bit Enumeration + LIS (Binary Search)O(32 * n log n)O(n)Optimal general solution; converts the constraint into standard LIS

Video Solution

Leetcode Biweekly Contest 175 || Q1, Q2, Q3, Q4 || Divide & Conquer DP, Binary Search || C++ || 2X 🚀Rajan Keshari ( CSE - IIT Dhanbad )1,535 views views

Watch 8 more video solutions →

Frequently Asked Questions

Is Longest Strictly Increasing Subsequence With Non-Zero Bitwise AND easy or hard?
The problem is rated Medium because it requires combining two ideas: bit manipulation and the optimized LIS algorithm. Recognizing the shared-bit property and reducing the problem to multiple LIS computations is the main challenge.
Longest Strictly Increasing Subsequence With Non-Zero Bitwise AND Python/Java solution
Implement bit enumeration and run a binary-search-based LIS for each bit. Python typically uses the bisect module for the LIS step, while Java uses Collections.binarySearch or a custom binary search on the tails array.
How to solve Longest Strictly Increasing Subsequence With Non-Zero Bitwise AND in O(n log n)?
Filter numbers by a specific bit and compute the Longest Increasing Subsequence using the patience sorting method with binary search. Repeat this for each bit position from 0 to 31 and take the maximum LIS length found. Each LIS computation costs O(n log n), leading to O(32 * n log n) overall.
What is the best approach for Longest Strictly Increasing Subsequence With Non-Zero Bitwise AND?
The most efficient approach is bit enumeration combined with the Longest Increasing Subsequence algorithm. Iterate through all 32 bit positions, keep numbers containing the chosen bit, and compute LIS using binary search. This guarantees every adjacent pair has a non‑zero AND and runs in O(32 * n log n) time with O(n) space.
Is Longest Strictly Increasing Subsequence With Non-Zero Bitwise AND asked at Google/Amazon/Meta?
Problems combining LIS with bit manipulation patterns commonly appear in interviews at companies like Google, Amazon, and Meta. Variants that mix subsequence DP with bit constraints are especially common in medium to hard algorithm rounds.
What data structure is used in Longest Strictly Increasing Subsequence With Non-Zero Bitwise AND?
The optimized solution uses an array or list called the tails array for the LIS patience sorting technique along with binary search. Bit masking operations are used to filter numbers sharing a particular bit.
What is the time complexity of Longest Strictly Increasing Subsequence With Non-Zero Bitwise AND?
A straightforward dynamic programming solution runs in O(n^2) time by checking every pair (j, i) for increasing order and non-zero AND. The optimized solution enumerates 32 bits and runs LIS with binary search, resulting in O(32 * n log n) time and O(n) space.

Ready to solve this problem?

Practice Longest Strictly Increasing Subsequence With Non-Zero Bitwise AND with our built-in code editor and test cases.

Practice on FleetCode