Back to problems Sign in to unlock
Leetcode
Medium
Arrays
Binary Search
Heaps
Matrices
Google
Microsoft
Kth Smallest Element In A Sorted Matrix
Find the th smallest value in an matrix where each row and column is sorted in non-decreasing order.
Acceptance 100%
Problem Statement
Given an matrix of integers, each row sorted from left to right and each column sorted from top to bottom, return the th smallest element in the matrix.
The matrix may contain duplicate values, and the th smallest element is counted by position in the sorted order, not by distinct value.
Input Format
- A 2D integer matrix
matrixof sizen x n - An integer
k
The matrix satisfies:
- each row is sorted in non-decreasing order
- each column is sorted in non-decreasing order
Output Format
- Return the integer value of the th smallest element in the matrix.
Constraints
- Rows and columns are sorted in non-decreasing order
Examples
Sample cases returned by the problem API.
Example 1
Input
matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
Output
13
Explanation
The sorted order is [1,5,9,10,11,12,13,13,15], so the 8th smallest value is 13.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.