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 of 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;
int max_intersection( int * center, int length, int k)
{
sort(center, center + 3);
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;
}
int main()
{
int center[3] = { 1, 2, 3 };
int L = 1;
int K = 1;
cout << max_intersection(center, L, K);
}
|
Java
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));
}
}
|
Python3
def max_intersection(center, length, k):
center.sort();
if (center[ 2 ] - center[ 0 ] > = 2 * k + length):
return 0 ;
elif (center[ 2 ] - center[ 0 ] > = 2 * k):
return ( 2 * k - (center[ 2 ] - center[ 0 ] - length));
else :
return length;
center = [ 1 , 2 , 3 ];
L = 1 ;
K = 1 ;
print (max_intersection(center, L, K));
|
C#
using System;
class GFG
{
static int max_intersection( int []center,
int length, int k)
{
Array.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()
{
int []center = { 1, 2, 3 };
int L = 1;
int K = 1;
Console.WriteLine(max_intersection(center, L, K));
}
}
|
PHP
<?php
function max_intersection( $center ,
$length , $k )
{
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 ;
}
$center = array (1, 2, 3);
$L = 1;
$K = 1;
echo max_intersection( $center , $L , $K );
?>
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
01 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...