Saturday, September 21, 2024
Google search engine
HomeData Modelling & AISearch element in a sorted matrix

Search element in a sorted matrix

Given a sorted matrix mat[n][m] and an element ā€˜xā€™. Find the position of x in the matrix if it is present, else print -1. Matrix is sorted in a way such that all elements in a row are sorted in increasing order and for row ā€˜iā€™, where 1 <= i <= n-1, the first element of row ā€˜iā€™ is greater than or equal to the last element of row ā€˜i-1ā€™. The approach should have O(log n + log m) time complexity.

Examples:Ā 

Input : mat[][] = { {1, 5, 9},
                    {14, 20, 21},
                    {30, 34, 43} }
        x = 14
Output : Found at (1, 0)

Input : mat[][] = { {1, 5, 9, 11},
                    {14, 20, 21, 26},
                    {30, 34, 43, 50} }
        x = 42
Output : -1

The Brute Force and Easy Way To do it:

Ā The Approach: the approach is very simple that we use to have linear search/mapping thing.

C++




#include <iostream>
#include<bits/stdc++.h>
using namespace std;
Ā 
int main() {
Ā Ā Ā Ā int n = 4; // no. of rows
Ā Ā Ā Ā int m = 5; // no. of columns
Ā Ā Ā Ā int a[n][m] = {{0, 6, 8, 9, 11},
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {20, 22, 28, 29, 31},
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {36, 38, 50, 61, 63},
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {64, 66, 100, 122, 128}};
Ā Ā Ā Ā int k = 31; // element to search
Ā Ā Ā Ā map<int,pair<int,int>>mp;
Ā Ā Ā Ā for(int i=0;i<n;i++){
Ā Ā Ā Ā Ā for(int j=0;j<m;j++){
Ā Ā Ā Ā Ā Ā Ā Ā if(k==a[i][j]){
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout<<"Found at ("<<i<<","<<j<<")"<<endl;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā mp[a[i][j]]={i,j};
Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā Ā if(mp.find(k)!=mp.end()){
Ā Ā Ā Ā Ā Ā //cout<<"("<<mp[k]<<",)"<<endl;
Ā Ā Ā Ā Ā Ā cout<<"This is how we can found using mapping: "<<endl;
Ā Ā Ā Ā Ā Ā cout<<"("<<mp[k].first<<","<<mp[k].second<<")"<<endl;
Ā Ā Ā Ā }else{
Ā Ā Ā Ā Ā cout<<"Not Found"<<endl;
Ā Ā Ā Ā }
Ā Ā Ā Ā return 0;
}


Java




import java.util.HashMap;
import java.util.Map;
Ā 
class Main {
Ā Ā public static void main(String[] args) {
Ā Ā Ā Ā int n = 4; // no. of rows
Ā Ā Ā Ā int m = 5; // no. of columns
Ā Ā Ā Ā int[][] a = { { 0, 6, 8, 9, 11 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 20, 22, 28, 29, 31 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 36, 38, 50, 61, 63 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 64, 66, 100, 122, 128 } };
Ā Ā Ā Ā int k = 31; // element to search
Ā 
Ā Ā Ā Ā // Building the map
Ā Ā Ā Ā Map<Integer, int[]> mp = new HashMap<>();
Ā Ā Ā Ā for (int i = 0; i < n; i++) {
Ā Ā Ā Ā Ā Ā for (int j = 0; j < m; j++) {
Ā Ā Ā Ā Ā Ā Ā Ā if (k == a[i][j]) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("Found at (" + i + "," + j + ")");
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā mp.put(a[i][j], new int[] { i, j });
Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Checking if coordinate is found
Ā Ā Ā Ā if (mp.containsKey(k)) {
Ā Ā Ā Ā Ā Ā System.out.println("This is how we can found using mapping: ");
Ā Ā Ā Ā Ā Ā int[] indexes = mp.get(k);
Ā Ā Ā Ā Ā Ā System.out.println("(" + indexes[0] + "," + indexes[1] + ")");
Ā Ā Ā Ā } else {
Ā Ā Ā Ā Ā Ā System.out.println("Not Found");
Ā Ā Ā Ā }
Ā Ā }
}


Python3




# Python program.
Ā 
n = 4 # no. of rows
m = 5 # no. of columns
a = [[0, 6, 8, 9, 11],
Ā Ā Ā Ā Ā [20, 22, 28, 29, 31],
Ā Ā Ā Ā Ā [36, 38, 50, 61, 63],
Ā Ā Ā Ā Ā [64, 66, 100, 122, 128]]
Ā 
k = 31 # element to search
mp = {}
Ā 
for i in range(n):
Ā Ā Ā Ā for j in range(m):
Ā Ā Ā Ā Ā Ā Ā Ā if(k==a[i][j]):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print("Found at (", i, ",", j, ")")
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā mp[a[i][j]] = [i, j]
Ā 
Ā Ā Ā Ā Ā 
if k in mp:
Ā Ā # cout<<"("<<mp[k]<<",)"<<endl;
Ā Ā print("This is how we can found using mapping: ")
Ā Ā print("(", mp[k][0], ",", mp[k][1], ")")
else:
Ā Ā Ā Ā print("Not Found")
Ā 
# The code is contributed by Gautam goel.


C#




// C# code to implement the approach
Ā 
using System;
using System.Collections.Generic;
Ā 
class MainClass {
Ā Ā Ā Ā public static void Main(string[] args)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int n = 4; // no. of rows
Ā Ā Ā Ā Ā Ā Ā Ā int m = 5; // no. of columns
Ā Ā Ā Ā Ā Ā Ā Ā int[, ] a = { { 0, 6, 8, 9, 11 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 20, 22, 28, 29, 31 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 36, 38, 50, 61, 63 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 64, 66, 100, 122, 128 } };
Ā Ā Ā Ā Ā Ā Ā Ā int k = 31; // element to search
Ā Ā Ā Ā Ā Ā Ā Ā Dictionary<int, Tuple<int, int> > mp
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new Dictionary<int, Tuple<int, int> >();
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 0; j < m; j++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (k == a[i, j]) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine("Found at ({0},{1})",
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i, j);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā mp[a[i, j]] = Tuple.Create(i, j);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā if (mp.ContainsKey(k)) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "This is how we can found using mapping: ");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine("({0},{1})", mp[k].Item1,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā mp[k].Item2);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine("Not Found");
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
}
Ā 
// This code is contributed by phasing17


Javascript




// JS code to implement the approach
Ā 
let n = 4; // no. of rows
let m = 5; // no. of columns
let a = [[0, 6, 8, 9, 11],
Ā Ā Ā Ā Ā Ā Ā Ā [20, 22, 28, 29, 31],
Ā Ā Ā Ā Ā Ā Ā Ā [36, 38, 50, 61, 63],
Ā Ā Ā Ā Ā Ā Ā Ā [64, 66, 100, 122, 128]];
let k = 31; // element to search
Ā 
// initializing Map
let mp = new Map();
Ā 
// Building map and checking for match
for(let i=0;i<n;i++){
Ā for(let j=0;j<m;j++){
Ā Ā Ā Ā if(k==a[i][j]){
Ā Ā Ā Ā Ā Ā console.log(`Found at (${i},${j})`);
Ā Ā Ā Ā }
Ā Ā Ā Ā mp.set(a[i][j], [i,j]);
Ā Ā }
}
Ā 
// Displaying result
if(mp.has(k)){
Ā Ā Ā Ā console.log("This is how we can found using mapping: ");
Ā Ā Ā Ā console.log(`(${mp.get(k)[0]},${mp.get(k)[1]})`);
}else{
Ā console.log("Not Found");
}
Ā 
// This code is contributed by phasing17


Output

Found at (1,4)
This is how we can found using mapping: 
(1,4)

Time complexity: O(n+m), For traversing.
Auxiliary Space: O(n+m), For mapping.

Please note that this problem is different from Search in a row-wise and column-wise sorted matrix. Here matrix is more strictly sorted as the first element of a row is greater than the last element of the previous row.

A Simple Solution is to one by one compare x with every element of the matrix. If matches, then return the position. If we reach the end, return -1. The time complexity of this solution is O(n x m).

An efficient solution is to typecast a given 2D array to a 1D array, then apply binary search on the typecasted array but will require extra space to store this array.

Another efficient approach that doesnā€™t require typecasting is explained below.Ā 

1) Perform binary search on the middle column 
   till only two elements are left or till the
   middle element of some row in the search is
   the required element 'x'. This search is done
   to skip the rows that are not required
2) The two left elements must be adjacent. Consider
   the rows of two elements and do following
   a) check whether the element 'x' equals to the 
      middle element of any one of the 2 rows
   b) otherwise according to the value of the 
      element 'x' check whether it is present in 
      the 1st half of 1st row, 2nd half of 1st row, 
      1st half of 2nd row or 2nd half of 2nd row. 

Note: This approach works for the matrix n x m 
      where 2 <= n. The algorithm can be modified
      for matrix 1 x m, we just need to check whether
      2nd row exists or not      

Example:Ā Ā 

Consider:    | 1  2  3  4| 
x = 3, mat = | 5  6  7  8|   Middle column:
             | 9 10 11 12|    = {2, 6, 10, 14}
             |13 14 15 16|   perform binary search on them
                             since, x < 6, discard the 
                             last 2 rows as 'a' will 
                             not lie in them(sorted matrix)
Now, only two rows are left
             | 1  2  3  4| 
x = 3, mat = | 5  6  7  8|   Check whether element is present
                             on the middle elements of these
                             rows = {2, 6}
                             x != 2 or 6
If not, consider the four sub-parts
1st half of 1st row = {1}, 2nd half of 1st row = {3, 4}
1st half of 2nd row = {5}, 2nd half of 2nd row = {7, 8}

According the value of 'x' it will be searched in the
2nd half of 1st row = {3, 4} and found at (i, j): (0, 2)                              

Implementation:

C++




// C++ implementation to search an element in a
// sorted matrix
#include <bits/stdc++.h>
using namespace std;
Ā 
const int MAX = 100;
Ā 
// This function does Binary search for x in i-th
// row. It does the search from mat[i][j_low] to
// mat[i][j_high]
void binarySearch(int mat[][MAX], int i, int j_low,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int j_high, int x)
{
Ā Ā Ā Ā while (j_low <= j_high)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int j_mid = (j_low + j_high) / 2;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Element found
Ā Ā Ā Ā Ā Ā Ā Ā if (mat[i][j_mid] == x)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout << "Found at (" << i << ", "
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā << j_mid << ")";
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā else if (mat[i][j_mid] > x)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j_high = j_mid - 1;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j_low = j_mid + 1;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // element not found
Ā Ā Ā Ā cout << "Element no found";
}
Ā 
// Function to perform binary search on the mid
// values of row to get the desired pair of rows
// where the element can be found
void sortedMatrixSearch(int mat[][MAX], int n,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int m, int x)
{
Ā Ā Ā Ā // Single row matrix
Ā Ā Ā Ā if (n == 1)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, 0, 0, m-1, x);
Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Do binary search in middle column.
Ā Ā Ā Ā // Condition to terminate the loop when the
Ā Ā Ā Ā // 2 desired rows are found
Ā Ā Ā Ā int i_low = 0;
Ā Ā Ā Ā int i_high = n-1;
Ā Ā Ā Ā int j_mid = m/2;
Ā Ā Ā Ā while ((i_low+1) < i_high)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int i_mid = (i_low + i_high) / 2;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // element found
Ā Ā Ā Ā Ā Ā Ā Ā if (mat[i_mid][j_mid] == x)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout << "Found at (" << i_mid << ", "
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā << j_mid << ")";
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā else if (mat[i_mid][j_mid] > x)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i_high = i_mid;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i_low = i_mid;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // If element is present on the mid of the
Ā Ā Ā Ā // two rows
Ā Ā Ā Ā if (mat[i_low][j_mid] == x)
Ā Ā Ā Ā Ā Ā Ā Ā cout << "Found at (" << i_low << ","
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā << j_mid << ")";
Ā Ā Ā Ā else if (mat[i_low+1][j_mid] == x)
Ā Ā Ā Ā Ā Ā Ā Ā cout << "Found at (" << (i_low+1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā << ", " << j_mid << ")";
Ā 
Ā Ā Ā Ā // search element on 1st half of 1st row
Ā Ā Ā Ā else if (x <= mat[i_low][j_mid-1])
Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low, 0, j_mid-1, x);
Ā 
Ā Ā Ā Ā // Search element on 2nd half of 1st row
Ā Ā Ā Ā else if (x >= mat[i_low][j_mid+1]Ā  &&
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā x <= mat[i_low][m-1])
Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low, j_mid+1, m-1, x);
Ā 
Ā Ā Ā Ā // Search element on 1st half of 2nd row
Ā Ā Ā Ā else if (x <= mat[i_low+1][j_mid-1])
Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low+1, 0, j_mid-1, x);
Ā 
Ā Ā Ā Ā // search element on 2nd half of 2nd row
Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low+1, j_mid+1, m-1, x);
}
Ā 
// Driver program to test above
int main()
{
Ā Ā Ā Ā int n = 4, m = 5, x = 8;
Ā Ā Ā Ā int mat[][MAX] = {{0, 6, 8, 9, 11},
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {20, 22, 28, 29, 31},
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {36, 38, 50, 61, 63},
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {64, 66, 100, 122, 128}};
Ā 
Ā Ā Ā Ā sortedMatrixSearch(mat, n, m, x);
Ā Ā Ā Ā return 0;
}


Java




// java implementation to search
// an element in a sorted matrix
import java.io.*;
Ā 
class GFG
{
Ā Ā Ā Ā static int MAX = 100;
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā // This function does Binary search for x in i-th
Ā Ā Ā Ā // row. It does the search from mat[i][j_low] to
Ā Ā Ā Ā // mat[i][j_high]
Ā Ā Ā Ā static void binarySearch(int mat[][], int i, int j_low,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int j_high, int x)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā while (j_low <= j_high)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int j_mid = (j_low + j_high) / 2;
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Element found
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (mat[i][j_mid] == x)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println ( "Found at (" + i
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + ", " + j_mid +")");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else if (mat[i][j_mid] > x)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j_high = j_mid - 1;
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j_low = j_mid + 1;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // element not found
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println ( "Element no found");
Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā // Function to perform binary search on the mid
Ā Ā Ā Ā // values of row to get the desired pair of rows
Ā Ā Ā Ā // where the element can be found
Ā Ā Ā Ā static void sortedMatrixSearch(int mat[][], int n,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int m, int x)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // Single row matrix
Ā Ā Ā Ā Ā Ā Ā Ā if (n == 1)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, 0, 0, m - 1, x);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Do binary search in middle column.
Ā Ā Ā Ā Ā Ā Ā Ā // Condition to terminate the loop when the
Ā Ā Ā Ā Ā Ā Ā Ā // 2 desired rows are found
Ā Ā Ā Ā Ā Ā Ā Ā int i_low = 0;
Ā Ā Ā Ā Ā Ā Ā Ā int i_high = n - 1;
Ā Ā Ā Ā Ā Ā Ā Ā int j_mid = m / 2;
Ā Ā Ā Ā Ā Ā Ā Ā while ((i_low + 1) < i_high)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int i_mid = (i_low + i_high) / 2;
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // element found
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (mat[i_mid][j_mid] == x)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println ( "Found at (" + i_mid +", "
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + j_mid +")");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else if (mat[i_mid][j_mid] > x)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i_high = i_mid;
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i_low = i_mid;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // If element is present on
Ā Ā Ā Ā Ā Ā Ā Ā // the mid of the two rows
Ā Ā Ā Ā Ā Ā Ā Ā if (mat[i_low][j_mid] == x)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println ( "Found at (" + i_low + ","
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + j_mid +")");
Ā Ā Ā Ā Ā Ā Ā Ā else if (mat[i_low + 1][j_mid] == x)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println ( "Found at (" + (i_low + 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + ", " + j_mid +")");
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // search element on 1st half of 1st row
Ā Ā Ā Ā Ā Ā Ā Ā else if (x <= mat[i_low][j_mid - 1])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low, 0, j_mid - 1, x);
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Search element on 2nd half of 1st row
Ā Ā Ā Ā Ā Ā Ā Ā else if (x >= mat[i_low][j_mid + 1] &&
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā x <= mat[i_low][m - 1])
Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low, j_mid + 1, m - 1, x);
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Search element on 1st half of 2nd row
Ā Ā Ā Ā Ā Ā Ā Ā else if (x <= mat[i_low + 1][j_mid - 1])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low + 1, 0, j_mid - 1, x);
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // search element on 2nd half of 2nd row
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low + 1, j_mid + 1, m - 1, x);
Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā // Driver program
Ā Ā Ā Ā public static void main (String[] args)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int n = 4, m = 5, x = 8;
Ā Ā Ā Ā Ā Ā Ā Ā int mat[][] = {{0, 6, 8, 9, 11},
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {20, 22, 28, 29, 31},
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {36, 38, 50, 61, 63},
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {64, 66, 100, 122, 128}};
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā sortedMatrixSearch(mat, n, m, x);
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā }
}
Ā 
// This code is contributed by vt_m


