Maximum ranges that can be uniquely represented by any integer from the range | C ++ program for the above approach ; Function to find the maximum number of ranges where each range can be uniquely represented by an integer ; Sort the ranges in ascending order ; Stores the count of ranges ; Stores previously assigned range ; Traverse the vector arr [ ] ; Skip the similar ranges of size 1 ; Find the range of integer available to be assigned ; Check if an integer is available to be assigned ; Update the previously assigned range ; Update the count of range ; Return the maximum count of ranges ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }