## Search in a row wise and column wise sorted matrix

Given an n x n matrix, where every row and column is sorted in increasing order. Given a number x, how to decide whether this x is in the matrix. The designed algorithm should have linear time complexity.

Thanks to devendraiiit for suggesting below approach.

1) Start with top right element

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

**Implementation:**

#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++; } printf("\n Element not found"); 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); getchar(); return 0; }

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).

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

### Related Tpoics:

- Find the number of zeroes
- Find if there is a subarray with 0 sum
- Divide and Conquer | Set 5 (Strassen’s Matrix Multiplication)
- Count all possible groups of size 2 or 3 that have sum as multiple of 3
- Sort n numbers in range from 0 to n^2 – 1 in linear time
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space
- Suffix Array | Set 1 (Introduction)
- Count all possible paths from top left to bottom right of a mXn matrix

Writing code in comment?Please useideone.comand share the link here.