# 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

The line segments will be as follows:

- Line-Segment 1: (Center1-L/2, Center1+L/2)
- Line-Segment 2: (Center2-L/2, Center2+L/2)
- 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[2] – center[0] – 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 <bits/stdc++.h> ` `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[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 ` `int` `main() ` `{ ` ` ` `int` `center[3] = { 1, 2, 3 }; ` ` ` `int` `L = 1; ` ` ` `int` `K = 1; ` ` ` `cout << max_intersection(center, L, K); ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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[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() ` `{ ` ` ` `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 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation ` `// of above approach ` ` ` `// Function to print the ` `// maximum intersection ` `function` `max_intersection(` `$center` `, ` ` ` `$length` `, ` `$k` `) ` `{ ` ` ` `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 ` `$center` `= ` `array` `(1, 2, 3); ` `$L` `= 1; ` `$K` `= 1; ` `echo` `max_intersection(` `$center` `, ` `$L` `, ` `$K` `); ` ` ` `// This code is contributed ` `// by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1

## Recommended Posts:

- Maximum number of line intersections formed through intersection of N planes
- Given n line segments, find if any two segments intersect
- Equation of straight line passing through a given point which bisects it into two equal line segments
- Length of the perpendicular bisector of the line joining the centers of two circles
- Ratio of the distance between the centers of the circles and the point of intersection of two transverse common tangents to the circles
- Ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles
- How to check if two given line segments intersect?
- Klee's Algorithm (Length Of Union Of Segments of a line)
- Number of horizontal or vertical line segments to connect 3 points
- Maximum sum of the array after dividing it into three segments
- Maximum number of segments that can contain the given points
- Maximum points of intersection n lines
- Maximum points of intersection n circles
- Count maximum points on same line
- Maximum number that can be display on Seven Segment Display using N segments

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.