Find smallest range containing at least 1 elements from given N ranges
Given N ranges of the form [L, R], the task is to find the range having the minimum number of integers such that at least one point of all the given N ranges exists in that range.
Example:
Input: ranges[] = {{1, 6}, {2, 7}, {3, 8}, {4, 9}}
Output: 6 6
Explanation: All the given ranges contain 6 as an integer between them. Therefore, [6, 6] is a valid range having 1 integer which is the minimum possible. The other valid ranges are [4, 4] and [5, 5].
Input: ranges[] = {{1, 4}, {4, 5}, {7, 9}, {9, 12}}
Output: 4 9
Approach: This problem can be solved using a Greedy Approach using the following observations:
- Suppose L is the minimum endpoint over all the given ranges. Then, it can be observed that all the given ranges must contain a point in the range [L, ∞].
- Similarly, suppose R is the maximum starting point over all the given ranges. Then, based on the similar observation as above, all the ranges must also contain a point in the range [-∞, R].
Therefore, using the above observations, the required range will be [L, R]. In cases where L > R, the answer can be any integer between L and R as all of them will make a valid range.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minRequiredRange(
vector<pair< int , int > > ranges,
int N)
{
int L = INT_MAX;
int R = INT_MIN;
for ( int i = 0; i < N; i++) {
L = min(L, ranges[i].second);
R = max(R, ranges[i].first);
}
if (R < L) {
cout << L << " " << L;
}
else {
cout << L << " " << R;
}
}
int main()
{
vector<pair< int , int > > ranges{
{ 1, 4 }, { 4, 5 }, { 7, 9 }, { 9, 12 }
};
minRequiredRange(ranges, ranges.size());
return 0;
}
|
Java
class GFG {
public static void minRequiredRange( int [][] ranges, int N) {
int L = Integer.MAX_VALUE;
int R = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; i++) {
L = Math.min(L, ranges[i][ 1 ]);
R = Math.max(R, ranges[i][ 0 ]);
}
if (R < L) {
System.out.println(L + " " + L);
} else {
System.out.println(L + " " + R);
}
}
public static void main(String args[]) {
int [][] ranges = { { 1 , 4 }, { 4 , 5 }, { 7 , 9 }, { 9 , 12 } };
minRequiredRange(ranges, ranges.length);
}
}
|
Python3
def minRequiredRange(ranges, N):
L = 10 * * 9
R = 10 * * - 9
for i in range (N):
L = min (L, ranges[i][ "second" ])
R = max (R, ranges[i][ "first" ])
if (R < L):
print (f "{L} {L}" )
else :
print (f "{L} {R}" )
ranges = [{ "first" : 1 , "second" : 4 }, { "first" : 4 , "second" : 5 }, {
"first" : 7 , "second" : 9 }, { "first" : 9 , "second" : 12 }
]
minRequiredRange(ranges, len (ranges))
|
C#
using System;
class GFG {
public static void minRequiredRange( int [, ] ranges,
int N)
{
int L = Int32.MaxValue;
int R = Int32.MinValue;
for ( int i = 0; i < N; i++) {
L = Math.Min(L, ranges[i, 1]);
R = Math.Max(R, ranges[i, 0]);
}
if (R < L) {
Console.WriteLine(L + " " + L);
}
else {
Console.WriteLine(L + " " + R);
}
}
public static void Main( string [] args)
{
int [, ] ranges
= { { 1, 4 }, { 4, 5 }, { 7, 9 }, { 9, 12 } };
minRequiredRange(ranges, ranges.GetLength(0));
}
}
|
Javascript
<script>
function minRequiredRange(
ranges,
N) {
let L = Number.MAX_VALUE;
let R = Number.MIN_VALUE;
for (let i = 0; i < N; i++) {
L = Math.min(L, ranges[i].second);
R = Math.max(R, ranges[i].first);
}
if (R < L) {
document.write(L + " " + L);
}
else {
document.write(L + " " + R);
}
}
let ranges = [
{ first: 1, second: 4 }, { first: 4, second: 5 }, { first: 7, second: 9 }, { first: 9, second: 12 }
];
minRequiredRange(ranges, ranges.length);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
24 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...