codeparrot/xlcost-text-to-code huggingface dataset:
Minimum cost to generate any permutation of the given string | C ++ program for the above approach ; Function that returns true if the current bit is set ; Function to find the minimum cost to form any permutation of string s ; Base Case ; Return the precomputed state ; Iterate over the string and check all possible characters available for current position ; Check if character can be placed at current position ; As there is no previous character so the cost for 1 st character is 0 ; Find the cost of current character and move to next position ; Store the answer for each current state ; Function that generates any permutation of the given string with minimum cost ; Initialize dp table ; Set all the bits of the current character id ; Minimum cost of generating the permutation ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int mask , int i ) { int c = ( mask & ( 1 << i ) ) ; return c != 0 ; } int solve ( vector < vector < int > > a , string s , int n , int prev , int mask , vector < vector < int > > dp ) { if ( mask == 0 ) return 0 ; if ( dp [ mask ] [ prev + 1 ] != -1 ) return dp [ mask ] [ prev + 1 ] ; int ans = 10000 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int id = s [ i ] - ' a ' ; if ( check ( mask , id ) ) { if ( prev == -1 ) { ans = min ( ans , solve ( a , s , n , id , mask ^ ( 1 << id ) , dp ) ) ; } else { ans = min ( ans , a [ prev ] [ id ] + solve ( a , s , n , id , mask ^ ( 1 << id ) , dp ) ) ; } } } dp [ mask ] [ prev + 1 ] = ans ; return ans ; } void generatePermutation ( int mask , int n , vector < vector < int > > a , string s ) { vector < vector < int > > dp ( ( 1 << n ) + 5 , vector < int > ( n + 5 , -1 ) ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int id = s [ i ] - ' a ' ; mask |= ( 1 << id ) ; } cout << solve ( a , s , n , -1 , mask , dp ) << endl ; } int main ( ) { int N = 5 ; string str = " abcde " ; vector < vector < int > > mat = { { 0 , 5 , 1 , 5 , 3 } , { 4 , 0 , 9 , 4 , 2 } , { 7 , 9 , 0 , 10 , 7 } , { 1 , 2 , 8 , 0 , 2 } , { 3 , 9 , 7 , 7 , 0 } } ; generatePermutation ( 0 , N , mat , str ) ; return 0 ; }
Minimize Cost to reduce the Array to a single element by given operations | C ++ program for the above approach ; Function to generate the cost using Prefix Sum Array technique ; Function to combine the sum of the two subproblems ; Function to minimize the cost to add the array elements to a single element ; Check if the value is already stored in the array ; Compute left subproblem ; Compute left subproblem ; Calculate minimum cost ; Store the answer to avoid recalculation ; Driver code ; Initialise dp array ; Preprocessing the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int inf = 10000000 ; vector < int > preprocess ( vector < int > a , int n ) { vector < int > p ( n ) ; p [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { p [ i ] = p [ i - 1 ] + a [ i ] ; } return p ; } int Combine ( vector < int > p , int i , int j ) { if ( i == 0 ) return p [ j ] ; else return p [ j ] - p [ i - 1 ] ; } int minCost ( vector < int > a , int i , int j , int k , vector < int > prefix , vector < vector < int > > dp ) { if ( i >= j ) return 0 ; if ( dp [ i ] [ j ] != -1 ) return dp [ i ] [ j ] ; int best_cost = inf ; for ( int pos = i ; pos < j ; pos ++ ) { int left = minCost ( a , i , pos , k , prefix , dp ) ; int right = minCost ( a , pos + 1 , j , k , prefix , dp ) ; best_cost = min ( best_cost , left + right + ( k * Combine ( prefix , i , j ) ) ) ; } return dp [ i ] [ j ] = best_cost ; } int main ( ) { int n = 4 ; vector < int > a = { 4 , 5 , 6 , 7 } ; int k = 3 ; vector < vector < int > > dp ; dp . resize ( n + 1 , vector < int > ( n + 1 ) ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < n + 1 ; j ++ ) { dp [ i ] [ j ] = -1 ; } } vector < int > prefix = preprocess ( a , n ) ; cout << minCost ( a , 0 , n - 1 , k , prefix , dp ) << endl ; return 0 ; }
Minimize operations required to obtain N | Function to find the minimum number of operations ; Storing the minimum operations to obtain all numbers up to N ; Initilal state ; Iterate for the remaining numbers ; If the number can be obtained by multiplication by 2 ; If the number can be obtained by multiplication by 3 ; Obtaining the number by adding 1 ; Return the minm operations to obtain n ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( int n ) { int dp [ n + 1 ] ; dp [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i ] = INT_MAX ; if ( i % 2 == 0 ) { int x = dp [ i / 2 ] ; if ( x + 1 < dp [ i ] ) { dp [ i ] = x + 1 ; } } if ( i % 3 == 0 ) { int x = dp [ i / 3 ] ; if ( x + 1 < dp [ i ] ) { dp [ i ] = x + 1 ; } } int x = dp [ i - 1 ] ; if ( x + 1 < dp [ i ] ) { dp [ i ] = x + 1 ; } } return dp [ n ] ; } int main ( ) { int n = 15 ; cout << minOperations ( n ) ; return 0 ; }
Longest Reverse Bitonic Sequence | C ++ program to find the longest Reverse bitonic Subsequence ; Function to return the length of the Longest Reverse Bitonic Subsequence in the array ; Allocate memory for LIS [ ] and initialize LIS values as 1 for all indexes ; Compute LIS values from left to right for every index ; Initialize LDS for all indexes as 1 ; Compute LDS values for every index from right to left ; Find the maximum value of lis [ i ] + lds [ i ] - 1 in the array ; Return the maximum ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ReverseBitonic ( int arr [ ] , int N ) { int i , j ; int lds [ N ] ; for ( i = 0 ; i < N ; i ++ ) { lds [ i ] = 1 ; } for ( i = 1 ; i < N ; i ++ ) { for ( j = 0 ; j < i ; j ++ ) { if ( arr [ i ] < arr [ j ] && lds [ i ] < lds [ j ] + 1 ) { lds [ i ] = lds [ j ] + 1 ; } } } int lis [ N ] ; for ( i = 0 ; i < N ; i ++ ) { lis [ i ] = 1 ; } for ( i = N - 2 ; i >= 0 ; i -- ) { for ( j = N - 1 ; j > i ; j -- ) { if ( arr [ i ] < arr [ j ] && lis [ i ] < lis [ j ] + 1 ) { lis [ i ] = lis [ j ] + 1 ; } } } int max = lis [ 0 ] + lds [ 0 ] - 1 ; for ( i = 1 ; i < N ; i ++ ) { if ( lis [ i ] + lds [ i ] - 1 > max ) { max = lis [ i ] + lds [ i ] - 1 ; } } return max ; } int main ( ) { int arr [ ] = { 0 , 8 , 4 , 12 , 2 , 10 , 6 , 14 , 1 , 9 , 5 , 13 , 3 , 11 , 7 , 15 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " Length ▁ of ▁ LBS ▁ is ▁ % d STRNEWLINE " , ReverseBitonic ( arr , N ) ) ; return 0 ; }
Maze With N doors and 1 Key | C ++ implementation of the approach ; Recursive function to check whether there is a path from the top left cell to the bottom right cell of the maze ; Check whether the current cell is within the maze ; If key is required to move further ; If the key hasn 't been used before ; If current cell is the destination ; Either go down or right ; Key has been used before ; If current cell is the destination ; Either go down or right ; Driver code ; If there is a path from the cell ( 0 , 0 )
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findPath ( vector < vector < int > > maze , int xpos , int ypos , bool key ) { if ( xpos < 0 || xpos >= maze . size ( ) || ypos < 0 || ypos >= maze . size ( ) ) return false ; if ( maze [ xpos ] [ ypos ] == '1' ) { if ( key == true ) if ( xpos == maze . size ( ) - 1 && ypos == maze . size ( ) - 1 ) return true ; return findPath ( maze , xpos + 1 , ypos , false ) || findPath ( maze , xpos , ypos + 1 , false ) ; return false ; } if ( xpos == maze . size ( ) - 1 && ypos == maze . size ( ) - 1 ) return true ; return findPath ( maze , xpos + 1 , ypos , key ) || findPath ( maze , xpos , ypos + 1 , key ) ; } bool mazeProb ( vector < vector < int > > maze , int xpos , int ypos ) { bool key = true ; if ( findPath ( maze , xpos , ypos , key ) ) return true ; return false ; } int main ( ) { vector < vector < int > > maze = { { '0' , '0' , '1' } , { '1' , '0' , '1' } , { '1' , '1' , '0' } } ; int n = maze . size ( ) ; if ( mazeProb ( maze , 0 , 0 ) ) cout << " Yes " ; else cout << " No " ; }
Minimum cost to reach end of array array when a maximum jump of K index is allowed | C ++ implementation of the approach ; Function to return the minimum cost to reach the last index ; If we reach the last index ; Already visited state ; Initially maximum ; Visit all possible reachable index ; If inside range ; We cannot move any further ; Memoize ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int FindMinimumCost ( int ind , int a [ ] , int n , int k , int dp [ ] ) { if ( ind == ( n - 1 ) ) return 0 ; else if ( dp [ ind ] != -1 ) return dp [ ind ] ; else { int ans = INT_MAX ; for ( int i = 1 ; i <= k ; i ++ ) { if ( ind + i < n ) ans = min ( ans , abs ( a [ ind + i ] - a [ ind ] ) + FindMinimumCost ( ind + i , a , n , k , dp ) ) ; else break ; } return dp [ ind ] = ans ; } } int main ( ) { int a [ ] = { 10 , 30 , 40 , 50 , 20 } ; int k = 3 ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int dp [ n ] ; memset ( dp , -1 , sizeof dp ) ; cout << FindMinimumCost ( 0 , a , n , k , dp ) ; return 0 ; }
Sum of all even factors of numbers in the range [ l , r ] | C ++ implementation of the approach ; Function to calculate the prefix sum of all the even factors ; Add i to all the multiples of i ; Update the prefix sum ; Function to return the sum of all the even factors of the numbers in the given range ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE const int MAX = 100000 ; ll prefix [ MAX ] ; void sieve_modified ( ) { for ( int i = 2 ; i < MAX ; i += 2 ) { for ( int j = i ; j < MAX ; j += i ) prefix [ j ] += i ; } for ( int i = 1 ; i < MAX ; i ++ ) prefix [ i ] += prefix [ i - 1 ] ; } ll sumEvenFactors ( int L , int R ) { return ( prefix [ R ] - prefix [ L - 1 ] ) ; } int main ( ) { sieve_modified ( ) ; int l = 6 , r = 10 ; cout << sumEvenFactors ( l , r ) ; return 0 ; }
Count of numbers between range having only non | C ++ implementation of the approach ; states - position , sum , rem , tight sum can have values upto 162 , if we are dealing with numbers upto 10 ^ 18 when all 18 digits are 9 , then sum is 18 * 9 = 162 ; n is the sum of digits and number should be divisible by m ; Function to return the count of required numbers from 0 to num ; Last position ; If this result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is 1 , means number has already become smaller so we can place any digit , otherwise num [ pos ] ; If the current digit is zero and nonz is 1 , we can 't place it ; Function to convert x into its digit vector and uses count ( ) function to return the required count ; Initialize dp ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int M = 20 ; int dp [ M ] [ 165 ] [ M ] [ 2 ] ; int n , m ; int count ( int pos , int sum , int rem , int tight , int nonz , vector < int > num ) { if ( pos == num . size ( ) ) { if ( rem == 0 && sum == n ) return 1 ; return 0 ; } if ( dp [ pos ] [ sum ] [ rem ] [ tight ] != -1 ) return dp [ pos ] [ sum ] [ rem ] [ tight ] ; int ans = 0 ; int limit = ( tight ? 9 : num [ pos ] ) ; for ( int d = 0 ; d <= limit ; d ++ ) { if ( d == 0 && nonz ) continue ; int currSum = sum + d ; int currRem = ( rem * 10 + d ) % m ; int currF = tight || ( d < num [ pos ] ) ; ans += count ( pos + 1 , currSum , currRem , currF , nonz d , num ) ; } return dp [ pos ] [ sum ] [ rem ] [ tight ] = ans ; } int solve ( int x ) { vector < int > num ; while ( x ) { num . push_back ( x % 10 ) ; x /= 10 ; } reverse ( num . begin ( ) , num . end ( ) ) ; memset ( dp , -1 , sizeof ( dp ) ) ; return count ( 0 , 0 , 0 , 0 , 0 , num ) ; } int main ( ) { int L = 1 , R = 100 ; n = 8 , m = 2 ; cout << solve ( R ) - solve ( L ) ; return 0 ; }
Count of Numbers in Range where the number does not contain more than K non zero digits | CPP Program to find the count of numbers in a range where the number does not contain more than K non zero digits ; states - position , count , tight ; K is the number of non zero digits ; This function returns the count of required numbers from 0 to num ; Last position ; If count of non zero digits is less than or equal to K ; If this result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is 1 , means number has already become smaller so we can place any digit , otherwise num [ pos ] ; If the current digit is nonzero increment currCnt ; At this position , number becomes smaller ; Next recursive call ; This function converts a number into its digit vector and uses above function to compute the answer ; Initialize dp ; Driver Code to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int M = 20 ; int dp [ M ] [ M ] [ 2 ] ; int K ; int countInRangeUtil ( int pos , int cnt , int tight , vector < int > num ) { if ( pos == num . size ( ) ) { if ( cnt <= K ) return 1 ; return 0 ; } if ( dp [ pos ] [ cnt ] [ tight ] != -1 ) return dp [ pos ] [ cnt ] [ tight ] ; int ans = 0 ; int limit = ( tight ? 9 : num [ pos ] ) ; for ( int dig = 0 ; dig <= limit ; dig ++ ) { int currCnt = cnt ; if ( dig != 0 ) currCnt ++ ; int currTight = tight ; if ( dig < num [ pos ] ) currTight = 1 ; ans += countInRangeUtil ( pos + 1 , currCnt , currTight , num ) ; } return dp [ pos ] [ cnt ] [ tight ] = ans ; } int countInRange ( int x ) { vector < int > num ; while ( x ) { num . push_back ( x % 10 ) ; x /= 10 ; } reverse ( num . begin ( ) , num . end ( ) ) ; memset ( dp , -1 , sizeof ( dp ) ) ; return countInRangeUtil ( 0 , 0 , 0 , num ) ; } int main ( ) { int L = 1 , R = 1000 ; K = 3 ; cout << countInRange ( R ) - countInRange ( L - 1 ) << endl ; L = 9995 , R = 10005 , K = 2 ; cout << countInRange ( R ) - countInRange ( L - 1 ) << endl ; return 0 ; }
Balanced expressions such that given positions have opening brackets | Set 2 | C ++ implementation of above approach using memoization ; Function to find Number of proper bracket expressions ; If open - closed brackets < 0 ; If index reaches the end of expression ; If brackets are balanced ; If already stored in dp ; If the current index has assigned open bracket ; Move forward increasing the length of open brackets ; Move forward by inserting open as well as closed brackets on that index ; return the answer ; Driver Code ; DP array to precompute the answer ; Open brackets at position 1 ; Calling the find function to calculate the answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  1000 NEW_LINE int find ( int index , int openbrk , int n , int dp [ N ] [ N ] , int adj [ ] ) { if ( openbrk < 0 ) return 0 ; if ( index == n ) { if ( openbrk == 0 ) return 1 ; else return 0 ; } if ( dp [ index ] [ openbrk ] != -1 ) return dp [ index ] [ openbrk ] ; if ( adj [ index ] == 1 ) { dp [ index ] [ openbrk ] = find ( index + 1 , openbrk + 1 , n , dp , adj ) ; } else { dp [ index ] [ openbrk ] = find ( index + 1 , openbrk + 1 , n , dp , adj ) + find ( index + 1 , openbrk - 1 , n , dp , adj ) ; } return dp [ index ] [ openbrk ] ; } int main ( ) { int dp [ N ] [ N ] ; int n = 2 ; memset ( dp , -1 , sizeof ( dp ) ) ; int adj [ 4 ] = { 1 , 0 , 0 , 0 } ; cout << find ( 0 , 0 , 2 * n , dp , adj ) << endl ; return 0 ; }
Maximize array elements upto given number | CPP code to find maximum value of number obtained by using array elements recursively . ; Utility function to find maximum possible value ; If entire array is traversed , then compare current value in num to overall maximum obtained so far . ; Case 1 : Subtract current element from value so far if result is greater than or equal to zero . ; Case 2 : Add current element to value so far if result is less than or equal to maxLimit . ; Function to find maximum possible value that can be obtained using array elements and given number . ; variable to store maximum value that can be obtained . ; variable to store current index position . ; call to utility function to find maximum possible value that can be obtained . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMaxValUtil ( int arr [ ] , int n , int num , int maxLimit , int ind , int & ans ) { if ( ind == n ) { ans = max ( ans , num ) ; return ; } if ( num - arr [ ind ] >= 0 ) { findMaxValUtil ( arr , n , num - arr [ ind ] , maxLimit , ind + 1 , ans ) ; } if ( num + arr [ ind ] <= maxLimit ) { findMaxValUtil ( arr , n , num + arr [ ind ] , maxLimit , ind + 1 , ans ) ; } } int findMaxVal ( int arr [ ] , int n , int num , int maxLimit ) { int ans = 0 ; int ind = 0 ; findMaxValUtil ( arr , n , num , maxLimit , ind , ans ) ; return ans ; } int main ( ) { int num = 1 ; int arr [ ] = { 3 , 10 , 6 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int maxLimit = 15 ; cout << findMaxVal ( arr , n , num , maxLimit ) ; return 0 ; }
Print equal sum sets of array ( Partition problem ) | Set 1 | CPP program to print equal sum two subsets of an array if it can be partitioned into subsets . ; / Function to print the equal sum sets of the array . ; / Print set 1. ; / Print set 2. ; / Utility function to find the sets of the array which / have equal sum . ; / If entire array is traversed , compare both the sums . ; / If sums are equal print both sets and return / true to show sets are found . ; / If sums are not equal then return sets are not / found . ; / Add current element to set 1. ; / Recursive call after adding current element to / set 1. ; / If this inclusion results in equal sum sets / partition then return true to show desired sets are / found . ; / If not then backtrack by removing current element / from set1 and include it in set 2. ; / Recursive call after including current element to / set 2. ; / Return true if array arr can be partitioned / into two equal sum sets or not . ; / Calculate sum of elements in array . ; / If sum is odd then array cannot be / partitioned . ; / Declare vectors to store both the sets . ; / Find both the sets . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSets ( vector < int > set1 , vector < int > set2 ) { int i ; for ( i = 0 ; i < set1 . size ( ) ; i ++ ) { cout << set1 [ i ] << " ▁ " ; } cout << " STRNEWLINE " ; for ( i = 0 ; i < set2 . size ( ) ; i ++ ) { cout << set2 [ i ] << " ▁ " ; } } bool findSets ( int arr [ ] , int n , vector < int > & set1 , vector < int > & set2 , int sum1 , int sum2 , int pos ) { if ( pos == n ) { if ( sum1 == sum2 ) { printSets ( set1 , set2 ) ; return true ; } else return false ; } set1 . push_back ( arr [ pos ] ) ; bool res = findSets ( arr , n , set1 , set2 , sum1 + arr [ pos ] , sum2 , pos + 1 ) ; if ( res ) return res ; set1 . pop_back ( ) ; set2 . push_back ( arr [ pos ] ) ; res = findSets ( arr , n , set1 , set2 , sum1 , sum2 + arr [ pos ] , pos + 1 ) ; if ( res == false ) if ( ! set2 . empty ( ) ) set2 . pop_back ( ) ; return res ; } bool isPartitionPoss ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( sum % 2 != 0 ) return false ; vector < int > set1 , set2 ; return findSets ( arr , n , set1 , set2 , 0 , 0 , 0 ) ; } int main ( ) { int arr [ ] = { 5 , 5 , 1 , 11 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( ! isPartitionPoss ( arr , n ) ) { cout << " - 1" ; } return 0 ; }
Maximum subarray sum in O ( n ) using prefix sum | C ++ program to find out maximum subarray sum in linear time using prefix sum . ; Function to compute maximum subarray sum in linear time . ; Initialize minimum prefix sum to 0. ; Initialize maximum subarray sum so far to - infinity . ; Initialize and compute the prefix sum array . ; loop through the array , keep track of minimum prefix sum so far and maximum subarray sum . ; Driver Program ; Test case 1 ; Test case 2
#include <iostream> NEW_LINE #include <limits> NEW_LINE using namespace std ; int maximumSumSubarray ( int arr [ ] , int n ) { int min_prefix_sum = 0 ; int res = numeric_limits < int > :: min ( ) ; int prefix_sum [ n ] ; prefix_sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { res = max ( res , prefix_sum [ i ] - min_prefix_sum ) ; min_prefix_sum = min ( min_prefix_sum , prefix_sum [ i ] ) ; } return res ; } int main ( ) { int arr1 [ ] = { -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 } ; int n1 = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; cout << maximumSumSubarray ( arr1 , n1 ) << endl ; int arr2 [ ] = { 4 , -8 , 9 , -4 , 1 , -8 , -1 , 6 } ; int n2 = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; cout << maximumSumSubarray ( arr2 , n2 ) ; return 0 ; }
Counting numbers of n digits that are monotone | CPP program to count numbers of n digits that are monotone . ; Considering all possible digits as { 1 , 2 , 3 , . .9 } ; DP [ i ] [ j ] is going to store monotone numbers of length i + 1 considering j + 1 digits . ; Unit length numbers ; Single digit numbers ; Filling rest of the entries in bottom up manner . ; Driver code .
#include <cstring> NEW_LINE #include <iostream> NEW_LINE int static const DP_s = 9 ; int getNumMonotone ( int len ) { int DP [ len ] [ DP_s ] ; memset ( DP , 0 , sizeof ( DP ) ) ; for ( int i = 0 ; i < DP_s ; ++ i ) DP [ 0 ] [ i ] = i + 1 ; for ( int i = 0 ; i < len ; ++ i ) DP [ i ] [ 0 ] = 1 ; for ( int i = 1 ; i < len ; ++ i ) for ( int j = 1 ; j < DP_s ; ++ j ) DP [ i ] [ j ] = DP [ i - 1 ] [ j ] + DP [ i ] [ j - 1 ] ; return DP [ len - 1 ] [ DP_s - 1 ] ; } int main ( ) { std :: cout << getNumMonotone ( 10 ) ; return 0 ; }
Newman | C ++ program to find the n - th element of Newman - Conway Sequence ; Function to find the n - th element ; Declare array to store sequence ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sequence ( int n ) { int f [ n + 1 ] ; int i ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; f [ 2 ] = 1 ; for ( i = 3 ; i <= n ; i ++ ) f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ; return f [ n ] ; } int main ( ) { int n = 10 ; cout << sequence ( n ) ; return 0 ; }
Maximum product of an increasing subsequence | Dynamic programming C ++ implementation of maximum product of an increasing subsequence ; Returns product of maximum product increasing subsequence . ; Initialize MPIS values ; Compute optimized MPIS values considering every element as ending element of sequence ; Pick maximum of all product values ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; ll lis ( ll arr [ ] , ll n ) { ll mpis [ n ] ; for ( int i = 0 ; i < n ; i ++ ) mpis [ i ] = arr [ i ] ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && mpis [ i ] < ( mpis [ j ] * arr [ i ] ) ) mpis [ i ] = mpis [ j ] * arr [ i ] ; return * max_element ( mpis , mpis + n ) ; } int main ( ) { ll arr [ ] = { 3 , 100 , 4 , 5 , 150 , 6 } ; ll n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " % lld " , lis ( arr , n ) ) ; return 0 ; }
Hosoya 's Triangle | CPP Program to print Hosoya 's triangle of height n. ; Base case ; Recursive step ; Print the Hosoya triangle of height n . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Hosoya ( int n , int m ) { if ( ( n == 0 && m == 0 ) || ( n == 1 && m == 0 ) || ( n == 1 && m == 1 ) || ( n == 2 && m == 1 ) ) return 1 ; if ( n > m ) return Hosoya ( n - 1 , m ) + Hosoya ( n - 2 , m ) ; else if ( m == n ) return Hosoya ( n - 1 , m - 1 ) + Hosoya ( n - 2 , m - 2 ) ; else return 0 ; } void printHosoya ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) cout << Hosoya ( i , j ) << " ▁ " ; cout << endl ; } } int main ( ) { int n = 5 ; printHosoya ( n ) ; return 0 ; }
Eulerian Number | CPP Program to find Eulerian number A ( n , m ) ; Return euleriannumber A ( n , m ) ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int eulerian ( int n , int m ) { if ( m >= n n == 0 ) return 0 ; if ( m == 0 ) return 1 ; return ( n - m ) * eulerian ( n - 1 , m - 1 ) + ( m + 1 ) * eulerian ( n - 1 , m ) ; } int main ( ) { int n = 3 , m = 1 ; cout << eulerian ( n , m ) << endl ; return 0 ; }