31. Kth Element in Matrix

โœ… GFG solution to the Kth Element in Matrix problem: find the kth smallest in a sorted matrix using binary search or heap. ๐Ÿš€

The problem can be found at the following link: ๐Ÿ”— Question Link

๐Ÿงฉ Problem Description

Given a n x n matrix in which each row and column is sorted in ascending order, find the kth smallest element in the matrix.

๐Ÿ“˜ Examples

Example 1

Input: n = 4, mat = [
  [16, 28, 60, 64],
  [22, 41, 63, 91],
  [27, 50, 87, 93],
  [36, 78, 87, 94]
], k = 3

Output: 27
Explanation: The 3rd smallest element in the sorted order is 27.

Example 2

๐Ÿ”’ Constraints

  • $1 \le n \le 500$

  • $1 \le \text{mat}[i][j] \le 10^4$

  • $1 \le k \le n^2$

โœ… My Approach

We have three common approaches for finding the kth smallest element in a row- and column-sorted matrix:

1๏ธโƒฃ Binary Search on Value Range

  1. Idea:

    • Let low = mat[0][0], high = mat[n-1][n-1].

    • Perform a binary search on the value range [low, high] to find the smallest mid such that at least k elements in the matrix are โ‰ค mid.

  2. Counting โ‰ค mid:

    • For each row i, maintain a pointer j starting at n-1.

    • While j โ‰ฅ 0 and mat[i][j] > mid, decrement j.

    • Count of elements โ‰ค mid in row i is j + 1.

    • Sum up counts over all rows.

  3. Binary Search Steps:

    • Compute mid = low + (high - low) / 2.

    • If count(mid) < k, set low = mid + 1.

    • Else, set high = mid.

    • Continue until low == high.

  4. Result:

    • Return low, which is the kth smallest element.

๐Ÿ“ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n ร— log(max - min)), where max and min are the matrix's largest and smallest values respectively.

  • Expected Auxiliary Space Complexity: O(1), since we do not use extra memory except

๐Ÿง‘โ€๐Ÿ’ป Code (C++)

โšก View Alternative Approaches with Code and Analysis

๐Ÿ“Š 2๏ธโƒฃ Min Heap (Priority Queue)

๐Ÿ’ก Algorithm Steps:

  1. Create a min-heap (priority queue) of tuples (value, row, col).

  2. Push the first element of each row:

  3. Repeat k-1 times:

    • Pop the smallest tuple (val, r, c).

    • If c+1 < n, push (matrix[r][c+1], r, c+1) into the heap.

  4. After k-1 pops, the top of the heap holds the kth smallest element.

๐Ÿ“ Complexity Analysis:

  • Time: โฑ๏ธ O(k ร— log n)

  • Auxiliary Space: ๐Ÿ’พ O(n)

โœ… Why This Approach?

  • Leverages built-in min-heap for simplicity.

  • Efficient when k is relatively small compared to nยฒ.

๐Ÿ“Š 3๏ธโƒฃ Flatten + Sort

๐Ÿ’ก Algorithm Steps:

  1. Flatten all elements of the matrix into a single 1D vector flat:

  2. Sort flat:

  3. Return flat[k-1].

๐Ÿ“ Complexity Analysis:

  • Time: โฑ๏ธ O(nยฒ ร— log n)

  • Auxiliary Space: ๐Ÿ’พ O(nยฒ)

โœ… Why This Approach?

  • Most straightforward: flatten, sort, index.

  • Only suitable for small matrices due to O(nยฒ log n).

๐Ÿ†š ๐Ÿ” Comparison of Approaches

๐Ÿš€ Approach

โฑ๏ธ Time Complexity

๐Ÿ’พ Space Complexity

โœ… Pros

โš ๏ธ Cons

๐Ÿ” Binary Search

๐ŸŸข O(n ร— log(max_valโˆ’min_val))

๐ŸŸข O(1)

โšก Fastest for large matrices

๐Ÿงฎ Counting โ‰ค mid for each row

๐Ÿ“ฆ Min Heap

๐ŸŸก O(k ร— log n)

๐ŸŸข O(n)

๐Ÿ”ง Straightforward with built-in heap

๐Ÿข Slower if k is large (close to nยฒ)

๐Ÿงฎ Flatten + Sort

๐Ÿ”ธ O(nยฒ ร— log n)

๐Ÿ”ธ O(nยฒ)

๐Ÿช„ Simplest to implement

๐Ÿšซ Inefficient for large n (time & space heavy)

๐Ÿ† Best Choice Recommendation

๐ŸŽฏ Scenario

๐ŸŽ–๏ธ Recommended Approach

โšก Need fastest overall (large n, large k)

๐Ÿฅ‡ Binary Search

๐Ÿงต Prefer heap logic (k relatively small)

๐Ÿฅˆ Min Heap

๐Ÿ“‹ Quick prototype/brute-force for small matrices

๐Ÿฅ‰ Flatten + Sort

๐Ÿง‘โ€๐Ÿ’ป Code (Java)

๐Ÿ Code (Python)

๐Ÿง  Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐Ÿ“ฌ Any Questions?. Letโ€™s make this learning journey more collaborative!

โญ If you find this helpful, please give this repository a star! โญ


๐Ÿ“Visitor Count

Visitor counter

Last updated