codeparrot/xlcost-text-to-code huggingface dataset:
Check if a decreasing Array can be sorted using Triple cyclic shift | C ++ program for the above approach ; If array is 3 2 1 can ' t ▁ ▁ be ▁ sorted ▁ because ▁ 2 ▁ is ▁ in ▁ ▁ its ▁ correct ▁ position , ▁ 1 ▁ ▁ and ▁ 3 ▁ can ' t shift right because cyclic right shift takes place between 3 elements ; Check if its possible to sort ; Number of swap is N / 2 always for this approach ; Printing index of the cyclic right shift ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortarray ( int arr [ ] , int N ) { if ( N == 3 ) cout << " NO " << endl ; else if ( N % 4 == 0 N % 4 == 1 ) { cout << " YES " << endl ; cout << ( N / 2 ) << endl ; int k = 1 ; for ( int l = 0 ; l < ( N / 4 ) ; l ++ ) { cout << k << " ▁ " << k + 1 << " ▁ " << N << endl ; cout << k + 1 << " ▁ " << N << " ▁ " << N - 1 << endl ; k = k + 2 ; N = N - 2 ; } } else cout << " NO " << endl ; } int main ( ) { int N = 5 ; int arr [ ] = { 5 , 4 , 3 , 2 , 1 } ; sortarray ( arr , N ) ; return 0 ; }
Find least non | C ++ program to find the least non - overlapping number from a given set intervals ; function to find the smallest non - overlapping number ; create a visited array ; find the first missing value ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1e5 + 5 ; void find_missing ( vector < pair < int , int > > interval ) { vector < int > vis ( MAX ) ; for ( int i = 0 ; i < interval . size ( ) ; ++ i ) { int start = interval [ i ] . first ; int end = interval [ i ] . second ; vis [ start ] ++ ; vis [ end + 1 ] -- ; } for ( int i = 1 ; i < MAX ; i ++ ) { vis [ i ] += vis [ i - 1 ] ; if ( ! vis [ i ] ) { cout << i << endl ; return ; } } } int main ( ) { vector < pair < int , int > > interval = { { 0 , 14 } , { 86 , 108 } , { 22 , 30 } , { 5 , 17 } } ; find_missing ( interval ) ; return 0 ; }
Find least non | C ++ program to find the least non - overlapping number from a given set intervals ; function to find the smallest non - overlapping number ; Sort the intervals based on their starting value ; check if any missing value exist ; finally print the missing value ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find_missing ( vector < pair < int , int > > interval ) { sort ( interval . begin ( ) , interval . end ( ) ) ; int mx = 0 ; for ( int i = 0 ; i < ( int ) interval . size ( ) ; ++ i ) { if ( interval [ i ] . first > mx ) { cout << mx ; return ; } else mx = max ( mx , interval [ i ] . second + 1 ) ; } cout << mx ; } int main ( ) { vector < pair < int , int > > interval = { { 0 , 14 } , { 86 , 108 } , { 22 , 30 } , { 5 , 17 } } ; find_missing ( interval ) ; return 0 ; }
Maximum bitwise OR value of subsequence of length K | C ++ program for the above approach ; Function to convert bit array to decimal number ; Return the final result ; Function to find the maximum Bitwise OR value of subsequence of length K ; Initialize bit array of size 32 with all value as 0 ; Iterate for each index of bit [ ] array from 31 to 0 , and check if the ith value of bit array is 0 ; Check for maximum contributing element ; Update the bit array if max_contributing element is found ; Decrement the value of K ; Return the result ; Driver Code ; Given array arr [ ] ; Length of subsequence ; Function Call
#include <iostream> NEW_LINE using namespace std ; int build_num ( int bit [ ] ) { int ans = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) if ( bit [ i ] ) ans += ( 1 << i ) ; return ans ; } int maximumOR ( int arr [ ] , int n , int k ) { int bit [ 32 ] = { 0 } ; for ( int i = 31 ; i >= 0 ; i -- ) { if ( bit [ i ] == 0 && k > 0 ) { int temp = build_num ( bit ) ; int temp1 = temp ; int val = -1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( temp1 < ( temp arr [ j ] ) ) { temp1 = temp | arr [ j ] ; val = arr [ j ] ; } } if ( val != -1 ) { k -- ; for ( int j = 0 ; j < 32 ; j ++ ) { if ( val & ( 1 << j ) ) bit [ j ] ++ ; } } } } return build_num ( bit ) ; } int main ( ) { int arr [ ] = { 5 , 9 , 7 , 19 } ; int k = 3 ; int n = sizeof arr / sizeof arr [ 0 ] ; cout << maximumOR ( arr , n , k ) ; return 0 ; }
Lexicographically smallest string after M operations | C ++ implementation to find the lexicographical smallest string after performing M operations ; Function to find the lexicographical smallest string after performing M operations ; Size of the given string ; Declare an array a ; For each i , a [ i ] contain number of operations to update s [ i ] to ' a ' ; Check if m >= ar [ i ] , then update s [ i ] to ' a ' decrement k by a [ i ] ; Form a cycle of 26 ; update last element of string with the value s [ i ] + ( k % 26 ) ; Return the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void smallest_string ( string s , int m ) { int n = s . size ( ) ; int a [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int distance = s [ i ] - ' a ' ; if ( distance == 0 ) a [ i ] = 0 ; else a [ i ] = 26 - distance ; } for ( int i = 0 ; i < n ; i ++ ) { if ( m >= a [ i ] ) { s [ i ] = ' a ' ; m = m - a [ i ] ; } } m = m % 26 ; s [ n - 1 ] = s [ n - 1 ] + m ; cout << s ; } int main ( ) { string str = " aazzx " ; int m = 6 ; smallest_string ( str , m ) ; return 0 ; }
Maximize jobs that can be completed under given constraint | C ++ Program to implement the above approach ; Function to find maxiumum number of jobs ; Min Heap ; Sort ranges by start day ; Stores the minimum and maximum day in the ranges ; Iterating from min_day to max_day ; Insert the end day of the jobs which can be completed on i - th day in a priority queue ; Pop all jobs whose end day is less than current day ; If queue is empty , no job can be completed on the i - th day ; Increment the count of jobs completed ; Pop the job with least end day ; Return the jobs on the last day ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_maximum_jobs ( int N , vector < pair < int , int > > ranges ) { priority_queue < int , vector < int > , greater < int > > queue ; sort ( ranges . begin ( ) , ranges . end ( ) ) ; int min_day = ranges [ 0 ] . first ; int max_day = 0 ; for ( int i = 0 ; i < N ; i ++ ) max_day = max ( max_day , ranges [ i ] . second ) ; int index = 0 , count_jobs = 0 ; for ( int i = min_day ; i <= max_day ; i ++ ) { while ( index < ranges . size ( ) && ranges [ index ] . first <= i ) { queue . push ( ranges [ index ] . second ) ; index ++ ; } while ( ! queue . empty ( ) && queue . top ( ) < i ) queue . pop ( ) ; if ( queue . empty ( ) ) continue ; count_jobs ++ ; queue . pop ( ) ; } return count_jobs ; } int main ( ) { int N = 5 ; vector < pair < int , int > > ranges ; ranges . push_back ( { 1 , 5 } ) ; ranges . push_back ( { 1 , 5 } ) ; ranges . push_back ( { 1 , 5 } ) ; ranges . push_back ( { 2 , 3 } ) ; ranges . push_back ( { 2 , 3 } ) ; cout << find_maximum_jobs ( N , ranges ) ; }
Minimize number of boxes by putting small box inside bigger one | C ++ implementation to minimize the number of the box by putting small box inside the bigger one ; Function to minimize the count ; Initial number of box ; Sort array of box size in increasing order ; check is current box size is smaller than next box size ; Decrement box count Increment current box count Increment next box count ; Check if both box have same size ; Print the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minBox ( int arr [ ] , int n ) { int box = n ; sort ( arr , arr + n ) ; int curr_box = 0 , next_box = 1 ; while ( curr_box < n && next_box < n ) { if ( arr [ curr_box ] < arr [ next_box ] ) { box -- ; curr_box ++ ; next_box ++ ; } else if ( arr [ curr_box ] == arr [ next_box ] ) next_box ++ ; } cout << box << endl ; } int main ( ) { int size [ ] = { 1 , 2 , 3 } ; int n = sizeof ( size ) / sizeof ( size [ 0 ] ) ; minBox ( size , n ) ; return 0 ; }
Sort a string lexicographically using triple cyclic shifts | C ++ Program for sorting a string using cyclic shift of three indices ; Store the indices which haven 't attained  its correct position ; Store the indices undergoing cyclic shifts ; If the element is not at it 's correct position ; Check if all 3 indices can be placed to respective correct indices in a single move ; If the i - th index is still not present in its correct position , store the index ; To check if swapping two indices places them in their correct position ; for ( int i = 0 ; i < indices . size ( ) ; i ++ ) { cout << indices [ i ] [ 0 ] << " ▁ " << indices [ i ] [ 1 ] << " ▁ " << indices [ i ] [ 2 ] << endl ; } ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortString ( vector < int > & arr , int n , int moves ) { vector < int > pos ; vector < vector < int > > indices ; bool flag = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != i ) { if ( arr [ arr [ arr [ i ] ] ] == i && arr [ arr [ i ] ] != i ) { int temp = arr [ arr [ i ] ] ; indices . push_back ( { i , arr [ i ] , arr [ arr [ i ] ] } ) ; swap ( arr [ i ] , arr [ arr [ i ] ] ) ; swap ( arr [ i ] , arr [ temp ] ) ; } } if ( arr [ i ] != i ) { pos . push_back ( i ) ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != i ) { int pos1 = i , pos2 = arr [ i ] ; int pos3 = arr [ arr [ i ] ] ; if ( pos3 != pos1 ) { indices . push_back ( { pos1 , pos2 , pos3 } ) ; swap ( arr [ pos1 ] , arr [ pos2 ] ) ; swap ( arr [ pos1 ] , arr [ pos3 ] ) ; pos . erase ( find ( pos . begin ( ) , pos . end ( ) , pos2 ) ) ; pos . erase ( find ( pos . begin ( ) , pos . end ( ) , pos3 ) ) ; if ( arr [ pos1 ] == pos1 ) { pos . erase ( find ( pos . begin ( ) , pos . end ( ) , pos1 ) ) ; } } else { if ( pos3 == * pos . begin ( ) ) { if ( * pos . begin ( ) != pos . back ( ) ) { auto it = ++ pos . begin ( ) ; pos3 = * ( it ) ; if ( * it != pos . back ( ) && pos3 == pos2 ) { pos3 = * ( ++ it ) ; } else if ( * it == pos . back ( ) && pos3 == pos2 ) { flag = true ; break ; } } else { flag = true ; break ; } } indices . push_back ( { pos1 , pos2 , pos3 } ) ; swap ( arr [ pos1 ] , arr [ pos2 ] ) ; swap ( arr [ pos1 ] , arr [ pos3 ] ) ; pos . erase ( find ( pos . begin ( ) , pos . end ( ) , pos2 ) ) ; } } if ( arr [ i ] != i ) { i -- ; } } if ( flag == true || indices . size ( ) > moves ) { cout << " Not ▁ Possible " << endl ; } else { cout << indices . size ( ) << endl ; } } int main ( ) { string s = " adceb " ; vector < int > arr ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { arr . push_back ( s [ i ] - ' a ' ) ; } sortString ( arr , s . size ( ) , floor ( s . size ( ) / 2 ) ) ; }
Count of minimum reductions required to get the required sum K | C ++ Program to find the count of minimum reductions required to get the required sum K ; Function to return the count of minimum reductions ; If the sum is already less than K ; Sort in non - increasing order of difference ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countReductions ( vector < pair < int , int > > & v , int K ) { int sum = 0 ; for ( auto i : v ) { sum += i . first ; } if ( sum <= K ) { return 0 ; } sort ( v . begin ( ) , v . end ( ) , [ & ] ( pair < int , int > a , pair < int , int > b ) { return ( a . first - a . second ) > ( b . first - b . second ) ; } ) ; int i = 0 ; while ( sum > K && i < v . size ( ) ) { sum -= ( v [ i ] . first - v [ i ] . second ) ; i ++ ; } if ( sum <= K ) return i ; return -1 ; } int main ( ) { int N = 4 , K = 25 ; vector < pair < int , int > > v ( N ) ; v [ 0 ] = { 10 , 5 } ; v [ 1 ] = { 20 , 9 } ; v [ 2 ] = { 12 , 10 } ; v [ 3 ] = { 4 , 2 } ; cout << countReductions ( v , K ) << endl ; return 0 ; }
Merge two unsorted linked lists to get a sorted list | C ++ program for the above approach ; Create structure for a node ; Function to print the linked list ; Store the head of the linked list into a temporary node * and iterate ; Function takes the head of the LinkedList and the data as argument and if no LinkedList exists , it creates one with the head pointing to first node . If it exists already , it appends given node at end of the last node ; Create a new node ; Insert data into the temporary node and point it 's next to NULL ; Check if head is null , create a linked list with temp as head and tail of the list ; Else insert the temporary node after the tail of the existing node and make the temporary node as the tail of the linked list ; Return the list ; Function to concatenate the two lists ; Iterate through the head1 to find the last node join the next of last node of head1 to the 1 st node of head2 ; return the concatenated lists as a single list - head1 ; Sort the linked list using bubble sort ; Compares two adjacent elements and swaps if the first element is greater than the other one . ; Driver Code ; Given Linked List 1 ; Given Linked List 2 ; Merge the two lists in a single list ; Sort the unsorted merged list ; Print the final sorted merged list
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int data ; node * next ; } ; void setData ( node * head ) { node * tmp ; tmp = head ; while ( tmp != NULL ) { cout << tmp -> data << " ▁ - > ▁ " ; tmp = tmp -> next ; } } node * getData ( node * head , int num ) { node * temp = new node ; node * tail = head ; temp -> data = num ; temp -> next = NULL ; if ( head == NULL ) { head = temp ; tail = temp ; } else { while ( tail != NULL ) { if ( tail -> next == NULL ) { tail -> next = temp ; tail = tail -> next ; } tail = tail -> next ; } } return head ; } node * mergelists ( node * * head1 , node * * head2 ) { node * tail = * head1 ; while ( tail != NULL ) { if ( tail -> next == NULL && head2 != NULL ) { tail -> next = * head2 ; break ; } tail = tail -> next ; } return * head1 ; } void sortlist ( node * * head1 ) { node * curr = * head1 ; node * temp = * head1 ; while ( curr -> next != NULL ) { temp = curr -> next ; while ( temp != NULL ) { if ( temp -> data < curr -> data ) { int t = temp -> data ; temp -> data = curr -> data ; curr -> data = t ; } temp = temp -> next ; } curr = curr -> next ; } } int main ( ) { node * head1 = new node ; node * head2 = new node ; head1 = NULL ; head2 = NULL ; head1 = getData ( head1 , 4 ) ; head1 = getData ( head1 , 7 ) ; head1 = getData ( head1 , 5 ) ; head2 = getData ( head2 , 2 ) ; head2 = getData ( head2 , 1 ) ; head2 = getData ( head2 , 8 ) ; head2 = getData ( head2 , 1 ) ; head1 = mergelists ( & head1 , & head2 ) ; sortlist ( & head1 ) ; setData ( head1 ) ; return 0 ; }
Find K elements whose absolute difference with median of array is maximum | C ++ implementation to find first K elements whose difference with the median of array is maximum ; Function for calculating median ; check for even case ; Function to find the K maximum absolute difference with the median of the array ; Sort the array . ; Store median ; Find and store difference ; If diff [ i ] is greater print it Else print diff [ j ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double findMedian ( int a [ ] , int n ) { if ( n % 2 != 0 ) return ( double ) a [ n / 2 ] ; return ( double ) ( a [ ( n - 1 ) / 2 ] + a [ n / 2 ] ) / 2.0 ; } void kStrongest ( int arr [ ] , int n , int k ) { sort ( arr , arr + n ) ; double median = findMedian ( arr , n ) ; int diff [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { diff [ i ] = abs ( median - arr [ i ] ) ; } int i = 0 , j = n - 1 ; while ( k > 0 ) { if ( diff [ i ] > diff [ j ] ) { cout << arr [ i ] << " ▁ " ; i ++ ; } else { cout << arr [ j ] << " ▁ " ; j -- ; } k -- ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int k = 3 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; kStrongest ( arr , n , k ) ; return 0 ; }
Sort an array by swapping elements of different type specified by another array | C ++ program to check if it is possible to sort the array in non - decreasing order by swapping elements of different types ; Function to check if it is possible to sort the array in non - decreasing order by swapping elements of different types ; Consider the array to be already sorted ; checking if array is already sorted ; Check for a pair which is in decreasing order ; Count the frequency of each type of element ; type0 stores count of elements of type 0 ; type1 stores count of elements of type 1 ; Return true if array is already sorted ; Return false if all elements are of same type and array is unsorted ; Possible for all other cases ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool sorting_possible ( int a [ ] , int b [ ] , int n ) { bool sorted = true ; int type1 = 0 , type0 = 0 , i ; for ( i = 1 ; i < n ; i ++ ) { if ( a [ i ] < a [ i - 1 ] ) { sorted = false ; break ; } } for ( i = 0 ; i < n ; i ++ ) { if ( b [ i ] == 0 ) type0 ++ ; else type1 ++ ; } if ( sorted ) return true ; else if ( type1 == n type0 == n ) return false ; else return true ; } int main ( ) { int a [ ] = { 15 , 1 , 2 , 17 , 6 } ; int b [ ] = { 1 , 1 , 0 , 1 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; bool res = sorting_possible ( a , b , n ) ; if ( res ) cout << " Yes " ; else cout << " No " ; }
Sort permutation of N natural numbers using triple cyclic right swaps | C ++ implementation to find the number of operations required to sort the elements of the array ; Function to sort the permutation with the given operations ; Visited array to check the array element is at correct position or not ; Loop to iterate over the elements of the given array ; Condition to check if the elements is at its correct position ; Condition to check if the element is included in any previous cyclic rotations ; Loop to find the cyclic rotations in required ; Condition to check if the cyclic rotation is a valid rotation ; Loop to find the index of the cyclic rotation for the current index ; Condition to if the cyclic rotation is a valid rotation ; Loop to find all the valid operations required to sort the permutation ; Total operation required ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE void sortPermutation ( ll arr [ ] , ll n ) { vector < pair < ll , pair < ll , ll > > > ans ; vector < ll > p ; bool visited [ 200005 ] = { 0 } ; for ( ll i = 1 ; i <= n ; i ++ ) { if ( arr [ i ] == i ) { visited [ i ] = 1 ; continue ; } else { if ( ! visited [ i ] ) { ll x = i ; vector < ll > v ; while ( ! visited [ x ] ) { visited [ x ] = 1 ; v . push_back ( x ) ; x = arr [ x ] ; } if ( ( v . size ( ) - 3 ) % 2 == 0 ) { for ( ll i = 1 ; i < v . size ( ) ; i += 2 ) { ans . push_back ( make_pair ( v [ 0 ] , make_pair ( v [ i ] , v [ i + 1 ] ) ) ) ; } continue ; } p . push_back ( v [ 0 ] ) ; p . push_back ( v [ v . size ( ) - 1 ] ) ; for ( ll i = 1 ; i < v . size ( ) - 1 ; i += 2 ) { ans . push_back ( make_pair ( v [ 0 ] , make_pair ( v [ i ] , v [ i + 1 ] ) ) ) ; } } } } if ( p . size ( ) % 4 ) { cout << -1 << " STRNEWLINE " ; return ; } for ( ll i = 0 ; i < p . size ( ) ; i += 4 ) { ans . push_back ( make_pair ( p [ i ] , make_pair ( p [ i + 1 ] , p [ i + 2 ] ) ) ) ; ans . push_back ( make_pair ( p [ i + 2 ] , make_pair ( p [ i ] , p [ i + 3 ] ) ) ) ; } cout << ans . size ( ) << " STRNEWLINE " ; for ( ll i = 0 ; i < ans . size ( ) ; i ++ ) { cout << ans [ i ] . first << " ▁ " << ans [ i ] . second . first << " ▁ " << ans [ i ] . second . second << " STRNEWLINE " ; } } int main ( ) { ll arr [ ] = { 0 , 3 , 2 , 4 , 1 } ; ll n = 4 ; sortPermutation ( arr , n ) ; return 0 ; }
Maximize array sum by X increments when each element is divided by 10 | C ++ program for the above problem ; initialize variables ; add the current contribution of the element to the answer ; if the value is already maximum then we can 't change it ; moves required to move to the next multiple of 10 ; no of times we can add 10 to this value so that its value does not exceed 1000. ; sort the array ; adding the values to increase the numbers to the next multiple of 10 ; if the total moves are less than X then increase the answer ; if the moves exceed X then we cannot increase numbers ; if there still remain some moves ; remaining moves ; add minimim of increments and remaining / 10 to the answer ; output the final answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximizeval10 ( int a [ ] , int n , int k ) { int increments = 0 ; int ans = 0 ; vector < int > v ; for ( int i = 0 ; i < n ; i ++ ) { ans += ( a [ i ] / 10 ) ; if ( a [ i ] == 1000 ) continue ; else { v . push_back ( 10 - a [ i ] % 10 ) ; increments += ( 100 - ( ( a [ i ] ) / 10 ) - 1 ) ; } } sort ( v . begin ( ) , v . end ( ) ) ; int sum = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { sum += v [ i ] ; if ( sum <= k ) { ans ++ ; } else break ; } if ( sum < k ) { int remaining = k - sum ; ans += min ( increments , remaining / 10 ) ; } cout << ans ; } int main ( ) { int N = 4 ; int X = 6 ; int A [ N ] = { 4 , 8 , 8 , 8 } ; maximizeval10 ( A , N , X ) ; return 0 ; }
Split array into two subarrays such that difference of their maximum is minimum | C ++ Program to split a given array such that the difference between their maximums is minimized . ; Sort the array ; Return the difference between two highest elements ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinDif ( int arr [ ] , int N ) { sort ( arr , arr + N ) ; return ( arr [ N - 1 ] - arr [ N - 2 ] ) ; } int main ( ) { int arr [ ] = { 7 , 9 , 5 , 10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMinDif ( arr , N ) ; return 0 ; }
Count of available non | C ++ implementation of the above approach ; 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
#include <iostream> NEW_LINE using namespace std ; int partition ( int arr [ ] , int l , int h ) { int pivot = arr [ l ] ; int i = l + 1 ; int j = h ; while ( i <= j ) { while ( i <= h && arr [ i ] < pivot ) { i ++ ; } while ( j > l && arr [ j ] > pivot ) { j -- ; } if ( i < j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; i ++ ; j -- ; } else i ++ ; } arr [ l ] = arr [ j ] ; arr [ j ] = pivot ; return j ; } void sortArray ( int arr [ ] , int l , int h ) { if ( l >= h ) return ; int pivot = partition ( arr , l , h ) ; sortArray ( arr , l , pivot - 1 ) ; sortArray ( arr , pivot + 1 , h ) ; } int findMaxIntervals ( int start [ ] , int end [ ] , int n , int R ) { int ans = 0 ; int prev = 0 ; int currActive = 0 ; int i = 0 ; int j = 0 ; if ( start [ 0 ] > 0 ) ans ++ ; while ( i < n && j < n ) { if ( start [ i ] < end [ j ] ) { i ++ ; currActive ++ ; } else if ( start [ i ] > end [ j ] ) { j ++ ; currActive -- ; } else { i ++ ; j ++ ; } if ( currActive == 0 ) { ans ++ ; } } if ( end [ n - 1 ] < R ) ans ++ ; return ans ; } int main ( ) { int R , N ; R = 10 ; N = 3 ; int start [ N ] = { 2 , 5 , 8 } ; int end [ N ] = { 3 , 9 , 10 } ; sortArray ( start , 0 , N - 1 ) ; sortArray ( end , 0 , N - 1 ) ; cout << findMaxIntervals ( start , end , N , R ) ; }