Search in a 2D Matrix
Binary Search on 2D Arrays DSA practice problem on Onlearn.
Difficulty: medium.
Topics: Search a target element in a sorted 2D matrix with specific properties, Matrices, Two-dimensional Array Traversal, Brute Force, Binary Search, Time Complexity, Space Complexity, matrix, matrix indexing, search algorithms, binary search, time complexity analysis, Matrix Search.
Problem Statement You have been given a 2 D array mat of size N x M where N and M denote the number of rows and columns, respectively. The elements of each row are sorted in non decreasing order. Moreover, the first element of a row is greater than the last element of the previous row (if it exists). You are given an integer target, and your task is to find if it exists in the given mat or not. Input Specification N: Number of rows M: Number of columns target: The integer to search for mat: A 2D array of integers. Output Specification Return true if the target exists in mat, otherwise return false. Examples Example 1: Example 2: