Maximum possible intersection by moving centers of line segments | Java implementation of above approach ; Function to print the maximum intersection ; Case 1 ; Case 2 ; Case 3 ; Driver Code
import java . util . * ; class GFG { static int max_intersection ( int center [ ] , int length , int k ) { Arrays . sort ( center ) ; 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 ; } public static void main ( String args [ ] ) { int center [ ] = { 1 , 2 , 3 } ; int L = 1 ; int K = 1 ; System . out . println ( max_intersection ( center , L , K ) ) ; } }