- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find the largest interval that contains exactly one of the given N integers In C++

Suppose we have an array of N distinct integers. We have to find the max element in an interval [L, R] such that the interval contains exactly one of the given N integers and 1 <= L <= R <= 10^{5}.

So if the array is like Arr = [5, 10, 200], then output is 99990. So all possible intervals are [1, 9], [6, 199] and [11, 100000]. The last one has the maximum integers like 99990

The idea is simple. We will fix the element that we want our interval to contain. Now, we will see how we can extend our interval to left and right without overlapping other elements. So we have to sort the array first, then for a fixed element, we determine the end using previous and next element. We will take care of the corner cases. So when we are fixing the first and last intervals. This way for every element i, we find the maximum length of the interval.

## Example

#include<iostream> #include<algorithm> #include<vector> using namespace std; int maximumSize(vector<int>& vec, int n) { vec.push_back(0); vec.push_back(100001); n += 2; sort(vec.begin(), vec.end()); int max_value = 0; for (int i = 1; i < n - 1; i++) { int Left = vec[i - 1] + 1; int Right = vec[i + 1] - 1; int count = Right - Left + 1; max_value = max(max_value, count); } return max_value; } int main() { vector<int> v; v.push_back(200); v.push_back(10); v.push_back(5); int n = v.size(); cout << "Maximum Size is: " << maximumSize(v, n); }

## Output

Maximum Size is: 99990

- Related Questions & Answers
- Find n positive integers that satisfy the given equations in C++
- Given an array of integers, find the pair of adjacent elements that has the largest product and return that product JavaScript
- Find the largest good number in the divisors of given number N in C++
- Find the largest number that can be formed with the given digits in C++
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K in C++
- Find the minimum and maximum values that can be calculated by summing exactly four of the five integers in JavaScript
- Find the largest after deleting the given elements in C++
- Program to find number of sublists that contains exactly k different words in Python
- Find a range that covers all the elements of given N ranges in C++
- Find permutation of first N natural numbers that satisfies the given condition in C++
- Find the largest twins in given range in C++
- Find integers that divides maximum number of elements of the array in C++
- Maximum GCD of N integers with given product in C++
- Find N integers with given difference between product and sum in C++
- How to find the position of minimum value in a vector that contains integers as strings in R?