Python3




# Python3 implementation
# to search an element in a
# sorted matrix
MAX = 100
Ā 
# This function does Binary
# search for x in i-th
# row. It does the search
# from mat[i][j_low] to
# mat[i][j_high]
def binarySearch(mat, i, j_low,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j_high, x):
Ā 
Ā Ā Ā Ā while (j_low <= j_high):
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā j_mid = (j_low + j_high) // 2
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # Element found
Ā Ā Ā Ā Ā Ā Ā Ā if (mat[i][j_mid] == x):
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print("Found at (", i, ",", j_mid, ")")
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā elif (mat[i][j_mid] > x):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j_high = j_mid - 1
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j_low = j_mid + 1
Ā Ā Ā Ā 
Ā Ā Ā Ā # Element not found
Ā Ā Ā Ā print ("Element no found")
Ā 
# Function to perform binary
# search on the mid values of
# row to get the desired pair of rows
# where the element can be found
def sortedMatrixSearch(mat, n, m, x):
Ā 
Ā Ā Ā Ā # Single row matrix
Ā Ā Ā Ā if (n == 1):
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, 0, 0, m - 1, x)
Ā Ā Ā Ā Ā Ā Ā Ā return
Ā 
Ā Ā Ā Ā # Do binary search in middle column.
Ā Ā Ā Ā # Condition to terminate the loop
Ā Ā Ā Ā # when the 2 desired rows are found
Ā Ā Ā Ā i_low = 0
Ā Ā Ā Ā i_high = n - 1
Ā Ā Ā Ā j_mid = m // 2
Ā Ā Ā Ā while ((i_low + 1) < i_high):
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā i_mid = (i_low + i_high) // 2
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # element found
Ā Ā Ā Ā Ā Ā Ā Ā if (mat[i_mid][j_mid] == x):
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print ("Found at (", i_mid, ",", j_mid, ")")
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā elif (mat[i_mid][j_mid] > x):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i_high = i_mid
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i_low = i_mid
Ā 
Ā Ā Ā Ā # If element is present on the mid of the
Ā Ā Ā Ā # two rows
Ā Ā Ā Ā if (mat[i_low][j_mid] == x):
Ā Ā Ā Ā Ā Ā Ā Ā print ("Found at (" , i_low, ",", j_mid , ")")
Ā Ā Ā Ā elif (mat[i_low + 1][j_mid] == x):
Ā Ā Ā Ā Ā Ā Ā Ā print ("Found at (", (i_low + 1), ",", j_mid, ")")
Ā 
Ā Ā Ā Ā # search element on 1st half of 1st row
Ā Ā Ā Ā elif (x <= mat[i_low][j_mid - 1]):
Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low, 0, j_mid - 1, x)
Ā 
Ā Ā Ā Ā # Search element on 2nd half of 1st row
Ā Ā Ā Ā elif (x >= mat[i_low][j_mid + 1] and
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā x <= mat[i_low][m - 1]):
Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low, j_mid + 1, m - 1, x)
Ā 
Ā Ā Ā Ā # Search element on 1st half of 2nd row
Ā Ā Ā Ā elif (x <= mat[i_low + 1][j_mid - 1]):
Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low + 1, 0, j_mid - 1, x)
Ā Ā 
Ā Ā Ā Ā # Search element on 2nd half of 2nd row
Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low + 1, j_mid + 1, m - 1, x)
Ā 
# Driver program to test above
if __name__ == "__main__":
Ā 
Ā Ā Ā Ā n = 4
Ā Ā Ā Ā m = 5
Ā Ā Ā Ā x = 8
Ā Ā Ā Ā mat = [[0, 6, 8, 9, 11],
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [20, 22, 28, 29, 31],
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [36, 38, 50, 61, 63],
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [64, 66, 100, 122, 128]]
Ā Ā Ā Ā sortedMatrixSearch(mat, n, m, x)
Ā Ā Ā Ā 
# This code is contributed by Chitranayal


C#




// C# implementation to search
// an element in a sorted matrix
using System;
Ā 
class GFG
{
Ā Ā Ā Ā // This function does Binary search for x in i-th
Ā Ā Ā Ā // row. It does the search from mat[i][j_low] to
Ā Ā Ā Ā // mat[i][j_high]
Ā Ā Ā Ā static void binarySearch(int [,]mat, int i, int j_low,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int j_high, int x)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā while (j_low <= j_high)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int j_mid = (j_low + j_high) / 2;
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Element found
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (mat[i,j_mid] == x)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.Write ( "Found at (" + i +
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ", " + j_mid +")");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else if (mat[i,j_mid] > x)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j_high = j_mid - 1;
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j_low = j_mid + 1;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // element not found
Ā Ā Ā Ā Ā Ā Ā Ā Console.Write ( "Element no found");
Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā // Function to perform binary search on the mid
Ā Ā Ā Ā // values of row to get the desired pair of rows
Ā Ā Ā Ā // where the element can be found
Ā Ā Ā Ā static void sortedMatrixSearch(int [,]mat, int n,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int m, int x)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // Single row matrix
Ā Ā Ā Ā Ā Ā Ā Ā if (n == 1)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, 0, 0, m - 1, x);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Do binary search in middle column.
Ā Ā Ā Ā Ā Ā Ā Ā // Condition to terminate the loop when the
Ā Ā Ā Ā Ā Ā Ā Ā // 2 desired rows are found
Ā Ā Ā Ā Ā Ā Ā Ā int i_low = 0;
Ā Ā Ā Ā Ā Ā Ā Ā int i_high = n - 1;
Ā Ā Ā Ā Ā Ā Ā Ā int j_mid = m / 2;
Ā Ā Ā Ā Ā Ā Ā Ā while ((i_low + 1) < i_high)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int i_mid = (i_low + i_high) / 2;
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // element found
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (mat[i_mid,j_mid] == x)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.Write ( "Found at (" + i_mid +
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ", "Ā Ā Ā  + j_mid +")");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else if (mat[i_mid,j_mid] > x)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i_high = i_mid;
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i_low = i_mid;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // If element is present on
Ā Ā Ā Ā Ā Ā Ā Ā // the mid of the two rows
Ā Ā Ā Ā Ā Ā Ā Ā if (mat[i_low,j_mid] == x)
Ā Ā Ā Ā Ā Ā Ā Ā Console.Write ( "Found at (" + i_low +
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "," + j_mid +")");
Ā Ā Ā Ā Ā Ā Ā Ā else if (mat[i_low + 1,j_mid] == x)
Ā Ā Ā Ā Ā Ā Ā Ā Console.Write ( "Found at (" + (i_low
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + 1) + ", " + j_mid +")");
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // search element on 1st half of 1st row
Ā Ā Ā Ā Ā Ā Ā Ā else if (x <= mat[i_low,j_mid - 1])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low, 0, j_mid - 1, x);
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Search element on 2nd half of 1st row
Ā Ā Ā Ā Ā Ā Ā Ā else if (x >= mat[i_low,j_mid + 1] &&
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā x <= mat[i_low,m - 1])
Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low, j_mid + 1, m - 1, x);
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Search element on 1st half of 2nd row
Ā Ā Ā Ā Ā Ā Ā Ā else if (x <= mat[i_low + 1,j_mid - 1])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low + 1, 0, j_mid - 1, x);
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // search element on 2nd half of 2nd row
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low + 1, j_mid + 1, m - 1, x);
Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā // Driver program
Ā Ā Ā Ā public static void Main (String[] args)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int n = 4, m = 5, x = 8;
Ā Ā Ā Ā Ā Ā Ā Ā int [,]mat = {{0, 6, 8, 9, 11},
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {20, 22, 28, 29, 31},
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {36, 38, 50, 61, 63},
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {64, 66, 100, 122, 128}};
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā sortedMatrixSearch(mat, n, m, x);
Ā Ā Ā Ā }
}
Ā 
// This code is contributed by parashar...


Javascript




<script>
Ā 
// Javascript implementation to search
// an element in a sorted matrix
let MAX = 100;
Ā 
// This function does Binary search for x in i-th
// row. It does the search from mat[i][j_low] to
// mat[i][j_high]
function binarySearch(mat, i, j_low, j_high, x)
{
Ā Ā Ā Ā while (j_low <= j_high)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā let j_mid = Math.floor((j_low + j_high) / 2);
Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Element found
Ā Ā Ā Ā Ā Ā Ā Ā if (mat[i][j_mid] == x)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā document.write("Found at (" + i + ", " +
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j_mid +")");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā else if (mat[i][j_mid] > x)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j_high = j_mid - 1;
Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j_low = j_mid + 1;
Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā // Element not found
Ā Ā Ā Ā document.write( "Element no found<br>");
}
Ā 
// Function to perform binary search on the mid
// values of row to get the desired pair of rows
// where the element can be found
function sortedMatrixSearch(mat, n, m, x)
{
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā // Single row matrix
Ā Ā Ā Ā if (n == 1)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, 0, 0, m - 1, x);
Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā }
Ā Ā 
Ā Ā Ā Ā // Do binary search in middle column.
Ā Ā Ā Ā // Condition to terminate the loop when the
Ā Ā Ā Ā // 2 desired rows are found
Ā Ā Ā Ā let i_low = 0;
Ā Ā Ā Ā let i_high = n - 1;
Ā Ā Ā Ā let j_mid = Math.floor(m / 2);
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā while ((i_low + 1) < i_high)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā let i_mid = Math.floor((i_low + i_high) / 2);
Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Element found
Ā Ā Ā Ā Ā Ā Ā Ā if (mat[i_mid][j_mid] == x)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā document.write("Found at (" + i_mid +
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ", " + j_mid +")");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā else if (mat[i_mid][j_mid] > x)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i_high = i_mid;
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i_low = i_mid;
Ā Ā Ā Ā }
Ā Ā 
Ā Ā Ā Ā // If element is present on
Ā Ā Ā Ā // the mid of the two rows
Ā Ā Ā Ā if (mat[i_low][j_mid] == x)
Ā Ā Ā Ā Ā Ā Ā Ā document.write("Found at (" + i_low +
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "," + j_mid +")");
Ā Ā Ā Ā else if (mat[i_low + 1][j_mid] == x)
Ā Ā Ā Ā Ā Ā Ā Ā document.write("Found at (" + (i_low + 1) +
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ", " + j_mid +")");
Ā Ā 
Ā Ā Ā Ā // Search element on 1st half of 1st row
Ā Ā Ā Ā else if (x <= mat[i_low][j_mid - 1])
Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low, 0, j_mid - 1, x);
Ā Ā 
Ā Ā Ā Ā // Search element on 2nd half of 1st row
Ā Ā Ā Ā else if (x >= mat[i_low][j_mid + 1] &&
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā x <= mat[i_low][m - 1])
Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low, j_mid + 1,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā m - 1, x);
Ā Ā 
Ā Ā Ā Ā // Search element on 1st half of 2nd row
Ā Ā Ā Ā else if (x <= mat[i_low + 1][j_mid - 1])
Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low + 1, 0,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j_mid - 1, x);
Ā Ā 
Ā Ā Ā Ā // search element on 2nd half of 2nd row
Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(mat, i_low + 1, j_mid + 1,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā m - 1, x);
}
Ā 
// Driver code
let n = 4, m = 5, x = 8;
let mat = [ [ 0, 6, 8, 9, 11 ],
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 20, 22, 28, 29, 31 ],
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 36, 38, 50, 61, 63 ],
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 64, 66, 100, 122, 128 ] ];
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
sortedMatrixSearch(mat, n, m, x);
Ā 
// This code is contributed by ab2127
Ā 
</script>


