intermediate12 min read·Updated October 7, 2026

Search 2D Matrix (Young Tableau): Finding 5 & 20

Master the staircase search mental model. Walk through finding 5 and 20 in a Young tableau with invariants, step-by-step traces, and edge cases.

By Learnisim AI·Published October 7, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
  • 2D array indexing
  • basic comparison operators
  • loop invariants
Search 2D Matrix (Young Tableau): Finding 5 & 20 The Staircase Search: O(N + M) elimination vs O(N*M) brute force Young Tableau (Row & Col Sorted) 1 4 7 11 2 5* 8 14 3 6 9 16 10 13 17 21 ★ Start at Top-Right Corner (Row 0, Col C-1) Staircase Search Rules If Current > Target Move Left (col --) Eliminates current column If Current < Target Move Down (row ++) Eliminates current row Efficiency & Invariants Time: O(N + M) Space: O(1) Auxiliary No extra memory needed Trace: Finding 5 vs Absent 20 Tracing Target = 5 (Present) 1. Start at (0,3) value 11 2. 11 > 5 → Move Left to col 2 (7) 3. 7 > 5 → Move Left to col 1 (4) 4. 4 < 5 → Move Down to row 1 (5!) Tracing Target = 20 (Absent) 1. Start at top-right (11) 2. Step down/left through matrix 3. Index goes out of bounds (> max row) Result: Target 20 not found safely Why Linear Scan Fails (O(N*M)) vs Staircase Search (O(N+M)) By starting at the top-right corner, every single step strictly discards either an entire row or an entire column!
Search 2D Matrix (Young Tableau): Finding 5 & 20 overview diagram
Why

Why Searching a Sorted 2D Matrix Demands More Than a Linear Scan

Phase 1: The Problem of the Sorted Grid

Imagine you are handed a grid of integers where every row is sorted from left to right, and every column is sorted from top to bottom. This structure is often called a Young tableau or a sorted 2D matrix. Your mission is simple: determine if the number 5 is hiding inside the grid, and verify whether the number 20 is completely absent.
Here is our locked working example matrix:
If you treat this grid like a flat, one-dimensional list and scan every single cell from top-left to bottom-right, you will find 5 at row index 1, column index 1, and you will eventually exhaust all 25 elements to confirm 20 is missing. But what happens when the matrix scales to ? A brute-force linear search inspects every element, costing time and completely ignoring the strict ordering guarantees built into the rows and columns.
Even a standard binary search on every individual row takes time, which misses the cross-row relationships. We need a strategy that actively eliminates entire rows or columns in a single comparison. Without a deliberate geometric entry point, we waste time walking blindly through values that could easily be skipped.
Try this: Given matrix: 5x5. Search targets: 5 (present) and 20 (absent).
Why Searching a Sorted 2D Matrix Demands More Than Linear Scan Young Tableau 5x5: Rows & Columns sorted. Search Targets: Find 5 (present) & 20 (absent) Sorted Matrix (Young Tableau) 1 4 7 11 15 2 5* 8 12 19 3 6 9 16 22 10 13 14 17 24 18 21 23 26 30 Linear Scan & Row-wise Binary Search • Brute-force linear scan: Inspects all 25 elements one by one. Costs O(m × n). Ignores row/column ordering guarantees completely. • Row-wise binary search: Searches each row independently in O(m log n). Misses cross-row relationships, wasting valuable steps. Optimal Staircase Search (O(m + n)) 1. Start at Top-Right Corner (Value: 15) If target < current, move LEFT (smaller column). If target > current, move DOWN (larger row). 2. Target Verification Results: • Find 5: Present at row 1, col 1 (Found efficiently!) • Find 20: Path safely exits grid boundaries (Absent!) Start Corner
Why Searching a Sorted 2D Matrix Demands More Than a Linear Scan diagram
Model

The Staircase Search Model for Young Tableaux

When searching our locked example matrix for 5 and 20, we cannot afford to inspect every element in an brute-force sweep. Instead, we need a spatial model that exploits the row-sorted and column-sorted invariants of the Young tableau. Imagine standing at the top-right corner element 15, located at row 0, column 4. If we look left, the numbers decrease (). If we look down, the numbers increase ().
This specific corner acts as a decision pivot. It gives us a binary choice at every step: if our target is smaller than the current element, all elements to the right and below are larger, so we can safely eliminate the entire column by moving left. If our target is larger, all elements above and to the left are smaller, so we can safely eliminate the entire row by moving down. We never start at the top-left corner 1, because from 1, both moving right (4) and moving down (2) lead to larger numbers—leaving us with no monotonic way to discard data.

