Given an array arr[] consisting of N ranges of the form {L, R}, the task is to find the maximize the number of ranges such that each range can be uniquely represented by any integer from that range.
Examples:
Input: arr[] = {{1, 2}, {2, 3}, {3, 4}}
Output: 3
Explanation:
Number of ranges can be maximized by following representations:
- Range {1, 2} can be represented by 1.
- Range {2, 3} can be represented by 2.
- Range {3, 4} can be represented by 3.
Therefore, the maximized count of ranges is 3.
Input: arr[] = {{1, 4}, {4, 4}, {2, 2}, {3, 4}, {1, 1}}
Output: 4
Naive Approach: The simplest approach to solve the given problem is to sort the ranges and iterate from the minimum value of L to the maximum value of R to greedily assign an integer for a particular range and keep a count of the possible assigned integers. After completing the above steps, print the count obtained as the result.
Time Complexity: O(M * N), where M is the maximum element among the given ranges.
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by using the Greedy Approach, the idea is to assign the minimum unique value from each range by selecting the given ranges in increasing order. Follow the steps to solve the problem:
- Sort the given array of ranges in ascending order.
- Initialize a vector, say prev as arr[] that stores the previously assigned values to the given ranges.
- Initialize a variable, say count as 1 to store the maximum number of ranges where each range can be uniquely represented by an integer from the range.
- Traverse the given array arr[] over the range [1, N – 1] and perform the following steps:
- Initialize a vector, say current[] as arr[i] that stores the values assigned to the current range.
- If the value of max(current[i][1], prev[i][1]) – max(current[i][0], prev[i][0] – 1) is positive, then update the value of prev as {1 + max(current[i][0], prev[i][0] – 1), max(current[i][1], prev[i][1])} and increment the value of count by 1.
- Otherwise, check of the next range.
- After completing the above steps, print the value of count as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxRanges(vector<vector< int > > arr,
int N)
{
sort(arr.begin(), arr.end());
int count = 1;
vector< int > prev = arr[0];
for ( int i = 1; i < N; i++) {
vector< int > last = arr[i - 1];
vector< int > current = arr[i];
if (last[0] == current[0]
&& last[1] == current[1]
&& current[1] == current[0])
continue ;
int start = max(prev[0], current[0] - 1);
int end = max(prev[1], current[1]);
if (end - start > 0) {
prev[0] = 1 + start;
prev[1] = end;
count++;
}
}
return count;
}
int main()
{
vector<vector< int > > range = {
{ 1, 4 }, { 4, 4 },
{ 2, 2 }, { 3, 4 },
{ 1, 1 }
};
int N = range.size();
cout << maxRanges(range, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
static int maxRanges(Integer arr[][], int N)
{
Arrays.sort(arr, (a, b) -> {
if (a[ 0 ].equals(b[ 0 ]))
return Integer.compare(a[ 1 ], b[ 1 ]);
return Integer.compare(a[ 0 ], b[ 0 ]);
});
int count = 1 ;
Integer prev[] = arr[ 0 ];
for ( int i = 1 ; i < N; i++) {
Integer last[] = arr[i - 1 ];
Integer current[] = arr[i];
if (last[ 0 ] == current[ 0 ]
&& last[ 1 ] == current[ 1 ]
&& current[ 1 ] == current[ 0 ])
continue ;
int start = Math.max(prev[ 0 ], current[ 0 ] - 1 );
int end = Math.max(prev[ 1 ], current[ 1 ]);
if (end - start > 0 ) {
prev[ 0 ] = 1 + start;
prev[ 1 ] = end;
count++;
}
}
return count;
}
public static void main(String[] args)
{
Integer range[][] = {
{ 1 , 4 }, { 4 , 4 }, { 2 , 2 }, { 3 , 4 }, { 1 , 1 }
};
int N = range.length;
System.out.print(maxRanges(range, N));
}
}
|
Python3
def maxRanges(arr, N):
arr.sort()
count = 1
prev = arr[ 0 ]
for i in range ( 1 , N):
last = arr[i - 1 ]
current = arr[i]
if (last[ 0 ] = = current[ 0 ]
and last[ 1 ] = = current[ 1 ]
and current[ 1 ] = = current[ 0 ]):
continue
start = max (prev[ 0 ], current[ 0 ] - 1 )
end = max (prev[ 1 ], current[ 1 ])
if (end - start > 0 ):
prev[ 0 ] = 1 + start
prev[ 1 ] = end
count + = 1
return count
if __name__ = = "__main__" :
arr = [
[ 1 , 4 ], [ 4 , 4 ],
[ 2 , 2 ], [ 3 , 4 ],
[ 1 , 1 ]]
N = len (arr)
print (maxRanges(arr, N))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
namespace MaxRanges
{
class Program
{
static void Main( string [] args)
{
List< int []> range = new List< int []> { new int [] { 1, 4 }, new int [] { 4, 4 }, new int [] { 2, 2 }, new int [] { 3, 4 }, new int [] { 1, 1 } };
int N = range.Count;
Console.WriteLine(MaxRanges(range, N));
}
static int MaxRanges(List< int []> arr, int N)
{
arr = arr.OrderBy(x => x[0]).ToList();
int count = 1;
int [] prev = arr[0];
for ( int i = 1; i < N; i++)
{
int [] last = arr[i - 1];
int [] current = arr[i];
if (last[0] == current[0]
&& last[1] == current[1]
&& current[1] == current[0])
continue ;
int start = Math.Max(prev[0], current[0] - 1);
int end = Math.Max(prev[1], current[1]);
if (end - start > 0)
{
prev[0] = 1 + start;
prev[1] = end;
count++;
}
}
return count;
}
}
}
|
Javascript
<script>
function maxRanges(arr, N) {
arr.sort((a, b) => {
return b[0] - a[0]
})
let count = 1;
let prev = arr[0];
for (let i = 1; i < N; i++) {
let last = arr[i - 1];
let current = arr[i];
if (last[0] == current[0]
&& last[1] == current[1]
&& current[1] == current[0]) {
continue ;
}
let start = Math.max(prev[0], current[0] - 1);
let end = Math.max(prev[1], current[1]);
if ((end - start) > 0) {
prev[0] = 1 + start;
prev[1] = end;
count++;
}
}
return count;
}
let range = [
[1, 4], [4, 4],
[2, 2], [3, 4],
[1, 1]];
let N = range.length;
document.write(maxRanges(range, N));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(1)