You will be given ‘M’ ranges each consisting of a left limit and a right limit. Then you will be given an integer ‘n’ and you need to select those particular ranges such that
for each range (L,R) L<= n <= R, or n remains inside that range.
We must access the acceptable ranges in the descending order of the right limit.
For example :
Ranges :
4 16
3 18
1 5
integer : 10
acceptable ranges : 4 16 and 3 18. (1,2) does not include 10.
answer:
3 18
4 16
(3 18) will come first as the right limit is greater (18 > 16).
Try storing the ranges in vector of std::pairs <int,int>
Sort them on el.second, where el is a pair in the vector
Check the range condition after accepting the input and just don’t push_back into the vector if n is out of range
The question appears to be different from what you asked. From the first look, it appears to me that it is all about first finding overlapping segments and then selecting the ones with the most bad points, not very sure about the correctness but it can be done in O(N.logN) that is acceptable as per the given constraints.