- 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

# Inserting a new interval in a sorted array of intervals in JavaScript

For the purpose of this question, we define an interval as an array of two numbers where the first number is always smaller than the second number.

For example −

[4, 6], [2, 3], [6, 8], [2, 7], [1, 8] are all examples of valid intervals.

Suppose, we have an array of intervals which is sorted according to the their start times (the first elements of each interval).

The intervals in the array are non-overlapping which means that for any two arbitrary adjacent intervals,

[m, n], [x, y] m < n < x < y

Therefore, one such example of this array of intervals can be −

const arr = [[ 2, 4], [5, 7], [9, 10], [13, 17]];

We are required to write a JavaScript function that takes in one such array of intervals as the first argument and a single interval as the second argument.

The function should then insert the interval at its correct position in the array, maintain the nonoverlapping property of the array.

If required we can merge two or more intervals in the array in order to keep the array intervals non-overlapping.

For example, if for the above array of intervals, the interval we need to insert is [6, 13] Then the output should look like −

const output = [[2, 4], [5, 17]];

## Example

Following is the code −

const arr = [[2, 4], [5, 7], [9, 10], [13, 17]]; const interval = [6, 13]; const insertWithin = (arr = [], interval = []) => { const res = []; let ind = 0; while (arr[ind] && arr[ind][1] < interval[0]) { res.push(arr[ind]); ++ind; }; let start = interval[0]; let end = interval[1]; while (arr[ind] && arr[ind][0] <= interval[1]) { start = Math.min(start, arr[ind][0]); end = Math.max(end, arr[ind][1]); ++ind; } res.push([start, end]); while (arr[ind]) { res.push(arr[ind]); ++ind; } return res; }; console.log(insertWithin(arr, interval));

## Output

Following is the console output −

[[2, 4], [5, 17]]

- Related Questions & Answers
- Inserting an Element in Interval Heaps
- Program to find length of longest interval from a list of intervals in Python
- Insert a number into a sorted array of numbers JavaScript
- Removing duplicates from a sorted array of literals in JavaScript
- Return a sorted array in lexicographical order in JavaScript
- Searching in a sorted 2-D array in JavaScript
- Finding desired numbers in a sorted array in JavaScript
- Merge two sorted arrays to form a resultant sorted array in JavaScript
- Square of a sorted array in C++
- Inserting a key into a tree in Javascript
- Inserting a node in a Javascript AVL Tree
- Parse array to equal intervals in JavaScript
- Inserting data into a new column of an already existing table in MySQL?
- Checking for majority element in a sorted array in JavaScript
- Can we use INTERVAL keyword while inserting date records in a MySQL table?