Given N ranges [L, R] and an integer K, the task is to check if there are any K ranges which overlap at any point.
Examples:
Input: ranges[][] = {{1, 3}, {2, 4}, {3, 4}, {7, 10}}, K = 3
Output: Yes
3 is a common point among the
ranges {1, 3}, {2, 4} and {3, 4}.Input: ranges[][] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}, K = 2
Output: No
Approach: The idea is to make a vector of pair and store the starting point for every range as pair in this vector as (starting point, -1) and the ending point as (ending point, 1). Now, sort the vector then traverse the vector and if the current element is a starting point then push it in a stack and if it is an ending point then pop an element from the stack. If at any instance of time, the size of the stack is greater than or equal K then print Yes else print No in the end.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Comparator to sort the vector of pairs bool sortby(const pair<int, int>& a, const pair<int, int>& b) { if (a.first != b.first) return a.first < b.first; return (a.second < b.second); } // Function that returns true if any k // segments overlap at any point bool kOverlap(vector<pair<int, int> > pairs, int k) { // Vector to store the starting point // and the ending point vector<pair<int, int> > vec; for (int i = 0; i < pairs.size(); i++) { // Starting points are marked by -1 // and ending points by +1 vec.push_back({ pairs[i].first, -1 }); vec.push_back({ pairs[i].second, +1 }); } // Sort the vector by first element sort(vec.begin(), vec.end()); // Stack to store the overlaps stack<pair<int, int> > st; for (int i = 0; i < vec.size(); i++) { // Get the current element pair<int, int> cur = vec[i]; // If it is the starting point if (cur.second == -1) { // Push it in the stack st.push(cur); } // It is the ending point else { // Pop an element from stack st.pop(); } // If more than k ranges overlap if (st.size() >= k) { return true; } } return false; } // Driver code int main() { vector<pair<int, int> > pairs; pairs.push_back(make_pair(1, 3)); pairs.push_back(make_pair(2, 4)); pairs.push_back(make_pair(3, 5)); pairs.push_back(make_pair(7, 10)); int n = pairs.size(), k = 3; if (kOverlap(pairs, k)) cout << "Yes"; else cout << "No"; return 0; } |
Python3
# Python implementation of the approach # Function that returns true if any k # segments overlap at any point def kOverlap(pairs: list, k): # Vector to store the starting point # and the ending point vec = list() for i in range(len(pairs)): # Starting points are marked by -1 # and ending points by +1 vec.append((pairs[0], -1)) vec.append((pairs[1], 1)) # Sort the vector by first element vec.sort(key = lambda a: a[0]) # Stack to store the overlaps st = list() for i in range(len(vec)): # Get the current element cur = vec[i] # If it is the starting point if cur[1] == -1: # Push it in the stack st.append(cur) # It is the ending point else: # Pop an element from stack st.pop() # If more than k ranges overlap if len(st) >= k: return True return False # Driver Code if __name__ == "__main__": pairs = list() pairs.append((1, 3)) pairs.append((2, 4)) pairs.append((3, 5)) pairs.append((7, 10)) n = len(pairs) k = 3 if kOverlap(pairs, k): print("Yes") else: print("No") # This code is contributed by # sanjeev2552 |
Yes
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:
- Queries to check if a number lies in N ranges of L-R
- Print matrix after applying increment operations in M ranges
- Find k-th smallest element in given n ranges
- Search an element in given N ranges
- Find the kth element in the series generated by the given N ranges
- Number of intersections between two ranges
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Check if a point is inside, outside or on the ellipse
- Check if a point is inside, outside or on the parabola
- Check if digit cube limit of an integer arrives at fixed point or a limit cycle
- Find a Fixed Point (Value equal to index) in a given array
- Unbounded Binary Search Example (Find the point where a monotonically increasing function becomes positive first time)
- Precision of floating point numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())
- Binary Search for Rational Numbers without using floating point arithmetic
- Best meeting point in 2D binary array
- Finding the best fit rectangle that covers a given point
- Find normal at a given point on the curve
- Find Tangent at a given point on the curve
- Lower Insertion Point
- Find a partition point in array to maximize its xor sum
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

