# How to find Kth smallest element in a Sorted Matrix!?

## Given an `n x n``matrix` where each of the rows and columns is sorted in ascending order, return the`kth`smallest element in the matrix.
`        int n = matrix.size();        int low = matrix, high = matrix[n-1][n-1];                while(low < high) {            int mid = low + (high - low)/2;                        if(count(matrix, mid) < k) low = mid + 1;            else high = mid;                    }`
`1    5    710   11   1312   14   18`
`int kthSmallest(vector<vector<int>>& matrix, int k) {        int n = matrix.size();        int low = matrix, high = matrix[n-1][n-1];                while(low < high) {            int mid = low + (high - low)/2;                        if(count(matrix, mid) < k) low = mid + 1;            else high = mid;                    }                return low;    }int count(vector<vector<int>>& grid, int mid) {        int count = 0, n = grid.size();                for(int i=0; i<n; i++) {            for(int j = 0; j<n && grid[i][j] <= mid; j++) count++;        }                return count;    }`