codeparrot/xlcost-text-to-code huggingface dataset:
Maximum sum of elements divisible by K from the given array |
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { int k = 16 ; vector < int > arr = { 43 , 1 , 17 , 26 , 15 } ; int n = arr . size ( ) ; vector < vector < int > > dp ( n + 2 , vector < int > ( k , 0 ) ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 0 ; j < k ; j ++ ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } dp [ i ] [ arr [ i - 1 ] % k ] = max ( dp [ i ] [ arr [ i - 1 ] % k ] , arr [ i - 1 ] ) ; for ( int j = 0 ; j < k ; j ++ ) { int m = ( j + arr [ i - 1 ] ) % k ; if ( dp [ i - 1 ] [ j ] != 0 ) dp [ i ] [ m ] = max ( dp [ i ] [ m ] , arr [ i - 1 ] + dp [ i - 1 ] [ j ] ) ; } } cout << dp [ n ] [ 0 ] ; return 0 ; }
Maximum contiguous decreasing sequence obtained by removing any one element | C ++ program to find maximum length of decreasing sequence by removing at most one element ; Function to find the maximum length ; Initialise maximum length to 1 ; Initialise left [ ] to find the length of decreasing sequence from left to right ; Initialise right [ ] to find the length of decreasing sequence from right to left ; Initially store 1 at each index of left and right array ; Iterate over the array arr [ ] to store length of decreasing sequence that can be obtained at every index in the right array ; Store the length of longest continuous decreasing sequence in maximum ; Iterate over the array arr [ ] to store length of decreasing sequence that can be obtained at every index in the left array ; Check if we can obtain a longer decreasing sequence after removal of any element from the array arr [ ] with the help of left [ ] & right [ ] ; Return maximum length of sequence ; Driver code ; Function calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLength ( int * a , int n ) { int maximum = 1 ; int left [ n ] ; int right [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { left [ i ] = 1 ; right [ i ] = 1 ; } for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( a [ i ] > a [ i + 1 ] ) { right [ i ] = right [ i + 1 ] + 1 ; } maximum = max ( maximum , right [ i ] ) ; } for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] < a [ i - 1 ] ) { left [ i ] = left [ i - 1 ] + 1 ; } } if ( n > 2 ) { for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( a [ i - 1 ] > a [ i + 1 ] ) { maximum = max ( maximum , left [ i - 1 ] + right [ i + 1 ] ) ; } } } return maximum ; } int main ( ) { int arr [ 6 ] = { 8 , 7 , 3 , 5 , 2 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxLength ( arr , n ) << endl ; return 0 ; }
Longest alternating subsequence in terms of positive and negative integers | C ++ program to find the length of longest alternate subsequence ; LAS [ i ] [ pos ] array to find the length of LAS till index i by including or excluding element arr [ i ] on the basis of value of pos ; Base Case ; If current element is positive and pos is true Include the current element and change pos to false ; Recurr for the next iteration ; If current element is negative and pos is false Include the current element and change pos to true ; Recurr for the next iteration ; If current element is excluded , reccur for next iteration ; Driver 's Code ; Print LAS
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LAS [ 1000 ] [ 2 ] = { false } ; int solve ( int arr [ ] , int n , int i , bool pos ) { if ( i == n ) return 0 ; if ( LAS [ i ] [ pos ] ) return LAS [ i ] [ pos ] ; int inc = 0 , exc = 0 ; if ( arr [ i ] > 0 && pos == true ) { pos = false ; inc = 1 + solve ( arr , n , i + 1 , pos ) ; } else if ( arr [ i ] < 0 && pos == false ) { pos = true ; inc = 1 + solve ( arr , n , i + 1 , pos ) ; } exc = solve ( arr , n , i + 1 , pos ) ; LAS [ i ] [ pos ] = max ( inc , exc ) ; return LAS [ i ] [ pos ] ; } int main ( ) { int arr [ ] = { -1 , 2 , 3 , 4 , 5 , -6 , 8 , -99 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << max ( solve ( arr , n , 0 , 0 ) , solve ( arr , n , 0 , 1 ) ) ; }
Number of subsequences with negative product | C ++ implementation of the approach ; Function to return the count of all the subsequences with negative product ; To store the count of positive elements in the array ; To store the count of negative elements in the array ; If the current element is positive ; If the current element is negative ; For all the positive elements of the array ; For all the negative elements of the array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cntSubSeq ( int arr [ ] , int n ) { int pos_count = 0 ; int neg_count = 0 ; int result ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) pos_count ++ ; if ( arr [ i ] < 0 ) neg_count ++ ; } result = pow ( 2 , pos_count ) ; if ( neg_count > 0 ) result *= pow ( 2 , neg_count - 1 ) ; else result = 0 ; return result ; } int main ( ) { int arr [ ] = { 3 , -4 , -1 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << cntSubSeq ( arr , n ) ; return 0 ; }
Find minimum number of steps to reach the end of String | C ++ implementation of the approach ; Function to return the minimum number of steps to reach the end ; If the end can 't be reached ; Already at the end ; If the length is 2 or 3 then the end can be reached in a single step ; For the other cases , solve the problem using dynamic programming ; It requires no move from the end to reach the end ; From the 2 nd last and the 3 rd last index , only a single move is required ; Update the answer for every index ; If the current index is not reachable ; To store the minimum steps required from the current index ; If it is a valid move then update the minimum steps required ; Update the minimum steps required starting from the current index ; Cannot reach the end starting from str [ 0 ] ; Return the minimum steps required ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSteps ( string str , int n , int k ) { if ( str [ n - 1 ] == '0' ) return -1 ; if ( n == 1 ) return 0 ; if ( n < 4 ) return 1 ; int dp [ n ] ; dp [ n - 1 ] = 0 ; dp [ n - 2 ] = 1 ; dp [ n - 3 ] = 1 ; for ( int i = n - 4 ; i >= 0 ; i -- ) { if ( str [ i ] == '0' ) continue ; int steps = INT_MAX ; if ( i + k < n && str [ i + k ] == '1' ) steps = min ( steps , dp [ i + k ] ) ; if ( str [ i + 1 ] == '1' ) steps = min ( steps , dp [ i + 1 ] ) ; if ( str [ i + 2 ] == '1' ) steps = min ( steps , dp [ i + 2 ] ) ; dp [ i ] = ( steps == INT_MAX ) ? steps : 1 + steps ; } if ( dp [ 0 ] == INT_MAX ) return -1 ; return dp [ 0 ] ; } int main ( ) { string str = "101000011" ; int n = str . length ( ) ; int k = 5 ; cout << minSteps ( str , n , k ) ; return 0 ; }
Length of longest Palindromic Subsequence of even length with no two adjacent characters same | C ++ implementation of above approach ; To store the values of subproblems ; Function to find the Longest Palindromic subsequence of even length with no two adjacent characters same ; Base cases If the string length is 1 return 0 ; If the string length is 2 ; Check if the characters match ; If the value with given parameters is previously calculated ; If the first and last character of the string matches with the given character ; Remove the first and last character and call the function for all characters ; If it does not match ; Then find the first and last index of given character in the given string ; Take the substring from i to j and call the function with substring and the given character ; Store the answer for future use ; Driver code ; Check for all starting characters
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define lli  long long int NEW_LINE unordered_map < string , lli > dp ; lli solve ( string s , char c ) { if ( s . length ( ) == 1 ) return 0 ; if ( s . length ( ) == 2 ) { if ( s [ 0 ] == s [ 1 ] && s [ 0 ] == c ) return 1 ; else return 0 ; } if ( dp [ s + " ▁ " + c ] ) return dp [ s + " ▁ " + c ] ; lli ans = 0 ; if ( s [ 0 ] == s [ s . length ( ) - 1 ] && s [ 0 ] == c ) { for ( char c1 = ' a ' ; c1 <= ' z ' ; c1 ++ ) if ( c1 != c ) ans = max ( ans , 1 + solve ( s . substr ( 1 , s . length ( ) - 2 ) , c1 ) ) ; } else { for ( lli i = 0 ; i < s . length ( ) ; i ++ ) { if ( s [ i ] == c ) { for ( lli j = s . length ( ) - 1 ; j > i ; j -- ) if ( s [ j ] == c ) { if ( j == i ) break ; ans = solve ( s . substr ( i , j - i + 1 ) , c ) ; break ; } break ; } } } dp [ s + " ▁ " + c ] = ans ; return dp [ s + " ▁ " + c ] ; } int main ( ) { string s = " abscrcdba " ; lli ma = 0 ; for ( char c1 = ' a ' ; c1 <= ' z ' ; c1 ++ ) ma = max ( ma , solve ( s , c1 ) * 2 ) ; cout << ma << endl ; return 0 ; }
Eggs dropping puzzle | Set 2 | C ++ implementation of the approach ; Function to return the minimum number of trials needed in the worst case with n eggs and k floors ; Fill all the entries in table using optimal substructure property ; Return the minimum number of moves ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int eggDrop ( int n , int k ) { vector < vector < int > > dp ( k + 1 , vector < int > ( n + 1 , 0 ) ) ; int x = 0 ; while ( dp [ x ] [ n ] < k ) { x ++ ; for ( int i = 1 ; i <= n ; i ++ ) dp [ x ] [ i ] = dp [ x - 1 ] [ i - 1 ] + dp [ x - 1 ] [ i ] + 1 ; } return x ; } int main ( ) { int n = 2 , k = 36 ; cout << eggDrop ( n , k ) ; return 0 ; }
Maximum length of Strictly Increasing Sub | C ++ implementation of the approach ; Function to return the maximum length of strictly increasing subarray after removing atmost one element ; Create two arrays pre and pos ; Find out the contribution of the current element in array [ 0 , i ] and update pre [ i ] ; Find out the contribution of the current element in array [ N - 1 , i ] and update pos [ i ] ; Calculate the maximum length of the stricly increasing subarray without removing any element ; Calculate the maximum length of the strictly increasing subarray after removing the current element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxIncSubarr ( int a [ ] , int n ) { int pre [ n ] = { 0 } ; int pos [ n ] = { 0 } ; pre [ 0 ] = 1 ; pos [ n - 1 ] = 1 ; int l = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] > a [ i - 1 ] ) pre [ i ] = pre [ i - 1 ] + 1 ; else pre [ i ] = 1 ; } l = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( a [ i ] < a [ i + 1 ] ) pos [ i ] = pos [ i + 1 ] + 1 ; else pos [ i ] = 1 ; } int ans = 0 ; l = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] > a [ i - 1 ] ) l ++ ; else l = 1 ; ans = max ( ans , l ) ; } for ( int i = 1 ; i <= n - 2 ; i ++ ) { if ( a [ i - 1 ] < a [ i + 1 ] ) ans = max ( pre [ i - 1 ] + pos [ i + 1 ] , ans ) ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << maxIncSubarr ( arr , n ) ; return 0 ; }
Number of square matrices with all 1 s | C ++ program to return the number of square submatrices with all 1 s ; Function to return the number of square submatrices with all 1 s ; Initialize count variable ; If a [ i ] [ j ] is equal to 0 ; Calculate number of square submatrices ending at ( i , j ) ; Calculate the sum of the array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define n  3 NEW_LINE #define m  3 NEW_LINE int countSquareMatrices ( int a [ ] [ m ] , int N , int M ) { int count = 0 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { if ( a [ i ] [ j ] == 0 ) continue ; a [ i ] [ j ] = min ( min ( a [ i - 1 ] [ j ] , a [ i ] [ j - 1 ] ) , a [ i - 1 ] [ j - 1 ] ) + 1 ; } } for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) count += a [ i ] [ j ] ; return count ; } int main ( ) { int arr [ ] [ m ] = { { 1 , 0 , 1 } , { 1 , 1 , 0 } , { 1 , 1 , 0 } } ; cout << countSquareMatrices ( arr , n , m ) ; return 0 ; }
Count number of increasing sub | C ++ implementation of the approach ; Segment tree array ; Function for point update in segment tree ; Base case ; If l == r == up ; Mid element ; Updating the segment tree ; Function for the range sum - query ; Base case ; Mid element ; Calling for the left and the right subtree ; Function to return the count ; Copying array arr to sort it ; Sorting array brr ; Map to store the rank of each element ; dp array ; To store the final answer ; Updating the dp array ; Rank of the element ; Solving the dp - states using segment tree ; Updating the final answer ; Updating the segment tree ; Returning the final answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  10000 NEW_LINE int seg [ 3 * N ] ; int update ( int in , int l , int r , int up_in , int val ) { if ( r < up_in l > up_in ) return seg [ in ] ; if ( l == up_in and r == up_in ) return seg [ in ] = val ; int m = ( l + r ) / 2 ; return seg [ in ] = update ( 2 * in + 1 , l , m , up_in , val ) + update ( 2 * in + 2 , m + 1 , r , up_in , val ) ; } int query ( int in , int l , int r , int l1 , int r1 ) { if ( l > r ) return 0 ; if ( r < l1 l > r1 ) return 0 ; if ( l1 <= l and r <= r1 ) return seg [ in ] ; int m = ( l + r ) / 2 ; return query ( 2 * in + 1 , l , m , l1 , r1 ) + query ( 2 * in + 2 , m + 1 , r , l1 , r1 ) ; } int findCnt ( int * arr , int n ) { int brr [ n ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; sort ( brr , brr + n ) ; map < int , int > r ; for ( int i = 0 ; i < n ; i ++ ) r [ brr [ i ] ] = i + 1 ; int dp [ n ] = { 0 } ; int ans = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int rank = r [ arr [ i ] ] ; dp [ i ] = 1 + query ( 0 , 0 , n - 1 , rank , n - 1 ) ; ans += dp [ i ] ; update ( 0 , 0 , n - 1 , rank - 1 , dp [ i ] + query ( 0 , 0 , n - 1 , rank - 1 , rank - 1 ) ) ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 10 , 9 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << findCnt ( arr , n ) ; return 0 ; }
Minimum cost to build N blocks from one block | C ++ program to Minimum cost to build N blocks from one block ; Function to calculate min cost to build N blocks ; Initialize base case ; Recurence when i is odd ; Recurence when i is even ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCost ( int n , int x , int y , int z ) { int dp [ n + 1 ] = { 0 } ; dp [ 0 ] = dp [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 1 ) { dp [ i ] = min ( dp [ ( i + 1 ) / 2 ] + x + z , dp [ i - 1 ] + y ) ; } else { dp [ i ] = min ( dp [ i / 2 ] + x , dp [ i - 1 ] + y ) ; } } return dp [ n ] ; } int main ( ) { int n = 5 , x = 2 , y = 1 , z = 3 ; cout << minCost ( n , x , y , z ) << endl ; return 0 ; }
Size of the largest divisible subset in an Array | C ++ implementation of the above approach ; Function to find the size of the largest divisible subarray ; Mark all elements of the array ; Traverse reverse ; For all multiples of i ; Return the required answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  1000005 NEW_LINE int maximum_set ( int a [ ] , int n ) { int dp [ N ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) dp [ a [ i ] ] = 1 ; int ans = 1 ; for ( int i = N - 1 ; i >= 1 ; i -- ) { if ( dp [ i ] != 0 ) { for ( int j = 2 * i ; j < N ; j += i ) { dp [ i ] = max ( dp [ i ] , 1 + dp [ j ] ) ; ans = max ( ans , dp [ i ] ) ; } } } return ans ; } int main ( ) { int arr [ ] = { 3 , 4 , 6 , 8 , 10 , 18 , 21 , 24 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maximum_set ( arr , n ) ; return 0 ; }
Longest sub | C ++ implementation of the approach ; Function to return the length of the largest sub - string divisible by 3 ; Base - case ; If the state has been solved before then return its value ; Marking the state as solved ; Recurrence relation ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100 NEW_LINE int dp [ N ] [ 3 ] ; bool v [ N ] [ 3 ] ; int findLargestString ( string & s , int i , int r ) { if ( i == s . size ( ) ) { if ( r == 0 ) return 0 ; else return INT_MIN ; } if ( v [ i ] [ r ] ) return dp [ i ] [ r ] ; v [ i ] [ r ] = 1 ; dp [ i ] [ r ] = max ( 1 + findLargestString ( s , i + 1 , ( r * 2 + ( s [ i ] - '0' ) ) % 3 ) , findLargestString ( s , i + 1 , r ) ) ; return dp [ i ] [ r ] ; } int main ( ) { string s = "101" ; cout << findLargestString ( s , 0 , 0 ) ; return 0 ; }
Number of sub | C ++ implementation of th approach ; Function to return the number of sub - sequences divisible by 3 ; Base - cases ; If the state has been solved before then return its value ; Marking the state as solved ; Recurrence relation ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100 NEW_LINE int dp [ N ] [ 3 ] ; bool v [ N ] [ 3 ] ; int findCnt ( string & s , int i , int r ) { if ( i == s . size ( ) ) { if ( r == 0 ) return 1 ; else return 0 ; } if ( v [ i ] [ r ] ) return dp [ i ] [ r ] ; v [ i ] [ r ] = 1 ; dp [ i ] [ r ] = findCnt ( s , i + 1 , ( r * 2 + ( s [ i ] - '0' ) ) % 3 ) + findCnt ( s , i + 1 , r ) ; return dp [ i ] [ r ] ; } int main ( ) { string s = "11" ; cout << ( findCnt ( s , 0 , 0 ) - 1 ) ; return 0 ; }
Remove minimum elements from ends of array so that sum decreases by at least K | O ( N ) | C ++ implementation of the approach ; Function to return the count of minimum elements to be removed from the ends of the array such that the sum of the array decrease by at least K ; To store the final answer ; Maximum possible sum required ; Left point ; Right pointer ; Total current sum ; Two pointer loop ; If the sum fits ; Update the answer ; Update the total sum ; Increment the left pointer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCount ( int * arr , int n , int k ) { int ans = 0 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; sum -= k ; int l = 0 ; int r = 0 ; int tot = 0 ; while ( l < n ) { if ( tot <= sum ) { ans = max ( ans , r - l ) ; if ( r == n ) break ; tot += arr [ r ++ ] ; } else { tot -= arr [ l ++ ] ; } } return ( n - ans ) ; } int main ( ) { int arr [ ] = { 1 , 11 , 5 , 5 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int k = 11 ; cout << minCount ( arr , n , k ) ; return 0 ; }
Minimum cost to merge all elements of List | C ++ implementation of the approach ; To store the states of DP ; Function to return the minimum merge cost ; Base case ; If the state has been solved before ; Marking the state as solved ; Reference to dp [ i ] [ j ] ; To store the sum of all the elements in the subarray arr [ i ... j ] ; Loop to iterate the recurrence ; Returning the solved value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  401 NEW_LINE int dp [ N ] [ N ] ; bool v [ N ] [ N ] ; int minMergeCost ( int i , int j , int * arr ) { if ( i == j ) return 0 ; if ( v [ i ] [ j ] ) return dp [ i ] [ j ] ; v [ i ] [ j ] = 1 ; int & x = dp [ i ] [ j ] ; x = INT_MAX ; int tot = 0 ; for ( int k = i ; k <= j ; k ++ ) tot += arr [ k ] ; for ( int k = i + 1 ; k <= j ; k ++ ) { x = min ( x , tot + minMergeCost ( i , k - 1 , arr ) + minMergeCost ( k , j , arr ) ) ; } return x ; } int main ( ) { int arr [ ] = { 1 , 3 , 7 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << minMergeCost ( 0 , n - 1 , arr ) ; return 0 ; }
Expected number of moves to reach the end of a board | Dynamic programming | C ++ implementation of the approach ; To store the states of dp ; To determine whether a state has been solved before ; Function to return the count ; Base cases ; If a state has been solved before it won 't be evaluated again ; Recurrence relation ; Driver code
#include <bits/stdc++.h> NEW_LINE #define maxSize  50 NEW_LINE using namespace std ; double dp [ maxSize ] ; int v [ maxSize ] ; double expectedSteps ( int x ) { if ( x == 0 ) return 0 ; if ( x <= 5 ) return 6 ; if ( v [ x ] ) return dp [ x ] ; v [ x ] = 1 ; dp [ x ] = 1 + ( expectedSteps ( x - 1 ) + expectedSteps ( x - 2 ) + expectedSteps ( x - 3 ) + expectedSteps ( x - 4 ) + expectedSteps ( x - 5 ) + expectedSteps ( x - 6 ) ) / 6 ; return dp [ x ] ; } int main ( ) { int n = 10 ; cout << expectedSteps ( n - 1 ) ; return 0 ; }
Number of subsequences in a given binary string divisible by 2 | C ++ implementation of the approach ; Function to return the count of the required subsequences ; To store the final answer ; Multiplier ; Loop to find the answer ; Condition to update the answer ; updating multiplier ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubSeq ( string str , int len ) { int ans = 0 ; int mul = 1 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == '0' ) ans += mul ; mul *= 2 ; } return ans ; } int main ( ) { string str = "10010" ; int len = str . length ( ) ; cout << countSubSeq ( str , len ) ; return 0 ; }
Knapsack with large Weights | C ++ implementation of the approach ; To store the states of DP ; Function to solve the recurrence relation ; Base cases ; Marking state as solved ; Recurrence relation ; Function to return the maximum weight ; Iterating through all possible values to find the the largest value that can be represented by the given weights ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define V_SUM_MAX  1000 NEW_LINE #define N_MAX  100 NEW_LINE #define W_MAX  10000000 NEW_LINE int dp [ V_SUM_MAX + 1 ] [ N_MAX ] ; bool v [ V_SUM_MAX + 1 ] [ N_MAX ] ; int solveDp ( int r , int i , int * w , int * val , int n ) { if ( r <= 0 ) return 0 ; if ( i == n ) return W_MAX ; if ( v [ r ] [ i ] ) return dp [ r ] [ i ] ; v [ r ] [ i ] = 1 ; dp [ r ] [ i ] = min ( solveDp ( r , i + 1 , w , val , n ) , w [ i ] + solveDp ( r - val [ i ] , i + 1 , w , val , n ) ) ; return dp [ r ] [ i ] ; } int maxWeight ( int * w , int * val , int n , int c ) { for ( int i = V_SUM_MAX ; i >= 0 ; i -- ) { if ( solveDp ( i , 0 , w , val , n ) <= c ) { return i ; } } return 0 ; } int main ( ) { int w [ ] = { 3 , 4 , 5 } ; int val [ ] = { 30 , 50 , 60 } ; int n = sizeof ( w ) / sizeof ( int ) ; int C = 8 ; cout << maxWeight ( w , val , n , C ) ; return 0 ; }
Sum of lengths of all paths possible in a given tree | C ++ implementation of the approach ; Number of vertices ; Adjacency list representation of the tree ; Array that stores the subtree size ; Array to mark all the vertices which are visited ; Utility function to create an edge between two vertices ; Add a to b 's list ; Add b to a 's list ; Function to calculate the subtree size ; Mark visited ; For every adjacent node ; If not already visited ; Recursive call for the child ; Function to calculate the contribution of each edge ; Mark current node as visited ; For every adjacent node ; If it is not already visisted ; Function to return the required sum ; First pass of the dfs ; Second pass ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int sz = 1e5 ; int n ; vector < int > tree [ sz ] ; int subtree_size [ sz ] ; int vis [ sz ] ; void addEdge ( int a , int b ) { tree [ a ] . push_back ( b ) ; tree [ b ] . push_back ( a ) ; } int dfs ( int node ) { vis [ node ] = 1 ; subtree_size [ node ] = 1 ; for ( auto child : tree [ node ] ) { if ( ! vis [ child ] ) { subtree_size [ node ] += dfs ( child ) ; } } return subtree_size [ node ] ; } void contribution ( int node , int & ans ) { vis [ node ] = true ; for ( int child : tree [ node ] ) { if ( ! vis [ child ] ) { ans += ( subtree_size [ child ] * ( n - subtree_size [ child ] ) ) ; contribution ( child , ans ) ; } } } int getSum ( ) { memset ( vis , 0 , sizeof ( vis ) ) ; dfs ( 0 ) ; int ans = 0 ; memset ( vis , 0 , sizeof ( vis ) ) ; contribution ( 0 , ans ) ; return ans ; } int main ( ) { n = 5 ; addEdge ( 0 , 1 ) ; addEdge ( 0 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 1 , 4 ) ; cout << getSum ( ) ; return 0 ; }