Consider an infinite x-y plane. Infinite people walk on the plane in upward or +ve Y direction. At each integer point on the x-axis, only one person walks. Suppose, several barriers exist parallel to the x-axis. Barriers are provided as three truplets

- X1 – the x-point at which the barrier starts
- X2 – the x-point at which the barrier ends.
- Y – the point on the Y axis where the barrier lies.

Calculate how many people will get stuck at any point due to the barriers. Use of extra space to keep track of different points on x axis is not allowed.

Examples:

Input : barriers[] = {{3 6 2}, {-7 4 3} Output : 14 The barrier from 3 to 6 blocks 3, 4, 5, 6, so 4 persons blocked till now. The barrier from -7 to 4 blocks -7,-6,-5,-4,- 3, -2, -1, 0, 1, 2, 3, 4. But, 3 and 4 have already been blocked. So, total persons blocked is 14.

**Asked in: Microsoft IDC Internship.**

A simple approach is to use a very long array initialized to zero. Then we can mark those values by 1 which are in the barrier by looping through each barrier. This would solve the case of overlapping of barriers.

But we cannot use another array as mentioned before. Thus, we use sorting and simple math. Following are the steps:

**1.** Sort all barriers by x1 (Starting point)

**2. ** After sorting, 3 cases arrive:

……I. The next barrier does not overlap with the previous. In this case, we simply add count of points covered by current barrier.

……II. The next barrier partly overlaps the previous one. In this point we add non-overlapping points covered by current barrier.

……III. The next barrier completely overlaps the previous barrier. In this case, we simply ignore current barrier.

Below is the implementation of above approach.

`// CPP program to find number of people ` `// that are stopped by barriers ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `struct` `Barrier ` `{ ` ` ` `int` `x1, x2, y; ` `}; ` ` ` `// Compares two Barriers according to x1 ` `bool` `compareBarrier(Barrier b1, Barrier b2) ` `{ ` ` ` `return` `(b1.x1 < b2.x1); ` `} ` ` ` `// Returns number of people blocked by ` `// array of barriers arr[0..n-1] ` `int` `countStopped(Barrier arr[], ` `int` `n) ` `{ ` ` ` `// Sort barriers according to x1. ` ` ` `sort(arr, arr+n, compareBarrier); ` ` ` ` ` `// End point of previous barrier ` ` ` `// Initializing with some value ` ` ` `// smaller than lowest x1. ` ` ` `int` `prev_end = arr[0].x1 - 1; ` ` ` ` ` `// Traverse through all bariers ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `{ ` ` ` `// If current barrier doesn't overlap ` ` ` `// with previous ` ` ` `if` `(prev_end < arr[i].x1) ` ` ` `{ ` ` ` `count += (arr[i].x2 - arr[i].x1 + 1); ` ` ` `prev_end = arr[i].x2; ` ` ` `} ` ` ` ` ` `// If current barrier overlaps and ` ` ` `// blocks some more people ` ` ` `else` `if` `(prev_end < arr[i].x2) ` ` ` `{ ` ` ` `count += (arr[i].x2 - prev_end); ` ` ` `prev_end = arr[i].x2; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `Barrier arr[] = {{3, 6, 2}, {-7, 4, 3}}; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `cout << countStopped(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output :

14

We can easily note that there is no importance of y in the question, so it may not be stored.

Time Complexity : O(n Log n)

This article is contributed by **Suprotik Dey**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if any two intervals overlap among a given set of intervals
- Find Non-overlapping intervals among a given set of intervals
- Count of available non-overlapping intervals to be inserted to make interval [0, R]
- Find the point on X-axis from given N points having least Sum of Distances from all other points
- Find least non-overlapping number from a given set of intervals
- Find the point where maximum intervals overlap
- Maximal Disjoint Intervals
- Maximum number of overlapping Intervals
- Check if the given point lies inside given N points of a Convex Polygon
- Maximum number of segments that can contain the given points
- Minimum Initial Points to Reach Destination
- Sum of Manhattan distances between all pairs of points
- Maximum distance between two points in coordinate plane using Rotating Caliper's Method
- Print 2-D co-ordinate points in ascending order followed by their frequencies
- Find the K closest points to origin using Priority Queue
- Count pairs from two sorted arrays whose sum is equal to a given value x
- Count pairs from two linked lists whose sum is equal to a given value
- Count quadruples from four sorted arrays whose sum is equal to a given value x
- Count subtrees that sum up to a given value x only using single recursive function
- Count pairs from two sorted matrices with given sum