Open In App

Maximum possible intersection by moving centers of line segments

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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:
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:
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: 
 

  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[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);
}


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[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


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
?>


Javascript




<script>
 
// Javascript implementation
// of above approach
 
// Function to print the
// maximum intersection
function max_intersection(center,length, k)
{
    center.sort();
   
    // 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 program
     
    let center = [ 1, 2, 3 ];
    let L = 1;
    let K = 1;
    document.write( max_intersection(center, L, K));
    
</script>


Output: 

1

 

Time Complexity: O(1)

Auxiliary Space: O(1)



Last Updated : 01 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads