Maximum points covered after removing an Interval

Given N intervals in the form [l, r] and an integer Q. The task is to find the interval which when removed results in the coverage of the maximum number of points (Union of all the rest of the intervals). Note that all the given intervals cover numbers between 1 to Q only.

Examples:Ā 

Input: intervals[][] = {{1, 4}, {4, 5}, {5, 6}, {6, 7}, {3, 5}}, Q = 7Ā 
Output: Maximum Coverage is 7 after removing interval at index 4Ā 
When last interval is removed we are able to cover the given points using rest of the intervalsĀ 
{1, 2, 3, 4, 5, 6, 7}, which is maximum coverage possible.Ā 
(The answer will also be same if we remove interval {4, 5} or {5, 6} )

Input: intervals[][] = {{3, 3}, {2, 2}, {3, 4}}, Q = 4Ā 
Output: Maximum Coverage is 3 after removing interval at index 0Ā 

Approach:Ā 

  • First use an array mark[] of size n + 1. If mark[i] = k, this means exactly k intervals have point i in them.
  • Maintain count, total number of numbers that are covered by all the intervals.
  • Now we have to iterate through all the intervals, and check if each interval is removed then how many numbers will be removed from count.
  • To check new count after removal of each interval, we need to maintain an array count1[], where count1[i] tells how many numbers from 1 to i have exactly one interval in which they appear.
  • New count for any interval will be count – (count1[r] – count1[l-1]). Since only those numbers which occur exactly in one interval and belong to [l, r] have to be excluded from actual count.

