BackhardBit ManipulationGoogleAmazon

Vault Registry Resolver 13 Solution

Problem Statement

You are given an N × M grid of non‑negative integers. Starting from any cell in the first row, you must move to the last row by repeatedly moving to one of the three cells directly below: (i+1, j‑1), (i+1, j), or (i+1, j+1) (indices are 0‑based and moves that leave the grid are prohibited). While traversing, maintain a cumulative bitwise OR of all visited cell values. Your task is to determine the maximum possible number of distinct bits set in the final OR value when a path reaches any cell of the last row. Output this maximum count.

Input format:

  • The first line contains two integers N and M (the number of rows and columns).
  • The next N lines each contain M integers a[i][j] (0 ≤ a[i][j] < 2^20).

Output format:

  • A single integer: the greatest number of set bits achievable by any valid top‑to‑bottom path.
Example 1
Input
3 3 1 2 4 8 16 32 64 128 256
Output
3

Explanation: All possible top‑to‑bottom paths consist of three cells. Any chosen path yields an OR that contains exactly three distinct bits because each selected value lies in a separate power‑of‑two range (1, 16, 256 or 4, 32, 64, etc.). Hence the maximum number of set bits is 3.

Example 2
Input
3 3 5 1 3 2 7 6 8 4 9
Output
4

Explanation: Consider the path (0,0) → (1,1) → (2,2) with values 5 (101₂), 7 (111₂) and 9 (1001₂). The cumulative OR is 5 | 7 | 9 = 1111₂, which has four set bits (positions 0,1,2,3). No other path can produce more than four distinct bits, so the answer is 4.

Example 3
Input
2 4 12 3 5 6 9 10 11 7
Output
4

Explanation: One optimal path is (0,0) → (1,1) → (1,2) → (1,3) using values 12 (1100₂), 10 (1010₂) and 7 (0111₂). The OR of these numbers is 1100 | 1010 | 0111 = 1111₂, which contains four set bits. No path can achieve five or more bits, so the maximum is 4.

Constraints

  • 1 ≤ N, M ≤ 30
  • 0 ≤ a[i][j] < 2^20
  • The algorithm should run in O(N · M · 2^B) time where B is the number of bits (≤ 20), fitting within typical limits for the given N and M.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Vault Registry Resolver 13 — Problem Statement & Solution Guide

Bit ManipulationHard2D Grid DP
TimeO(N·M·log U)
|
SpaceO(N·M)

Problem Description

You are given an N × M grid of non‑negative integers. Starting from any cell in the first row, you must move to the last row by repeatedly moving to one of the three cells directly below: (i+1, j‑1), (i+1, j), or (i+1, j+1) (indices are 0‑based and moves that leave the grid are prohibited). While traversing, maintain a cumulative bitwise OR of all visited cell values. Your task is to determine the maximum possible number of distinct bits set in the final OR value when a path reaches any cell of the last row. Output this maximum count.

Input format:

- The first line contains two integers N and M (the number of rows and columns).

- The next N lines each contain M integers a[i][j] (0 ≤ a[i][j] < 2^20).

Output format:

- A single integer: the greatest number of set bits achievable by any valid top‑to‑bottom path.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Vault Registry Resolver 13"

hard

WHY DOES IT MATTER?

Bit‑mask feasibility combined with greedy bit selection turns an exponential search into a near‑linear one.

OPTIMIZATION CHALLENGE

The key is reducing the state space from 2^B per cell to a single boolean feasibility per mask.

REAL-WORLD CONNECTION

Similar techniques are used in hardware routing where signal masks must satisfy constraints across layers.

Cache the reachable rows for each mask and reuse the DP table across successive bit checks to avoid recomputation.

COMPLEXITY AT A GLANCE

⏱ Time:O(N·M·log U)
💾 Space:O(N·M)

Core Theory — Why This Approach?

The problem can be modeled as a path‑finding task on a directed acyclic graph where each node represents a cell and edges connect to the three permissible cells below. The objective is to minimize the cumulative bitwise OR of the visited values, which is not additive; instead, each bit can only turn from 0 to 1 and never revert, making the search space exponential if approached naively.

To break this exponential blow‑up we exploit the monotonicity of the OR operation: if a set of bits is already forced to 1, adding more cells cannot clear them. This allows a binary‑search‑style feasibility check on the answer’s bits. For a candidate mask X we run a DP (or BFS) that only traverses cells whose values are subsets of X; if a path exists from the top to the bottom, X is feasible. Repeating this from the most significant bit down to the least yields the minimal possible OR in O(N·M·log U) time, where U is the maximum cell value.

Interview Questions on This Problem

Q1Why does a simple DP that stores the exact OR for each cell explode in state space?

Because each cell can accumulate up to 2^B distinct OR values (B = number of bits), leading to exponential growth. The monotonic nature of OR lets us prune states using a bit‑mask feasibility test instead.

Q2How does the bit‑wise binary search guarantee the optimal OR value?

It iteratively fixes bits from high to low, keeping a bit only if no feasible path exists without it. This greedy fixing works because turning a bit off can only make the feasibility condition stricter.

Q3What is the time complexity of the feasibility DP for a fixed mask?

It visits each cell at most once, performing O(1) work per cell, so O(N·M). The outer bit‑search adds a factor of log U, giving the overall bound.

Examples

Example 1

Input

3 3
1 2 4
8 16 32
64 128 256

Output

3

Explanation: All possible top‑to‑bottom paths consist of three cells. Any chosen path yields an OR that contains exactly three distinct bits because each selected value lies in a separate power‑of‑two range (1, 16, 256 or 4, 32, 64, etc.). Hence the maximum number of set bits is 3.

Example 2

Input

3 3
5 1 3
2 7 6
8 4 9

Output

4

Explanation: Consider the path (0,0) → (1,1) → (2,2) with values 5 (101₂), 7 (111₂) and 9 (1001₂). The cumulative OR is 5 | 7 | 9 = 1111₂, which has four set bits (positions 0,1,2,3). No other path can produce more than four distinct bits, so the answer is 4.

Example 3

Input

2 4
12 3 5 6
9 10 11 7

Output

4

Explanation: One optimal path is (0,0) → (1,1) → (1,2) → (1,3) using values 12 (1100₂), 10 (1010₂) and 7 (0111₂). The OR of these numbers is 1100 | 1010 | 0111 = 1111₂, which contains four set bits. No path can achieve five or more bits, so the maximum is 4.

Constraints

  • 1 ≤ N, M ≤ 30
  • 0 ≤ a[i][j] < 2^20
  • The algorithm should run in O(N · M · 2^B) time where B is the number of bits (≤ 20), fitting within typical limits for the given N and M.

Optimal Approach & Strategy

Perform a bitwise greedy search: for each bit from high to low, run a DP/BFS that only uses cells fitting the current mask to test path existence.

Brute Force Approach

Enumerate every possible path (3^(N‑1) choices) and compute its OR, which is infeasible for N,M up to 10^3.

Verified Code Solutions

JavaScript Solution
Time: O(N·M·log U)
function sumLessThanK(nums, k) {
   let sum = 0;
   for (let num of nums) {
       if (num <= k) {
           sum += num;
       }
   }
   return sum;
}

Asked in Top Tech Interviews

GoogleAmazonMicrosoft

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.