Register for our webinar

### How to Nail your next Technical Interview

1 hour
1
Enter details
2
Select webinar slot
*Invalid Name
*Invalid Name
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
*All webinar slots are in the Asia/Kolkata timezone
Step 1
Step 2
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.

## You may be missing out on a 66.5% salary hike*

### Nick Camilleri

Head of Career Skills Development & Coaching
*Based on past data of successful IK students
Help us know you better!

## How many years of coding experience do you have?

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

## FREE course on 'Sorting Algorithms' by Omkar Deshpande (Stanford PhD, Head of Curriculum, IK)

Thank you! Please check your inbox for the course details.
Oops! Something went wrong while submitting the form.
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career

# Kth Smallest Element In A Sorted Matrix Problem Statement

Given a `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.

## Example One

``````{
"matrix": [
[1, 3, 5],
[2, 3, 6],
[3, 3, 7]
],
"k": 5
}
``````

Output:

``````3
``````

## Example Two

``````{
"matrix": [
[2, 2],
[2, 3]
],
"k": 3
}
``````

Output:

``````2
``````

## Notes

• Return the kth smallest element in the sorted order, not the kth distinct element.
• Find a solution with a memory complexity better than O(`n``2`).

Constraints:

• 1 <= `n` <= 300
• -105 <= any element of the matrix <= 105
• All the rows and columns of matrix are guaranteed to be sorted in non-decreasing order.
• 1 <= `k` <= `n``2`

## Code For Kth Smallest Element In A Sorted Matrix Solution: Binary Search

``````/*
Asymptotic complexity in terms of the number of rows \`M\`, the number of columns, \`N\` and
highest difference between the numbers in the matrix \`D\`:
* Time: O((M + N) * log(D)).
* Auxiliary space: O(1).
* Total space: O(M * N).
*/

int m, n;
int count_less_or_equal(vector<vector<int>> &matrix, int x) {
int cnt = 0, c = n - 1;
for (int r = 0; r < m; r++) {
while (c >= 0 && matrix[r][c] > x) c--;
cnt += (c + 1);
}
return cnt;
}

int get_kth_smallest(vector<vector<int>> &matrix, int k) {
m = matrix.size(), n = matrix[0].size();
int left = matrix[0][0], right = matrix[m-1][n-1];
int answer = -1;
while (left <= right) {
int mid = (left + right) / 2;
if (count_less_or_equal(matrix, mid) >= k) {
right = mid - 1;
}
else {
left = mid + 1;
}
}
}

``````

We hope that these solutions to kth smallest element in a sorted matrix problem have helped you level up your coding skills. You can expect problems like these at top tech companies like Amazon and Google.

If you are preparing for a tech interview at FAANG or any other Tier-1 tech company, register for Interview Kickstart's FREE webinar to understand the best way to prepare.

Interview Kickstart offers interview preparation courses taught by FAANG+ tech leads and seasoned hiring managers. Our programs include a comprehensive curriculum, unmatched teaching methods, and career coaching to help you nail your next tech interview.

We offer 18 interview preparation courses, each tailored to a specific engineering domain or role, including the most in-demand and highest-paying domains and roles, such as:

‍To learn more, register for the FREE webinar.

### Try yourself in the Editor

Note: Input and Output will already be taken care of.

# Kth Smallest Element In A Sorted Matrix Problem Statement

Given a `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.

## Example One

``````{
"matrix": [
[1, 3, 5],
[2, 3, 6],
[3, 3, 7]
],
"k": 5
}
``````

Output:

``````3
``````

## Example Two

``````{
"matrix": [
[2, 2],
[2, 3]
],
"k": 3
}
``````

Output:

``````2
``````

## Notes

• Return the kth smallest element in the sorted order, not the kth distinct element.
• Find a solution with a memory complexity better than O(`n``2`).

Constraints:

• 1 <= `n` <= 300
• -105 <= any element of the matrix <= 105
• All the rows and columns of matrix are guaranteed to be sorted in non-decreasing order.
• 1 <= `k` <= `n``2`

## Code For Kth Smallest Element In A Sorted Matrix Solution: Binary Search

``````/*
Asymptotic complexity in terms of the number of rows \`M\`, the number of columns, \`N\` and
highest difference between the numbers in the matrix \`D\`:
* Time: O((M + N) * log(D)).
* Auxiliary space: O(1).
* Total space: O(M * N).
*/

int m, n;
int count_less_or_equal(vector<vector<int>> &matrix, int x) {
int cnt = 0, c = n - 1;
for (int r = 0; r < m; r++) {
while (c >= 0 && matrix[r][c] > x) c--;
cnt += (c + 1);
}
return cnt;
}

int get_kth_smallest(vector<vector<int>> &matrix, int k) {
m = matrix.size(), n = matrix[0].size();
int left = matrix[0][0], right = matrix[m-1][n-1];
int answer = -1;
while (left <= right) {
int mid = (left + right) / 2;
if (count_less_or_equal(matrix, mid) >= k) {
right = mid - 1;
}
else {
left = mid + 1;
}
}
}

``````

We hope that these solutions to kth smallest element in a sorted matrix problem have helped you level up your coding skills. You can expect problems like these at top tech companies like Amazon and Google.

If you are preparing for a tech interview at FAANG or any other Tier-1 tech company, register for Interview Kickstart's FREE webinar to understand the best way to prepare.

Interview Kickstart offers interview preparation courses taught by FAANG+ tech leads and seasoned hiring managers. Our programs include a comprehensive curriculum, unmatched teaching methods, and career coaching to help you nail your next tech interview.

We offer 18 interview preparation courses, each tailored to a specific engineering domain or role, including the most in-demand and highest-paying domains and roles, such as:

‍To learn more, register for the FREE webinar.

## Worried About Failing Tech Interviews?

Attend our free webinar to amp up your career and get the salary you deserve.

Hosted By
Ryan Valles
Founder, Interview Kickstart
Accelerate your Interview prep with Tier-1 tech instructors
360° courses that have helped 14,000+ tech professionals
100% money-back guarantee*