Below is the implementation of the above approach:Ā 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
Ā 
// Function To find the required interval
void solve(int interval[][2], int N, int Q)
{
Ā Ā Ā Ā int Mark[Q] = { 0 };
Ā Ā Ā Ā for (int i = 0; i < N; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā int l = interval[i][0] - 1;
Ā Ā Ā Ā Ā Ā Ā Ā int r = interval[i][1] - 1;
Ā Ā Ā Ā Ā Ā Ā Ā for (int j = l; j <= r; j++)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Mark[j]++;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Total Count of covered numbers
Ā Ā Ā Ā int count = 0;
Ā Ā Ā Ā for (int i = 0; i < Q; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā if (Mark[i])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Array to store numbers that occur
Ā Ā Ā Ā // exactly in one interval till ith interval
Ā Ā Ā Ā int count1[Q] = { 0 };
Ā Ā Ā Ā if (Mark[0] == 1)
Ā Ā Ā Ā Ā Ā Ā Ā count1[0] = 1;
Ā Ā Ā Ā for (int i = 1; i < Q; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā if (Mark[i] == 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count1[i] = count1[i - 1] + 1;
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count1[i] = count1[i - 1];
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā int maxindex;
Ā Ā Ā Ā int maxcoverage = 0;
Ā Ā Ā Ā for (int i = 0; i < N; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā int l = interval[i][0] - 1;
Ā Ā Ā Ā Ā Ā Ā Ā int r = interval[i][1] - 1;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Calculate New count by removing all numbers
Ā Ā Ā Ā Ā Ā Ā Ā // in range [l, r] occurring exactly once
Ā Ā Ā Ā Ā Ā Ā Ā int elem1;
Ā Ā Ā Ā Ā Ā Ā Ā if (l != 0)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elem1 = count1[r] - count1[l - 1];
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elem1 = count1[r];
Ā Ā Ā Ā Ā Ā Ā Ā if (count - elem1 >= maxcoverage) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maxcoverage = count - elem1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maxindex = i;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā Ā Ā Ā cout << "Maximum Coverage is " << maxcoverage
Ā Ā Ā Ā Ā Ā Ā Ā Ā << " after removing interval at index "
Ā Ā Ā Ā Ā Ā Ā Ā Ā << maxindex;
}
Ā 
// Driver code
int main()
{
Ā Ā Ā Ā int interval[][2] = { { 1, 4 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 4, 5 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 5, 6 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 6, 7 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 3, 5 } };
Ā Ā Ā Ā int N = sizeof(interval) / sizeof(interval[0]);
Ā Ā Ā Ā int Q = 7;
Ā Ā Ā Ā solve(interval, N, Q);
Ā 
Ā Ā Ā Ā return 0;
}


Java




// Java implementation of the approach
Ā 
class GFG
{
Ā 
// Function To find the required interval
static void solve(int interval[][], int N, int Q)
{
Ā Ā Ā Ā int Mark[] = new int[Q];
Ā Ā Ā Ā for (int i = 0; i < N; i++)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int l = interval[i][0] - 1;
Ā Ā Ā Ā Ā Ā Ā Ā int r = interval[i][1] - 1;
Ā Ā Ā Ā Ā Ā Ā Ā for (int j = l; j <= r; j++)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Mark[j]++;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Total Count of covered numbers
Ā Ā Ā Ā int count = 0;
Ā Ā Ā Ā for (int i = 0; i < Q; i++)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā if (Mark[i] != 0)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Array to store numbers that occur
Ā Ā Ā Ā // exactly in one interval till ith interval
Ā Ā Ā Ā int count1[] = new int[Q];
Ā Ā Ā Ā if (Mark[0] == 1)
Ā Ā Ā Ā Ā Ā Ā Ā count1[0] = 1;
Ā Ā Ā Ā for (int i = 1; i < Q; i++)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā if (Mark[i] == 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count1[i] = count1[i - 1] + 1;
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count1[i] = count1[i - 1];
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā int maxindex = 0;
Ā Ā Ā Ā int maxcoverage = 0;
Ā Ā Ā Ā for (int i = 0; i < N; i++)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int l = interval[i][0] - 1;
Ā Ā Ā Ā Ā Ā Ā Ā int r = interval[i][1] - 1;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Calculate New count by removing all numbers
Ā Ā Ā Ā Ā Ā Ā Ā // in range [l, r] occurring exactly once
Ā Ā Ā Ā Ā Ā Ā Ā int elem1;
Ā Ā Ā Ā Ā Ā Ā Ā if (l != 0)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elem1 = count1[r] - count1[l - 1];
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elem1 = count1[r];
Ā Ā Ā Ā Ā Ā Ā Ā if (count - elem1 >= maxcoverage)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maxcoverage = count - elem1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maxindex = i;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā Ā Ā Ā System.out.println("Maximum Coverage is " + maxcoverage
Ā Ā Ā Ā Ā Ā Ā Ā + " after removing interval at index "
Ā Ā Ā Ā Ā Ā Ā Ā + maxindex);
}
Ā 
// Driver code
public static void main(String[] args)
{
Ā Ā Ā Ā Ā Ā Ā Ā int interval[][] = { { 1, 4 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 4, 5 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 5, 6 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 6, 7 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 3, 5 } };
Ā Ā Ā Ā int N = interval.length;
Ā Ā Ā Ā int Q = 7;
Ā Ā Ā Ā solve(interval, N, Q);
}
}
Ā 
/* This code contributed by PrinciRaj1992 */


Python3




# Python3 implementation of the approach
Ā 
# Function To find the required interval
def solve(interval, N, Q):
Ā 
Ā Ā Ā Ā Mark = [0 for i in range(Q)]
Ā Ā Ā Ā for i in range(N):
Ā Ā Ā Ā Ā Ā Ā Ā l = interval[i][0] - 1
Ā Ā Ā Ā Ā Ā Ā Ā r = interval[i][1] - 1
Ā Ā Ā Ā Ā Ā Ā Ā for j in range(l, r + 1):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Mark[j] += 1
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā # Total Count of covered numbers
Ā Ā Ā Ā count = 0
Ā Ā Ā Ā for i in range(Q):
Ā Ā Ā Ā Ā Ā Ā Ā if (Mark[i]):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count += 1
Ā 
Ā Ā Ā Ā # Array to store numbers that occur
Ā Ā Ā Ā # exactly in one interval till ith interval
Ā Ā Ā Ā count1 = [0 for i in range(Q)]
Ā Ā Ā Ā if (Mark[0] == 1):
Ā Ā Ā Ā Ā Ā Ā Ā count1[0] = 1
Ā Ā Ā Ā for i in range(1, Q):
Ā Ā Ā Ā Ā Ā Ā Ā if (Mark[i] == 1):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count1[i] = count1[i - 1] + 1
Ā Ā Ā Ā Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count1[i] = count1[i - 1]
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā maxindex = 0
Ā Ā Ā Ā maxcoverage = 0
Ā Ā Ā Ā for i in range(N):
Ā Ā Ā Ā Ā Ā Ā Ā l = interval[i][0] - 1
Ā Ā Ā Ā Ā Ā Ā Ā r = interval[i][1] - 1
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # Calculate New count by removing all numbers
Ā Ā Ā Ā Ā Ā Ā Ā # in range [l, r] occurring exactly once
Ā Ā Ā Ā Ā Ā Ā Ā elem1 = 0
Ā Ā Ā Ā Ā Ā Ā Ā if (l != 0):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elem1 = count1[r] - count1[l - 1]
Ā Ā Ā Ā Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elem1 = count1[r]
Ā Ā Ā Ā Ā Ā Ā Ā if (count - elem1 >= maxcoverage):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maxcoverage = count - elem1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maxindex = i
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā print("Maximum Coverage is", maxcoverage,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "after removing interval at index", maxindex)
Ā 
# Driver code
interval = [[ 1, 4 ],
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 4, 5 ],
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 5, 6 ],
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 6, 7 ],
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 3, 5 ]]
N = len(interval)
Q = 7
solve(interval, N, Q)
Ā 
# This code is contributed by mohit kumar


C#




// C# implementation of the approach
using System;
Ā 
class GFG
{
Ā 
// Function To find the required interval
static void solve(int[,] interval, int N, int Q)
{
Ā Ā Ā Ā int[] Mark = new int[Q];
Ā Ā Ā Ā for (int i = 0; i < N; i++)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int l = interval[i,0] - 1;
Ā Ā Ā Ā Ā Ā Ā Ā int r = interval[i,1] - 1;
Ā Ā Ā Ā Ā Ā Ā Ā for (int j = l; j <= r; j++)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Mark[j]++;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Total Count of covered numbers
Ā Ā Ā Ā int count = 0;
Ā Ā Ā Ā for (int i = 0; i < Q; i++)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā if (Mark[i] != 0)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Array to store numbers that occur
Ā Ā Ā Ā // exactly in one interval till ith interval
Ā Ā Ā Ā int[] count1 = new int[Q];
Ā Ā Ā Ā if (Mark[0] == 1)
Ā Ā Ā Ā Ā Ā Ā Ā count1[0] = 1;
Ā Ā Ā Ā for (int i = 1; i < Q; i++)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā if (Mark[i] == 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count1[i] = count1[i - 1] + 1;
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count1[i] = count1[i - 1];
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā int maxindex = 0;
Ā Ā Ā Ā int maxcoverage = 0;
Ā Ā Ā Ā for (int i = 0; i < N; i++)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int l = interval[i,0] - 1;
Ā Ā Ā Ā Ā Ā Ā Ā int r = interval[i,1] - 1;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Calculate New count by removing all numbers
Ā Ā Ā Ā Ā Ā Ā Ā // in range [l, r] occurring exactly once
Ā Ā Ā Ā Ā Ā Ā Ā int elem1;
Ā Ā Ā Ā Ā Ā Ā Ā if (l != 0)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elem1 = count1[r] - count1[l - 1];
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elem1 = count1[r];
Ā Ā Ā Ā Ā Ā Ā Ā if (count - elem1 >= maxcoverage)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maxcoverage = count - elem1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maxindex = i;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā Ā Ā Ā Console.WriteLine("Maximum Coverage is " + maxcoverage
Ā Ā Ā Ā Ā Ā Ā Ā + " after removing interval at index "
Ā Ā Ā Ā Ā Ā Ā Ā + maxindex);
}
Ā 
// Driver code
public static void Main()
{
Ā Ā Ā Ā int[,] interval = { { 1, 4 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 4, 5 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 5, 6 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 6, 7 },
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 3, 5 } };
Ā Ā Ā Ā int N = interval.Length;
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā int Q = 7;
Ā Ā Ā Ā solve(interval, N/2, Q);
}
}
Ā 
/* This code contributed by Code_Mech */


Javascript




<script>
Ā 
// Javascript implementation of the approach
Ā 
// Function To find the required interval
function solve(interval, N, Q)
{
Ā Ā Ā Ā var Mark = Array(Q).fill(0);
Ā Ā Ā Ā for (var i = 0; i < N; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā var l = interval[i][0] - 1;
Ā Ā Ā Ā Ā Ā Ā Ā var r = interval[i][1] - 1;
Ā Ā Ā Ā Ā Ā Ā Ā for (var j = l; j <= r; j++)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Mark[j]++;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Total Count of covered numbers
Ā Ā Ā Ā var count = 0;
Ā Ā Ā Ā for (var i = 0; i < Q; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā if (Mark[i])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Array to store numbers that occur
Ā Ā Ā Ā // exactly in one interval till ith interval
Ā Ā Ā Ā var count1 = Array(Q).fill(0);
Ā Ā Ā Ā if (Mark[0] == 1)
Ā Ā Ā Ā Ā Ā Ā Ā count1[0] = 1;
Ā Ā Ā Ā for (var i = 1; i < Q; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā if (Mark[i] == 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count1[i] = count1[i - 1] + 1;
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count1[i] = count1[i - 1];
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā var maxindex;
Ā Ā Ā Ā var maxcoverage = 0;
Ā Ā Ā Ā for (var i = 0; i < N; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā var l = interval[i][0] - 1;
Ā Ā Ā Ā Ā Ā Ā Ā var r = interval[i][1] - 1;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Calculate New count by removing all numbers
Ā Ā Ā Ā Ā Ā Ā Ā // in range [l, r] occurring exactly once
Ā Ā Ā Ā Ā Ā Ā Ā var elem1;
Ā Ā Ā Ā Ā Ā Ā Ā if (l != 0)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elem1 = count1[r] - count1[l - 1];
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elem1 = count1[r];
Ā Ā Ā Ā Ā Ā Ā Ā if (count - elem1 >= maxcoverage) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maxcoverage = count - elem1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maxindex = i;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā Ā Ā Ā document.write( "Maximum Coverage is " + maxcoverage
Ā Ā Ā Ā Ā Ā Ā Ā Ā + " after removing interval at index "
Ā Ā Ā Ā Ā Ā Ā Ā Ā + maxindex);
}
Ā 
// Driver code
var interval = [ [ 1, 4 ],
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 4, 5 ],
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 5, 6 ],
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 6, 7 ],
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 3, 5 ] ];
var N = interval.length;
var Q = 7;
solve(interval, N, Q);
Ā 
</script>


Output

Maximum Coverage is 7 after removing interval at index 4

Time Complexity: O(N + Q), where N is the given size of the 2-D array and Q is the given input.
Auxiliary Space: O(Q), where Q is the given input.

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 zambiatek!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button