- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing

- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM

- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers

- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources

# Nth smallest element in sorted 2-D array in JavaScript

## Problem

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;

## Output

Then the output should be −

const output = 11;

## Output Explanation:

11 is the fifth smallest element in the matrix.

## Example

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));

## Code Explanation:

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.

## Output

The output in the console will be −

11

- Related Articles
- Finding smallest element in a sorted array which is rotated in JavaScript
- Searching in a sorted 2-D array in JavaScript
- Finding first unique element in sorted array in JavaScript
- Kth Smallest Element in a Sorted Matrix in Python
- Checking for majority element in a sorted array in JavaScript
- Finding sum of every nth element of array in JavaScript
- Find the element that appears once in sorted array - JavaScript
- Finding the first unique element in a sorted array in JavaScript
- Find the Smallest element from a string array in JavaScript
- Finding the nth power of array element present at nth index using JavaScript
- Missing Element in Sorted Array in C++
- Reshaping 2-D array in JavaScript
- How to remove every Nth element from an array JavaScript?
- Single Element in a Sorted Array in C++
- Reduce an array to the sum of every nth element - JavaScript