# Search in a row wise and column wise sorted matrix

Given an n x n matrix and a number x, find position of x in the matrix if it is present in it. Else print “Not Found”. In the given matrix, every row and column is sorted in increasing order. The designed algorithm should have linear time complexity.

Example :

```Input : mat[4][4] = { {10, 20, 30, 40},
{15, 25, 35, 45},
{27, 29, 37, 48},
{32, 33, 39, 50}};
x = 29
Output : Found at (2, 1)

Input : mat[4][4] = { {10, 20, 30, 40},
{15, 25, 35, 45},
{27, 29, 37, 48},
{32, 33, 39, 50}};
x = 100
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

A simple solution is to search one by one. Time complexity of this solution is O(n2).

A better solution is to use Divide and Conquer to find the element. Time complexity of this solution is O(n1.58). Please refer this article for details.

Below is an efficient solution that works in O(n) time.
2) Loop: compare this element e with x
….i) if they are equal then return its position
…ii) e < x then move it to down (if out of bound of matrix then break return false) ..iii) e > x then move it to left (if out of bound of matrix then break return false)
3) repeat the i), ii) and iii) till you find element or returned false

Thanks to devendraiiit for suggesting below approach.

Implementation:

## C

```// C program to search an element in row-wise
// and column-wise sorted matrix
#include<stdio.h>

/* Searches the element x in mat[][]. If the
element is found, then prints its position
and returns true, otherwise prints "not found"
and returns false */
int search(int mat[4][4], int n, int x)
{
int i = 0, j = n-1;  //set indexes for top right element
while ( i < n && j >= 0 )
{
if ( mat[i][j] == x )
{
printf("n Found at %d, %d", i, j);
return 1;
}
if ( mat[i][j] > x )
j--;
else //  if mat[i][j] < x
i++;
}

return 0;  // if ( i==n || j== -1 )
}

// driver program to test above function
int main()
{
int mat[4][4] = { {10, 20, 30, 40},
{15, 25, 35, 45},
{27, 29, 37, 48},
{32, 33, 39, 50},
};
search(mat, 4, 29);
return 0;
}
```

## Java

```// JAVA Code for Search in a row wise and
// column wise sorted matrix

class GFG{

/* Searches the element x in mat[][]. If the
element is found, then prints its position
and returns true, otherwise prints "not found"
and returns false */
private static void search(int[][] mat, int n, int x) {

int i = 0, j = n-1;  //set indexes for top right
// element

while ( i < n && j >= 0 )
{
if ( mat[i][j] == x )
{
System.out.print("n Found at "+ i + " " + j);
return;
}
if ( mat[i][j] > x )
j--;
else //  if mat[i][j] < x
i++;
}

return;  // if ( i==n || j== -1 )

}
// driver program to test above function
public static void main(String[] args) {
int mat[][] = { {10, 20, 30, 40},
{15, 25, 35, 45},
{27, 29, 37, 48},
{32, 33, 39, 50} };

search(mat, 4, 29);
}

}
// This code is contributed by Arnav Kr. Mandal.
```

Output :
`n Found at 2, 1`

Time Complexity: O(n)

The above approach will also work for m x n matrix (not only for n x n). Complexity would be O(m + n).

Related Article :
Search element in a sorted matrix

### Asked in: Inmobi,Accolite,Directi ,Groupon,MakeMyTrip,Ola-Cabs,One97,Oracle,Paytm,Polycom,TinyOwl,Visa

Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same problem.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
2.6 Average Difficulty : 2.6/5.0
Based on 153 vote(s)

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.