codeparrot/xlcost-text-to-code huggingface dataset:
Longest subarray forming a Geometic Progression ( GP ) | C ++ Program to implement the above approach ; Function to return the length of the longest subarray forming a GP in a sorted array ; Base Case ; Stores the length of GP and the common ratio ; Stores the maximum length of the GP ; Traverse the array ; Check if the common ratio is valid for GP ; If the current common ratio is equal to previous common ratio ; Increment the length of the GP ; Store the max length of GP ; Otherwise ; Update the common ratio ; Update the length of GP ; Store the max length of GP ; Update the length of GP ; Store the max length of GP ; Return the max length of GP ; Driver Code ; Given array ; Length of the array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestGP ( int A [ ] , int N ) { if ( N < 2 ) return N ; int length = 1 , common_ratio = 1 ; int maxlength = 1 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i + 1 ] % A [ i ] == 0 ) { if ( A [ i + 1 ] / A [ i ] == common_ratio ) { length = length + 1 ; maxlength = max ( maxlength , length ) ; } else { common_ratio = A [ i + 1 ] / A [ i ] ; length = 2 ; } } else { maxlength = max ( maxlength , length ) ; length = 1 ; } } maxlength = max ( maxlength , length ) ; return maxlength ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 , 7 , 14 , 28 , 56 , 89 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << longestGP ( arr , N ) ; return 0 ; }
Maximum number of uncrossed lines between two given arrays | C ++ program for the above approach ; Function to count maximum number of uncrossed lines between the two given arrays ; Stores the length of lcs obtained upto every index ; Iterate over first array ; Iterate over second array ; Update value in dp table ; If both characters are equal ; Update the length of lcs ; If both characters are not equal ; Update the table ; Return the answer ; Driver Code ; Given array A [ ] and B [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int uncrossedLines ( int * a , int * b , int n , int m ) { int dp [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { if ( i == 0 j == 0 ) dp [ i ] [ j ] = 0 ; else if ( a [ i - 1 ] == b [ j - 1 ] ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } return dp [ n ] [ m ] ; } int main ( ) { int A [ ] = { 3 , 9 , 2 } ; int B [ ] = { 3 , 2 , 9 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int M = sizeof ( B ) / sizeof ( B [ 0 ] ) ; cout << uncrossedLines ( A , B , N , M ) ; return 0 ; }
Minimize flips required to make all shortest paths from top | C ++ program for the above approach ; Function to count the minimum number of flips required ; Dimensions of matrix ; Stores the count the flips ; Check if element is same or not ; Return the final count ; Driver Code ; Given Matrix ; Given path as a string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minFlips ( vector < vector < int > > & mat , string s ) { int N = mat . size ( ) ; int M = mat [ 0 ] . size ( ) ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] != s [ i + j ] - '0' ) { count ++ ; } } } return count ; } int main ( ) { vector < vector < int > > mat = { { 1 , 0 , 1 } , { 0 , 1 , 1 } , { 0 , 0 , 0 } } ; string s = "10001" ; cout << minFlips ( mat , s ) ; return 0 ; }
Generate a pair of integers from a range [ L , R ] whose LCM also lies within the range | C ++ implementation of the above approach ; Checking if any pair is possible or not in range ( l , r ) ; If not possible print ( - 1 ) ; Print LCM pair ; Driver code ; Function call
#include <iostream> NEW_LINE using namespace std ; void lcmpair ( int l , int r ) { int x , y ; x = l ; y = 2 * l ; if ( y > r ) { cout << " - 1 STRNEWLINE " ; } else { cout << " X ▁ = ▁ " << x << " ▁ Y ▁ = ▁ " << y << " STRNEWLINE " ; } } int main ( ) { int l = 13 , r = 69 ; lcmpair ( l , r ) ; return 0 ; }
Print all possible shortest chains to reach a target word | C ++ Program to implement the above approach ; Function to print all possible shortest sequences starting from start to target . ; Find words differing by a single character with word ; Find next word in dict by changing each element from ' a ' to ' z ' ; Function to get all the shortest possible sequences starting from ' start ' to ' target ' ; Store all the shortest path . ; Store visited words in list ; Queue used to find the shortest path ; Stores the distinct words from given list ; Stores whether the shortest path is found or not ; Explore the next level ; Find words differing by a single character ; Add words to the path . ; Found the target ; If already reached target ; Erase all visited words . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void displaypath ( vector < vector < string > > & res ) { for ( int i = 0 ; i < res . size ( ) ; i ++ ) { cout << " [ ▁ " ; for ( int j = 0 ; j < res [ 0 ] . size ( ) ; j ++ ) { cout << res [ i ] [ j ] << " , ▁ " ; } cout << " ▁ ] STRNEWLINE " ; } } vector < string > addWord ( string word , unordered_set < string > & dict ) { vector < string > res ; for ( int i = 0 ; i < word . size ( ) ; i ++ ) { char s = word [ i ] ; for ( char c = ' a ' ; c <= ' z ' ; c ++ ) { word [ i ] = c ; if ( dict . count ( word ) ) res . push_back ( word ) ; } word [ i ] = s ; } return res ; } vector < vector < string > > findLadders ( vector < string > & Dict , string beginWord , string endWord ) { vector < vector < string > > res ; unordered_set < string > visit ; queue < vector < string > > q ; unordered_set < string > dict ( Dict . begin ( ) , Dict . end ( ) ) ; q . push ( { beginWord } ) ; bool flag = false ; while ( ! q . empty ( ) ) { int size = q . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) { vector < string > cur = q . front ( ) ; q . pop ( ) ; vector < string > newadd ; newadd = addWord ( cur . back ( ) , dict ) ; for ( int j = 0 ; j < newadd . size ( ) ; j ++ ) { vector < string > newline ( cur . begin ( ) , cur . end ( ) ) ; newline . push_back ( newadd [ j ] ) ; if ( newadd [ j ] == endWord ) { flag = true ; res . push_back ( newline ) ; } visit . insert ( newadd [ j ] ) ; q . push ( newline ) ; } } if ( flag ) { break ; } for ( auto it : visit ) { dict . erase ( it ) ; } visit . clear ( ) ; } return res ; } int main ( ) { vector < string > str { " ted " , " tex " , " red " , " tax " , " tad " , " den " , " rex " , " pee " } ; string beginWord = " red " ; string endWord = " tax " ; vector < vector < string > > res = findLadders ( str , beginWord , endWord ) ; displaypath ( res ) ; }
Count non | C ++ Program to implement the above approach ; Function to reverse a number ; Store the reverse of N ; Return reverse of N ; Function to get the count of non - palindromic numbers having same first and last digit ; Store the required count ; Traverse the array ; Store reverse of arr [ i ] ; Check for palindrome ; IF non - palindromic ; Check if first and last digits are equal ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int revNum ( int N ) { int x = 0 ; while ( N ) { x = x * 10 + N % 10 ; N = N / 10 ; } return x ; } int ctNonPalin ( int arr [ ] , int N ) { int Res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int x = revNum ( arr [ i ] ) ; if ( x == arr [ i ] ) { continue ; } else { Res += ( arr [ i ] % 10 == N % 10 ) ; } } return Res ; } int main ( ) { int arr [ ] = { 121 , 134 , 2342 , 4514 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << ctNonPalin ( arr , N ) ; }
Smallest subarray from a given Array with sum greater than or equal to K | Set 2 | C ++ program for the above approach ; Function to find the smallest subarray with sum greater than or equal target ; DP table to store the computed subproblems ; Initialize first column with 0 ; Initialize first row with 0 ; Check for invalid condition ; Fill up the dp table ; Print the minimum length ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minlt ( vector < int > arr , int target , int n ) { vector < vector < int > > dp ( arr . size ( ) + 1 , vector < int > ( target + 1 , -1 ) ) ; for ( int i = 0 ; i < arr . size ( ) + 1 ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( int j = 0 ; j < target + 1 ; j ++ ) dp [ 0 ] [ j ] = INT_MAX ; for ( int i = 1 ; i <= arr . size ( ) ; i ++ ) { for ( int j = 1 ; j <= target ; j ++ ) { if ( arr [ i - 1 ] > j ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } else { dp [ i ] [ j ] = min ( dp [ i - 1 ] [ j ] , ( dp [ i ] [ j - arr [ i - 1 ] ] ) != INT_MAX ? ( dp [ i ] [ j - arr [ i - 1 ] ] + 1 ) : INT_MAX ) ; } } } if ( dp [ arr . size ( ) ] [ target ] == INT_MAX ) { return -1 ; } else { return dp [ arr . size ( ) ] [ target ] ; } } int main ( ) { vector < int > arr = { 2 , 3 , 5 , 4 , 1 } ; int target = 11 ; int n = arr . size ( ) ; cout << minlt ( arr , target , n ) ; }
Minimum absolute difference of server loads | C ++ 14 program to implement the above approach ; Function which returns the minimum difference of loads ; Compute the overall server load ; Stores the results of subproblems ; Fill the partition table in bottom up manner ; If i - th server is included ; If i - th server is excluded ; Server A load : total_sum - ans Server B load : ans Diff : abs ( total_sum - 2 * ans ) ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minServerLoads ( int n , vector < int > & servers ) { int totalLoad = 0 ; for ( int i : servers ) totalLoad += i ; int requiredLoad = totalLoad / 2 ; vector < vector < int > > dp ( n + 1 , vector < int > ( requiredLoad + 1 , 0 ) ) ; for ( int i = 1 ; i < n + 1 ; i ++ ) { for ( int j = 1 ; j < requiredLoad + 1 ; j ++ ) { if ( servers [ i - 1 ] > j ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; else dp [ i ] [ j ] = max ( dp [ i - 1 ] [ j ] , servers [ i - 1 ] + dp [ i - 1 ] [ j - servers [ i - 1 ] ] ) ; } } return totalLoad - 2 * dp [ n ] [ requiredLoad ] ; } int main ( ) { int N = 5 ; vector < int > servers = { 1 , 2 , 3 , 4 , 5 } ; cout << ( minServerLoads ( N , servers ) ) ; }
Length of longest subarray with positive product | C ++ program to implement the above approach ; Function to find the length of longest subarray whose product is positive ; Stores the length of current subarray with positive product ; Stores the length of current subarray with negative product ; Stores the length of the longest subarray with positive product ; Reset the value ; If current element is positive ; Increment the length of subarray with positive product ; If at least one element is present in the subarray with negative product ; Update res ; If current element is negative ; Increment the length of subarray with negative product ; If at least one element is present in the subarray with positive product ; Update res ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLenSub ( int arr [ ] , int N ) { int Pos = 0 ; int Neg = 0 ; int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { Pos = Neg = 0 ; } else if ( arr [ i ] > 0 ) { Pos += 1 ; if ( Neg != 0 ) { Neg += 1 ; } res = max ( res , Pos ) ; } else { swap ( Pos , Neg ) ; Neg += 1 ; if ( Pos != 0 ) { Pos += 1 ; } res = max ( res , Pos ) ; } } return res ; } int main ( ) { int arr [ ] = { -1 , -2 , -3 , 0 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxLenSub ( arr , N ) ; }
Print all strings of maximum length from an array of strings | C ++ program to implement the above approach ; Function to find the length of the longest string from the given array of strings ; Traverse the array ; Stores the length of current string ; Update maximum length ; Return the maximum length ; Function to print the longest strings from the array ; Find the strings having length equals to len ; Print the resultant vector of strings ; Function to print all the longest strings from the array ; Find the length of longest string ; Find and print all the strings having length equals to max ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLength ( vector < string > arr ) { int len = INT_MIN ; int N = arr . size ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int l = arr [ i ] . size ( ) ; if ( len < l ) { len = l ; } } return len ; } void maxStrings ( vector < string > arr , int len ) { int N = arr . size ( ) ; vector < string > ans ; for ( int i = 0 ; i < N ; i ++ ) { if ( len == arr [ i ] . size ( ) ) { ans . push_back ( arr [ i ] ) ; } } for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { cout << ans [ i ] << " ▁ " ; } } void printStrings ( vector < string > & arr ) { int max = maxLength ( arr ) ; maxStrings ( arr , max ) ; } int main ( ) { vector < string > arr = { " aba " , " aa " , " ad " , " vcd " , " aba " } ; printStrings ( arr ) ; return 0 ; }
Minimize count of flips required such that no substring of 0 s have length exceeding K | C ++ program to implement the above approach ; Function to return minimum number of flips required ; Base Case ; Stores the count of minimum number of flips ; Stores the count of zeros in current substring ; If current character is 0 ; Continue ongoing substring ; Start a new substring ; If k consecutive zeroes are obtained ; End segment ; Return the result ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int min_flips ( string & str , int k ) { if ( str . size ( ) == 0 ) return 0 ; int ans = 0 ; int cnt_zeros = 0 ; for ( char ch : str ) { if ( ch == '0' ) { ++ cnt_zeros ; } else { cnt_zeros = 0 ; } if ( cnt_zeros == k ) { ++ ans ; cnt_zeros = 0 ; } } return ans ; } int main ( ) { string str = "11100000011" ; int k = 3 ; cout << min_flips ( str , k ) ; return 0 ; }
Find all possible subarrays having product less than or equal to K | C ++ program to implement the above approach ; Function to return all possible subarrays having product less than or equal to K ; Store the required subarrays ; Stores the product of current subarray ; Stores the starting index of the current subarray ; Check for empty array ; Iterate over the array ; Calculate product ; If product exceeds K ; Reduce product ; Increase starting index of current subarray ; Stores the subarray elements ; Store the subarray elements ; Add the subarrays to the list ; Return the final list of subarrays ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < vector < int > > maxSubArray ( int arr [ ] , int n , int K ) { vector < vector < int > > solution ; int multi = 1 ; int start = 0 ; if ( n <= 1 K < 0 ) { return solution ; } for ( int i = 0 ; i < n ; i ++ ) { multi = multi * arr [ i ] ; while ( multi > K ) { multi = multi / arr [ start ] ; start ++ ; } vector < int > list ; for ( int j = i ; j >= start ; j -- ) { list . insert ( list . begin ( ) , arr [ j ] ) ; solution . push_back ( list ) ; } } return solution ; } int main ( ) { int arr [ ] = { 2 , 7 , 1 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 7 ; vector < vector < int > > v = maxSubArray ( arr , n , K ) ; cout << " [ " ; bool first = true ; for ( auto x : v ) { if ( ! first ) { cout << " , ▁ " ; } else { first = false ; } cout << " [ " ; bool ff = true ; for ( int y : x ) { if ( ! ff ) { cout << " , ▁ " ; } else { ff = false ; } cout << y ; } cout << " ] " ; } cout << " ] " ; return 0 ; }
Count of collisions at a point ( X , Y ) | C ++ 14 program to implement the above approach ; Function to find the count of possible pairs of collisions ; Stores the time at which points reach the origin ; Calculate time for each point ; Sort the times ; Counting total collisions ; Count of elements arriving at a given point at the same time ; Driver Code ; Given set of points with speed ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( vector < vector < int > > & D , int N , int X , int Y ) { vector < double > T ; for ( int i = 0 ; i < N ; i ++ ) { int x = D [ i ] [ 0 ] ; int y = D [ i ] [ 1 ] ; double speed = D [ i ] [ 2 ] ; double time = ( ( x * x - X * X ) + ( y * y - Y * Y ) ) / ( speed * speed ) ; T . push_back ( time ) ; } sort ( T . begin ( ) , T . end ( ) ) ; int i = 0 ; int total = 0 ; while ( i < T . size ( ) - 1 ) { int count = 1 ; while ( i < T . size ( ) - 1 and T [ i ] == T [ i + 1 ] ) { count += 1 ; i += 1 ; } total += ( count * ( count - 1 ) ) / 2 ; i += 1 ; } return total ; } int main ( ) { int N = 5 ; vector < vector < int > > D = { { 5 , 12 , 1 } , { 16 , 63 , 5 } , { -10 , 24 , 2 } , { 7 , 24 , 2 } , { -24 , 7 , 2 } } ; int X = 0 , Y = 0 ; cout << ( solve ( D , N , X , Y ) ) ; return 0 ; }
Longest subarray forming an Arithmetic Progression ( AP ) | C ++ Program to implement the above approach ; Function to return the length of longest subarray forming an AP ; Minimum possible length of required subarray is 2 ; Stores the length of the current subarray ; Stores the common difference of the current AP ; Stores the common difference of the previous AP ; If the common differences are found to be equal ; Continue the previous subarray ; Start a new subarray ; Update the length to store maximum length ; Update the length to store maximum length ; Return the length of the longest subarray ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMaxLength ( int arr [ ] , int N ) { int res = 2 ; int dist = 2 ; int curradj = ( arr [ 1 ] - arr [ 0 ] ) ; int prevadj = ( arr [ 1 ] - arr [ 0 ] ) ; for ( int i = 2 ; i < N ; i ++ ) { curradj = arr [ i ] - arr [ i - 1 ] ; if ( curradj == prevadj ) { dist ++ ; } else { prevadj = curradj ; res = max ( res , dist ) ; dist = 2 ; } } res = max ( res , dist ) ; return res ; } int main ( ) { int arr [ ] = { 10 , 7 , 4 , 6 , 8 , 10 , 11 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getMaxLength ( arr , N ) ; }
Minimize cost to empty a given string by removing characters alphabetically | C ++ program for the above approach ; Function to find the minimum cost required to remove each character of the string in alphabetical order ; Stores the frequency of characters of the string ; Iterate through the string ; Count the number of characters smaller than the present character ; If no smaller character precedes current character ; Increase the frequency of the current character ; Return the total cost ; Driver Code ; Given string str ; Function call
#include <bits/stdc++.h> NEW_LINE #include <vector> NEW_LINE using namespace std ; int minSteps ( string str , int N ) { int smaller , cost = 0 ; int f [ 26 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { int curr_ele = str [ i ] - ' a ' ; smaller = 0 ; for ( int j = 0 ; j <= curr_ele ; j ++ ) { if ( f [ j ] ) smaller += f [ j ] ; } if ( smaller == 0 ) cost += ( i + 1 ) ; else cost += ( i - smaller + 1 ) ; f [ str [ i ] - ' a ' ] ++ ; } return cost ; } int main ( ) { string str = " abcab " ; int N = str . size ( ) ; cout << minSteps ( str , N ) ; return 0 ; }
Count of Rectangles with area K made up of only 1 s from given Binary Arrays | C ++ Program to implement the above approach ; Function to find the subarrays of all possible lengths made up of only 1 s ; Stores the frequency of the subarrays ; Check if the previous value was also 0 ; If the previous value was 1 ; Find the subarrays of each size from 1 to count ; If A [ ] is of the form ... .111 ; Function to find the count of all possible rectangles ; Size of each of the arrays ; Stores the count of subarrays of each size consisting of only 1 s from array A [ ] ; Stores the count of subarrays of each size consisting of only 1 s from array B [ ] ; Iterating over all subarrays consisting of only 1 s in A [ ] ; If i is a factor of K , then there is a subarray of size K / i in B [ ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > findSubarrays ( vector < int > & a ) { int n = a . size ( ) ; vector < int > freq ( n + 1 ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { if ( count == 0 ) continue ; else { int value = count ; for ( int j = 1 ; j <= count ; j ++ ) { freq [ j ] += value ; value -- ; } count = 0 ; } } else count ++ ; } if ( count > 0 ) { int value = count ; for ( int j = 1 ; j <= count ; j ++ ) { freq [ j ] += value ; value -- ; } } return freq ; } void countRectangles ( vector < int > & a , vector < int > & b , int K ) { int n = a . size ( ) ; int m = b . size ( ) ; vector < int > subA = findSubarrays ( a ) ; vector < int > subB = findSubarrays ( b ) ; int total = 0 ; for ( int i = 1 ; i < subA . size ( ) ; i ++ ) { if ( K % i == 0 and ( K / i ) <= m ) { total = total + subA [ i ] * subB [ K / i ] ; } } cout << total ; } int main ( ) { vector < int > a = { 0 , 0 , 1 , 1 } ; vector < int > b = { 1 , 0 , 1 } ; int K = 2 ; countRectangles ( a , b , K ) ; return 0 ; }
Print Longest Bitonic subsequence ( Space Optimized Approach ) | C ++ Program to implement the above approach ; Function to print the longest bitonic subsequence ; Function to generate the longest bitonic subsequence ; Store the lengths of LIS ending at every index ; Store the lengths of LDS ending at every index ; Compute LIS for all indices ; Compute LDS for all indices ; Find the index having maximum value of lis [ i ] + lds [ i ] - 1 ; Stores the count of elements in increasing order in Bitonic subsequence ; Store the increasing subsequence ; Sort the bitonic subsequence to arrange smaller elements at the beginning ; Stores the count of elements in decreasing order in Bitonic subsequence ; Print the longest bitonic sequence ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printRes ( vector < int > & res ) { int n = res . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { cout << res [ i ] << " ▁ " ; } } void printLBS ( int arr [ ] , int N ) { int lis [ N ] ; int lds [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { lis [ i ] = lds [ i ] = 1 ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { if ( lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; } } } for ( int i = N - 1 ; i >= 0 ; i -- ) { for ( int j = N - 1 ; j > i ; j -- ) { if ( arr [ j ] < arr [ i ] ) { if ( lds [ i ] < lds [ j ] + 1 ) lds [ i ] = lds [ j ] + 1 ; } } } int MaxVal = arr [ 0 ] , inx = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( MaxVal < lis [ i ] + lds [ i ] - 1 ) { MaxVal = lis [ i ] + lds [ i ] - 1 ; inx = i ; } } int ct1 = lis [ inx ] ; vector < int > res ; for ( int i = inx ; i >= 0 && ct1 > 0 ; i -- ) { if ( lis [ i ] == ct1 ) { res . push_back ( arr [ i ] ) ; ct1 -- ; } } reverse ( res . begin ( ) , res . end ( ) ) ; int ct2 = lds [ inx ] - 1 ; for ( int i = inx ; i < N && ct2 > 0 ; i ++ ) { if ( lds [ i ] == ct2 ) { res . push_back ( arr [ i ] ) ; ct2 -- ; } } printRes ( res ) ; } int main ( ) { int arr [ ] = { 80 , 60 , 30 , 40 , 20 , 10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printLBS ( arr , N ) ; }
Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible | C ++ Program for the above approach ; Function to check if the array can be partitioned into atmost d subarray with sum atmost T ; Initial partition ; Current sum ; If current sum exceeds T ; Create a new partition ; If count of partitions exceed d ; Function to find the minimum possible value of T ; Stores the maximum and total sum of elements ; Maximum element ; Sum of all elements ; Calculate median T ; If atmost D partitions possible ; Check for smaller T ; Otherwise ; Check for larger T ; Print the minimum T required ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool possible ( int T , int arr [ ] , int n , int d ) { int partition = 1 ; int total = 0 ; for ( int i = 0 ; i < n ; i ++ ) { total = total + arr [ i ] ; if ( total > T ) { partition = partition + 1 ; total = arr [ i ] ; if ( partition > d ) { return false ; } } } return true ; } void calcT ( int n , int d , int arr [ ] ) { int mx = -1 , sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mx = max ( mx , arr [ i ] ) ; sum = sum + arr [ i ] ; } int lb = mx ; int ub = sum ; while ( lb < ub ) { int T_mid = lb + ( ub - lb ) / 2 ; if ( possible ( T_mid , arr , n , d ) == true ) { ub = T_mid ; } else { lb = T_mid + 1 ; } } cout << lb << endl ; } int main ( ) { int d = 2 ; int arr [ ] = { 1 , 1 , 1 , 1 , 1 } ; int n = sizeof arr / sizeof arr [ 0 ] ; calcT ( n , d , arr ) ; return 0 ; }