codeparrot/xlcost-text-to-code huggingface dataset:
Number of Co | C ++ program to count the pairs whose sum of digits is co - prime ; Function to find the elements after doing the sum of digits ; Traverse from a to b ; Find the sum of the digits of the elements in the given range one by one ; Function to count the co - prime pairs ; Function to make the pairs by doing the sum of digits ; Count pairs that are co - primes ; Driver code ; Function to count the pairs
#include <bits/stdc++.h> NEW_LINE using namespace std ; int makePairs ( vector < int > & pairs , int a , int b ) { for ( int i = a ; i <= b ; i ++ ) { int sumOfDigits = 0 , k = i ; while ( k > 0 ) { sumOfDigits += k % 10 ; k /= 10 ; } if ( sumOfDigits <= 162 ) pairs . push_back ( sumOfDigits ) ; } } int countCoPrime ( int a , int b ) { vector < int > pairs ; makePairs ( pairs , a , b ) ; int count = 0 ; for ( int i = 0 ; i < pairs . size ( ) ; i ++ ) for ( int j = i + 1 ; j < pairs . size ( ) ; j ++ ) if ( __gcd ( pairs [ i ] , pairs [ j ] ) == 1 ) count ++ ; return count ; } int main ( ) { int a = 12 , b = 15 ; cout << countCoPrime ( a , b ) ; return 0 ; }
Number of Co | C ++ program to count the pairs whose sum of digits is co - prime ; Recursive function to return the frequency of numbers having their sum of digits i ; Returns 1 or 0 ; Returns value of the dp if already stored ; Loop from digit 0 to 9 ; To change the tight to 1 ; Calling the recursive function to find the frequency ; Function to find out frequency of numbers from 1 to N having their sum of digits from 1 to 162 and store them in array ; Number to string conversion ; Calling the recursive function and pushing it into array ; Function to find the pairs ; Calling the formArray function of a - 1 numbers ; Calling the formArray function of b numbers ; Subtracting the frequency of higher number array with lower number array and thus finding the range of numbers from a to b having sum from 1 to 162 ; To find out total number of pairs which are co - prime ; Driver code ; Function to count the pairs
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; ll recursive ( ll idx , ll sum , ll tight , string st , ll dp [ 20 ] [ 2 ] [ 166 ] , ll num ) { if ( idx == num ) return sum == 0 ; if ( dp [ idx ] [ tight ] [ sum ] != -1 ) return dp [ idx ] [ tight ] [ sum ] ; bool newTight ; ll ans = 0 ; ll d ; for ( d = 0 ; d < 10 ; ++ d ) { newTight = false ; if ( tight && st [ idx ] - '0' < d ) continue ; if ( tight && st [ idx ] - '0' == d ) newTight = true ; if ( sum >= d ) ans += recursive ( idx + 1 , sum - d , newTight , st , dp , num ) ; } return dp [ idx ] [ tight ] [ sum ] = ans ; } vector < ll > formArray ( ll N ) { ll dp [ 20 ] [ 2 ] [ 166 ] ; memset ( dp , -1 , sizeof dp ) ; ostringstream x ; x << N ; string st = x . str ( ) ; ll num = st . size ( ) ; vector < ll > arr ; for ( int i = 1 ; i <= 162 ; ++ i ) { arr . push_back ( recursive ( 0 , i , 1 , st , dp , num ) ) ; } return arr ; } ll findPair ( ll a , ll b ) { vector < ll > arr_smaller = formArray ( a - 1 ) ; vector < ll > arr_greater = formArray ( b ) ; for ( int i = 0 ; i < arr_greater . size ( ) ; ++ i ) arr_greater [ i ] -= arr_smaller [ i ] ; int ans = 0 ; for ( int i = 1 ; i <= 162 ; ++ i ) { for ( int j = i + 1 ; j <= 162 ; ++ j ) { if ( __gcd ( i , j ) == 1 ) ans = ( ans + arr_greater [ i - 1 ] * arr_greater [ j - 1 ] ) ; } } return ans ; } int main ( ) { ll a = 12 , b = 15 ; cout << findPair ( a , b ) ; return 0 ; }
Semiperfect Number | C ++ program to check if the number is semi - perfect or not ; code to find all the factors of the number excluding the number itself ; vector to store the factors ; note that this loop runs till sqrt ( n ) ; if the value of i is a factor ; condition to check the divisor is not the number itself ; return the vector ; Function to check if the number is semi - perfect or not ; find the divisors ; sorting the vector ; subset to check if no is semiperfect ; initialising 1 st column to true ; initializing 1 st row except zero position to 0 ; loop to find whether the number is semiperfect ; calculation to check if the number can be made by summation of divisors ; if not possible to make the number by any combination of divisors ; driver code to check if possible
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > factors ( int n ) { vector < int > v ; v . push_back ( 1 ) ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v . push_back ( i ) ; if ( n / i != i ) { v . push_back ( n / i ) ; } } } return v ; } bool check ( int n ) { vector < int > v ; v = factors ( n ) ; sort ( v . begin ( ) , v . end ( ) ) ; int r = v . size ( ) ; bool subset [ r + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= r ; i ++ ) subset [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= n ; i ++ ) subset [ 0 ] [ i ] = false ; for ( int i = 1 ; i <= r ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( j < v [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; else { subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - v [ i - 1 ] ] ; } } } if ( ( subset [ r ] [ n ] ) == 0 ) return false ; else return true ; } int main ( ) { int n = 40 ; if ( check ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Number of ways to represent a number as sum of k fibonacci numbers | C ++ implementation of above approach ; to store fibonacci numbers 42 second number in fibonacci series largest possible integer ; Function to generate fibonacci series ; Recursive function to return the number of ways ; base condition ; for recursive function call ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fib [ 43 ] = { 0 } ; void fibonacci ( ) { fib [ 0 ] = 1 ; fib [ 1 ] = 2 ; for ( int i = 2 ; i < 43 ; i ++ ) fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } int rec ( int x , int y , int last ) { if ( y == 0 ) { if ( x == 0 ) return 1 ; return 0 ; } int sum = 0 ; for ( int i = last ; i >= 0 and fib [ i ] * y >= x ; i -- ) { if ( fib [ i ] > x ) continue ; sum += rec ( x - fib [ i ] , y - 1 , i ) ; } return sum ; } int main ( ) { fibonacci ( ) ; int n = 13 , k = 3 ; cout << " Possible ▁ ways ▁ are : ▁ " << rec ( n , k , 42 ) ; return 0 ; }
Minimum cost to reach the top of the floor by climbing stairs | C ++ program to find the minimum cost required to reach the n - th floor ; function to find the minimum cost to reach N - th floor ; declare an array ; base case ; initially to climb till 0 - th or 1 th stair ; iterate for finding the cost ; return the minimum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumCost ( int cost [ ] , int n ) { int dp [ n ] ; if ( n == 1 ) return cost [ 0 ] ; dp [ 0 ] = cost [ 0 ] ; dp [ 1 ] = cost [ 1 ] ; for ( int i = 2 ; i < n ; i ++ ) { dp [ i ] = min ( dp [ i - 1 ] , dp [ i - 2 ] ) + cost [ i ] ; } return min ( dp [ n - 2 ] , dp [ n - 1 ] ) ; } int main ( ) { int a [ ] = { 16 , 19 , 10 , 12 , 18 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << minimumCost ( a , n ) ; return 0 ; }
Minimum cost to reach the top of the floor by climbing stairs | C ++ program to find the minimum cost required to reach the n - th floor space - optimized solution ; function to find the minimum cost to reach N - th floor ; traverse till N - th stair ; update the last two stairs value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumCost ( int cost [ ] , int n ) { int dp1 = 0 , dp2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int dp0 = cost [ i ] + min ( dp1 , dp2 ) ; dp2 = dp1 ; dp1 = dp0 ; } return min ( dp1 , dp2 ) ; } int main ( ) { int a [ ] = { 2 , 5 , 3 , 1 , 7 , 3 , 4 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << minimumCost ( a , n ) ; return 0 ; }
Sudo Placement [ 1.5 ] | Wolfish | C ++ program for SP - Wolfish ; Function to find the maxCost of path from ( n - 1 , n - 1 ) to ( 0 , 0 ) ; base condition ; reaches the point ; if the state has been visited previously ; i + j ; check if it is a power of 2 , then only move diagonally ; if not a power of 2 then move side - wise ; Function to return the maximum cost ; calling dp function to get the answer ; Driver Code ; Function calling to get the answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int size = 1000 ; int maxCost ( int a [ ] [ size ] , int m , int n , int dp [ ] [ size ] ) { if ( n < 0 m < 0 ) return -1e9 ; else if ( m == 0 && n == 0 ) return 0 ; else if ( dp [ m ] [ n ] != -1 ) return dp [ m ] [ n ] ; else { int num = m + n ; if ( ( num & ( num - 1 ) ) == 0 ) return dp [ m ] [ n ] = a [ m ] [ n ] + maxCost ( a , m - 1 , n - 1 , dp ) ; else return dp [ m ] [ n ] = ( a [ m ] [ n ] + max ( maxCost ( a , m - 1 , n , dp ) , maxCost ( a , m , n - 1 , dp ) ) ) ; } } int answer ( int a [ ] [ size ] , int n ) { int dp [ size ] [ size ] ; memset ( dp , -1 , sizeof dp ) ; return maxCost ( a , n - 1 , n - 1 , dp ) ; } int main ( ) { int a [ ] [ size ] = { { 1 , 2 , 3 , 1 } , { 4 , 5 , 6 , 1 } , { 7 , 8 , 9 , 1 } , { 1 , 1 , 1 , 1 } } ; int n = 4 ; cout << answer ( a , n ) ; return 0 ; }
Edit distance and LCS ( Longest Common Subsequence ) | CPP program to find Edit Distance ( when only twooperations are allowed , insert and delete ) using LCS . ; Find LCS ; Edit distance is delete operations + insert operations . ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int editDistanceWith2Ops ( string & X , string & Y ) { int m = X . length ( ) , n = Y . length ( ) ; int L [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i ] [ j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; else L [ i ] [ j ] = max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } int lcs = L [ m ] [ n ] ; return ( m - lcs ) + ( n - lcs ) ; } int main ( ) { string X = " abc " , Y = " acd " ; cout << editDistanceWith2Ops ( X , Y ) ; return 0 ; }
Longest Common Subsequence | DP using Memoization | A Naive C ++ recursive implementation of LCS of two strings ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Driver Code ; Find the length of string
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lcs ( string X , string Y , int m , int n ) { if ( m == 0 n == 0 ) return 0 ; if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; else return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ; } int main ( ) { string X = " AGGTAB " ; string Y = " GXTXAYB " ; int m = X . length ( ) ; int n = Y . length ( ) ; cout << " Length ▁ of ▁ LCS : ▁ " << lcs ( X , Y , m , n ) ; return 0 ; }
Number of different cyclic paths of length N in a tetrahedron | C ++ program count total number of paths to reach B from B ; Function to count the number of steps in a tetrahedron ; initially coming to B is B -> B ; cannot reach A , D or C ; iterate for all steps ; recurrence relation ; memoize previous values ; returns steps ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int countPaths ( int n ) { int zB = 1 ; int zADC = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int nzB = zADC * 3 ; int nzADC = ( zADC * 2 + zB ) ; zB = nzB ; zADC = nzADC ; } return zB ; } int main ( ) { int n = 3 ; cout << countPaths ( n ) ; return 0 ; }
Maximum sub | C ++ implementation to find the maximum area sub - matrix having count of 1 ' s ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; function to find the length of longest subarray having count of 1 ' s ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; unordered_map ' um ' implemented as hash table ; traverse the given array ; accumulating sum ; when subarray starts form index '0' ; make an entry for ' sum ' if it is not present in ' um ' ; check if ' sum - 1' is present in ' um ' or not ; update ' start ' , ' finish ' and maxLength ; required maximum length ; function to find the maximum area sub - matrix having count of 1 ' s ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; variables to store final and intermediate results ; set the left column ; Initialize all elements of temp as 0 ; Set the right column for the left column set by outer loop ; Calculate sum between current left and right for every row ' i ' , consider '0' as ' - 1' ; function to set the ' start ' and ' finish ' variables having index values of temp [ ] which contains the longest subarray of temp [ ] having count of 1 ' s ▁ ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; Compare with maximum area so far and accordingly update the final variables ; Print final values ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define SIZE  10 NEW_LINE int lenOfLongSubarr ( int arr [ ] , int n , int & start , int & finish ) { unordered_map < int , int > um ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == 1 ) { start = 0 ; finish = i ; maxLen = i + 1 ; } else if ( um . find ( sum ) == um . end ( ) ) um [ sum ] = i ; if ( um . find ( sum - 1 ) != um . end ( ) ) { if ( maxLen < ( i - um [ sum - 1 ] ) ) start = um [ sum - 1 ] + 1 ; finish = i ; maxLen = i - um [ sum - 1 ] ; } } return maxLen ; } void largestSubmatrix ( int mat [ SIZE ] [ SIZE ] , int n ) { int finalLeft , finalRight , finalTop , finalBottom ; int temp [ n ] , maxArea = 0 , len , start , finish ; for ( int left = 0 ; left < n ; left ++ ) { memset ( temp , 0 , sizeof ( temp ) ) ; for ( int right = left ; right < n ; right ++ ) { for ( int i = 0 ; i < n ; ++ i ) temp [ i ] += mat [ i ] [ right ] == 0 ? -1 : 1 ; len = lenOfLongSubarr ( temp , n , start , finish ) ; if ( ( len != 0 ) && ( maxArea < ( finish - start + 1 ) * ( right - left + 1 ) ) ) { finalLeft = left ; finalRight = right ; finalTop = start ; finalBottom = finish ; maxArea = ( finish - start + 1 ) * ( right - left + 1 ) ; } } } cout << " ( Top , ▁ Left ) : ▁ ( " << finalTop << " , ▁ " << finalLeft << " ) STRNEWLINE " ; cout << " ( Bottom , ▁ Right ) : ▁ ( " << finalBottom << " , ▁ " << finalRight << " ) STRNEWLINE " ; cout << " Maximum ▁ area : ▁ " << maxArea ; } int main ( ) { int mat [ SIZE ] [ SIZE ] = { { 1 , 0 , 0 , 1 } , { 0 , 1 , 1 , 1 } , { 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 } } ; int n = 4 ; largestSubmatrix ( mat , n ) ; return 0 ; }
Minimum steps to reach target by a Knight | Set 2 | C ++ code for minimum steps for a knight to reach target position ; initializing the matrix . ; if knight is on the target position return 0. ; if already calculated then return that value . Taking absolute difference . ; there will be two distinct positions from the knight towards a target . if the target is in same row or column as of knight than there can be four positions towards the target but in that two would be the same and the other two would be the same . ; ( x1 , y1 ) and ( x2 , y2 ) are two positions . these can be different according to situation . From position of knight , the chess board can be divided into four blocks i . e . . N - E , E - S , S - W , W - N . ; ans will be , 1 + minimum of steps required from ( x1 , y1 ) and ( x2 , y2 ) . ; exchanging the coordinates x with y of both knight and target will result in same ans . ; Driver Code ; size of chess board n * n ; ( x , y ) coordinate of the knight . ( tx , ty ) coordinate of the target position . ; ( Exception ) these are the four corner points for which the minimum steps is 4. ; dp [ a ] [ b ] , here a , b is the difference of x & tx and y & ty respectively .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 8 ] [ 8 ] = { 0 } ; int getsteps ( int x , int y , int tx , int ty ) { if ( x == tx && y == ty ) return dp [ 0 ] [ 0 ] ; else { if ( dp [ abs ( x - tx ) ] [ abs ( y - ty ) ] != 0 ) return dp [ abs ( x - tx ) ] [ abs ( y - ty ) ] ; else { int x1 , y1 , x2 , y2 ; if ( x <= tx ) { if ( y <= ty ) { x1 = x + 2 ; y1 = y + 1 ; x2 = x + 1 ; y2 = y + 2 ; } else { x1 = x + 2 ; y1 = y - 1 ; x2 = x + 1 ; y2 = y - 2 ; } } else { if ( y <= ty ) { x1 = x - 2 ; y1 = y + 1 ; x2 = x - 1 ; y2 = y + 2 ; } else { x1 = x - 2 ; y1 = y - 1 ; x2 = x - 1 ; y2 = y - 2 ; } } dp [ abs ( x - tx ) ] [ abs ( y - ty ) ] = min ( getsteps ( x1 , y1 , tx , ty ) , getsteps ( x2 , y2 , tx , ty ) ) + 1 ; dp [ abs ( y - ty ) ] [ abs ( x - tx ) ] = dp [ abs ( x - tx ) ] [ abs ( y - ty ) ] ; return dp [ abs ( x - tx ) ] [ abs ( y - ty ) ] ; } } } int main ( ) { int i , n , x , y , tx , ty , ans ; n = 100 ; x = 4 ; y = 5 ; tx = 1 ; ty = 1 ; if ( ( x == 1 && y == 1 && tx == 2 && ty == 2 ) || ( x == 2 && y == 2 && tx == 1 && ty == 1 ) ) ans = 4 ; else if ( ( x == 1 && y == n && tx == 2 && ty == n - 1 ) || ( x == 2 && y == n - 1 && tx == 1 && ty == n ) ) ans = 4 ; else if ( ( x == n && y == 1 && tx == n - 1 && ty == 2 ) || ( x == n - 1 && y == 2 && tx == n && ty == 1 ) ) ans = 4 ; else if ( ( x == n && y == n && tx == n - 1 && ty == n - 1 ) || ( x == n - 1 && y == n - 1 && tx == n && ty == n ) ) ans = 4 ; else { dp [ 1 ] [ 0 ] = 3 ; dp [ 0 ] [ 1 ] = 3 ; dp [ 1 ] [ 1 ] = 2 ; dp [ 2 ] [ 0 ] = 2 ; dp [ 0 ] [ 2 ] = 2 ; dp [ 2 ] [ 1 ] = 1 ; dp [ 1 ] [ 2 ] = 1 ; ans = getsteps ( x , y , tx , ty ) ; } cout << ans << endl ; return 0 ; }
Sequence Alignment problem | CPP program to implement sequence alignment problem . ; function to find out the minimum penalty ; table for storing optimal substructure answers ; initialising the table ; calculating the minimum penalty ; Reconstructing the solution int l = n + m ; maximum possible length ; Final answers for the respective strings ; Since we have assumed the answer to be n + m long , we need to remove the extra gaps in the starting id represents the index from which the arrays xans , yans are useful ; Printing the final answer ; Driver code ; input strings ; initialising penalties of different types ; calling the function to calculate the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getMinimumPenalty ( string x , string y , int pxy , int pgap ) { int dp [ m + 1 ] [ n + 1 ] = { 0 } ; for ( i = 0 ; i <= ( n + m ) ; i ++ ) { dp [ i ] [ 0 ] = i * pgap ; dp [ 0 ] [ i ] = i * pgap ; } for ( i = 1 ; i <= m ; i ++ ) { for ( j = 1 ; j <= n ; j ++ ) { if ( x [ i - 1 ] == y [ j - 1 ] ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ; } else { dp [ i ] [ j ] = min ( { dp [ i - 1 ] [ j - 1 ] + pxy , dp [ i - 1 ] [ j ] + pgap , dp [ i ] [ j - 1 ] + pgap } ) ; } } } i = m ; j = n ; int xpos = l ; int ypos = l ; int xans [ l + 1 ] , yans [ l + 1 ] ; while ( ! ( i == 0 j == 0 ) ) { if ( x [ i - 1 ] == y [ j - 1 ] ) { xans [ xpos -- ] = ( int ) x [ i - 1 ] ; yans [ ypos -- ] = ( int ) y [ j - 1 ] ; i -- ; j -- ; } else if ( dp [ i - 1 ] [ j - 1 ] + pxy == dp [ i ] [ j ] ) { xans [ xpos -- ] = ( int ) x [ i - 1 ] ; yans [ ypos -- ] = ( int ) y [ j - 1 ] ; i -- ; j -- ; } else if ( dp [ i - 1 ] [ j ] + pgap == dp [ i ] [ j ] ) { xans [ xpos -- ] = ( int ) x [ i - 1 ] ; yans [ ypos -- ] = ( int ) ' _ ' ; i -- ; } else if ( dp [ i ] [ j - 1 ] + pgap == dp [ i ] [ j ] ) { xans [ xpos -- ] = ( int ) ' _ ' ; yans [ ypos -- ] = ( int ) y [ j - 1 ] ; j -- ; } } while ( xpos > 0 ) { if ( i > 0 ) xans [ xpos -- ] = ( int ) x [ -- i ] ; else xans [ xpos -- ] = ( int ) ' _ ' ; } while ( ypos > 0 ) { if ( j > 0 ) yans [ ypos -- ] = ( int ) y [ -- j ] ; else yans [ ypos -- ] = ( int ) ' _ ' ; } int id = 1 ; for ( i = l ; i >= 1 ; i -- ) { if ( ( char ) yans [ i ] == ' _ ' && ( char ) xans [ i ] == ' _ ' ) { id = i + 1 ; break ; } } cout << " Minimum ▁ Penalty ▁ in ▁ aligning ▁ the ▁ genes ▁ = ▁ " ; cout << dp [ m ] [ n ] << " STRNEWLINE " ; cout << " The ▁ aligned ▁ genes ▁ are ▁ : STRNEWLINE " ; for ( i = id ; i <= l ; i ++ ) { cout << ( char ) xans [ i ] ; } cout << " STRNEWLINE " ; for ( i = id ; i <= l ; i ++ ) { cout << ( char ) yans [ i ] ; } return ; } int main ( ) { string gene1 = " AGGGCT " ; string gene2 = " AGGCA " ; int misMatchPenalty = 3 ; int gapPenalty = 2 ; getMinimumPenalty ( gene1 , gene2 , misMatchPenalty , gapPenalty ) ; return 0 ; }
Number of subsets with sum divisible by m | C ++ program which returns the Number of sub sequences ( or subsets ) which are divisible by m . ; Use Dynamic Programming to find sum of subsequences . ; Find sum pf array elements ; dp [ i ] [ j ] would be > 0 if arr [ 0. . i - 1 ] has a subsequence with sum equal to j . ; There is always sum equals zero ; Fill up the dp table ; Initialize the counter ; Check if the sum exists ; check sum is divisible by m ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumSubSequence ( vector < int > arr , int len , int m ) { int sum = 0 ; for ( auto x : arr ) sum += x ; vector < vector < int > > dp ( len + 1 , vector < int > ( sum + 1 , 0 ) ) ; for ( int i = 0 ; i <= len ; i ++ ) dp [ i ] [ 0 ] ++ ; for ( int i = 1 ; i <= len ; i ++ ) { dp [ i ] [ arr [ i - 1 ] ] ++ ; for ( int j = 1 ; j <= sum ; j ++ ) { if ( dp [ i - 1 ] [ j ] > 0 ) { dp [ i ] [ j ] ++ ; dp [ i ] [ j + arr [ i - 1 ] ] ++ ; } } } int count = 0 ; for ( int j = 1 ; j <= sum ; j ++ ) if ( dp [ len ] [ j ] > 0 ) if ( j % m == 0 ) count += dp [ len ] [ j ] ; return count ; } int main ( ) { vector < int > arr { 1 , 2 , 3 } ; int m = 3 ; int len = arr . size ( ) ; cout << sumSubSequence ( arr , len , m ) << endl ; return 0 ; }
Longest Decreasing Subsequence | CPP program to find the length of the longest decreasing subsequence ; Function that returns the length of the longest decreasing subsequence ; Initialize LDS with 1 for all index The minimum LDS starting with any element is always 1 ; Compute LDS from every index in bottom up manner ; Select the maximum of all the LDS values ; returns the length of the LDS ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lds ( int arr [ ] , int n ) { int lds [ n ] ; int i , j , max = 0 ; 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 ; for ( i = 0 ; i < n ; i ++ ) if ( max < lds [ i ] ) max = lds [ i ] ; return max ; } int main ( ) { int arr [ ] = { 15 , 27 , 14 , 38 , 63 , 55 , 46 , 65 , 85 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Length ▁ of ▁ LDS ▁ is ▁ " << lds ( arr , n ) ; return 0 ; }
Total number of decreasing paths in a matrix | CPP program to count number of decreasing path in a matrix ; Function that returns the number of decreasing paths from a cell ( i , j ) ; checking if already calculated ; all possible paths ; counts the total number of paths ; In all four allowed direction . ; new co - ordinates ; Checking if not going out of matrix and next cell value is less than current cell value . ; function that returns the answer ; Function that counts the total decreasing path in the matrix ; Initialising dp [ ] [ ] to - 1. ; Calculating number of decreasing path from each cell . ; Driver Code ; function call that returns the count of decreasing paths in a matrix
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100 NEW_LINE int CountDecreasingPathsCell ( int mat [ MAX ] [ MAX ] , int dp [ MAX ] [ MAX ] , int n , int x , int y ) { if ( dp [ x ] [ y ] != -1 ) return dp [ x ] [ y ] ; int delta [ 4 ] [ 2 ] = { { 0 , 1 } , { 1 , 0 } , { -1 , 0 } , { 0 , -1 } } ; int newx , newy ; int ans = 1 ; for ( int i = 0 ; i < 4 ; i ++ ) { newx = x + delta [ i ] [ 0 ] ; newy = y + delta [ i ] [ 1 ] ; if ( newx >= 0 && newx < n && newy >= 0 && newy < n && mat [ newx ] [ newy ] < mat [ x ] [ y ] ) { ans += CountDecreasingPathsCell ( mat , dp , n , newx , newy ) ; } } return dp [ x ] [ y ] = ans ; } int countDecreasingPathsMatrix ( int n , int mat [ MAX ] [ MAX ] ) { int dp [ MAX ] [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) dp [ i ] [ j ] = -1 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) sum += CountDecreasingPathsCell ( mat , dp , n , i , j ) ; return sum ; } int main ( ) { int n = 2 ; int mat [ MAX ] [ MAX ] = { { 1 , 2 } , { 1 , 3 } } ; cout << countDecreasingPathsMatrix ( n , mat ) << endl ; return 0 ; }