Given n pairs (S[i], F[i]) where for every i, S[i]< F[i]. Two ranges are said to intersect if and only if either of them does not fully lie inside the other one that is only one point of a pair lies between the start and end of the other pair. We have to print all the intersecting ranges for each pair.

**Note:** All the endpoints that is F[i] are integers and are also unique. None of the pairs start and end at the same time. Also, no pair’s endpoint is the same as the start of the other.

**Examples:**

Input :

n = 6, v = {{9, 12}, {2, 11}, {1, 3}, {6, 10}, {5, 7}, {4, 8}}Output :

{9, 12} is intersecting with: {6, 10} {2, 11}

{2, 11} is intersecting with: {1, 3} {9, 12}

{1, 3} is intersecting with: {2, 11}

{6, 10} is intersecting with: {5, 7} {4, 8} {9, 12}

{5, 7} is intersecting with: {6, 10}

{4, 8} is intersecting with: {6, 10}Explanation:

The first pair(9, 12) is intersecting with second(2, 11) and fourth(6, 10) pair.

The second pair(2, 11) is intersecting with third(1, 3) and first(9, 12) pairs.

The third pair(1, 3) is intersecting with the second(2, 11) pair.

The forth pair(6, 10) is intersecting with fifth(5, 7), sixth(4, 8) and first(9, 12) pair.

The fifth pair(5, 7) is intersecting with the fourth(6, 10) pair.

The sixth pair(4, 8) is intersecting with the fourth(6, 10) pair.

Input :n = 5, v = {1, 3}, {2, 4}, {5, 9}, {6, 8}, {7, 10}}Output :

{1, 3} is intersecting with: {2, 4}

{2, 4} is intersecting with: {1, 3}

{5, 9} is intersecting with: {7, 10}

{6, 8} is intersecting with: {7, 10}

{7, 10} is intersecting with: {6, 8} {5, 9}Explanation:

The first pair(1, 3) is intersecting with the second(2, 4) pair.

The second pair(2, 4) is intersecting with the first(1, 3) pair.

The third pair(5, 9) is intersecting with the fifth(7, 10) pair.

The fourth pair(6, 8) is intersecting with the fifth(7, 10) pair.

The fifth pair(7, 10) is intersecting with the third(5, 9) and fourth(6, 8) pair.

**Approach:**

The above-mentioned problem can be solved by using sorting. Firstly, we have to insert every first element of the pair and the second element of the pair in a single vector along with the position of each. Then sort all the elements with respect to the first element of the pair. After that use a **set data structure** for second elements in the pair. Then we have to iterate in the vector in which we have stored the first and second element with their respective positions and if the first element is found then compute all ranges that are intersecting with the current pair from the set and if the second element of the pair is encountered then simply erase that second pair from the set. Otherwise add the second element of the current pair.

Below is the implementation of above approach:

`// CPP Program to Find all the` `// intersecting pairs from a given array` ` ` `#include <bits/stdc++.h>` `using` `namespace` `std;` ` ` `// Function that print intersecting pairs` `// for each pair in the vector` `void` `findIntersection(vector<pair<` `int` `, ` `int` `> > v, ` `int` `n)` `{` ` ` `vector<pair<` `int` `, ` `int` `> > data;` ` ` ` ` `vector<vector<` `int` `> > answer(n);` ` ` ` ` `// Store each pair with their positions` ` ` `for` `(` `int` `i = 0; i < n; i++) {` ` ` `data.push_back(make_pair(v[i].first, i));` ` ` ` ` `data.push_back(make_pair(v[i].second, i));` ` ` `}` ` ` ` ` `// Sort the vector with respect to` ` ` `// first element in the pair` ` ` `sort(data.begin(), data.end());` ` ` ` ` `int` `curr = 0;` ` ` ` ` `// set data structure for keeping` ` ` `// the second element of each pair` ` ` `set<pair<` `int` `, ` `int` `> > s;` ` ` ` ` `// Iterating data vector` ` ` `for` `(` `auto` `it : data) {` ` ` ` ` `// check if all pairs are taken` ` ` `if` `(curr >= n)` ` ` `break` `;` ` ` ` ` `// check if current value is a second element` ` ` `// then remove it from the set` ` ` `if` `(s.count(it))` ` ` ` ` `s.erase(it);` ` ` ` ` `else` `{` ` ` ` ` `// index of the current pair` ` ` `int` `i = it.second;` ` ` ` ` `// Computing the second element of current pair` ` ` `int` `j = v[i].second;` ` ` ` ` `// Iterating in the set` ` ` `for` `(` `auto` `k : s) {` ` ` ` ` `// Check if the set element` ` ` `// has higher value than the current` ` ` `// element's second element` ` ` `if` `(k.first > j)` ` ` `break` `;` ` ` ` ` `int` `index = k.second;` ` ` ` ` `answer[i].push_back(index);` ` ` ` ` `answer[index].push_back(i);` ` ` ` ` `curr++;` ` ` ` ` `// Check if curr is equal to` ` ` `// all available pairs or not` ` ` `if` `(curr >= n)` ` ` `break` `;` ` ` `}` ` ` ` ` `// Insert second element` ` ` `// of current pair in the set` ` ` `s.insert(make_pair(j, i));` ` ` `}` ` ` `}` ` ` ` ` `// Printing the result` ` ` `for` `(` `int` `i = 0; i < n; i++) {` ` ` ` ` `cout << ` `"{"` `<< v[i].first << ` `", "` `<< v[i].second << ` `"}"` ` ` `<< ` `" is intersecting with: "` `;` ` ` ` ` `for` `(` `int` `j = 0; j < answer[i].size(); j++)` ` ` ` ` `cout << ` `"{"` `<< v[answer[i][j]].first << ` `", "` ` ` `<< v[answer[i][j]].second << ` `"}"` ` ` `<< ` `" "` `;` ` ` ` ` `cout << ` `"\n"` `;` ` ` `}` `}` ` ` `// Driver Code` `int` `main()` `{` ` ` ` ` `// initialise the size of vector` ` ` `int` `n = 6;` ` ` ` ` `// initialise the vector` ` ` `vector<pair<` `int` `, ` `int` `> > v = { { 9, 12 },` ` ` `{ 2, 11 },` ` ` `{ 1, 3 },` ` ` `{ 6, 10 },` ` ` `{ 5, 7 },` ` ` `{ 4, 8 } };` ` ` ` ` `findIntersection(v, n);` ` ` ` ` `return` `0;` `}` |

**Output:**

{9, 12} is intersecting with: {6, 10} {2, 11} {2, 11} is intersecting with: {1, 3} {9, 12} {1, 3} is intersecting with: {2, 11} {6, 10} is intersecting with: {5, 7} {4, 8} {9, 12} {5, 7} is intersecting with: {6, 10} {4, 8} is intersecting with: {6, 10}

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. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend live classes with industry experts, please refer **Geeks Classes Live**