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: 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 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++
#include <iostream>
using namespace std;
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;
}
void sortArray( int arr[], int l, int h)
{
if (l >= h)
return ;
int pivot = partition(arr, l, h);
sortArray(arr, l, pivot - 1);
sortArray(arr, pivot + 1, h);
}
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 (start[0] > 0)
ans++;
while (i < n && j < n) {
if (start[i] < end[j]) {
i++;
currActive++;
}
else if (start[i] > end[j]) {
j++;
currActive--;
}
else {
i++;
j++;
}
if (currActive == 0) {
ans++;
}
}
if (end[n - 1] < R)
ans++;
return ans;
}
int main()
{
int R, N;
R = 10;
N = 3;
int start[N] = { 2, 5, 8 };
int end[N] = { 3, 9, 10 };
sortArray(start, 0, N - 1);
sortArray(end, 0, N - 1);
cout << findMaxIntervals(
start, end, N, R);
}
|
Java
import java.util.*;
class GFG{
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;
}
static void sortArray( int arr[], int l, int h)
{
if (l >= h)
return ;
int pivot = partition(arr, l, h);
sortArray(arr, l, pivot - 1 );
sortArray(arr, pivot + 1 , h);
}
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 (start[ 0 ] > 0 )
ans++;
while (i < n && j < n)
{
if (start[i] < end[j])
{
i++;
currActive++;
}
else if (start[i] > end[j])
{
j++;
currActive--;
}
else
{
i++;
j++;
}
if (currActive == 0 )
{
ans++;
}
}
if (end[n - 1 ] < R)
ans++;
return ans;
}
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 };
sortArray(start, 0 , N - 1 );
sortArray(end, 0 , N - 1 );
System.out.print(findMaxIntervals(start, end, N, R));
}
}
|
Python3
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
def sortArray(arr, l, h):
if (l > = h):
return
pivot = partition(arr, l, h)
sortArray(arr, l, pivot - 1 )
sortArray(arr, pivot + 1 , h)
def findMaxIntervals(start, end, n, R):
ans = 0
prev = 0
currActive = 0
i = 0
j = 0
if (start[ 0 ] > 0 ):
ans + = 1
while (i < n and j < n):
if (start[i] < end[j]):
i + = 1
currActive + = 1
elif (start[i] > end[j]):
j + = 1
currActive - = 1
else :
i + = 1
j + = 1
if (currActive = = 0 ):
ans + = 1
if (end[n - 1 ] < R):
ans + = 1
return ans
if __name__ = = '__main__' :
R = 10
N = 3
start = [ 2 , 5 , 8 ]
end = [ 3 , 9 , 10 ]
sortArray(start, 0 , N - 1 )
sortArray(end, 0 , N - 1 )
print (findMaxIntervals(start, end, N, R))
|
C#
using System;
class GFG{
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;
}
static void sortArray( int []arr, int l, int h)
{
if (l >= h)
return ;
int pivot = partition(arr, l, h);
sortArray(arr, l, pivot - 1);
sortArray(arr, pivot + 1, h);
}
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 (start[0] > 0)
ans++;
while (i < n && j < n)
{
if (start[i] < end[j])
{
i++;
currActive++;
}
else if (start[i] > end[j])
{
j++;
currActive--;
}
else
{
i++;
j++;
}
if (currActive == 0)
{
ans++;
}
}
if (end[n - 1] < R)
ans++;
return ans;
}
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 };
sortArray(start, 0, N - 1);
sortArray(end, 0, N - 1);
Console.Write(findMaxIntervals(start, end, N, R));
}
}
|
Javascript
<script>
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 sortArray(arr, l, h)
{
if (l >= h)
return ;
let pivot = partition(arr, l, h);
sortArray(arr, l, pivot - 1);
sortArray(arr, pivot + 1, h);
}
function findMaxIntervals(start,
end, n, R)
{
let ans = 0;
let prev = 0;
let currActive = 0;
let i = 0;
let j = 0;
if (start[0] > 0)
ans++;
while (i < n && j < n)
{
if (start[i] < end[j])
{
i++;
currActive++;
}
else if (start[i] > end[j])
{
j++;
currActive--;
}
else
{
i++;
j++;
}
if (currActive == 0)
{
ans++;
}
}
if (end[n - 1] < R)
ans++;
return ans;
}
let R, N;
R = 10;
N = 3;
let start = [ 2, 5, 8 ];
let end = [ 3, 9, 10 ];
sortArray(start, 0, N - 1);
sortArray(end, 0, N - 1);
document.write(findMaxIntervals(start, end, N, R));
</script>
|
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
10 Sep, 2021
Like Article
Save Article