# Maximum points covered after removing an Interval

• Difficulty Level : Medium
• Last Updated : 14 May, 2021

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 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 ``#define ll long long int``using` `namespace` `std;` `// Function To find the required interval``void` `solve(``int` `interval[], ``int` `N, ``int` `Q)``{``    ``int` `Mark[Q] = { 0 };``    ``for` `(``int` `i = 0; i < N; i++) {``        ``int` `l = interval[i] - 1;``        ``int` `r = interval[i] - 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 == 1)``        ``count1 = 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] - 1;``        ``int` `r = interval[i] - 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[] = { { 1, 4 },``                          ``{ 4, 5 },``                          ``{ 5, 6 },``                          ``{ 6, 7 },``                          ``{ 3, 5 } };``    ``int` `N = ``sizeof``(interval) / ``sizeof``(interval);``    ``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 == 1)``        ``count1 = 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

 ``

Output:

`Maximum Coverage is 7 after removing interval at index 4`

My Personal Notes arrow_drop_up