Phase 2: The Pivot Coordinate

To formalize this staircase navigation through our locked matrix, our state pointer starts at row and column (the value 15). At each iteration, we compare against our target. For target 5, since , we decrement to move left to 11. For target 20, since , we increment to move down to 19. Every single comparison drops either one entire row or one entire column from consideration.
Try this: Given the locked matrix:
[[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10,13,14,17,24],
[18,21,23,26,30]]
Explain why starting the search pointer at matrix[0][4] allows us to eliminate a whole column when target < matrix[0][4], whereas starting at matrix[0][0] provides no such monotonic guarantee.
Staircase Search Model for Young Tableaux Start at Top-Right (Row 0, Col N-1): Left decreases, Down increases 4 × 4 Young Tableau (Row & Col Sorted) 1 4 7 12 2 5 8 14 3 9 15 18 6 11 19 22 START (0, 3) TARGET 5 FOUND 1. Why Start at Top-Right Corner (12)? • Left decreases, Down increases (Decision Pivot). • Top-Left (1) fails: both Right & Down increase! Eliminates an entire row or column with every single comparison. 2. Trace for Target = 5 (Found in 3 steps) 1. Compare 12 vs 5: 12 > 5 ➔ Move Left to 7 2. Compare 7 vs 5: 7 > 5 ➔ Move Left to 4 3. Compare 4 vs 5: 4 < 5 ➔ Move Down to 5 (Found!) Worst-case time complexity: O(N + M) instead of O(N × M). 3. Trace for Target = 20 (Not in Matrix) • 12 → 14 → 18 → 22 (Exceeds bounds and terminates) Guaranteed to find target or exit matrix efficiently. Key Invariant: Every step discards either an entire row or an entire column without backtracking.
The Staircase Search Model for Young Tableaux diagram
Notation

Formal Matrix Coordinates and Invariant Definitions

Phase 3: Formalizing the Staircase Search

To prove why our search for 5 and 20 runs in time, we need precise mathematical notation for our matrix and its sorted properties. Let the matrix be denoted as , where is the number of rows and is the number of columns. In our locked working example, and , and the matrix is explicitly given by:
Each element is represented by , where is the row index and is the column index. The Young tableau properties guarantee two strict inequalities for any valid coordinates:
1. Row-sorted order: for all valid and .
2. Column-sorted order: for all valid and .
Our pointer pair starts at the top-right corner, meaning and . For our target , the state transition at any step is governed by a strict trichotomy:
- If , the search terminates successfully with a match.
- If , the current value is strictly greater than the target. Because row is sorted in ascending order to the right, every element to the right of is also greater than . Thus, we decrement the column index: .
- If , the current value is strictly smaller than the target. Because column is sorted in ascending order downward, every element above or at row in this column is also smaller than or equal to . Thus, we increment the row index: .
Try this: Given A[0][4] = 15 and target = 20, state whether r or c updates and write the resulting inequality for the next pointer state.
Worked example

Tracing the Staircase Search for 5 and 20

Phase 4: Worked Example

Let us execute our staircase search on the locked matrix:

Part A: Searching for Target = 5

Given: , , initial pointer at top-right .
Steps:
1. Check . Since , we discard column 4 by decrementing to 3. Current pointer: , value = 11.
2. Check . Since , we decrement to 2. Current pointer: , value = 7.
3. Check . Since , we decrement to 1. Current pointer: , value = 4.
4. Check . Since , we discard row 0 by incrementing to 1. Current pointer: , value = 5.
5. Check . The current value matches our target .
Result: Found at , returning true.

Part B: Searching for Target = 20

Given: , , initial pointer at top-right .
Steps:
1. Start at , value = 15. $\implies r (1, 4) \), value = 19.
2. At , value = 19. $\implies r (2, 4) \), value = 22.
3. At , value = 22. $\implies c (2, 3) \), value = 16.
4. At , value = 16. $\implies r (3, 3) \), value = 17.
5. At , value = 17. $\implies r (4, 3) \), value = 26.
6. At , value = 26. $\implies c (4, 2) \), value = 23.
7. At , value = 23. $\implies c (4, 1) \), value = 21.
8. At , value = 21. $\implies c (4, 0) \), value = 18.
9. At , value = 18. $\implies r (5, 0) \), which violates .
Result: Pointer falls out of bounds without finding 20, returning false.
Try this: Trace the algorithm manually for target = 13 starting from (0, 4). List the sequence of coordinates and values visited until found.
Phase 4 Worked Example: Staircase Search Trace Side-by-side execution for Target = 5 (Found) vs Target = 20 (Not Found) Part A: Target = 5 (Found at r=1, c=1) 1 4 7 11 15 2 5✓ 8 12 19 3 6 9 16 22 10 13 14 17 24 18 21 23 26 30 Path: (0,4)→(0,3)→(0,2)→(0,1)→(1,1) Compare 5 vs 15,11,7,4 → decrement c Result: Match found at (1,1) (returns true) Part B: Target = 20 (Out of Bounds) 1 4 7 11 15 2 5 8 12 19 3 6 9 16 22 10 13 14 17 24 18 21 21 23 26 30 Path: (0,4)→(1,4)→(2,4)→(2,3)→(3,3)→(4,3)→(4,2)→(4,1)→(4,0) Reaches (4,0) val=18 → 20>18 → r becomes 5 Result: Falls Out of Bounds → false
Tracing the Staircase Search for 5 and 20 diagram
Practice

Practice the Staircase Search on a Modified Target

Now that you have seen how the staircase search navigates from the top-right corner to isolate 5 and 20, it is time to test your mental trace on a new target. Consider the same Young tableau from our working example:
Your task is to trace the execution steps when searching for the target value 13. Start at the top-right corner element 15 at coordinates , and apply the staircase comparison rules: if the current value equals 13, return true; if 13 is less than the current value, decrement the column index ; if 13 is greater, increment the row index . Write down the sequence of visited cells until you find 13 or exceed the matrix bounds.
Try this: Given the 5x5 matrix and starting position r=0, c=4 (value 15):
1.Compare 15 with target 13.
2.Since 13 < 15, move left (c = 3, value = 11).
3.Continue the trace until 13 is reached.
Apply

Transferring the Staircase Pattern to Monotonic Row-Column Matrices

Phase 6: Transfer

Having mastered the top-right staircase search on our locked matrix to locate 5 and 20 in time, we can now extract the underlying structural pattern. The core trick—starting at a corner where movement in one direction strictly increases values and movement in the orthogonal direction strictly decreases values—is not unique to strict Young tableaux. It applies to any matrix where rows and columns are individually sorted in the same monotonic direction.
Suppose you encounter a variant grid where each row is sorted left-to-right and each column is sorted top-to-bottom, but adjacent rows do not strictly interleave their values like a strict Young tableau. Does the top-right corner (matrix[0][n-1]) still work as our pivot? Yes! Moving left decreases values, and moving down increases values, preserving the exact same elimination invariant we used for 5 and 20.
However, what happens if we attempt to apply this same staircase logic to a matrix where rows are sorted left-to-right and columns are sorted bottom-to-top instead? The monotonicity breaks our pivot logic: moving down now decreases values rather than increasing them. Recognizing these structural symmetries allows you to re-orient your starting corner—choosing bottom-left or top-right—to match the matrix's specific gradient.
By abstracting the staircase search from raw indices to a gradient-following agent, you can solve related search problems in compressed quad-trees, sorted sub-grid lookups, and multidimensional threshold queries without rewriting your core traversal logic.

FAQ

How does the staircase search find 5 and 20 in the example matrix?
Starting at the top-right corner (15), comparing 5 moves the pointer left because 5 < 15, eventually finding 5. For 20, the pointer moves down and left until it falls out of bounds, confirming 20 is absent.
Why start at the top-right or bottom-left corner instead of top-left?
Starting at the top-left means both right and down neighbors are larger, eliminating the ability to make a definitive elimination choice. Top-right and bottom-left corners offer orthogonal monotonic directions (one way decreases, the other increases).
What is the time and space complexity of the Young tableau search?
The time complexity is O(m + n) where m is rows and n is columns, because each step eliminates either an entire row or an entire column. Space complexity is O(1) as it uses iterative pointer adjustments.

Keep learning