BackmediumBit ManipulationGoogleAmazon

Tome Signal Extractor 21 Solution

Problem Statement

You are given a 2D grid of integers grid with dimensions m rows and n columns. Each cell grid[i][j] contains a non-negative integer value representing a signal strength. Your task is to determine the maximum sum of values along a path that starts at the top-left corner grid[0][0] and ends at the bottom-right corner grid[m-1][n-1]. Movement is restricted to only right or down directions. Additionally, you must apply a bit manipulation constraint: at any step, if the current cell's value has an odd number of set bits (1s) in its binary representation, you may choose to skip adding this cell's value to the path sum (effectively treating it as 0 for that step), but you must still move through the cell. If the number of set bits is even, you must include the cell's value in the sum. Return the maximum possible sum under these rules.

Input: A 2D array grid of integers. Output: An integer representing the maximum path sum.

Example 1
Input
grid = [[1, 2], [3, 4]]
Output
8

Explanation: Path 1: (0,0) -> (0,1) -> (1,1). - (0,0): 1 (binary 1, 1 set bit, odd) -> can skip or take. Take 1. - (0,1): 2 (binary 10, 1 set bit, odd) -> can skip or take. Take 2. - (1,1): 4 (binary 100, 1 set bit, odd) -> can skip or take. Take 4. Sum = 1 + 2 + 4 = 7. Path 2: (0,0) -> (1,0) -> (1,1). - (0,0): 1 (odd bits) -> Take 1. - (1,0): 3 (binary 11, 2 set bits, even) -> Must take 3. - (1,1): 4 (odd bits) -> Take 4. Sum = 1 + 3 + 4 = 8. Max is 8.

Example 2
Input
grid = [[5, 7], [11, 13]]
Output
29

Explanation: Path 1: (0,0) -> (0,1) -> (1,1). - (0,0): 5 (101, 2 bits, even) -> Must take 5. - (0,1): 7 (111, 3 bits, odd) -> Can skip. Skip 0. - (1,1): 13 (1101, 3 bits, odd) -> Can skip. Take 13. Sum = 5 + 0 + 13 = 18. Path 2: (0,0) -> (1,0) -> (1,1). - (0,0): 5 (even) -> Must take 5. - (1,0): 11 (1011, 3 bits, odd) -> Can skip. Take 11. - (1,1): 13 (odd) -> Take 13. Sum = 5 + 11 + 13 = 29. Max is 29.

Example 3
Input
grid = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
Output
5

Explanation: All cells have value 1 (binary 1, 1 set bit, odd). We can choose to skip or take. To maximize sum, we take all. Path length is 5 cells. Sum = 1+1+1+1+1 = 5.

Constraints

  • 1 <= m, n <= 100
  • 0 <= grid[i][j] <= 10^9
  • grid[0][0] and grid[m-1][n-1] are always non-negative
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

Tome Signal Extractor 21 — Problem Statement & Solution Guide

Bit ManipulationMedium2D Grid DP
TimeO(m*n)
|
SpaceO(n)

Problem Description

You are given a 2D grid of integers grid with dimensions m rows and n columns. Each cell grid[i][j] contains a non-negative integer value representing a signal strength. Your task is to determine the maximum sum of values along a path that starts at the top-left corner grid[0][0] and ends at the bottom-right corner grid[m-1][n-1]. Movement is restricted to only right or down directions. Additionally, you must apply a bit manipulation constraint: at any step, if the current cell's value has an odd number of set bits (1s) in its binary representation, you may choose to skip adding this cell's value to the path sum (effectively treating it as 0 for that step), but you must still move through the cell. If the number of set bits is even, you must include the cell's value in the sum. Return the maximum possible sum under these rules.

Input: A 2D array grid of integers.

Output: An integer representing the maximum path sum.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Tome Signal Extractor 21"

medium

WHY DOES IT MATTER?

Dynamic programming transforms exponential combinatorial problems into tractable linear solutions.

OPTIMIZATION CHALLENGE

The key is to recognize overlapping sub‑problems and replace recursion with iterative state propagation.

REAL-WORLD CONNECTION

It mirrors routing algorithms that compute optimal paths in network grids or warehouse robot navigation.

Always pre‑compute border values and use a 1‑D DP array to keep cache‑friendly memory access.

COMPLEXITY AT A GLANCE

