
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum points covered after removing an Interval in C++
In this tutorial, we will be discussing a program to find maximum points covered after removing an Interval
For this we will be provided with N intervals and the maximum range value . Our task is to find that one interval which when removed will give us the maxim numbers in the given range from 1 to maximum range value
Example
#include <bits/stdc++.h> #define ll long long int using namespace std; //finding 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]++; } //counting covered numbers int count = 0; for (int i = 0; i < Q; i++) { if (Mark[i]) count++; } 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; 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; } 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; }
Output
Maximum Coverage is 7 after removing interval at index 4
- Related Articles
- Maximize the maximum subarray sum after removing at most one element in C++
- Removing the Min Element from Interval Heaps
- MySQL query to get records after an interval of 8 months
- Cell Count After Removing Corner Diagonals in Python
- Smallest number after removing n digits in JavaScript
- Python Pandas IntervalIndex - Indicates if an interval is empty (contains no points)
- Balance a string after removing extra brackets in C++
- Find the maximum distance covered using n bikes in Python
- Maximum subarray sum in an array created after repeated concatenation in C++
- Maximum value in an array after m range increment operations in C++
- Python Pandas IntervalIndex - Check if an interval that contains points is empty or not
- Maximum sum subarray removing at most one element in C++
- Program to find maximum score from removing stones in Python
- Program to count maximum score from removing substrings in Python
- C++ code to find xth element after removing numbers

Advertisements