- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Klee’s Algorithm (Length Of Union Of Segments of a line) in C++

In this tutorial, we are going to write a program that finds the length of union of segments of a line.

We are given starting and ending points of the line segment and we need to find the length of union of segments of the line.

The algorithm that we are going to use is called klee's algorithm.

Let's see the steps to solve the problem.

- Initialise the array with coordinates of all the segments.
- Initialise a vector called points with double the size of segments array.
- Iterate over the segments array.
- Fill the values of points array at index i * 2 with the first point of the current segment and false.
- Fill the values of points array at index i * 2 + 1 with the second point of the current segment and false.

- Sort the points array.
- Iterate over the points array with a counter variable.
- If the counter is greater than 0, then add the first points of i and i - 1 to the result.
- Decrement the counter if there is second point else increment it.

- Return the result.

## Example

Let's see the code.

#include<bits/stdc++.h> using namespace std; int segmentUnionLength(const vector<pair <int,int>> &segments) { int n = segments.size(); vector<pair<int, bool>> points(n * 2); for (int i = 0; i < n; i++) { points[i*2] = make_pair(segments[i].first, false); points[i*2 + 1] = make_pair(segments[i].second, true); } sort(points.begin(), points.end()); int result = 0, count = 0; for (int i = 0; i < n * 2; i++){ if (count) { result += points[i].first - points[i-1].first; } points[i].second ? count-- : count++; } return result; } int main() { vector<pair<int,int>> segments; segments.push_back(make_pair(1, 3)); segments.push_back(make_pair(2, 7)); segments.push_back(make_pair(6, 12)); segments.push_back(make_pair(13, 5)); cout << segmentUnionLength(segments) << endl; return 0; }

## Output

If you run the above code, then you will get the following result.

6

## Conclusion

If you have any queries in the tutorial, mention them in the comment section.

- Related Questions & Answers
- Maximum length of segments of 0’s and 1’s in C++
- Maximum number of parallelograms that can be made using the given length of line segments in C++
- Maximize the number of segments of length p, q and r in C++
- Find all disjointed intersections in a set of vertical line segments in JavaScript
- Maximum possible intersection by moving centers of line segments in C++
- Program to find number of sets of k-non-overlapping line segments in Python
- How do I find the intersection of two line segments in Matplotlib?
- Number of Segments in a String in C++
- C++ Program to get maximum area of rectangle made from line segments
- Check if two line segments intersect
- Maximum number of segments of lengths a, b and c in C++
- C++ program to find the number of triangles amongst horizontal and vertical line segments
- Union of SortedSet to a collection in C#
- JavaScript Union of two objects
- Union of two HashSet in C#

Advertisements