0
0
DSA C++programming~10 mins

Search in 2D Matrix in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Atarget
Bi
Cj
Dmatrix
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.
2fill in blank
medium

Complete 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'
Atarget
Bleft
Cmid
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with indices instead of the target value.
Using wrong variable names for comparison.
3fill in blank
hard

Fix 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'
Amid
Btarget
Ctop
Dbottom
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with mid index instead of target value.
Using wrong variable for comparison.
4fill in blank
hard

Fill 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'
A<
B>
Ctarget
Drow
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operator.
Passing wrong argument to binary search.
5fill in blank
hard

Fill 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'
Acols
Brows
Ctarget
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Using rows instead of cols for index calculations.
Comparing with wrong variable.