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 intusing namespace std;Ā
// Function To find the required intervalvoid 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 codeint 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 intervalstatic 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 codepublic 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 intervaldef 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 codeinterval = [[ 1, 4 ],Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 4, 5 ],Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 5, 6 ],Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 6, 7 ],Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 3, 5 ]]N = len(interval)Q = 7solve(interval, N, Q)Ā
# This code is contributed by mohit kumar |
C#
// C# implementation of the approachusing System;Ā
class GFG {Ā
// Function To find the required intervalstatic 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 codepublic 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 intervalfunction 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 codevar interval = [ [ 1, 4 ],Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 4, 5 ],Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 5, 6 ],Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 6, 7 ],Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 3, 5 ] ];var N = interval.length;var Q = 7;solve(interval, N, Q);Ā
</script> |
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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


