# Maximum ranges that can be uniquely represented by any integer from the range

• Difficulty Level : Basic
• Last Updated : 24 May, 2021

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:

1. Range {1, 2} can be represented by 1.
2. Range {2, 3} can be represented by 2.
3. 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], prev[i])max(current[i], prev[i] – 1) is positive, then update the value of prev as {1 + max(current[i], prev[i] – 1), max(current[i], prev[i])} 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 ``using` `namespace` `std;` `// Function to find the maximum number``// of ranges where each range can be``// uniquely represented by an integer``int` `maxRanges(vector > 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;` `    ``// 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 == current``            ``&& last == current``            ``&& current == current)``            ``continue``;` `        ``// Find the range of integer``        ``// available to be assigned``        ``int` `start = max(prev, current - 1);``        ``int` `end = max(prev, current);` `        ``// Check if an integer is``        ``// available to be assigned``        ``if` `(end - start > 0) {` `            ``// Update the previously``            ``// assigned range``            ``prev = 1 + start;``            ``prev = end;` `            ``// Update the count of range``            ``count++;``        ``}``    ``}` `    ``// Return the maximum count of``    ``// ranges``    ``return` `count;``}` `// Driver Code``int` `main()``{``    ``vector > 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

 ``

Output:

`4`

Time Complexity: O(N * log N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up