Count of available non | Function to find the pivot index ; Function to implement Quick Sort ; Pivot is pointing to pivot index before which every element is smaller and after pivot , every element is greater ; Sort the array before pivot element ; Sort the array after pivot element ; Function to count the available intervals from the given range of numbers ; If range starts after 0 then an interval is available from 0 to start [ 0 ] ; When a new interval starts ; Since index variable i is being incremented , the current active interval will also get incremented ; When the current interval ends ; Since index variable j is being decremented , the currect active interval will also get decremented ; When start and end both are same there is no change in currActive ; If the end of interval is before the range so interval is available at the end ; Driver code ; Sort the start array ; Sort the end array ; Calling the function
def partition ( arr , l , h ) : NEW_LINE INDENT pivot = arr [ l ] NEW_LINE i = l + 1 NEW_LINE j = h NEW_LINE while ( i <= j ) : NEW_LINE INDENT while ( i <= h and arr [ i ] < pivot ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT while ( j > l and arr [ j ] > pivot ) : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT if ( i < j ) : NEW_LINE INDENT temp = arr [ i ] NEW_LINE arr [ i ] = arr [ j ] NEW_LINE arr [ j ] = temp NEW_LINE i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE DEDENT DEDENT arr [ l ] = arr [ j ] NEW_LINE arr [ j ] = pivot NEW_LINE return j NEW_LINE DEDENT def sortArray ( arr , l , h ) : NEW_LINE INDENT if ( l >= h ) : NEW_LINE INDENT return NEW_LINE DEDENT pivot = partition ( arr , l , h ) NEW_LINE sortArray ( arr , l , pivot - 1 ) NEW_LINE sortArray ( arr , pivot + 1 , h ) NEW_LINE DEDENT def findMaxIntervals ( start , end , n , R ) : NEW_LINE INDENT ans = 0 NEW_LINE prev = 0 NEW_LINE currActive = 0 NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE if ( start [ 0 ] > 0 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT while ( i < n and j < n ) : NEW_LINE INDENT if ( start [ i ] < end [ j ] ) : NEW_LINE INDENT i += 1 NEW_LINE currActive += 1 NEW_LINE DEDENT elif ( start [ i ] > end [ j ] ) : NEW_LINE INDENT j += 1 NEW_LINE currActive -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE j += 1 NEW_LINE DEDENT if ( currActive == 0 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT if ( end [ n - 1 ] < R ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT R = 10 NEW_LINE N = 3 NEW_LINE start = [ 2 , 5 , 8 ] NEW_LINE end = [ 3 , 9 , 10 ] NEW_LINE sortArray ( start , 0 , N - 1 ) NEW_LINE sortArray ( end , 0 , N - 1 ) NEW_LINE print ( findMaxIntervals ( start , end , N , R ) ) NEW_LINE DEDENT