3

Minimize segments required to be removed such that at least one segment intersec...

 1 year ago
source link: https://www.geeksforgeeks.org/minimize-segments-required-to-be-removed-such-that-at-least-one-segment-intersects-with-all-remaining-segments/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Minimize segments required to be removed such that at least one segment intersects with all remaining segments

Given an array arr[] consisting of N pairs [L, R], where L and R denotes the start and end indices of a segment, the task is to find the minimum number of segments that must be deleted from the array such that the remaining array contains at least one segment which intersects with all other segments present in the array.

Examples:

Input: arr[] = {{1, 2}, {5, 6}, {6, 7}, {7, 10}, {8, 9}}
Output: 2
Explanation: Delete the segments {1, 2} and {5, 6}. Therefore, the remaining array contains the segment {7, 10} which intersects with all other segments.

Input: a[] = {{1, 2}, {2, 3}, {1, 5}, {4, 5}}
Output: 0
Explanation: The segment {1, 5} already intersects with all other remaining segments. Hence, no need to delete any segment.

Approach: The maximum possible answer is (N – 1), since after deleting (N – 1) segments from arr[], only one segment will be left. This segment intersects with itself. To achieve the minimum answer, the idea is to iterate through all the segments, and for each segment, check the number of segments which do not intersect with it.

Two segments [f1, s1] and [f2, s2] intersect only when max(f1, f2) ≤ min(s1, s2)
Therefore, if [f1, s1] does not intersect with [f2, s2], then there are only two possibilities:

  1. s1 < f2 i.e segment 1 ends before the start of segment 2
  2. f1 > s2  i.e segment 1 starts after the end of segment 2.

Follow the steps below to solve the problem:

  • Traverse the array arr[] and store the starting point and ending point of each segment in startPoints[], and endPoints[] respectively.
  • Sort both the arrays, startPoints[] and endPoints[] in increasing order.
  • Initialize ans as (N – 1) to store the number of minimum deletions required.
  • Again traverse the array, arr[] and for each segment:
    • Store the number of segments satisfying the first and the second condition of non-intersection in leftDelete and rightDelete respectively.
    • If leftDelete + rightDelete is less than ans, then set ans to leftDelete + rightDelete.
  • After the above steps, print the value of ans as the result.

Below is the implementation of the above approach:

  • C++14
  • Python3
  • Javascript
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum number
// of segments required to be deleted
void minSegments(pair<int, int> segments[],
int n)
{
// Stores the start and end points
int startPoints[n], endPoints[n];
// Traverse segments and fill the
// startPoints and endPoints
for (int i = 0; i < n; i++) {
startPoints[i] = segments[i].first;
endPoints[i] = segments[i].second;
}
// Sort the startPoints
sort(startPoints, startPoints + n);
// Sort the startPoints
sort(endPoints, endPoints + n);
// Store the minimum number of
// deletions required and
// initialize with (N - 1)
int ans = n - 1;
// Traverse the array segments[]
for (int i = 0; i < n; i++) {
// Store the starting point
int f = segments[i].first;
// Store the ending point
int s = segments[i].second;
// Store the number of segments
// satisfying the first condition
// of non-intersection
int leftDelete
= lower_bound(endPoints,
endPoints + n, f)
- endPoints;
// Store the number of segments
// satisfying the second condition
// of non-intersection
int rightDelete = max(
0, n - (int)(upper_bound(startPoints,
startPoints + n, s)
- startPoints));
// Update answer
ans = min(ans,
leftDelete
+ rightDelete);
}
// Print the answer
cout << ans;
}
// Driver Code
int main()
{
pair<int, int> arr[] = {
{ 1, 2 }, { 5, 6 },
{ 6, 7 }, { 7, 10 }, { 8, 9 }
};
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
minSegments(arr, N);
return 0;
}
Output: 
2

Time Complexity: O(N*(log N2))
Auxiliary Space: O(N)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK