Skip to main content

Command Palette

Search for a command to run...

🧠 Day 20: Search in a Sorted 2D Matrix

How I Grasped the Logic to Search Efficiently in a Sorted 2D Matrix

Published
4 min readView as Markdown
🧠 Day 20: Search in a Sorted 2D Matrix
R

Hi, I’m Richa — a Senior Frontend Engineer with 5+ years of experience building scalable, production-grade web interfaces for enterprise and consumer applications. I work primarily with React, TypeScript, and modern frontend architectures, focusing on component systems, performance, and maintainability. Most of my experience comes from building real-world products in regulated domains like banking and insurance, where clarity, reliability, and long-term ownership matter more than quick demos. Through this blog, I write about frontend engineering fundamentals, scalable UI design, problem-solving, and the lessons I’ve learned working on large codebases. My goal is to share practical insights — not shortcuts — for developers who want to grow strong engineering foundations. I also mentor early-career developers and strongly believe that curiosity, asking the right questions, and understanding why something works are more important than memorizing tools. If you’re serious about improving as an engineer, you’re in the right place.


Yesterday, I solved this problem using brute force.
Today, I’m trying to understand the optimized solution—not memorize it.

And honestly?
At first glance, this code scared me.

So I decided to pause everything and trace it like a beginner.

Friends gif. Jennifer Aniston as Rachel and Lisa Kudrow as Phoebe jump up and down, clapping their hands and screaming with excitement.


🧩 The Matrix We’ll Use for Understanding

mat = [
  [1,  2,  3,  4],
  [5,  6,  7,  8],
  [9, 10, 11, 12]
]

target = 8

Rows = 3
Cols = 4


🧠 Step 1: Pretend This Is a 1D Array

I did not flatten the matrix in code.
I only flattened it in my head.

Index:  0  1  2  3   4  5  6  7   8  9 10 11
Value: [1, 2, 3, 4 | 5, 6, 7, 8 | 9,10,11,12]

This is the key mental shift.


🧠 Step 2: Binary Search Setup

let left = 0;
let right = rows * cols - 1; // 11

So now:

left = 0
right = 11

🔁 WHILE LOOP — Iteration by Iteration

We now enter:

while (left <= right)

Let’s go iteration by iteration, like a beginner.


🔄 Iteration 1

Step A: Find mid

mid = (0 + 11) / 2 = 5

Step B: Convert mid → row & column

row = Math.floor(5 / 4) = 1
col = 5 % 4 = 1

ASCII view:

Matrix:
[
  [1, 2, 3, 4],
  [5, 6, 7, 8],  ← row = 1
  [9,10,11,12]
]

            ↑
          col = 1

So:

midValue = mat[1][1] = 6

Step C: Compare

6 < 8

So target must be on the right side.

left = mid + 1 = 6

🔄 Iteration 2

Now:

left = 6
right = 11

Step A: Find mid

mid = (6 + 11) / 2 = 8

Step B: Convert mid → row & column

row = Math.floor(8 / 4) = 2
col = 8 % 4 = 0

ASCII:

[
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9,10,11,12]  ← row = 2
   ↑
 col = 0

So:

midValue = mat[2][0] = 9

Step C: Compare

9 > 8

So target must be on the left side.

right = mid - 1 = 7

🔄 Iteration 3

Now:

left = 6
right = 7

Step A: Find mid

mid = (6 + 7) / 2 = 6

Step B: Convert mid → row & column

row = Math.floor(6 / 4) = 1
col = 6 % 4 = 2

ASCII:

[
  [1, 2, 3, 4],
  [5, 6, 7, 8],  ← row = 1
        ↑
      col = 2
]
midValue = 7

Step C: Compare

7 < 8

So:

left = mid + 1 = 7

🔄 Iteration 4 (Final)

left = 7
right = 7

Step A: mid

mid = 7

Step B: Convert mid

row = Math.floor(7 / 4) = 1
col = 7 % 4 = 3

ASCII:

[
  [1, 2, 3, 4],
  [5, 6, 7, 8],  ← row = 1
           ↑
         col = 3
]
midValue = 8

🎯 FOUND IT

midValue === target
return true;

found gif


📊 Dry Run Table (Beginner Gold)

IterLeftRightMidRowColValueAction
10115116Move Right
26118209Move Left
3676127Move Right
4777138Found

🧠 Final Beginner Realization

This problem is not about matrices.

It’s about:

  • Seeing sorted data

  • Applying binary search

  • Learning how index math maps back to 2D

Once you trace it like this once,
your brain stops panicking.


💬 One Honest Line Before Ending

If you couldn’t think of this solution on your own —
you’re normal.

Understanding > memorizing.

TV gif. Greg Miller from Kinda Funny leans back as he points a finger. Text, "Thank You Sir."