Maximum possible intersection by moving centers of line segments | Function to print the maximum intersection ; Case 1 ; Case 2 ; Case 3 ; driver program
function max_intersection ( center , length , k ) { center . sort ( ) ; if ( center [ 2 ] - center [ 0 ] >= 2 * k + length ) { return 0 ; } else if ( center [ 2 ] - center [ 0 ] >= 2 * k ) { return ( 2 * k - ( center [ 2 ] - center [ 0 ] - length ) ) ; } else return length ; } let center = [ 1 , 2 , 3 ] ; let L = 1 ; let K = 1 ; document . write ( max_intersection ( center , L , K ) ) ;