Open In App

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

Below is the implementation of the above approach: 
 




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




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


Article Tags :