Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return true if the target is found in the matrix.
DSA C++
bool searchMatrix(vector<vector<int>>& matrix, int target) {
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix[0].size(); j++) {
if (matrix[i][j] == [1]) {
return true;
}
}
}
return false;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using loop indices instead of the target value for comparison.
Comparing with the entire matrix instead of a single element.
✗ Incorrect
We compare each element matrix[i][j] with the target value to find a match.
2fill in blank
mediumComplete the code to perform binary search on a sorted row to find the target.
DSA C++
bool binarySearchRow(vector<int>& row, int target) {
int left = 0, right = row.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (row[mid] == [1]) {
return true;
} else if (row[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return false;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with indices instead of the target value.
Using wrong variable names for comparison.
✗ Incorrect
We compare the middle element row[mid] with the target value to check for equality.
3fill in blank
hardFix the error in the code to correctly search the matrix using binary search on rows.
DSA C++
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int top = 0, bottom = matrix.size() - 1;
while (top <= bottom) {
int mid = top + (bottom - top) / 2;
if (matrix[mid][0] == [1]) {
return true;
} else if (matrix[mid][0] < target) {
top = mid + 1;
} else {
bottom = mid - 1;
}
}
return false;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with mid index instead of target value.
Using wrong variable for comparison.
✗ Incorrect
We compare the first element of the middle row with the target to decide which half to search next.
4fill in blank
hardFill both blanks to complete the code that finds the correct row and then searches it.
DSA C++
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int top = 0, bottom = matrix.size() - 1;
while (top <= bottom) {
int mid = top + (bottom - top) / 2;
if (matrix[mid][0] == target) {
return true;
} else if (matrix[mid][0] [1] target) {
top = mid + 1;
} else {
bottom = mid - 1;
}
}
int row = bottom;
if (row < 0) return false;
return binarySearchRow(matrix[row], [2]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operator.
Passing wrong argument to binary search.
✗ Incorrect
We check if the first element of mid row is less than target to move top pointer. Then we search the found row for the target.
5fill in blank
hardFill all three blanks to complete the optimized search in 2D matrix.
DSA C++
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if (matrix.empty() || matrix[0].empty()) return false;
int rows = matrix.size(), cols = matrix[0].size();
int left = 0, right = rows * cols - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
int mid_value = matrix[mid / [1]][mid % [2]];
if (mid_value == [3]) {
return true;
} else if (mid_value < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return false;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using rows instead of cols for index calculations.
Comparing with wrong variable.
✗ Incorrect
We use 'cols' to convert 1D mid index to 2D indices. We compare mid_value with target to decide search direction.