Find a range that covers all the elements of given N ranges in C++

C++Server Side ProgrammingProgramming

Suppose we have a n ranges containing L and R. We have to check or find the index of 0 – based of the range which covers all the other given n – 1 ranges. If there is no such range, display -1. For example, if L = [2, 4, 3, 1], and R = [4, 6, 7, 9], then the output is 3. So it means the range at 3rd index (1 to 9) covers all the elements of other n – 1 ranges.

Since all L and R points are distinct, find the range of smallest L and largest R point, if both are the same range, then it indicates all other ranges lie within it. Otherwise it is not possible.

Example

 Live Demo

#include<iostream>
using namespace std;
int fact (int n) {
   if (n == 0)
      return 1;
   return n * fact(n-1);
}
void showRange(int n) {
   int a = fact(n + 2) + 2;
   int b = a + n - 1;
   cout << "[" << a << ", " << b << "]";
}
int main() {
   int n = 3 ;
   showRange(n);
}

Output

[122, 124]
raja
Published on 18-Dec-2019 11:20:45
Advertisements