codeparrot/xlcost-text-to-code huggingface dataset:
A variation of Rat in a Maze : multiple steps or jumps allowed | JavaScript implementation of the approach ; Function to check whether the path exists ; Declaring and initializing CRF ( can reach from ) matrix ; Using the DP to fill CRF matrix in correct order ; If it is possible to get to a valid location from cell maze [ k ] [ j ] ; If it is possible to get to a valid location from cell maze [ j ] [ k ] ; If CRF [ 0 ] [ 0 ] is false it means we cannot reach the end of the maze at all ; Filling the solution matrix using CRF ; Get to a valid location from the current cell ; Utility function to print the contents of a 2 - D array ; Driver code ; If path exists ; Print the path
var MAX = 50 ; function hasPath ( maze , sol , N ) { for ( var i = 0 ; i < N ; i ++ ) for ( var j = 0 ; j < N ; j ++ ) sol [ i ] [ j ] = 0 ; var CRF = Array . from ( Array ( N ) , ( ) => Array ( N ) ) ; ; for ( var i = 0 ; i < N ; i ++ ) for ( var j = 0 ; j < N ; j ++ ) CRF [ i ] [ j ] = false ; CRF [ N - 1 ] [ N - 1 ] = true ; for ( var k = N - 1 ; k >= 0 ; k -- ) { for ( var j = k ; j >= 0 ; j -- ) { if ( ! ( k == N - 1 && j == N - 1 ) ) { for ( var a = 0 ; a <= maze [ k ] [ j ] ; a ++ ) { if ( ( j + a < N && CRF [ k ] [ j + a ] == true ) || ( k + a < N && CRF [ k + a ] [ j ] == true ) ) { CRF [ k ] [ j ] = true ; break ; } } for ( var a = 0 ; a <= maze [ j ] [ k ] ; a ++ ) { if ( ( k + a < N && CRF [ j ] [ k + a ] == true ) || ( j + a < N && CRF [ j + a ] [ k ] == true ) ) { CRF [ j ] [ k ] = true ; break ; } } } } } if ( CRF [ 0 ] [ 0 ] == false ) return false ; var i = 0 , j = 0 ; while ( ! ( i == N - 1 && j == N - 1 ) ) { sol [ i ] [ j ] = 1 ; if ( maze [ i ] [ j ] > 0 ) for ( var a = 1 ; a <= maze [ i ] [ j ] ; a ++ ) { if ( ( j + a < N && CRF [ i ] [ j + a ] == true ) ) { j = j + a ; break ; } else if ( ( i + a < N && CRF [ i + a ] [ j ] == true ) ) { i = i + a ; break ; } } } sol [ N - 1 ] [ N - 1 ] = 1 ; return true ; } function printMatrix ( sol , N ) { for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < N ; j ++ ) document . write ( sol [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } var maze = [ [ 2 , 2 , 1 , 1 , 0 ] , [ 0 , 0 , 3 , 0 , 0 ] , [ 1 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 2 , 0 , 1 ] , [ 0 , 0 , 3 , 0 , 0 ] ] ; var N = maze . length ; var sol = Array . from ( Array ( N ) , ( ) => Array ( MAX ) ) ; if ( hasPath ( maze , sol , N ) ) printMatrix ( sol , N ) ; else document . write ( " " ) ;
Check if it is possible to get back to 12 '0 clock only by adding or subtracting given seconds | Function to check all combinations ; Generate all power sets ; Check for every combination ; Store sum for all combinations ; * Check if jth bit in the counter is set If set then print jth element from set ; sum += a [ j ] ; if set then consider as ' + ' ; sum -= a [ j ] ; else consider as ' - ' ; If we can get back to 0 ; Driver code
function checkCombinations ( a , n ) { var pow_set_size = parseInt ( Math . pow ( 2 , n ) ) ; var counter , j ; for ( counter = 0 ; counter < pow_set_size ; counter ++ ) { var sum = 0 ; for ( j = 0 ; j < n ; j ++ ) { if ( ( counter & ( 1 << j ) ) != 0 ) else } if ( sum % ( 24 * 60 ) == 0 ) return true ; } return false ; } var a = [ 60 , 60 , 120 ] ; var n = a . length ; if ( checkCombinations ( a , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find probability of selecting element from kth column after N iterations | Javascript implementation of the above approach ; Function to calculate probability ; declare dp [ ] [ ] and sum [ ] ; precalculate the first row ; calculate the probability for each element and update dp table ; return result ; Driver code
let n = 4 ; let m = 4 ; function calcProbability ( M , k ) { let dp = new Array ( m ) ; let sum = new Array ( n ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < n ; i ++ ) { sum [ i ] = 0 ; } for ( let j = 0 ; j < n ; j ++ ) { dp [ 0 ] [ j ] = M [ 0 ] [ j ] ; sum [ 0 ] = sum [ 0 ] + dp [ 0 ] [ j ] ; } for ( let i = 1 ; i < m ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] += dp [ i - 1 ] [ j ] / sum [ i - 1 ] + M [ i ] [ j ] ; sum [ i ] += dp [ i ] [ j ] ; } } return dp [ n - 1 ] [ k - 1 ] / sum [ n - 1 ] ; } let M = [ [ 1 , 1 , 0 , 3 ] , [ 2 , 3 , 2 , 3 ] , [ 9 , 3 , 0 , 2 ] , [ 2 , 3 , 2 , 2 ] ] ; let k = 3 ; document . write ( calcProbability ( M , k ) ) ;
Possible cuts of a number such that maximum parts are divisible by 3 | Function to find the maximum number of numbers divisible by 3 in a large number ; store size of the string ; Stores last index of a remainder ; last visited place of remainder zero is at 0. ; To store result from 0 to i ; get the remainder ; Get maximum res [ i ] value ; Driver Code
function MaximumNumbers ( s ) { let n = s . length ; let remIndex = [ - 1 , - 1 , - 1 ] ; remIndex [ 0 ] = 0 ; let res = new Array ( n + 1 ) ; for ( let i = 0 ; i < res . length ; i ++ ) { res [ i ] = 0 ; } let r = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { r = ( r + s [ i - 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) % 3 ; res [ i ] = res [ i - 1 ] ; if ( remIndex [ r ] != - 1 ) res [ i ] = Math . max ( res [ i ] , res [ remIndex [ r ] ] + 1 ) ; remIndex [ r ] = i + 1 ; } return res [ n ] ; } let s = " " ; document . write ( MaximumNumbers ( s ) ) ;
Minimum steps required to convert X to Y where a binary matrix represents the possible conversions | JavaScript implementation of the above approach ; dist will be the output matrix that will finally have the shortest distances between every pair of numbers ; Initially same as mat ; Add all numbers one by one to the set of intermediate numbers . Before start of an iteration , we have shortest distances between all pairs of numbers such that the shortest distances consider only the numbers in set { 0 , 1 , 2 , . . k - 1 } as intermediate numbers . After the end of an iteration , vertex no . k is added to the set of intermediate numbers and the set becomes { 0 , 1 , 2 , . . k } ; Pick all numbers as source one by one ; Pick all numbers as destination for the above picked source ; If number k is on the shortest path from i to j , then update the value of dist [ i ] [ j ] ; If no path ; Driver Code
var INF = 99999 ; function findMinimumSteps ( mat , x , y , n ) { var i , j , k ; var dist = Array ( n ) . fill ( ) . map ( ( ) => Array ( n ) . fill ( 0 ) ) ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( mat [ i ] [ j ] == 0 ) dist [ i ] [ j ] = INF ; else dist [ i ] [ j ] = 1 ; if ( i == j ) dist [ i ] [ j ] = 1 ; } } for ( k = 0 ; k < n ; k ++ ) { for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( dist [ i ] [ k ] + dist [ k ] [ j ] < dist [ i ] [ j ] ) dist [ i ] [ j ] = dist [ i ] [ k ] + dist [ k ] [ j ] ; } } } if ( dist [ x ] [ y ] < INF ) return dist [ x ] [ y ] ; else return - 1 ; } var mat = [ [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ] , [ 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ] ] ; var x = 2 , y = 3 ; var size = mat . length ; document . write ( findMinimumSteps ( mat , x , y , size ) ) ;
Count number of paths whose weight is exactly X and has at | Javascript program to count the number of paths ; Function to find the number of paths ; If the summation is more than X ; If exactly X weights have reached ; Already visited ; Count paths ; Traverse in all paths ; If the edge weight is M ; Edge 's weight is not M ; Driver Code ; Initialized the DP array with - 1 ; Function to count paths
let max = 4 ; let c = 2 ; function countPaths ( sum , get , m , n , dp ) { if ( sum < 0 ) return 0 ; if ( sum == 0 ) return get ; if ( dp [ sum ] [ get ] != - 1 ) return dp [ sum ] [ get ] ; let res = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { if ( i == m ) res += countPaths ( sum - i , 1 , m , n , dp ) ; else res += countPaths ( sum - i , get , m , n , dp ) ; } dp [ sum ] [ get ] = res ; return dp [ sum ] [ get ] ; } let n = 3 , m = 2 , x = 3 ; let dp = new Array ( max + 1 ) ; for ( let i = 0 ; i <= max ; i ++ ) { dp [ i ] = new Array ( 2 ) for ( let j = 0 ; j < 2 ; j ++ ) dp [ i ] [ j ] = - 1 ; } document . write ( countPaths ( x , 0 , m , n , dp ) ) ;
Minimum Operations to make value of all vertices of the tree Zero | A utility function to add an edge in an undirected graph ; A utility function to print the adjacency list representation of graph ; Utility Function for findMinOperation ( ) ; Base Case for current node ; iterate over the adjacency list for src ; calculate DP table for each child V ; Number of Increase Type operations for node src is equal to maximum of number of increase operations required by each of its child ; Number of Decrease Type operations for node src is equal to maximum of number of decrease operations required by each of its child ; After performing operations for subtree rooted at src A [ src ] changes by the net difference of increase and decrease type operations ; for negative value of node src ; Returns the minimum operations required to make value of all vertices equal to zero , uses findMinOperationUtil ( ) ; Initialise DP table ; find dp [ 1 ] [ 0 ] and dp [ 1 ] [ 1 ] ; Driver code ; Build the Graph / Tree
function addEdge ( adj , u , v ) { adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } function printGraph ( adj , V ) { for ( var v = 0 ; v < V ; ++ v ) { document . write ( " " + v + " " ) ; for ( var x of adj [ v ] ) document . write ( " " + x ) ; document . write ( " " ) ; } } function findMinOperationUtil ( dp , adj , A , src , parent ) { dp [ src ] [ 0 ] = dp [ src ] [ 1 ] = 0 ; for ( var V of adj [ src ] ) { if ( V == parent ) continue ; findMinOperationUtil ( dp , adj , A , V , src ) ; dp [ src ] [ 0 ] = Math . max ( dp [ src ] [ 0 ] , dp [ V ] [ 0 ] ) ; dp [ src ] [ 1 ] = Math . max ( dp [ src ] [ 1 ] , dp [ V ] [ 1 ] ) ; } A [ src - 1 ] += dp [ src ] [ 0 ] - dp [ src ] [ 1 ] ; if ( A [ src - 1 ] > 0 ) { dp [ src ] [ 1 ] += A [ src - 1 ] ; } else { dp [ src ] [ 0 ] += Math . abs ( A [ src - 1 ] ) ; } } function findMinOperation ( adj , A , V ) { var dp = Array . from ( Array ( V + 1 ) , ( ) => Array ( 2 ) . fill ( 0 ) ) ; findMinOperationUtil ( dp , adj , A , 1 , 0 ) ; var minOperations = dp [ 1 ] [ 0 ] + dp [ 1 ] [ 1 ] ; return minOperations ; } var V = 5 ; var adj = Array . from ( Array ( V + 1 ) , ( ) => Array ( ) ) ; addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 1 , 3 ) ; var A = [ 1 , - 1 , 1 ] ; var minOperations = findMinOperation ( adj , A , V ) ; document . write ( minOperations ) ;
Sum of kth powers of first n natural numbers | A global array to store factorials ; Function to calculate the factorials of all the numbers upto k ; Function to return the binomial coefficient ; nCr = ( n ! * ( n - r ) ! ) / r ! ; Function to return the sum of kth powers of n natural numbers ; When j is unity ; Calculating sum ( n ^ 1 ) of unity powers of n ; storing sum ( n ^ 1 ) for sum ( n ^ 2 ) ; If k = 1 then temp is the result ; For finding sum ( n ^ k ) removing 1 and n * kCk from ( n + 1 ) ^ k ; Removing all kC2 * sum ( n ^ ( k - 2 ) ) + ... + kCk - 1 * ( sum ( n ^ ( k - ( k - 1 ) ) ; Storing the result for next sum of next powers of k ; Driver code
var MAX_K = 15 ; var fac = Array ( MAX_K ) . fill ( 0 ) ; function factorial ( k ) { fac [ 0 ] = 1 ; for ( i = 1 ; i <= k + 1 ; i ++ ) { fac [ i ] = ( i * fac [ i - 1 ] ) ; } } function bin ( a , b ) { var ans = ( ( fac [ a ] ) / ( fac [ a - b ] * fac [ b ] ) ) ; return ans ; } function sumofn ( n , k ) { var p = 0 ; var num1 , temp ; var arr = Array ( 1000 ) . fill ( 0 ) ; for ( j = 1 ; j <= k ; j ++ ) { if ( j == 1 ) { num1 = ( n * ( n + 1 ) ) / 2 ; arr [ p ++ ] = num1 ; temp = num1 ; } else { temp = ( parseInt ( Math . pow ( n + 1 , j + 1 ) - 1 - n ) ) ; for ( s = 1 ; s < j ; s ++ ) { temp = temp - ( arr [ j - s - 1 ] * bin ( j + 1 , s + 1 ) ) ; } temp = temp / ( j + 1 ) ; arr [ p ++ ] = temp ; } } temp = arr [ p - 1 ] ; return temp ; } var n = 5 , k = 2 ; factorial ( k ) ; document . write ( sumofn ( n , k ) ) ;
Ways to fill N positions using M colors such that there are exactly K pairs of adjacent different colors | Javascript implementation of the approach ; Recursive function to find the required number of ways ; When all positions are filled ; If adjacent pairs are exactly K ; If already calculated ; Next position filled with same color ; Next position filled with different color So there can be m - 1 different colors ; Driver Code
let max = 4 ; function countWays ( index , cnt , dp , n , m , k ) { if ( index == n ) { if ( cnt == k ) return 1 ; else return 0 ; } if ( dp [ index ] [ cnt ] != - 1 ) return dp [ index ] [ cnt ] ; let ans = 0 ; ans += countWays ( index + 1 , cnt , dp , n , m , k ) ; ans += ( m - 1 ) * countWays ( index + 1 , cnt + 1 , dp , n , m , k ) ; return dp [ index ] [ cnt ] = ans ; } let n = 3 , m = 3 , k = 2 ; let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) { dp [ i ] = new Array ( max ) ; for ( let j = 0 ; j < max ; j ++ ) dp [ i ] [ j ] = - 1 ; } document . write ( m * countWays ( 1 , 0 , dp , n , m , k ) ) ;
Number of balanced bracket expressions that can be formed from a string | Max string length ; Function to check whether index start and end can form a bracket pair or not ; Check for brackets ( ) ; Check for brackets [ ] ; Check for brackets { } ; Function to find number of proper bracket expressions ; If starting index is greater than ending index ; If dp [ start ] [ end ] has already been computed ; Search for the bracket in from next index ; If bracket pair is formed , add number of combination ; If ? comes then all three bracket expressions are possible ; Return answer ; If n is odd , string cannot be balanced ; Driving function
let MAX = 300 ; function checkFunc ( i , j , st ) { if ( st [ i ] == ' ' && st [ j ] == ' ' ) return 1 ; if ( st [ i ] == ' ' && st [ j ] == ' ' ) return 1 ; if ( st [ i ] == ' ' && st [ j ] == ' ' ) return 1 ; if ( st [ i ] == ' ' && st [ j ] == ' ' ) return 1 ; if ( st [ i ] == ' ' && st [ j ] == ' ' ) return 1 ; if ( st [ i ] == ' ' && st [ j ] == ' ' ) return 1 ; if ( st [ i ] == ' ' && st [ j ] == ' ' ) return 1 ; if ( st [ i ] == ' ' && st [ j ] == ' ' ) return 1 ; if ( st [ i ] == ' ' && st [ j ] == ' ' ) return 1 ; return 0 ; } function countRec ( start , end , dp , st ) { let sum = 0 ; if ( start > end ) return 1 ; if ( dp [ start ] [ end ] != - 1 ) return dp [ start ] [ end ] ; let i , r = 0 ; for ( i = start + 1 ; i <= end ; i += 2 ) { if ( checkFunc ( start , i , st ) == 1 ) { sum = sum + countRec ( start + 1 , i - 1 , dp , st ) * countRec ( i + 1 , end , dp , st ) ; } else if ( st [ start ] == ' ' && st [ i ] == ' ' ) { sum = sum + countRec ( start + 1 , i - 1 , dp , st ) * countRec ( i + 1 , end , dp , st ) * 3 ; } } return dp [ start ] [ end ] = sum ; } function countWays ( st ) { let n = st . length ; if ( n % 2 == 1 ) return 0 ; let dp = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { dp [ i ] = new Array ( MAX ) ; for ( let j = 0 ; j < MAX ; j ++ ) dp [ i ] [ j ] = - 1 ; } return countRec ( 0 , n - 1 , dp , st ) ; } let st = " " ; document . write ( countWays ( st ) ) ;
Sum of Fibonacci numbers at even indexes upto N terms | Computes value of first fibonacci numbers and stores the even - indexed sum ; Initialize result ; Add remaining terms ; For even indices ; Return the alternting sum ; Get n ; Find the even - indiced sum
function calculateEvenSum ( n ) { if ( n <= 0 ) return 0 ; let fibo = Array ( 2 * n + 1 ) ; fibo [ 0 ] = 0 ; fibo [ 1 ] = 1 ; let sum = 0 ; for ( i = 2 ; i <= 2 * n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; if ( i % 2 == 0 ) sum += fibo [ i ] ; } return sum ; } let n = 8 ; document . write ( " " + n + " " + + calculateEvenSum ( n ) ) ;
Sum of Fibonacci numbers at even indexes upto N terms | javascript Program to find even indexed Fibonacci Sum in O ( Log n ) time . ; Create an array for memoization ; Returns n 'th Fibonacci number  using table f ; Base cases ; If fib ( n ) is already computed ; Applying above formula [ Note value n & 1 is 1 if n is odd , else 0 ] . ; Computes value of even - indexed Fibonacci Sum ; Get n ; Find the alternating sum
var MAX = 1000 ; var f = Array ( MAX ) . fill ( 0 ) ; function fib ( n ) { if ( n == 0 ) { return 0 ; } if ( n == 1 n == 2 ) { return ( f [ n ] = 1 ) ; } if ( f [ n ] == 1 ) { return f [ n ] ; } var k = ( n % 2 == 1 ) ? ( n + 1 ) / 2 : n / 2 ; f [ n ] = ( n % 2 == 1 ) ? ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) : ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ; return f [ n ] ; } function calculateEvenSum ( n ) { return ( fib ( 2 * n + 1 ) - 1 ) ; } var n = 8 ; document . write ( " " + n + " " + calculateEvenSum ( n ) ) ;
Gould 's Sequence | 32768 = 2 ^ 15 ; Array to store Sequence up to 2 ^ 16 = 65536 ; Utility function to pre - compute odd numbers in ith row of Pascals 's triangle ; First term of the Sequence is 1 ; Initialize i to 1 ; Initialize p to 1 ( i . e 2 ^ i ) in each iteration i will be pth power of 2 ; loop to generate gould 's Sequence ; i is pth power of 2 traverse the array from j = 0 to i i . e ( 2 ^ p ) ; double the value of arr [ j ] and store to arr [ i + j ] ; update i to next power of 2 ; increment p ; Function to print gould 's Sequence ; loop to generate gould 's Sequence up to n ; Driver code ; Get n ; Function call
var MAX = 32768 ; var arr = Array ( 2 * MAX ) ; function gouldSequence ( ) { arr [ 0 ] = 1 ; var i = 1 ; var p = 1 ; while ( i <= MAX ) { var j = 0 ; while ( j < i ) { arr [ i + j ] = 2 * arr [ j ] ; j ++ ; } i = ( 1 << p ) ; p ++ ; } } function printSequence ( n ) { for ( var i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } gouldSequence ( ) ; var n = 16 ; printSequence ( n ) ;
Count the number of ways to traverse a Matrix | Find factorial ; Find number of ways to reach mat [ m - 1 ] [ n - 1 ] from mat [ 0 ] [ 0 ] in a matrix mat [ ] [ ] ] ; Driver code ; Function Call
function factorial ( n ) { var res = 1 , i ; for ( i = 2 ; i <= n ; i ++ ) res *= i ; return res ; } function countWays ( m , n ) { m = m - 1 ; n = n - 1 ; return factorial ( m + n ) / ( factorial ( m ) * factorial ( n ) ) ; } var m = 5 ; var n = 5 ; var result = countWays ( m , n ) ; document . write ( result ) ;
Matrix Chain Multiplication ( A O ( N ^ 2 ) Solution ) | Matrix Mi has dimension p [ i - 1 ] x p [ i ] for i = 1. . n ; For simplicity of the program , one extra row and one extra column are allocated in dp [ ] [ ] . 0 th row and 0 th column of dp [ ] [ ] are not used ; cost is zero when multiplying one matrix . ; Simply following above recursive formula . ; Driver code
function MatrixChainOrder ( p , n ) { var dp = Array . from ( Array ( n ) , ( ) => Array ( n ) ) ; for ( var i = 1 ; i < n ; i ++ ) dp [ i ] [ i ] = 0 ; for ( var L = 1 ; L < n - 1 ; L ++ ) for ( var i = 1 ; i < n - L ; i ++ ) dp [ i ] [ i + L ] = Math . min ( dp [ i + 1 ] [ i + L ] + p [ i - 1 ] * p [ i ] * p [ i + L ] , dp [ i ] [ i + L - 1 ] + p [ i - 1 ] * p [ i + L - 1 ] * p [ i + L ] ) ; return dp [ 1 ] [ n - 1 ] ; } var arr = [ 10 , 20 , 30 , 40 , 30 ] ; var size = arr . length ; document . write ( " " + MatrixChainOrder ( arr , size ) ) ;
Count common subsequence in two strings | return the number of common subsequence in two strings ; for each character of S ; for each character in T ; if character are same in both the string ; Driver Program
function CommomSubsequencesCount ( s , t ) { var n1 = s . length ; var n2 = t . length ; var dp = Array . from ( Array ( n1 + 1 ) , ( ) => Array ( n2 + 1 ) ) ; for ( var i = 0 ; i <= n1 ; i ++ ) { for ( var j = 0 ; j <= n2 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( var i = 1 ; i <= n1 ; i ++ ) { for ( var j = 1 ; j <= n2 ; j ++ ) { if ( s [ i - 1 ] == t [ j - 1 ] ) dp [ i ] [ j ] = 1 + dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ j ] ; else dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ j ] - dp [ i - 1 ] [ j - 1 ] ; } } return dp [ n1 ] [ n2 ] ; } var s = " " ; var t = " " ; document . write ( CommomSubsequencesCount ( s , t ) ) ;
Minimum number of palindromes required to express N as a sum | Set 2 | A utility for creating palindrome ; checks if number of digits is odd or even if odd then neglect the last digit of input in finding reverse as in case of odd number of digits middle element occur once ; Creates palindrome by just appending reverse of number to itself ; Function to generate palindromes ; Run two times for odd and even length palindromes ; Creates palindrome numbers with first half as i . Value of j decides whether we need an odd length or even length palindrome . ; Swap values of left and right ; Function to find the minimum number of palindromes required to express N as a sum ; Checking if the number is a palindrome ; Checking if the number is a sum of two palindromes Getting the list of all palindromes upto N ; Sorting the list of palindromes ; The answer is three if the control reaches till this point ; Driver code
function createPalindrome ( input , isOdd ) { var n = input ; var palin = input ; if ( isOdd % 2 == 1 ) { n = parseInt ( n / 10 ) ; } while ( n > 0 ) { palin = palin * 10 + ( n % 10 ) ; n = parseInt ( n / 10 ) ; } return palin ; } function generatePalindromes ( N ) { var palindromes = [ ] ; var number ; for ( var j = 0 ; j < 2 ; j ++ ) { var i = 1 ; while ( ( number = createPalindrome ( i ++ , j ) ) <= N ) { palindromes . push ( number ) ; } } return palindromes ; } function reverse ( input ) { var temparray = input . split ( ' ' ) ; var left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { var temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return temparray . join ( ' ' ) ; } function minimumNoOfPalindromes ( N ) { var a = N . toString ( ) ; var b = N . toString ( ) ; b = reverse ( b ) ; if ( a == b ) { return 1 ; } var palindromes = generatePalindromes ( N ) ; palindromes . sort ( ) ; var l = 0 , r = palindromes . length - 1 ; while ( l < r ) { if ( palindromes [ l ] + palindromes [ r ] == N ) { return 2 ; } else if ( palindromes [ l ] + palindromes [ r ] < N ) { ++ l ; } else { -- r ; } } return 3 ; } var N = 65 ; document . write ( minimumNoOfPalindromes ( N ) ) ;
Minimum Cost to make two Numeric Strings Identical | Function to find weight of LCS ; if this state is already calculated then return ; adding required weight for particular match ; recurse for left and right child and store the max ; Function to calculate cost of string ; Driver code ; Minimum cost needed to make two strings identical
function lcs ( dp , a , b , m , n ) { if ( m < 0 n < 0 ) { return 0 ; } if ( dp [ m ] [ n ] != - 1 ) return dp [ m ] [ n ] ; let ans = 0 ; if ( a [ m ] == b [ n ] ) { ans = ( a [ m ] . charCodeAt ( 0 ) - 48 ) + lcs ( dp , a , b , m - 1 , n - 1 ) ; } else ans = Math . max ( lcs ( dp , a , b , m - 1 , n ) , lcs ( dp , a , b , m , n - 1 ) ) ; dp [ m ] [ n ] = ans ; return ans ; } function costOfString ( str ) { let cost = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) cost += ( str [ i ] . charCodeAt ( 0 ) - 48 ) ; return cost ; } let a = " " ; let b = " " ; let dp = new Array ( 101 ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 101 ) ; for ( let j = 0 ; j < 101 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } document . write ( ( costOfString ( a ) + costOfString ( b ) - 2 * lcs ( dp , a , b , a . length - 1 , b . length - 1 ) ) ) ;