Count of available non-overlapping intervals to be inserted to make interval [0, R]

Given an integer R which signifies the range [0, R] and two arrays start[] and end[] of size N which signifies the starting and ending intervals in the range [0, R]. The task is to count the number of available non-overlapping intervals which needs to be inserted in the arrays such that on merging the individual ranges in the arrays start[] and end[], the merged interval becomes [0, R].

Examples:

Input: R = 10, N = 3, start[] = {2, 5, 8}, end[] = {3, 9, 10}
Output: 2
Explanation:
The ranges {[2, 3], [5, 10]} are given by the array. In order to make the merged interval to [0, R], the ranges [0, 2] and [3, 5] should be inserted and merged. Therefore, two ranges needs to be inserted into the array.



Input: R = 8, N = 2, start[] = {2, 6}, end[] = {3, 7}
Output: 3
Explanation:
The ranges {[2, 3], [6, 7]} are given by the array. In order to make the merged interval to [0, R], the ranges [0, 2], [3, 6] and [7, 8] should be inserted and merged. Therefore, three ranges needs to be inserted into the array.

Approach: The idea is to use sorting to solve the problem.

  • Initially, both the given arrays are sorted so that used intervals come together.
  • Now, the arrays are iterated in a sorted way (i.e.), one pointer would be pointing to start of start and another pointer would be pointing to start of end.
  • If the current start element is smaller, this signifies that a new interval is being looked on and if the current end index is smaller, it signifies that the current interval is being exited.
  • In this process, the count of current active intervals are stored. If at any point current active count is 0, then there is an available interval. So, the count of the available interval is incremented.
  • In this way, both the arrays are iterated.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
  
#include <iostream>
using namespace std;
  
// The following two functions
// are used to sort the array.
// QuickSort is being implemented in
// the following functions
  
// Function to find the pivot index
int partiton(int arr[], int l, int h)
{
    int pivot = arr[l];
    int i = l + 1;
    int j = h;
  
    while (i <= j) {
  
        while (i <= h
               && arr[i] < pivot) {
            i++;
        }
  
        while (j > l
               && arr[j] > pivot) {
            j--;
        }
  
        if (i < j) {
  
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            i++;
            j--;
        }
        else
            i++;
    }
  
    arr[l] = arr[j];
    arr[j] = pivot;
    return j;
}
  
// Function to implement Quick Sort
void sortArray(int arr[], int l, int h)
{
    if (l >= h)
        return;
  
    // Pivot is pointing to pivot index
    // before which every element
    // is smaller and after pivot,
    // every element is greater
    int pivot = partiton(arr, l, h);
  
    // Sort the array before pivot element
    sortArray(arr, l, pivot - 1);
  
    // Sort the array after pivot element
    sortArray(arr, pivot + 1, h);
}
  
// Function to count the available intervals
// from the given range of numbers
int findMaxIntervals(int start[], int end[],
                     int n, int R)
{
    int ans = 0;
    int prev = 0;
    int currActive = 0;
    int i = 0;
    int j = 0;
  
    // If range starts after 0
    // then an interval is available
    // from 0 to start[0]
    if (start[0] > 0)
        ans++;
  
    while (i < n && j < n) {
        // When a new interval starts
        if (start[i] < end[j]) {
  
            // Since index variable i is being
            // incremented, the current active
            // interval will also get incremented
            i++;
            currActive++;
        }
  
        // When the current interval ends
        else if (start[i] > end[j]) {
  
            // Since index variable j is being
            // decremented, the currect active
            // interval will also get decremented
            j++;
            currActive--;
        }
  
        // When start and end both are same
        // there is no change in currActive
        else {
            i++;
            j++;
        }
        if (currActive == 0) {
            ans++;
        }
    }
  
    // If the end of interval
    // is before the range
    // so interval is available
    // at the end
    if (end[n - 1] < R)
        ans++;
    return ans;
}
  
// Driver code
int main()
{
    int R, N;
    R = 10;
    N = 3;
    int start[N] = { 2, 5, 8 };
    int end[N] = { 3, 9, 10 };
  
    // Sort the start array
    sortArray(start, 0, N - 1);
  
    // Sort the end array
    sortArray(end, 0, N - 1);
  
    // Calling the function
    cout << findMaxIntervals(
        start, end, N, R);
}

chevron_right


Output:

2

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.