- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose, we have a sorted array of arrays of Numbers (sorted in increasing order) like this −

const arr = [ [ 1, 5, 9], [10, 11, 13], [12, 13, 15] ];

We are required to write a JavaScript function that takes in in one such array as the first argument and a single Integer, num, as the second argument.

Our function is supposed to return the numth smallest element that exists in the array arr.

For example, if the input to the function is −

const arr = [ [ 1, 5, 9], [10, 11, 13], [12, 13, 15] ]; const num = 5;

Then the output should be −

const output = 11;

11 is the fifth smallest element in the matrix.

The code for this will be −

const arr = [ [ 1, 5, 9], [10, 11, 13], [12, 13, 15] ]; const num = 5; const kthSmallest = (arr = [], num = 1) => { let low = arr[0][0] let high = arr[arr.length-1][arr[0].length-1] + 1; while (low < high) { let mid = low + Math.floor((high-low)/2); let count = 0; for (let i = 0;i<arr.length;i++) { for (let j=0;j<arr.length;j++) { if (arr[i][j] <= mid) count++; else break; } } if (count < num) low = mid+1; else high = mid; } return low }; console.log(kthSmallest(arr, num));

The idea here is −

When we have a regular sorted 2d array, we use a range of indexes to find our target, for example,

low = 0, high = length-1, mid = (low+high)/2

if the target is greater than the number at index mid, then we search the right portion, if it's smaller, we search the left.

However, in this 2d arr sorted array, it is impossible to find such a mid index. The intuition here is to use a range of numbers to test against our k. We know the first number is the smallest and the last number is the largest, this means our target number must be something in between. We can use those two numbers as our low and high, and set our mid to be the number in between, and check how many of the numbers in the arr are smaller than that number and adjust low and high accordingly.

When we get exactly k numbers, we know we have found our answer. The extremely tricky part here is that just by looking at how we calculate mid, a lot of times the number we are testing agaist may not even be in the arr, because in the end, the numbers we are using are just arbitrary numbers.

To explain this we need to imagine that our program is at a stage where low and high are almost going to hit each other. if we counted less numbers than expected, we will set low=mid+1, this potentially just increments our mid by 1. And by incrementing our mid 1 by 1, We can make sure the numbers are included in the arr.

The output in the console will be −

11

- Related Questions & Answers
- Searching in a sorted 2-D array in JavaScript
- Reshaping 2-D array in JavaScript
- Finding smallest element in a sorted array which is rotated in JavaScript
- Grouping and sorting 2-D array in JavaScript
- Converting object to 2-D array in JavaScript
- Emulating a 2-d array using 1-d array in C++
- Finding transpose of a 2-D array JavaScript
- Sorting 2-D array of strings and finding the diagonal element using JavaScript
- Build maximum array based on a 2-D array - JavaScript
- Constructing 2-D array based on some constraints in JavaScript
- Sorting only columns of a 2-D array in JavaScript
- Kth Smallest Element in a Sorted Matrix in Python
- Finding first unique element in sorted array in JavaScript
- Counting largest numbers in row and column in 2-D array in JavaScript
- Finding sum of every nth element of array in JavaScript

Advertisements