Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 need 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:
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:
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 is stored. If at any point the 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:  

C++




// 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 partition(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 = partition(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);
}


Java




// Java implementation of the above approach
import java.util.*;
class GFG{
 
// The following two functions
// are used to sort the array.
// QuickSort is being implemented in
// the following functions
 
// Function to find the pivot index
static int partition(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
static 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 = partition(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
static 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
public static void main(String args[])
{
    int R, N;
    R = 10;
    N = 3;
    int start[] = new int[]{ 2, 5, 8 };
    int end[] = new int[]{ 3, 9, 10 };
 
    // Sort the start array
    sortArray(start, 0, N - 1);
 
    // Sort the end array
    sortArray(end, 0, N - 1);
 
    // Calling the function
    System.out.print(findMaxIntervals(start, end, N, R));
}
}
 
// This code is contributed by Code_Mech


Python3




# Python3 implementation of the above approach
 
# The following two functions
# are used to sort the array.
# QuickSort is being implemented in
# the following functions
 
# Function to find the pivot index
def partition(arr, l, h):
     
    pivot = arr[l]
    i = l + 1
    j = h
 
    while (i <= j):
        while (i <= h and arr[i] < pivot):
            i += 1
 
        while (j > l and arr[j] > pivot):
            j -= 1
 
        if (i < j):
            temp = arr[i]
            arr[i] = arr[j]
            arr[j] = temp
            i += 1
            j -= 1
        else:
            i += 1
 
    arr[l] = arr[j]
    arr[j] = pivot
    return j
 
# Function to implement Quick Sort
def sortArray(arr, l, h):
     
    if (l >= h):
        return
 
    # Pivot is pointing to pivot index
    # before which every element
    # is smaller and after pivot,
    # every element is greater
    pivot = partition(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
def findMaxIntervals(start, end, n, R):
     
    ans = 0
    prev = 0
    currActive = 0
    i = 0
    j = 0
 
    # If range starts after 0
    # then an interval is available
    # from 0 to start[0]
    if (start[0] > 0):
        ans += 1
 
    while (i < n and 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 += 1
            currActive += 1
 
        # When the current interval ends
        elif (start[i] > end[j]):
             
            # Since index variable j is being
            # decremented, the currect active
            # interval will also get decremented
            j += 1
            currActive -= 1
 
        # When start and end both are same
        # there is no change in currActive
        else:
            i += 1
            j += 1
        if (currActive == 0):
            ans += 1
 
    # If the end of interval
    # is before the range
    # so interval is available
    # at the end
    if (end[n - 1] < R):
        ans += 1
    return ans
 
# Driver code
if __name__ == '__main__':
     
    R = 10
    N = 3
    start = [ 2, 5, 8 ]
    end = [ 3, 9, 10 ]
 
    # Sort the start array
    sortArray(start, 0, N - 1)
 
    # Sort the end array
    sortArray(end, 0, N - 1)
 
    # Calling the function
    print(findMaxIntervals(start, end, N, R))
 
# This code is contributed by Surendra_Gangwar


C#




// C# implementation of the above approach
using System;
class GFG{
 
// The following two functions
// are used to sort the array.
// QuickSort is being implemented in
// the following functions
 
// Function to find the pivot index
static int partition(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
static 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 = partition(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
static 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
public static void Main()
{
    int R, N;
    R = 10;
    N = 3;
    int []start = new int[]{ 2, 5, 8 };
    int []end = new int[]{ 3, 9, 10 };
 
    // Sort the start array
    sortArray(start, 0, N - 1);
 
    // Sort the end array
    sortArray(end, 0, N - 1);
 
    // Calling the function
    Console.Write(findMaxIntervals(start, end, N, R));
}
}
 
// This code is contributed by Code_Mech


Javascript




<script>
 
// Javascript implementation of the above approach
 
// The following two functions
// are used to sort the array.
// QuickSort is being implemented in
// the following functions
   
// Function to find the pivot index
function partition(arr, l, h)
{
    let pivot = arr[l];
    let i = l + 1;
    let j = h;
   
    while (i <= j)
    {
        while (i <= h && arr[i] < pivot)
        {
            i++;
        }
   
        while (j > l && arr[j] > pivot)
        {
            j--;
        }
   
        if (i < j)
        {
            let 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
function sortArray(arr, l, h)
{
    if (l >= h)
        return;
   
    // Pivot is pointing to pivot index
    // before which every element
    // is smaller and after pivot,
    // every element is greater
    let pivot = partition(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
function findMaxIntervals(start,
                            end, n, R)
{
    let ans = 0;
    let prev = 0;
    let currActive = 0;
    let i = 0;
    let 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
     
    let R, N;
    R = 10;
    N = 3;
    let start = [ 2, 5, 8 ];
    let end = [ 3, 9, 10 ];
   
    // Sort the start array
    sortArray(start, 0, N - 1);
   
    // Sort the end array
    sortArray(end, 0, N - 1);
   
    // Calling the function
       document.write(findMaxIntervals(start, end, N, R));
     
</script>


Output: 

2

 



Last Updated : 10 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads