JavaScript Program to Find Kth Smallest/Largest Element in a Sorted Matrix
Last Updated :
08 May, 2024
Given an n x n matrix, where every row and column is sorted in non-decreasing order. Find the kth smallest/largest element in the given 2D array.
Examples:
Input:k = 3 and array =
10, 20, 30, 40
15, 25, 35, 45
24, 29, 37, 48
32, 33, 39, 50
Output: 20
40
Explanation: The 3rd smallest element is 20
The 3rd largest element is 45
Input:k = 7 and array =
10, 20, 30, 40
15, 25, 35, 45
24, 29, 37, 48
32, 33, 39, 50
Output: 30
35
Explanation: The 7th smallest element is 30
The 7th largest element is 35
Below are the approaches to finding kth smallest/largest element in a sorted matrix.
Using a flat array and Sorting
In this approach, we are using a flat array obtained from the matrix to find the kth smallest and kth largest elements by sorting them in ascending order and accessing the desired positions directly.
Example: The below example uses a flat array and Sorting to find kth smallest/largest element in a sorted matrix
JavaScript
let k = 3;
let matrix = [
[10, 20, 30, 40],
[15, 25, 35, 45],
[24, 29, 37, 48],
[32, 33, 39, 50]
];
let flatArray = matrix.flat().sort((a, b) => a - b);
let small = flatArray[k - 1];
let large = flatArray[flatArray.length - k];
console.log("Kth Smallest:", small);
console.log("Kth Largest:", large);
OutputKth Smallest: 20
Kth Largest: 45
Time Complexity: O(nlogn) time, where n is the total number of elements in the matrix.
Space Complexity: O(n)
Using Min-Heap
In this approach, we create separate min and max heaps by flattening the matrix, sorting each heap, and then directly accessing the kth smallest and kth largest elements from the heaps.
Example: The below example uses Min-Heap to find kth smallest/largest element in a sorted matrix
JavaScript
let k = 3;
let matrix = [
[10, 20, 30, 40],
[15, 25, 35, 45],
[24, 29, 37, 48],
[32, 33, 39, 50]
];
let minHeap = [];
let maxHeap = [];
for (let row of matrix) {
minHeap
.push(...row);
maxHeap
.push(...row);
}
minHeap
.sort((a, b) => a - b);
maxHeap
.sort((a, b) => b - a);
let small = minHeap[k - 1];
let large = maxHeap[k - 1];
console.log("Kth Smallest:", small);
console.log("Kth Largest:", large);
OutputKth Smallest: 20
Kth Largest: 45
Time Complexity: O(nlogk) time, where n is the total number of elements in the matrix and k is the value of k.
Space Complexity: O(n)
Similar Reads
JavaScript Program to Find kth Largest/Smallest Element in an Array JavaScript allows us to find kth largest/smallest element in an array. We are given an array containing some elements, we have to find kth smallest/largest element from the array where k is a number greater than zero and less than equal to the total number of elements present in the array. There are
5 min read
JavaScript Program to find the Nth smallest/largest element from an unsorted Array In this article, we will see how to find Nth largest and smallest element from an unsorted array. The Nth smallest/largest element from an unsorted array refers to the element that ranks at the Nth position when the array is sorted either in ascending (smallest) or descending (largest) order. It rep
4 min read
Javascript Program to Find maximum element of each row in a matrix Given a matrix, the task is to find the maximum element of each row.Examples: Input : [1, 2, 3] [1, 4, 9] [76, 34, 21]Output :3976Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2]Output :216556Approach : Approach is very simple. The idea is to run the loop for no_of_rows. Check each element inside
2 min read
Javascript Program to Find median in row wise sorted matrix We are given a row-wise sorted matrix of size r*c, we need to find the median of the matrix given. It is assumed that r*c is always odd.Examples: Input : 1 3 5 2 6 9 3 6 9Output : Median is 5If we put all the values in a sorted array A[] = 1 2 3 3 5 6 6 9 9)Input: 1 3 4 2 5 6 7 8 9Output: Median is
4 min read
Javascript Program for Sort the given matrix Given a n x n matrix. The problem is to sort the given matrix in strict order. Here strict order means that matrix is sorted in a way such that all elements in a row are sorted in increasing order and for row âiâ, where 1 <= i <= n-1, first element of row 'i' is greater than or equal to the la
2 min read
Javascript Program to Sort the matrix row-wise and column-wise Given a n x n matrix. The problem is to sort the matrix row-wise and column-wise.Examples: Input : mat[][] = { {4, 1, 3}, {9, 6, 8}, {5, 2, 7} }Output : 1 3 4 2 5 7 6 8 9Input : mat[][] = { {12, 7, 1, 8}, {20, 9, 11, 2}, {15, 4, 5, 13}, {3, 18, 10, 6} } Output : 1 5 8 12 2 6 10 15 3 7 11 18 4 9 13 2
3 min read
Javascript Program for Maximum and Minimum in a square matrix. Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : arr[][] = {5, 4, 9, 2, 0, 6, 3, 1, 8}; Output : Maximum = 9, Minimum = 0 Input : arr[][] = {-5, 3, 2, 4}; Output : Maximum = 4, Minimum = -5 Naive Method : We find maximum and minimum of matrix
3 min read
How to read a Matrix from user in Java? Given task is to read a matrix from the user. The size and number of elements of matrices are to be read from the keyboard. Java // Java program to read a matrix from user import java.util.Scanner; public class MatrixFromUser { // Function to read matrix public static void readMatrixByUser() { int m
2 min read
Min, Max and Mean of Off-Diagonal Elements in a Matrix in R A matrix is a combination of elements stacked together in either row or column format. A table-like structure formed of similar data type elements is known as a matrix. A matrix has two diagonals, one of which is known as the main diagonal. The main diagonal elements are characterized by the proper
3 min read
Kth Smallest Number in Multiplication Table Given three integers m, n, and k. Consider a grid of m * n, where mat[i][j] = i*j (1 based indexing). The task is to return the k'th smallest element in the m*n multiplication table.Examples: Input: m = 3, n = 3, k = 5Output: 3Explanation:The 5th smallest element is 3. Input: m = 2, n = 3, k = 6Outp
13 min read