# Kth Smallest Element in a Sorted Matrix – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.

Given an `n x n`

`matrix`

where each of the rows and columns is sorted in ascending order, return *the* `k`

^{th}*smallest element in the matrix*.

Note that it is the `k`

smallest element ^{th}**in the sorted order**, not the `k`

^{th}**distinct** element.

You must find a solution with a memory complexity better than `O(n`

.^{2})

**Example 1:**

Input:matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8Output:13Explanation:The elements in the matrix are [1,5,9,10,11,12,13,,15], and the 813^{th}smallest number is 13

**Example 2:**

Input:matrix = [[-5]], k = 1Output:-5

**Constraints:**

`n == matrix.length == matrix[i].length`

`1 <= n <= 300`

`-10`

^{9}<= matrix[i][j] <= 10^{9}- All the rows and columns of
`matrix`

are**guaranteed**to be sorted in**non-decreasing order**. `1 <= k <= n`

^{2}

# C++ Kth Smallest Element in a Sorted Matrix LeetCode Solution

class Solution { // 20 ms, faster than 98.92%

public:

int m, n;

int kthSmallest(vector<vector<int>>& matrix, int k) {

m = matrix.size(), n = matrix[0].size(); // For general, the matrix need not be a square

int left = matrix[0][0], right = matrix[m-1][n-1], ans = -1;

while (left <= right) {

int mid = (left + right) >> 1;

if (countLessOrEqual(matrix, mid) >= k) {

ans = mid;

right = mid – 1; // try to looking for a smaller value in the left side

} else left = mid + 1; // try to looking for a bigger value in the right side

}

return ans;

}

int countLessOrEqual(vector<vector<int>>& matrix, int x) {

int cnt = 0, c = n – 1; // start with the rightmost column

for (int r = 0; r < m; ++r) {

while (c >= 0 && matrix[r][c] > x) –c; // decrease column until matrix[r][c] <= x

cnt += (c + 1);

}

return cnt;

}

};

# Java Kth Smallest Element in a Sorted Matrix LeetCode Solution

class Solution { // 0 ms, faster than 100%

int m, n;

public int kthSmallest(int[][] matrix, int k) {

m = matrix.length; n = matrix[0].length; // For general, the matrix need not be a square

int left = matrix[0][0], right = matrix[m-1][n-1], ans = -1;

while (left <= right) {

int mid = (left + right) >> 1;

if (countLessOrEqual(matrix, mid) >= k) {

ans = mid;

right = mid – 1; // try to looking for a smaller value in the left side

} else left = mid + 1; // try to looking for a bigger value in the right side

}

return ans;

}

int countLessOrEqual(int[][] matrix, int x) {

int cnt = 0, c = n – 1; // start with the rightmost column

for (int r = 0; r < m; ++r) {

while (c >= 0 && matrix[r][c] > x) –c; // decrease column until matrix[r][c] <= x

cnt += (c + 1);

}

return cnt;

}

}

# Python 3 Kth Smallest Element in a Sorted Matrix LeetCode Solution

class Solution { // 0 ms, faster than 100%

int m, n;

public int kthSmallest(int[][] matrix, int k) {

m = matrix.length; n = matrix[0].length; // For general, the matrix need not be a square

int left = matrix[0][0], right = matrix[m-1][n-1], ans = -1;

while (left <= right) {

int mid = (left + right) >> 1;

if (countLessOrEqual(matrix, mid) >= k) {

ans = mid;

right = mid – 1; // try to looking for a smaller value in the left side

} else left = mid + 1; // try to looking for a bigger value in the right side

}

return ans;

}

int countLessOrEqual(int[][] matrix, int x) {

int cnt = 0, c = n – 1; // start with the rightmost column

for (int r = 0; r < m; ++r) {

while (c >= 0 && matrix[r][c] > x) –c; // decrease column until matrix[r][c] <= x

cnt += (c + 1);

}

return cnt;

}

}

Array-1180

String-562

Hash Table-412

Dynamic Programming-390

Math-368

Sorting-264

Greedy-257

Depth-First Search-256

Database-215

Breadth-First Search-200

Tree-195

Binary Search-191

Matrix-176

Binary Tree-160

Two Pointers-151

Bit Manipulation-140

Stack-133

Heap (Priority Queue)-117

Design-116

Graph-108

Simulation-103

Prefix Sum-96

Backtracking-92

Counting-86

Sliding Window-73

Linked List-69

Union Find-66

Ordered Set-48

Monotonic Stack-47

Recursion-43

Trie-41

Binary Search Tree-40

Divide and Conquer-40

Enumeration-39

Bitmask-37

Queue-33

Memoization-32

Topological Sort-31

Geometry-30

Segment Tree-27

Game Theory-24

Hash Function-24

Binary Indexed Tree-21

Interactive-18

Data Stream-17

String Matching-17

Rolling Hash-17

Shortest Path-16

Number Theory-16

Combinatorics-15

Randomized-12

Monotonic Queue-9

Iterator-9

Merge Sort-9

Concurrency-9

Doubly-Linked List-8

Brainteaser-8

Probability and Statistics-7

Quickselect-7

Bucket Sort-6

Suffix Array-6

Minimum Spanning Tree-5

Counting Sort-5

Shell-4

Line Sweep-4

Reservoir Sampling-4

Eulerian Circuit-3

Radix Sort-3

Strongly Connected Componen-t2

Rejection Sampling-2

Biconnected Component-1