# Maximum possible intersection by moving centers of line segments

Given three points on the X-axis which denotes the center of three line segments. The length of the line segment is also given as L. The task is to move the center of the given line segments by a distance K to maximize the length of intersection between the three lines.

Examples:

Input: c1 = 1, c2 = 2, c3 = 3, L = 1, K = 0
Output: 0
Since, no center can be moved, there is no intersection among three segments {(0.5, 1.5), (1.5, 2.5), (2.5, 3.5)}.

Input: c1 = 1, c2 = 2, c3 = 3, L = 1, K = 1
Output: 1
Center’s 1 and 3 can be shifted 1 unit ahead and forward respectively to overlap with center 2

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The line segments will be as follows:

1. Line-Segment 1: (Center1-L/2, Center1+L/2)
2. Line-Segment 2: (Center2-L/2, Center2+L/2)
3. Line-Segment 3: (Center3-L/2, Center3+L/2)

Approach: Initially sort the centers, since the middle segment will never move as left and right segment can always reach near to its center to increase the intersection length. There will be three cases which are to be dealt with:

• Case 1: When the distance between centers of the right and left is more than or equal to 2*K+L in such scenario intersection will always be zero. No overlapping is possible since even after shifting both centers with K distance still, they will be away at a distance of L or more.
• Case 2: When the distance between centers of the right and left is more than or equal to 2*K in such scenario there exists an intersection which is equal to (2 * k – (center – center – length)) which can be calculated using simple maths.
• Case 3: When the distance between centers of the right and left is less than 2*K in such case both centers can coincide with the middle center. Hence, the intersection is L.

Below is the implementation of the above approach:

## C++

 `#include ` `using` `namespace` `std; ` ` `  `// Function to print the maximum intersection  ` `int` `max_intersection(``int``* center, ``int` `length, ``int` `k) ` `{ ` `    ``sort(center, center + 3); ` ` `  `    ``// Case 1 ` `    ``if` `(center - center >= 2 * k + length) { ` `        ``return` `0; ` `    ``} ` ` `  `    ``// Case 2 ` `    ``else` `if` `(center - center >= 2 * k) { ` `        ``return` `(2 * k - (center - center - length)); ` `    ``} ` ` `  `    ``// Case 3 ` `    ``else` `        ``return` `length; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `center = { 1, 2, 3 }; ` `    ``int` `L = 1; ` `    ``int` `K = 1; ` `    ``cout << max_intersection(center, L, K); ` `} `

## Java

 `// Java implementation  ` `// of above approach ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` ` `  `// Function to print the ` `// maximum intersection  ` `static` `int` `max_intersection(``int` `center[],  ` `                            ``int` `length, ``int` `k)  ` `{  ` `    ``Arrays.sort(center);  ` ` `  `    ``// Case 1  ` `    ``if` `(center[``2``] - center[``0``] >= ``2` `* k + length)  ` `    ``{  ` `        ``return` `0``;  ` `    ``}  ` ` `  `    ``// Case 2  ` `    ``else` `if` `(center[``2``] - center[``0``] >= ``2` `* k) ` `    ``{  ` `        ``return` `(``2` `* k - (center[``2``] -  ` `                ``center[``0``] - length));  ` `    ``}  ` ` `  `    ``// Case 3  ` `    ``else` `        ``return` `length;  ` `}  ` ` `  `// Driver Code  ` `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));  ` `}  ` `} ` ` `  `// This code is contributed ` `// by Arnab Kundu `

## Python3

 `# Python3 implementation of above approach ` ` `  `# Function to print the maximum intersection  ` `def` `max_intersection(center, length, k):  ` ` `  `    ``center.sort();  ` ` `  `    ``# Case 1  ` `    ``if` `(center[``2``] ``-` `center[``0``] >``=` `2` `*` `k ``+` `length): ` `        ``return` `0``;  ` ` `  `    ``# Case 2  ` `    ``elif` `(center[``2``] ``-` `center[``0``] >``=` `2` `*` `k): ` `        ``return` `(``2` `*` `k ``-` `(center[``2``] ``-` `center[``0``] ``-` `length));  ` ` `  `    ``# Case 3  ` `    ``else``: ` `        ``return` `length;  ` ` `  `# Driver Code  ` `center ``=` `[``1``, ``2``, ``3``];  ` `L ``=` `1``;  ` `K ``=` `1``;  ` `print``(max_intersection(center, L, K));  ` ` `  `# This code is contributed ` `# by mits `

## C#

 `// C# implementation  ` `// of above approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `// Function to print the ` `// maximum intersection  ` `static` `int` `max_intersection(``int` `[]center,  ` `                            ``int` `length, ``int` `k)  ` `{  ` `    ``Array.Sort(center);  ` ` `  `    ``// Case 1  ` `    ``if` `(center - center >= 2 * k + length)  ` `    ``{  ` `        ``return` `0;  ` `    ``}  ` ` `  `    ``// Case 2  ` `    ``else` `if` `(center -  ` `             ``center >= 2 * k) ` `    ``{  ` `        ``return` `(2 * k - (center -  ` `                         ``center - length));  ` `    ``}  ` ` `  `    ``// Case 3  ` `    ``else` `        ``return` `length;  ` `}  ` ` `  `// Driver Code  ` `public` `static` `void` `Main() ` `{  ` `    ``int` `[]center = { 1, 2, 3 };  ` `    ``int` `L = 1;  ` `    ``int` `K = 1;  ` `    ``Console.WriteLine(max_intersection(center, L, K));  ` `}  ` `} ` ` `  `// This code is contributed ` `// by Subhadeep Gupta `

## PHP

 `= 2 * ``\$k` `+ ``\$length``)  ` `    ``{  ` `        ``return` `0;  ` `    ``}  ` ` `  `    ``// Case 2  ` `    ``else` `if` `(``\$center`` -  ` `             ``\$center`` >= 2 * ``\$k``) ` `    ``{  ` `        ``return` `(2 * ``\$k` `- (``\$center`` -  ` `             ``\$center`` - ``\$length``));  ` `    ``}  ` ` `  `    ``// Case 3  ` `    ``else` `        ``return` `\$length``;  ` `}  ` ` `  `// Driver Code  ` `\$center` `= ``array``(1, 2, 3);  ` `\$L` `= 1;  ` `\$K` `= 1;  ` `echo` `max_intersection(``\$center``, ``\$L``, ``\$K``);  ` ` `  `// This code is contributed ` `// by mits ` `?> `

Output:

```1
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.