Was it helpful?

Question


In this tutorial, we will be discussing a program to find maximum possible intersection by moving centers of line segments

For this we will be provided with the center of three line segments and their length. Our task is to move their center by K distance to increase the length of intersection region.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
//finding maximum intersection
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);
}

Output

1
raja
Published on 09-Sep-2020 12:21:51

Was it helpful?
Not affiliated with Tutorialspoint
scroll top