Output

Found at (0,2)

Time complexity: O(log n + log m). O(Log n) time is required to find the two desired rows. Then O(Log m) time is required for binary search in one of the four parts with a size equal to m/2.
Auxiliary Space: O(1)

This method is contributed by Ayush Jauhari.Ā 

Method 2: Using binary search in 2 dimensions

This method also has the same time complexity: O(log(m) + log(n)) and auxiliary space: O(1), but the algorithm is much easier and the code way cleaner to understand.

Approach: We can observe that any number (say k) that we want to find, must exist within a row, including the first and last elements of the row (if it exists at all). So we first find the row in which k must lie using binary search ( O(log N) ) and then use binary search again to search in that row( O(log M) ).

Algorithm:

1) first weā€™ll find the correct row, where k=2 might exist. To do this we will simultaneously apply binary search on the first and last column. Ā Ā 

Ā  Ā  low=0, high=n-1

Ā  Ā  Ā i) if( k< first element of row(a[mid][0]) ) => k must exist in the row above

Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  => high=mid-1;

Ā  Ā  Ā ii) if( k> last element of row(a[mid][m-1])) => k must exist in the row below

Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā => low=mid+1; Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā Ā 

Ā  Ā  Ā iii) if( k> first element of row(a[mid][0]) && Ā k< last element of row(a[mid][m-1]))

Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā => k must exist in this row

Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā => apply binary search in this row like in a 1-D array

Ā  Ā  Ā iv) i) if( k== first element of row(a[mid][0]) || Ā k== last element of row(a[mid][m-1])) => found Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā Ā 

Example:
let k=2; n=3,m=4;
    matrix a: [0, 1, 2, 3 ]
              [10,11,12,13]
              [20,21,22,23]

1) low=0, high=n-1(=2) => mid=1 //check 1st row     [0....3]
                                                 -->[10...13]<-- 
                                                    [20...23]                                                        
   k < a[mid][0] => high = mid-1;(=1)
2) low=0, high=1; =>mid=0; //check 0th row    -->[0...3]<--  
    k>a[mid][0] && k<a[mid][m-1]  => k must exist in this row
    
    now simply apply binary search in 1-D array: [0,1,2,3]                              

Below is the implementation of the above algorithm:

C++




//C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
Ā 
const int MAX = 100;
Ā 
void binarySearch(int a[][MAX], int n, int m, int k, int x)
// x is the row number
{
Ā Ā Ā Ā // now we simply have to apply binary search as we
Ā Ā Ā Ā // did in a 1-D array, for the elements in row
Ā Ā Ā Ā // number
Ā Ā Ā Ā // x
Ā 
Ā Ā Ā Ā int l = 0, r = m - 1, mid;
Ā Ā Ā Ā while (l <= r)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā mid = (l + r) / 2;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (a[x][mid] == k)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout << "Found at (" << x << "," << mid << ")" << endl;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (a[x][mid] > k)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = mid - 1;
Ā Ā Ā Ā Ā Ā Ā Ā if (a[x][mid] < k)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = mid + 1;
Ā Ā Ā Ā }
Ā Ā Ā Ā cout << "Element not found" << endl;
}
Ā 
void findRow(int a[][MAX], int n, int m, int k)
{
Ā 
Ā Ā Ā Ā int l = 0, r = n - 1, mid;
Ā 
Ā Ā Ā Ā while (l <= r)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā mid = (l + r) / 2;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // we'll check the left and
Ā Ā Ā Ā Ā Ā Ā Ā // right most elements
Ā Ā Ā Ā Ā Ā Ā Ā // of the row here itself
Ā Ā Ā Ā Ā Ā Ā Ā // for efficiency
Ā Ā Ā Ā Ā Ā Ā Ā if (k == a[mid][0]) // checking leftmost element
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout << "Found at (" << mid << ",0)" << endl;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (k == a[mid][m - 1]) // checking rightmost
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // element
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int t = m - 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout << "Found at (" << mid << "," << t << ")" << endl;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (k > a[mid][0] && k < a[mid][m - 1])
Ā Ā Ā Ā Ā Ā Ā Ā // this means the element
Ā Ā Ā Ā Ā Ā Ā Ā // must be within this row
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(a, n, m, k, mid);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // we'll apply binary
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // search on this row
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (k < a[mid][0])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = mid - 1;
Ā Ā Ā Ā Ā Ā Ā Ā if (k > a[mid][m - 1])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = mid + 1;
Ā Ā Ā Ā }
}
Ā 
//Driver Code
int main()
{
Ā Ā Ā Ā int n = 4; // no. of rows
Ā Ā Ā Ā int m = 5; // no. of columns
Ā 
Ā Ā Ā Ā int a[][MAX] = {{0, 6, 8, 9, 11},
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {20, 22, 28, 29, 31},
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {36, 38, 50, 61, 63},
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {64, 66, 100, 122, 128}};
Ā 
Ā Ā Ā Ā int k = 31; // element to search
Ā 
Ā 
Ā Ā Ā Ā findRow(a, n, m, k);
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā return 0;
}
// This code is contributed by nirajgusain5


Java




// Java program for the above approach
import java.util.*;
public class Main {
Ā 
Ā Ā Ā Ā static void findRow(int[][] a, int n, int m, int k)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int l = 0, r = n - 1, mid;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā while (l <= r) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā mid = (l + r) / 2;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // we'll check the left and
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // right most elements
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // of the row here itself
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // for efficiency
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (k == a[mid][0]) // checking leftmost element
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("Found at (" + mid + ","
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + "0)");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (k == a[mid][m - 1]) // checking rightmost
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // element
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int t = m - 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("Found at (" + mid + ","
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + t + ")");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (k > a[mid][0]
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā && k < a[mid]
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [m - 1]) // this means the element
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // must be within this row
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(a, n, m, k,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā mid); // we'll apply binary
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // search on this row
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (k < a[mid][0])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = mid - 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (k > a[mid][m - 1])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = mid + 1;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā static void binarySearch(int[][] a, int n, int m, int k,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int x) // x is the row number
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // now we simply have to apply binary search as we
Ā Ā Ā Ā Ā Ā Ā Ā // did in a 1-D array, for the elements in row
Ā Ā Ā Ā Ā Ā Ā Ā // number
Ā Ā Ā Ā Ā Ā Ā Ā // x
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā int l = 0, r = m - 1, mid;
Ā Ā Ā Ā Ā Ā Ā Ā while (l <= r) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā mid = (l + r) / 2;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (a[x][mid] == k) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("Found at (" + x + ","
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + mid + ")");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (a[x][mid] > k)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = mid - 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (a[x][mid] < k)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = mid + 1;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("Element not found");
Ā Ā Ā Ā }
Ā Ā Ā 
Ā Ā Ā Ā // Driver Code
Ā Ā Ā Ā public static void main(String args[])
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int n = 4; // no. of rows
Ā Ā Ā Ā Ā Ā Ā Ā int m = 5; // no. of columns
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā int a[][] = { { 0, 6, 8, 9, 11 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 20, 22, 28, 29, 31 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 36, 38, 50, 61, 63 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 64, 66, 100, 122, 128 } };
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā int k = 31; // element to search
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā findRow(a, n, m, k);
Ā Ā Ā Ā }
}


Python3




# Python program for the above approach
def findRow(a, n, m, k):
Ā Ā Ā Ā l = 0
Ā Ā Ā Ā r = n - 1
Ā Ā Ā Ā mid = 0
Ā Ā Ā Ā while (l <= r) :
Ā Ā Ā Ā Ā Ā Ā Ā mid = int((l + r) / 2)
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # we'll check the left and
Ā Ā Ā Ā Ā Ā Ā Ā # right most elements
Ā Ā Ā Ā Ā Ā Ā Ā # of the row here itself
Ā Ā Ā Ā Ā Ā Ā Ā # for efficiency
Ā Ā Ā Ā Ā Ā Ā Ā if(k == a[mid][0]): #checking leftmost element
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print("Found at (" , mid , ",", "0)", sep = "")
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if(k == a[mid][m - 1]): # checking rightmost element
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā t = m - 1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print("Found at (" , mid , ",", t , ")", sep = "")
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return
Ā Ā Ā Ā Ā Ā Ā Ā if(k > a[mid][0] and k < a[mid][m - 1]):Ā Ā Ā  # this means the element
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # must be within this row
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(a, n, m, k, mid)Ā Ā Ā  # we'll apply binary
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # search on this row
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return
Ā Ā Ā Ā Ā Ā Ā Ā if (k < a[mid][0]):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = mid - 1
Ā Ā Ā Ā Ā Ā Ā Ā if (k > a[mid][m - 1]):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = mid + 1
Ā 
def binarySearch(a, n, m, k, x):Ā Ā Ā  #x is the row number
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā # now we simply have to apply binary search as we
Ā Ā Ā Ā # did in a 1-D array, for the elements in row
Ā Ā Ā Ā # number
Ā Ā Ā Ā # x
Ā Ā Ā Ā l = 0
Ā Ā Ā Ā r = m - 1
Ā Ā Ā Ā mid = 0
Ā Ā Ā Ā while (l <= r):
Ā Ā Ā Ā Ā Ā Ā Ā mid = int((l + r) / 2)
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (a[x][mid] == k):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print("Found at (" , x , ",", mid , ")", sep = "")
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return
Ā Ā Ā Ā Ā Ā Ā Ā if (a[x][mid] > k):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = mid - 1
Ā Ā Ā Ā Ā Ā Ā Ā if (a[x][mid] < k):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = mid + 1
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā print("Element not found")
Ā 
# Driver Code
n = 4 # no. of rows
m = 5 # no. of columns
a = [[ 0, 6, 8, 9, 11], [20, 22, 28, 29, 31], [36, 38, 50, 61, 63 ], [64, 66, 100, 122, 128]]
k = 31Ā  # element to search
findRow(a, n, m, k)
Ā 
# This code is contributed by avanitrachhadiya2155


C#




