0
0
DSA C++programming~5 mins

Search in 2D Matrix in DSA C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the main idea behind searching in a 2D matrix where each row and column is sorted?
Start from the top-right corner and move left if the current element is greater than the target, or move down if it is smaller. This way, you eliminate a row or a column in each step.
Click to reveal answer
beginner
Why is starting from the top-right corner effective in searching a sorted 2D matrix?
Because from the top-right corner, moving left decreases the value and moving down increases the value, helping to decide the direction to find the target efficiently.
Click to reveal answer
intermediate
What is the time complexity of searching in a sorted 2D matrix using the top-right corner approach?
O(m + n), where m is the number of rows and n is the number of columns, because each step moves either one row down or one column left.
Click to reveal answer
beginner
In a 2D matrix sorted row-wise and column-wise, what happens if the target is smaller than the current element at top-right?
You move left to the previous column because all elements below are larger, so the target cannot be in the current column.
Click to reveal answer
intermediate
What is a simple alternative approach to search in a 2D matrix if it is treated as a sorted 1D array?
Use binary search by mapping 2D indices to 1D indices, since the matrix is sorted row-wise and column-wise, treating it as a single sorted list.
Click to reveal answer
Where do you start searching in a sorted 2D matrix to efficiently find a target?
ATop-right corner
BBottom-left corner
CTop-left corner
DBottom-right corner
If the current element is greater than the target, what is the next move in the top-right corner search?
AMove left
BMove down
CMove right
DMove up
What is the time complexity of the top-right corner search in a matrix with m rows and n columns?
AO(m * n)
BO(m + n)
CO(log(m * n))
DO(max(m, n))
Which of these is NOT true about the 2D matrix search starting at top-right?
AYou eliminate one row or column each step
BYou move left if current element is greater than target
CYou move down if current element is less than target
DYou move right if current element is less than target
What alternative method can be used if the matrix is sorted and treated as a 1D array?
ALinear search
BDepth-first search
CBinary search
DBreadth-first search
Explain the step-by-step process of searching for a target in a 2D matrix sorted by rows and columns using the top-right corner approach.
Think about how moving left or down helps eliminate parts of the matrix.
You got /5 concepts.
    Describe how binary search can be applied to a 2D matrix sorted row-wise and column-wise by treating it as a 1D array.
    Think about flattening the matrix conceptually without changing the data.
    You got /5 concepts.