
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find a range that covers all the elements of given N ranges in C++
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
#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]
- Related Articles
- XOR of all the elements in the given range [L, R] in C++
- Find k-th smallest element in given n ranges in C++
- Python – Extract range of Consecutive similar elements ranges from string list
- Check if an array contains all elements of a given range in Python
- Find the GCD that lies in given range
- C++ program to find the best fit rectangle that covers a given point
- Find the kth element in the series generated by the given N ranges in C++
- Elements to be added so that all elements of a range are present in array in C++
- Python – Test for all Even elements in the List for the given Range
- Write a Golang program to find duplicate elements in a given range
- JavaScript array: Find all elements that appear more than n times
- Find the number of divisors of all numbers in the range [1, n] in C++
- Count numbers in a range that are divisible by all array elements in C++
- Algorithm to sum ranges that lie within another separate range in JavaScript
- How to find the sum of all elements of a given array in JavaScript?

Advertisements