// C# program for the above approach
using System;
public class GFG
{
Ā 
Ā Ā static void findRow(int[,] a, int n, int m, int k)
Ā Ā {
Ā Ā Ā Ā int l = 0, r = n - 1, mid;
Ā 
Ā Ā Ā Ā while (l <= r) {
Ā Ā Ā Ā Ā Ā mid = (l + r) / 2;
Ā 
Ā Ā Ā Ā Ā Ā // we'll check the left and
Ā Ā Ā Ā Ā Ā // right most elements
Ā Ā Ā Ā Ā Ā // of the row here itself
Ā Ā Ā Ā Ā Ā // for efficiency
Ā Ā Ā Ā Ā Ā if (k == a[mid,0]) // checking leftmost element
Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine("Found at (" + mid + ","
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + "0)");
Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā if (k == a[mid,m - 1]) // checking rightmost
Ā Ā Ā Ā Ā Ā Ā Ā // element
Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int t = m - 1;
Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine("Found at (" + mid + ","
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + t + ")");
Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā if (k > a[mid,0]
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā && k < a[mid,m - 1]) // this means the element
Ā Ā Ā Ā Ā Ā Ā Ā // must be within this row
Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(a, n, m, k,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā mid); // we'll apply binary
Ā Ā Ā Ā Ā Ā Ā Ā // search on this row
Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā if (k < a[mid,0])
Ā Ā Ā Ā Ā Ā Ā Ā r = mid - 1;
Ā Ā Ā Ā Ā Ā if (k > a[mid,m - 1])
Ā Ā Ā Ā Ā Ā Ā Ā l = mid + 1;
Ā Ā Ā Ā }
Ā Ā }
Ā 
Ā Ā static void binarySearch(int[,] a, int n, int m, int k,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int x) // x is the row number
Ā Ā {
Ā Ā Ā Ā // now we simply have to apply binary search as we
Ā Ā Ā Ā // did in a 1-D array, for the elements in row
Ā Ā Ā Ā // number
Ā Ā Ā Ā // x
Ā 
Ā Ā Ā Ā int l = 0, r = m - 1, mid;
Ā Ā Ā Ā while (l <= r) {
Ā Ā Ā Ā Ā Ā mid = (l + r) / 2;
Ā 
Ā Ā Ā Ā Ā Ā if (a[x,mid] == k) {
Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine("Found at (" + x + ","
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + mid + ")");
Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā if (a[x,mid] > k)
Ā Ā Ā Ā Ā Ā Ā Ā r = mid - 1;
Ā Ā Ā Ā Ā Ā if (a[x,mid] < k)
Ā Ā Ā Ā Ā Ā Ā Ā l = mid + 1;
Ā Ā Ā Ā }
Ā Ā Ā Ā Console.WriteLine("Element not found");
Ā Ā }
Ā 
Ā Ā // Driver Code
Ā Ā static public void Main ()
Ā Ā {
Ā Ā Ā Ā int n = 4; // no. of rows
Ā Ā Ā Ā int m = 5; // no. of columns
Ā 
Ā Ā Ā Ā int[,] a = { { 0, 6, 8, 9, 11 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 20, 22, 28, 29, 31 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 36, 38, 50, 61, 63 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 64, 66, 100, 122, 128 } };
Ā 
Ā Ā Ā Ā int k = 31; // element to search
Ā 
Ā Ā Ā Ā findRow(a, n, m, k);
Ā Ā }
}
Ā 
// This code is contributed by rag2127


Javascript




<script>
Ā 
// JavaScript program for above approach
var MAX = 100;
Ā 
function binarySearch(a, n, m, k, x)
// x is the row number
{
Ā Ā Ā Ā // now we simply have to apply binary search as we
Ā Ā Ā Ā // did in a 1-D array, for the elements in row
Ā Ā Ā Ā // number
Ā Ā Ā Ā // x
Ā 
Ā Ā Ā Ā var l = 0, r = m - 1, mid;
Ā Ā Ā Ā while (l <= r)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā mid = (l + r) / 2;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (a[x][mid] == k)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā document.write( "Found at (" + x + "," + mid + ")<br>");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (a[x][mid] > k)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = mid - 1;
Ā Ā Ā Ā Ā Ā Ā Ā if (a[x][mid] < k)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = mid + 1;
Ā Ā Ā Ā }
Ā Ā Ā Ā document.write( "Element not found<br>");
}
Ā 
function findRow(a, n, m, k)
{
Ā 
Ā Ā Ā Ā var l = 0, r = n - 1, mid;
Ā 
Ā Ā Ā Ā while (l <= r)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā mid = parseInt((l + r) / 2);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // we'll check the left and
Ā Ā Ā Ā Ā Ā Ā Ā // right most elements
Ā Ā Ā Ā Ā Ā Ā Ā // of the row here itself
Ā Ā Ā Ā Ā Ā Ā Ā // for efficiency
Ā Ā Ā Ā Ā Ā Ā Ā if (k == a[mid][0]) // checking leftmost element
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā document.write( "Found at (" + mid + ",0)<br>");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (k == a[mid][m - 1]) // checking rightmost
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // element
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā var t = m - 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā document.write( "Found at (" + mid + "," + t + ")<br>");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (k > a[mid][0] && k < a[mid][m - 1])
Ā Ā Ā Ā Ā Ā Ā Ā // this means the element
Ā Ā Ā Ā Ā Ā Ā Ā // must be within this row
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā binarySearch(a, n, m, k, mid);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // we'll apply binary
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // search on this row
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (k < a[mid][0])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = mid - 1;
Ā Ā Ā Ā Ā Ā Ā Ā if (k > a[mid][m - 1])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = mid + 1;
Ā Ā Ā Ā }
}
Ā 
//Driver Code
var n = 4; // no. of rows
var m = 5; // no. of columns
var a = [[0, 6, 8, 9, 11],
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [20, 22, 28, 29, 31],
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [36, 38, 50, 61, 63],
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [64, 66, 100, 122, 128]];
var k = 31; // element to search
findRow(a, n, m, k);
Ā 
</script>


Output

Found at (1,4)

Time Complexity: O(log n + log m).Ā 
Auxiliary Space: O(1)

This method is contributed by Ayushwant Gaurav. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Ā 

Feeling lost in the world of random DSA topics, wasting time without progress? Itā€™s time for a change! Join our DSA course, where weā€™ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?