Find the largest interval that contains exactly one of the given N integers . | C ++ implementation of the approach ; Function to return the maximum size of the required interval ; Insert the borders for array ; Sort the elements in ascending order ; To store the maximum size ; To store the range [ L , R ] such that only v [ i ] lies within the range ; Total integers in the range ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSize ( vector < int > & v , int n ) { v . push_back ( 0 ) ; v . push_back ( 100001 ) ; n += 2 ; sort ( v . begin ( ) , v . end ( ) ) ; int mx = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { int L = v [ i - 1 ] + 1 ; int R = v [ i + 1 ] - 1 ; int cnt = R - L + 1 ; mx = max ( mx , cnt ) ; } return mx ; } int main ( ) { vector < int > v = { 200 , 10 , 5 } ; int n = v . size ( ) ; cout << maxSize ( v , n ) ; return 0 ; }