Pipeline Vector Architect 22 — Problem Statement & Solution Guide
Problem Description
You are given an array nums of length n containing non‑negative integers. Two positions i and j ( i < j ) are said to be *visible* if every element between them is not larger than the smaller of nums[i] and nums[j]; formally, for all k with i < k < j, nums[k] ≤ min(nums[i], nums[j]). Your task is to compute the maximum possible value of the bitwise AND operation ( nums[i] & nums[j] ) taken over all visible pairs. Output this maximum value. The solution must run in O(n) time and O(n) memory, which can be achieved by scanning the array with a monotonic decreasing stack and applying bitmasking on candidate pairs.
DSA Pattern Breakdown
DSA Pattern Breakdown
"Pipeline Vector Architect 22"
WHY DOES IT MATTER?
Monotonic stack patterns turn quadratic visibility checks into linear scans.
OPTIMIZATION CHALLENGE
The key is reducing pair checks from O(n^2) to O(n) by discarding dominated candidates early.
REAL-WORLD CONNECTION
Similar to skyline silhouette calculations where only the nearest taller building matters for line‑of‑sight.
Maintain the stack as a simple array with an index pointer to avoid extra object overhead.
COMPLEXITY AT A GLANCE
O(n)O(n)Core Theory — Why This Approach?
The visibility condition creates a monotonic relationship between two indices: any element between i and j must be ≤ min(nums[i], nums[j]). This allows us to treat the array as a series of “mountain peaks” where only the nearest higher or equal elements on each side can form a candidate pair, enabling a stack‑based linear scan. A naive O(n^2) check enumerates all pairs and verifies the condition, which explodes for n up to 2·10^5. The optimal paradigm uses a decreasing monotonic stack to maintain potential left‑hand candidates; when a new element arrives, we pop smaller values, compute the bitwise AND with the popped element (the only pair that can be visible with the current element), and keep track of the maximum, achieving O(n) time.
Interview Questions on This Problem
Q1Why does a monotonic decreasing stack correctly capture all visible pairs?
Because any element popped from the stack is guaranteed to have no larger element between it and the current index, satisfying the visibility rule. The stack preserves a decreasing sequence, so the current element can only be visible with the top after popping.
Q2What is the time complexity of the stack‑based solution and why?
It runs in O(n) because each array element is pushed and popped at most once. The total work is linear despite the inner while loop.
Q3How would you adapt the algorithm if the operation were bitwise OR instead of AND?
The visibility condition remains unchanged, so the same stack logic applies; only the computed value changes to nums[i] | nums[j]. The maximum OR is still tracked during each pop‑compute step.
Examples
Input
5 3 10 5 25 2
Output
8
Explanation: Step‑by‑step walkthrough of how output was derived.
Input
3 7 7 7
Output
7
Explanation: All three numbers are equal, so every pair is visible. The AND of any pair is 7 & 7 = 7, which is the maximum possible value.
Input
4 1 2 4 8
Output
0
Explanation: Each new element is larger than the previous one, therefore only adjacent pairs are visible. Their ANDs are: 1 & 2 = 0, 2 & 4 = 0, 4 & 8 = 0. No larger AND exists, so the answer is 0.
Constraints
- 1 <= nums.length <= 100000
- 0 <= nums[i] <= 2^31 - 1
- The algorithm must run in O(n) time and O(n) auxiliary space.
Optimal Approach & Strategy
Use a decreasing monotonic stack to maintain potential left partners; pop while the top is ≤ current, compute AND with each popped element, and push the current value.
Brute Force Approach
Check every i < j, verify the visibility condition by scanning the middle segment, and compute the AND; O(n^3) in the worst case.
Verified Code Solutions
function solution(nums, K) {
let architectValue = 0;
if (nums.length === 0) {
return architectValue;
}
for (let i = 0; i < nums.length; i++) {
if (nums[i] > K) {
architectValue = nums[i];
}
}
return architectValue;
}class Solution {
public:
int solution(vector<int> nums, int K) {
int architectValue = 0;
if (nums.size() == 0) {
return architectValue;
}
for (int i = 0; i < nums.size(); i++) {
if (nums[i] > K) {
architectValue = nums[i];
}
}
return architectValue;
}
};class Solution {
public int solution(int[] nums, int K) {
int architectValue = 0;
if (nums.length == 0) {
return architectValue;
}
for (int i = 0; i < nums.length; i++) {
if (nums[i] > K) {
architectValue = nums[i];
}
}
return architectValue;
}
}def solution(nums, K):
architect_value = 0
if len(nums) == 0:
return architect_value
for num in nums:
if isinstance(num, (int, float)) and num > K:
architect_value = num
return architect_valuefunction solution(nums, K) {
let architectValue = 0;
if (nums.length === 0) {
return architectValue;
}
for (let i = 0; i < nums.length; i++) {
if (nums[i] > K) {
architectValue = nums[i];
}
}
return architectValue;
}Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.