Kth Smallest Element in a Sorted Matrix

Given a sorted matrix of integers A of size N x M and an integer B.

Each of the rows and columns of matrix A are sorted in ascending order, find the Bth smallest element in the matrix.

Note:
Return The Bth smallest element in the sorted order, not the Bth distinct element.



Input Format

The first argument given is the integer matrix A.
The second argument given is an integer B.

Output Format

Return the Bth smallest element in the matrix.

Constraints

1 <= N, M <= 500
1 <= A[i] <= 10^9
1 <= B <= N*M

For Example

Input 1:
    A = [   [9, 11, 15]
            [10, 15, 17]    ]
    B = 6
Output 1:
    17

Input 2:
    A = [    [5, 9, 11]
            [9, 11, 13]
            [10, 12, 15]
            [13, 14, 16]
            [16, 20, 21] ]
    B = 12
Output 2:
    16
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving  Kth Smallest Element in a Sorted Matrix on Interview Code Editor
Sign Up
to access hints and editorial solutions for Kth Smallest Element in a Sorted Matrix

Discussion


Loading...
Click here to start solving coding interview questions