
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Search a 2D Matrix in C++
Suppose we have write an efficient algorithm to searches for a value in one m x n matrix. This matrix has some properties like below −
- Each row is sorted from left to right
- The first number of each row is greater than the last integer of the previous row.
So if the matrix is like −
1 | 3 | 5 | 7 |
10 | 11 | 16 | 20 |
23 | 30 | 34 | 50 |
53 | 62 | 78 | 98 |
And if the target value is 16, then the output will be True.
Let us see the steps −
- n := number of rows, if n is 0, then return false, m := number of columns, if m = 0, then return false
- low := 0 and high := n – 1
- while low < high
- mid := low + (high – low + 1)/2
- if mat[mid, 0] <= target, then low := mid, otherwise high := mid – 1
- rlow := 0 and rhigh := m – 1 and ans := 0
- while rlow <= rhigh
- mid := rlow + (rhigh - rlow)/2
- if mat[low, mid] = target, then ans := 1, and break the loop
- otherwise when matrix[low, mid] < target, then rlow := mid + 1
- else rhigh := mid – 1
- return ans
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; typedef long long int lli; class Solution { public: bool searchMatrix(vector<vector<int>>& matrix, int target) { lli n,m; n = matrix.size(); if(!n)return false; m = matrix[0].size(); if(!m)return false; lli low = 0, high = n-1; while(low<high){ lli mid = low + ( high - low +1)/2; if(matrix[mid][0]<=target)low = mid; else high = mid -1; } lli rlow = 0, rhigh = m-1; lli ans = 0; while(rlow<=rhigh){ lli mid = rlow+(rhigh - rlow)/2; if(matrix[low][mid] == target){ ans =1; break; }else if(matrix[low][mid]<target)rlow=mid+1; else rhigh= mid-1; } return ans; } }; main(){ Solution ob; vector<vector<int>> v = {{1,3,5,7},{10,11,16,20},{23,30,34,50},{53,62,78,98}}; cout << ob.searchMatrix(v, 16); }
Input
[[1,3,5,7],[10,11,16,20],[23,30,34,50],[53,62,78,98]] 16
Output
1
- Related Articles
- Search a 2D Matrix II in Python
- Maximum sum rectangle in a 2D matrix
- Print a 2D Array or Matrix in Java
- Construct a linked list from 2D matrix in C++
- Print concentric rectangular pattern in a 2d matrix in C++
- Maximum sum rectangle in a 2D matrix | DP-27 in C++
- Construct a linked list from 2D matrix (Iterative Approach) in C++
- How to plot a 2D matrix in Python with colorbar Matplotlib?
- Check for possible path in 2D matrix in C++
- Find the number of distinct islands in a 2D matrix in Python
- Prefix Sum of Matrix (Or 2D Array) in C++
- Search a string in Matrix Using Split function in Java
- Breadth First Search on Matrix in C++
- Breadth First Search on Matrix in Python
- Matrix product of a 2D (first argument) and a 1D array (second argument) in Numpy

Advertisements