Given an array **interval** of pairs of integers representing the starting and ending points of the interval of size **N**. The task is to find the smallest non-negative integer which is a non-overlapping number from the given set of intervals.

**Input constraints:**

**Examples:**

Input:interval = {{0, 4}, {6, 8}, {2, 3}, {9, 18}}

Output:5

Expalination:

The smallest non-negative integer which is non-overlapping to all set of the intervals is 5.

Input:interval = {{0, 14}, {86, 108}, {22, 30}, {5, 17}}

Output:18

**Naive Approach:**

- Create a visited array of size
**MAX**, and for every interval mark all value true from**start**to**end**. - Finally iterate from
**1**to**MAX**and find the smallest value which is not visited.

However, this approach will not work if the interval co-ordinates are up to 10 ^{9}.

**Time Complexity:** O (N^{ 2})

**Auxiliary Space:** O (MAX)

** Efficient Approach:**

- Istead of iterating from start to end just create a visited array and for each range, mark
**vis[start] = 1**and**vis[end+1] = -1**. - Take the prefix sum of the array.
- Then iterate over the array to find the first integer with value
**0**.

Here is the implementation of the above approach:

`// C++ program to find the ` `// least non-overlapping number ` `// from a given set intervals ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `const` `int` `MAX = 1e5 + 5; ` ` ` `// function to find the smallest ` `// non-overlapping number ` `void` `find_missing( ` ` ` `vector<pair<` `int` `, ` `int` `> > interval) ` `{ ` ` ` `// create a visited array ` ` ` `vector<` `int` `> vis(MAX); ` ` ` ` ` `for` `(` `int` `i = 0; i < interval.size(); ++i) { ` ` ` `int` `start = interval[i].first; ` ` ` `int` `end = interval[i].second; ` ` ` `vis[start]++; ` ` ` `vis[end + 1]--; ` ` ` `} ` ` ` ` ` `// find the first missing value ` ` ` `for` `(` `int` `i = 1; i < MAX; i++) { ` ` ` `vis[i] += vis[i - 1]; ` ` ` `if` `(!vis[i]) { ` ` ` `cout << i << endl; ` ` ` `return` `; ` ` ` `} ` ` ` `} ` `} ` `// Driver function ` `int` `main() ` `{ ` ` ` ` ` `vector<pair<` `int` `, ` `int` `> > interval ` ` ` `= { { 0, 14 }, { 86, 108 }, ` ` ` `{ 22, 30 }, { 5, 17 } }; ` ` ` `find_missing(interval); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

18

**Time Complexity:** O (N)

**Auxiliary Space:** O (MAX)

However, this approach will also not work if the interval co-ordinates are up to 10 ^{9}.

**Efficient Approach:**

- Sort the range by their start-coordinate and for each next range.
- Check if the starting point is greater than the maximum end-coordinate encountered so far, then a missing number can be found, and it will be
**previous_max + 1**.

Illustration:

Consider the following example:

interval[][] = { { 0, 14 }, { 86, 108 }, { 22, 30 }, { 5, 17 } };

After sorting, interval[][] = { { 0, 14 }, { 5, 17 }, { 22, 30 }, { 86, 108 }};

Initial mx = 0 and after considering first interval mx = max(0, 15) = 15

Since mx = 15 and 15 > 5 so after considering second interval mx = max(15, 18) = 18

now 18 < 22 so 18 is least non-overlapping number.

Here is the implementation of the above approach:

`// C++ program to find the ` `// least non-overlapping number ` `// from a given set intervals ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to find the smallest ` `// non-overlapping number ` `void` `find_missing( ` ` ` `vector<pair<` `int` `, ` `int` `> > interval) ` `{ ` ` ` `// Sort the intervals based on their ` ` ` `// starting value ` ` ` `sort(interval.begin(), interval.end()); ` ` ` ` ` `int` `mx = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < (` `int` `)interval.size(); ++i) { ` ` ` ` ` `// check if any missing vaue exist ` ` ` `if` `(interval[i].first > mx) { ` ` ` `cout << mx; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `else` ` ` `mx = max(mx, interval[i].second + 1); ` ` ` `} ` ` ` `// finally print the missing value ` ` ` `cout << mx; ` `} ` `// Driver function ` `int` `main() ` `{ ` ` ` ` ` `vector<pair<` `int` `, ` `int` `> > interval ` ` ` `= { { 0, 14 }, { 86, 108 }, ` ` ` `{ 22, 30 }, { 5, 17 } }; ` ` ` `find_missing(interval); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

18

**Time Complexity:** O (N * logN)

**Auxiliary Space:** O (1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find Non-overlapping intervals among a given set of intervals
- Check if any two intervals overlap among a given set of intervals
- Find Intersection of all Intervals
- Find intersection of intervals given by two lists
- Find the point where maximum intervals overlap
- Count the number of intervals in which a given value lies
- Maximum number of overlapping Intervals
- Maximal Disjoint Intervals
- Merge Overlapping Intervals
- Count points covered by given intervals
- Make the intervals non-overlapping by assigning them to two different processors
- Check if given intervals can be made non-overlapping by adding/subtracting some X
- Count of available non-overlapping intervals to be inserted to make interval [0, R]
- Find a number which give minimum sum when XOR with every number of array of integers
- Find smallest possible Number from a given large Number with same count of digits
- Find smallest number formed by inverting digits of given number N
- Find the Number Occurring Odd Number of Times
- Find smallest number with given number of digits and sum of digits under given constraints
- Find the number of pairs (a, b) such that a % b = K
- Find the number of Chicks in a Zoo at Nth day

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.