
- 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
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 <= 105.
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 Articles
- Find n positive integers that satisfy the given equations in C++
- Find the largest good number in the divisors of given number N 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 number that can be formed with the given digits in C++
- Find the largest twins in given range in C++
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K in C++
- Find the largest after deleting the given elements in C++
- Find the number of integers from 1 to n which contains digits 0’s and 1’s only in C++
- Find N integers with given difference between product and sum in C++
- Program to find number of sublists that contains exactly k different words in Python
- Maximum GCD of N integers with given product in C++
- Find the minimum and maximum values that can be calculated by summing exactly four of the five integers in JavaScript
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++
- 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++
