Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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] <= 109Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Bit Manipulation
[5,4,7]
[2,3,6]
[0,1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3825: Longest Strictly Increasing Subsequence With Non-Zero Bitwise AND
class Solution {
public int longestSubsequence(int[] nums) {
int ans = 0;
int mx = 0;
for (int x : nums) {
mx = Math.max(mx, x);
}
int m = 32 - Integer.numberOfLeadingZeros(mx);
for (int i = 0; i < m; i++) {
List<Integer> arr = new ArrayList<>();
for (int x : nums) {
if (((x >> i) & 1) == 1) {
arr.add(x);
}
}
ans = Math.max(ans, lis(arr));
}
return ans;
}
private int lis(List<Integer> arr) {
List<Integer> g = new ArrayList<>();
for (int x : arr) {
int l = 0, r = g.size();
while (l < r) {
int mid = (l + r) >>> 1;
if (g.get(mid) >= x) {
r = mid;
} else {
l = mid + 1;
}
}
if (l == g.size()) {
g.add(x);
} else {
g.set(l, x);
}
}
return g.size();
}
}
// Accepted solution for LeetCode #3825: Longest Strictly Increasing Subsequence With Non-Zero Bitwise AND
func longestSubsequence(nums []int) int {
ans := 0
m := bits.Len(uint(slices.Max(nums)))
for i := 0; i < m; i++ {
arr := make([]int, 0)
for _, x := range nums {
if (x>>i)&1 == 1 {
arr = append(arr, x)
}
}
ans = max(ans, lis(arr))
}
return ans
}
func lis(arr []int) int {
g := make([]int, 0)
for _, x := range arr {
j := sort.SearchInts(g, x)
if j == len(g) {
g = append(g, x)
} else {
g[j] = x
}
}
return len(g)
}
# Accepted solution for LeetCode #3825: Longest Strictly Increasing Subsequence With Non-Zero Bitwise AND
class Solution:
def longestSubsequence(self, nums: List[int]) -> int:
def lis(arr: List[int]) -> int:
g = []
for x in arr:
j = bisect_left(g, x)
if j == len(g):
g.append(x)
else:
g[j] = x
return len(g)
ans = 0
m = max(nums).bit_length()
for i in range(m):
arr = [x for x in nums if x >> i & 1]
ans = max(ans, lis(arr))
return ans
// Accepted solution for LeetCode #3825: Longest Strictly Increasing Subsequence With Non-Zero Bitwise AND
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3825: Longest Strictly Increasing Subsequence With Non-Zero Bitwise AND
// class Solution {
// public int longestSubsequence(int[] nums) {
// int ans = 0;
// int mx = 0;
// for (int x : nums) {
// mx = Math.max(mx, x);
// }
// int m = 32 - Integer.numberOfLeadingZeros(mx);
// for (int i = 0; i < m; i++) {
// List<Integer> arr = new ArrayList<>();
// for (int x : nums) {
// if (((x >> i) & 1) == 1) {
// arr.add(x);
// }
// }
// ans = Math.max(ans, lis(arr));
// }
// return ans;
// }
//
// private int lis(List<Integer> arr) {
// List<Integer> g = new ArrayList<>();
// for (int x : arr) {
// int l = 0, r = g.size();
// while (l < r) {
// int mid = (l + r) >>> 1;
// if (g.get(mid) >= x) {
// r = mid;
// } else {
// l = mid + 1;
// }
// }
// if (l == g.size()) {
// g.add(x);
// } else {
// g.set(l, x);
// }
// }
// return g.size();
// }
// }
// Accepted solution for LeetCode #3825: Longest Strictly Increasing Subsequence With Non-Zero Bitwise AND
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3825: Longest Strictly Increasing Subsequence With Non-Zero Bitwise AND
// class Solution {
// public int longestSubsequence(int[] nums) {
// int ans = 0;
// int mx = 0;
// for (int x : nums) {
// mx = Math.max(mx, x);
// }
// int m = 32 - Integer.numberOfLeadingZeros(mx);
// for (int i = 0; i < m; i++) {
// List<Integer> arr = new ArrayList<>();
// for (int x : nums) {
// if (((x >> i) & 1) == 1) {
// arr.add(x);
// }
// }
// ans = Math.max(ans, lis(arr));
// }
// return ans;
// }
//
// private int lis(List<Integer> arr) {
// List<Integer> g = new ArrayList<>();
// for (int x : arr) {
// int l = 0, r = g.size();
// while (l < r) {
// int mid = (l + r) >>> 1;
// if (g.get(mid) >= x) {
// r = mid;
// } else {
// l = mid + 1;
// }
// }
// if (l == g.size()) {
// g.add(x);
// } else {
// g.set(l, x);
// }
// }
// return g.size();
// }
// }
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.