Find the largest interval that contains exactly one of the given N integers . | 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
function maxSize ( v , n ) { v . push ( 0 ) ; v . push ( 100001 ) ; n += 2 ; v . sort ( ( a , b ) => a - b ) ; let mx = 0 ; for ( let i = 1 ; i < n - 1 ; i ++ ) { let L = v [ i - 1 ] + 1 ; let R = v [ i + 1 ] - 1 ; let cnt = R - L + 1 ; mx = Math . max ( mx , cnt ) ; } return mx ; } let v = [ 200 , 10 , 5 ] ; let n = v . length ; document . write ( maxSize ( v , n ) ) ;