⏱ Time:O(m*n)
💾 Space:O(n)

Core Theory — Why This Approach?

The maximum‑sum path problem on a 2‑D grid is a classic example of dynamic programming, where the optimal substructure property holds: the best path to any cell (i, j) is the value at that cell plus the maximum of the best paths to its top neighbor (i‑1, j) and left neighbor (i, j‑1). By filling a DP table row‑by‑row (or column‑by‑column) we propagate these local optima to the destination, guaranteeing a global optimum without recomputation. Naïve recursion explores every possible right/down combination, leading to exponential time (O(2^{m+n})) and stack overflow on large grids, which is infeasible for typical interview constraints (m, n up to 10^3). The optimal paradigm leverages overlapping subproblems and memoization, collapsing the exponential search space into a linear scan of the grid, achieving O(m·n) time and O(min(m, n)) auxiliary space when using a rolling array.

Interview Questions on This Problem

Q1Why does a simple recursive solution for the max‑sum path have exponential time complexity?

Each cell branches into two recursive calls (right and down), creating a binary tree of depth m+n. Without memoization, the same sub‑problems are recomputed many times, leading to O(2^{m+n}) calls.

Q2How can you reduce the space usage of the DP solution from O(m·n) to O(n)?

By processing the grid row‑wise and keeping only the current row’s DP values, we overwrite the previous row’s data because each cell depends only on the left and the cell directly above. This rolling array technique drops the space to O(n).

Q3What edge case must you handle when initializing the DP table for the first row and first column?

The first row can only be reached from the left and the first column only from above, so their DP values are cumulative sums of the preceding cells. Forgetting this leads to incorrect path sums for border cells.

Examples

Example 1

Input

grid = [[1, 2], [3, 4]]

Output

8

Explanation: Path 1: (0,0) -> (0,1) -> (1,1). - (0,0): 1 (binary 1, 1 set bit, odd) -> can skip or take. Take 1. - (0,1): 2 (binary 10, 1 set bit, odd) -> can skip or take. Take 2. - (1,1): 4 (binary 100, 1 set bit, odd) -> can skip or take. Take 4. Sum = 1 + 2 + 4 = 7. Path 2: (0,0) -> (1,0) -> (1,1). - (0,0): 1 (odd bits) -> Take 1. - (1,0): 3 (binary 11, 2 set bits, even) -> Must take 3. - (1,1): 4 (odd bits) -> Take 4. Sum = 1 + 3 + 4 = 8. Max is 8.

Example 2

Input

grid = [[5, 7], [11, 13]]

Output

29

Explanation: Path 1: (0,0) -> (0,1) -> (1,1). - (0,0): 5 (101, 2 bits, even) -> Must take 5. - (0,1): 7 (111, 3 bits, odd) -> Can skip. Skip 0. - (1,1): 13 (1101, 3 bits, odd) -> Can skip. Take 13. Sum = 5 + 0 + 13 = 18. Path 2: (0,0) -> (1,0) -> (1,1). - (0,0): 5 (even) -> Must take 5. - (1,0): 11 (1011, 3 bits, odd) -> Can skip. Take 11. - (1,1): 13 (odd) -> Take 13. Sum = 5 + 11 + 13 = 29. Max is 29.

Example 3

Input

grid = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]

Output

5

Explanation: All cells have value 1 (binary 1, 1 set bit, odd). We can choose to skip or take. To maximize sum, we take all. Path length is 5 cells. Sum = 1+1+1+1+1 = 5.

Constraints

  • 1 <= m, n <= 100
  • 0 <= grid[i][j] <= 10^9
  • grid[0][0] and grid[m-1][n-1] are always non-negative

Optimal Approach & Strategy

Iteratively fill a DP matrix (or 1‑D array) using the recurrence dp[i][j] = grid[i][j] + max(dp[i‑1][j], dp[i][j‑1]).

Brute Force Approach

Recursively explore every right/down combination from the start, accumulating sums and returning the maximum at the leaf.

Verified Code Solutions

JavaScript Solution
Time: O(m*n)
function solution(nums, K) {
   if (nums.length === 0 || nums.length === 1) return 0;
   nums.sort((a, b) => a - b);
   let sum = 0;
   for (let i = nums.length - 1; i >= 0; i--) {
       if (nums[i] > K) sum += nums[i];
       else break;
   }
   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.