Maximum ranges that can be uniquely represented by any integer from the range
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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the maximum number // of ranges where each range can be // uniquely represented by an integer int maxRanges(vector<vector< int > > arr, int N) { // Sort the ranges in ascending order sort(arr.begin(), arr.end()); // Stores the count of ranges int count = 1; // Stores previously assigned range vector< int > prev = arr[0]; // Traverse the vector arr[] for ( int i = 1; i < N; i++) { vector< int > last = arr[i - 1]; vector< int > current = arr[i]; // Skip the similar ranges // of size 1 if (last[0] == current[0] && last[1] == current[1] && current[1] == current[0]) continue ; // Find the range of integer // available to be assigned int start = max(prev[0], current[0] - 1); int end = max(prev[1], current[1]); // Check if an integer is // available to be assigned if (end - start > 0) { // Update the previously // assigned range prev[0] = 1 + start; prev[1] = end; // Update the count of range count++; } } // Return the maximum count of // ranges return count; } // Driver Code 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
// java program for the above approach import java.io.*; import java.lang.*; import java.util.*; public class GFG { // Function to find the maximum number // of ranges where each range can be // uniquely represented by an integer static int maxRanges(Integer arr[][], int N) { // Sort the ranges in ascending order 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 ]); }); // Stores the count of ranges int count = 1 ; // Stores previously assigned range Integer prev[] = arr[ 0 ]; // Traverse the vector arr[] for ( int i = 1 ; i < N; i++) { Integer last[] = arr[i - 1 ]; Integer current[] = arr[i]; // Skip the similar ranges // of size 1 if (last[ 0 ] == current[ 0 ] && last[ 1 ] == current[ 1 ] && current[ 1 ] == current[ 0 ]) continue ; // Find the range of integer // available to be assigned int start = Math.max(prev[ 0 ], current[ 0 ] - 1 ); int end = Math.max(prev[ 1 ], current[ 1 ]); // Check if an integer is // available to be assigned if (end - start > 0 ) { // Update the previously // assigned range prev[ 0 ] = 1 + start; prev[ 1 ] = end; // Update the count of range count++; } } // Return the maximum count of // ranges return count; } // Driver Code 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)); } } // This code is contributed by Kingash. |
Python3
# Python 3 program for the above approach # Function to find the maximum number # of ranges where each range can be # uniquely represented by an integer def maxRanges(arr, N): # Sort the ranges in ascending order arr.sort() # Stores the count of ranges count = 1 # Stores previously assigned range prev = arr[ 0 ] # Traverse the vector arr[] for i in range ( 1 , N): last = arr[i - 1 ] current = arr[i] # Skip the similar ranges # of size 1 if (last[ 0 ] = = current[ 0 ] and last[ 1 ] = = current[ 1 ] and current[ 1 ] = = current[ 0 ]): continue # Find the range of integer # available to be assigned start = max (prev[ 0 ], current[ 0 ] - 1 ) end = max (prev[ 1 ], current[ 1 ]) # Check if an integer is # available to be assigned if (end - start > 0 ): # Update the previously # assigned range prev[ 0 ] = 1 + start prev[ 1 ] = end # Update the count of range count + = 1 # Return the maximum count of # ranges return count # Driver Code if __name__ = = "__main__" : arr = [ [ 1 , 4 ], [ 4 , 4 ], [ 2 , 2 ], [ 3 , 4 ], [ 1 , 1 ]] N = len (arr) print (maxRanges(arr, N)) # This code is contributed by ukasp. |
Javascript
<script> // Javascript program for the above approach // Function to find the maximum number // of ranges where each range can be // uniquely represented by an integer function maxRanges(arr, N) { // Sort the ranges in ascending order arr.sort((a, b) => { return b[0] - a[0] }) // Stores the count of ranges let count = 1; // Stores previously assigned range let prev = arr[0]; // Traverse the vector arr[] for (let i = 1; i < N; i++) { let last = arr[i - 1]; let current = arr[i]; // Skip the similar ranges // of size 1 if (last[0] == current[0] && last[1] == current[1] && current[1] == current[0]) { continue ; } // Find the range of integer // available to be assigned let start = Math.max(prev[0], current[0] - 1); let end = Math.max(prev[1], current[1]); // Check if an integer is // available to be assigned if ((end - start) > 0) { // Update the previously // assigned range prev[0] = 1 + start; prev[1] = end; // Update the count of range count++; } } // Return the maximum count of // ranges return count; } // Driver Code let range = [ [1, 4], [4, 4], [2, 2], [3, 4], [1, 1]]; let N = range.length; document.write(maxRanges(range, N)); // This code is contributed by _Saurabh_jaiswal </script> |
4
Time Complexity: O(N * log N)
Auxiliary Space: O(1)