Binary Search pattern
Kth Smallest in a Sorted Matrix
Given an n x n matrix whose rows and columns are each sorted in ascending order, find the kth smallest element in the matrix considering its sorted order, counting duplicate values separately. Watch it run step by step, then write your own pseudocode and get it checked by AI.
- LeetCode 378
- Medium
- 2 approaches
Sign in with Google or GitHub and you land straight on this page. DSA comes with the all labs plan and every ChaiCode batch.
Binary SearchLeetCode 378
Medium
From the first idea to the best one
- 01 Brute force · flatten and sort O(n² log n²)O(n²) space
- 02 Optimized · binary search the value range O(n log range)O(1) space
What you will learn
- How to read Kth Smallest in a Sorted Matrix and spot the binary search pattern in it
- Brute force · flatten and sort: O(n² log n²) time, O(n²) space
- Optimized · binary search the value range: O(n log range) time, O(1) space
- The pseudocode for each approach, its line lit as the animation runs
- Practice: write your own pseudocode and get it checked
More Binary Search
- Minimum Shipping CapacityLC 1011 · Medium
- OverviewConcept
- Binary SearchLC 704 · Easy
- Search Insert PositionLC 35 · Easy
- Peak Index in a Mountain ArrayLC 852 · Medium
- Maximum Candies Allocated to K ChildrenLC 2226 · Medium
- Apple Harvest (Koko Eating Bananas)LC 875 · Medium
- Search in Rotated Sorted ArrayLC 33 · Medium
See Kth Smallest in a Sorted Matrix run, step by step
Sign in and this page opens on the animation, with all 11 Binary Search problems beside it.
DSA comes with the all labs plan and every ChaiCode batch.