Given two parallel traces of size N and an array containing complete N endpoints in array EndPoints[]. Given that there’s a straight line from every i, such that 1 <= i <= N, such that every line ought to finish at any of the EndPoint[j]. The duty is to get the depend of most variety of intersections from the traces.
Discover: Tright here ought to be a complete of n traces drawn and each endpoint given within the array ought to be coated.
Instance:
Enter: 7
EndPoints[]: {4, 1, 4, 6, 7, 7, 5}
Output: 6![]()
enter 1
As you may see within the diagram above, a line is drawn from every i (1, 2, 3, 4, 5, 6, 7) and the endpoint is without doubt one of the values from EndPoints[] given within the enter. It may be confirmed that there cannot be greater than 6 intersections on this case.
Enter: 5
EndPoints[]: {2, 1, 4, 3, 5}
Output: 2![]()
Enter 2
Naive Method:
Let’s take a look at two traces from i→EndPoints[i] and j→EndPoints[j].
- If ai<aj, there can by no means be any intersection.
- If ai>aj, there must be an intersection.
- If ai=aj, it’s attainable that there’s an intersection or not, relying on how we organize the traces on the underside terminal.
- Within the final case, if there are a number of traces that go to the identical section ai, we will make all pairs of them cross by arranging the factors wherein they hit this section from proper to left.
Since we wish to maximize the variety of intersections, we simply have to depend the variety of pairs (i,j) such that ai≥aj. You possibly can brute power all pairs in O(n2).
Beneath is the Implementation of the above method:
C++
#embody <bits/stdc++.h>
utilizing namespace std;
void maximumIntersection(int EndPoints[], int n)
{
// initializing the whole as 0
int complete = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// if the earlier worth is bigger then or
// equal to the present worth add it to the
// reply
if (EndPoints[i] >= EndPoints[j]) {
complete++;
}
}
}
// Test whether or not array incorporates all identical aspect
int first = EndPoints[0];
bool flag = false;
for (int i = 1; i < n; i++) {
// If sure then mark flag as false
if (EndPoints[i] != first) {
flag = false;
}
// In any other case mark flag as true
else {
flag = true;
}
}
if (flag == true) {
cout << "Most Intersection Potential: "
<< complete / 2 << 'n';
}
else {
cout << "Most Intersection Potential: " << complete
<< 'n';
}
}
// Driver Code
int primary()
{
int X = 7;
int EndPoints[] = { 4, 1, 4, 6, 7, 7, 5 };
int Y = 5;
int EndPoints2[] = { 2, 1, 4, 3, 5 };
int Z = 5;
int EndPoints3[] = { 1, 1, 1, 1, 1 };
maximumIntersection(EndPoints, X);
maximumIntersection(EndPoints2, Y);
maximumIntersection(EndPoints3, Z);
}
Most Intersection Potential: 6 Most Intersection Potential: 2 Most Intersection Potential: 5
Time Complexity: O(N2)
Auxiliary Area: O(1)
Environment friendly Method:
The Fundamental thought is to make use of onset template coverage based mostly information construction and depend frequency of duplicate components and carry out respective operations.
- The above method will also be optimized if are capable of depend the variety of pairs (i,j) such that ai≥aj in an environment friendly method.
- The ordered set retains the distinctive components in sorted order and offers a variety of gadgets strictly smaller than okay, which is required on this drawback.
order_of_key(okay) : Variety of gadgets strictly smaller than okay .
- Thus, we will loop over the enter array and add every aspect one after the other and might test order_of_key(aspect) at every iteration to get the variety of components lower than the aspect and add that to our sum.
- we should depend the working frequency of every aspect as a result of we all know that equal components additionally intersect one another, thus we will probably be including the working frequencies to our reply as properly.
Beneath is the Implementation of the above method:
C++
#embody <iostream>
utilizing namespace std;
// Necessary header recordsdata
#embody <ext/pb_ds/assoc_container.hpp> // Frequent file
#embody <ext/pb_ds/tree_policy.hpp>
#embody <purposeful> // for much less
#embody <iostream>
utilizing namespace std;
// defining required namespaces for coverage based mostly information
// construction
utilizing namespace __gnu_pbds;
template <class T>
utilizing oset
= tree<T, null_type, greater_equal<T>, rb_tree_tag,
tree_order_statistics_node_update>;
// declaration : oset<data_type> s;
void maximumIntersection(int EndPoints[], int N)
{
oset<int> st;
int ans = 0;
// counting the frequency as there will be duplicate
// components as properly
map<int, int> mp;
for (int i = 0; i < N; i++) {
st.insert(EndPoints[i]);
// including mp[EndPoints[i]] as a result of the weather that
// are equal to EndPoints[i] additionally intersects every
// different st.order_of_key(EndPoints[i]) will probably be
// returning the variety of components smaller than
// EndPoints[i]
ans += st.order_of_key(EndPoints[i])
+ mp[EndPoints[i]];
// including EndPoints[i] to our map for addressing
// equal components
mp[EndPoints[i]]++;
}
// if array incorporates all comparable components
if (mp.measurement() == 1) {
cout << "Most Intersection Potential: " << ans / 2
<< "n";
}
else {
cout << "Most Intersection Potential: " << ans
<< "n";
}
}
// Driver Code
int primary()
{
int X = 7;
int EndPoints[] = { 4, 1, 4, 6, 7, 7, 5 };
int Y = 5;
int EndPoints2[] = { 2, 1, 4, 3, 5 };
int Z = 5;
int EndPoints3[] = { 1, 1, 1, 1, 1 };
maximumIntersection(EndPoints, X);
maximumIntersection(EndPoints2, Y);
maximumIntersection(EndPoints3, Z);
}
Most Intersection Potential: 6 Most Intersection Potential: 2 Most Intersection Potential: 5
Time Complexity: O(N*logN)
Auxiliary Area: O(1)