- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum possible intersection by moving centers of line segments in C++
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
#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
Advertisements