Find the largest interval that contains exactly one of the given N integers . | Java 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
import static java . lang . Integer . max ; import java . util . * ; class GFG { static int maxSize ( Vector < Integer > v , int n ) { v . add ( 0 ) ; v . add ( 100001 ) ; n += 2 ; Collections . sort ( v ) ; int mx = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { int L = v . get ( i - 1 ) + 1 ; int R = v . get ( i + 1 ) - 1 ; int cnt = R - L + 1 ; mx = max ( mx , cnt ) ; } return mx ; } public static void main ( String [ ] args ) { Integer arr [ ] = { 200 , 10 , 5 } ; Vector v = new Vector ( Arrays . asList ( arr ) ) ; int n = v . size ( ) ; System . out . println ( maxSize ( v , n ) ) ; } }