codeparrot/xlcost-text-to-code huggingface dataset:
Find maximum possible value of advertising | Function to find maximum possible advertising value ; To store advertising value at i - th minute ; Base Case ; If no advertisement is taken on ith minute ; If advertisement is taken on i - th minute ; array [ ] [ 0 ] start time array [ ] [ 1 ] advertising value
function max_value ( array , M , K , N ) { var time = Array ( M ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { time [ array [ i ] [ 0 ] ] = array [ i ] [ 1 ] ; } var dp = Array . from ( Array ( M ) , ( ) => Array ( 2 ) ) ; dp [ 0 ] [ 0 ] = 0 ; dp [ 0 ] [ 1 ] = time [ 0 ] ; for ( var i = 1 ; i < M ; i ++ ) { dp [ i ] [ 0 ] = Math . max ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) ; dp [ i ] [ 1 ] = time [ i ] ; if ( i - K >= 0 ) { dp [ i ] [ 1 ] += Math . max ( dp [ i - K ] [ 0 ] , dp [ i - K ] [ 1 ] ) ; } } return Math . max ( dp [ M - 1 ] [ 0 ] , dp [ M - 1 ] [ 1 ] ) ; } var array = [ [ 0 , 10 ] , [ 4 , 110 ] , [ 5 , 30 ] ] ; var N = 3 ; var K = 4 ; var M = 6 ; document . write ( max_value ( array , M , K , N ) ) ;
Count numbers with exactly K non | To store digits of N ; visited map ; DP Table ; Push all the digits of N into digits vector ; Function returns the count ; If desired number is formed whose sum is odd ; If it is not present in map , mark it as true and return 1 ; Sum is present in map already ; Desired result not found ; If that state is already calculated just return that state value ; Upper limit ; To store the count of desired numbers ; If k is non - zero , i ranges from 0 to j else [ 1 , j ] ; If current digit is 0 , decrement k and recurse sum is not changed as we are just adding 0 that makes no difference ; If i is non zero , then k remains unchanged and value is added to sum ; Memoize and return ; K is the number of exact non - zero elements to have in number ; break N into its digits ; We keep record of 0 s we need to place in the number
let digits = [ ] ; let vis = new Array ( 170 ) ; vis . fill ( false ) ; let dp = new Array ( 19 ) ; for ( let i = 0 ; i < 19 ; i ++ ) { dp [ i ] = new Array ( 19 ) ; for ( let j = 0 ; j < 19 ; j ++ ) { dp [ i ] [ j ] = new Array ( 2 ) ; for ( let k = 0 ; k < 2 ; k ++ ) { dp [ i ] [ j ] [ k ] = new Array ( 170 ) ; for ( let l = 0 ; l < 170 ; l ++ ) { dp [ i ] [ j ] [ k ] [ l ] = 0 ; } } } } function ConvertIntoDigit ( n ) { while ( n > 0 ) { let dig = n % 10 ; digits . push ( dig ) ; n = parseInt ( n / 10 , 10 ) ; } digits . reverse ( ) ; } function solve ( idx , k , tight , sum ) { if ( idx == digits . length && k == 0 && sum % 2 == 1 ) { if ( ! vis [ sum ] ) { vis [ sum ] = true ; return 1 ; } return 0 ; } if ( idx > digits . length ) { return 0 ; } if ( dp [ idx ] [ k ] [ tight ] [ sum ] > 0 ) { return dp [ idx ] [ k ] [ tight ] [ sum ] ; } let j ; if ( idx < digits . length && tight == 0 ) { j = digits [ idx ] ; } else { j = 9 ; } let cnt = 0 ; for ( let i = ( k > 0 ? 0 : 1 ) ; i <= j ; i ++ ) { let newtight = tight ; if ( i < j ) { newtight = 1 ; } if ( i == 0 ) cnt += solve ( idx + 1 , k - 1 , newtight , sum ) ; else cnt += solve ( idx + 1 , k , newtight , sum + i ) ; } dp [ idx ] [ k ] [ tight ] [ sum ] = cnt ; return dp [ idx ] [ k ] [ tight ] [ sum ] ; } let N , k ; N = 169 ; k = 2 ; ConvertIntoDigit ( N ) ; k = digits . length - k ; document . write ( solve ( 0 , k , 0 , 0 ) ) ;
Count of subsets of integers from 1 to N having no adjacent elements | Function to count subsets ; Driver code
function countSubsets ( N ) { if ( N <= 2 ) return N ; if ( N == 3 ) return 2 ; var DP = Array ( N + 1 ) . fill ( 0 ) ; DP [ 0 ] = 0 ; DP [ 1 ] = 1 ; DP [ 2 ] = 2 ; DP [ 3 ] = 2 ; for ( i = 4 ; i <= N ; i ++ ) { DP [ i ] = DP [ i - 2 ] + DP [ i - 3 ] ; } return DP [ N ] ; } var N = 20 ; document . write ( countSubsets ( N ) ) ;
Count the number of ordered sets not containing consecutive numbers | DP table ; Function to calculate the count of ordered set for a given size ; Base cases ; If subproblem has been soved before ; Store and return answer to this subproblem ; Function returns the count of all ordered sets ; Prestore the factorial value ; Initialise the dp table ; Iterate all ordered set sizes and find the count for each one maximum ordered set size will be smaller than N as all elements are distinct and non consecutive . ; Multiply ny size ! for all the arrangements because sets are ordered . ; Add to total answer ; Driver code
var dp = Array . from ( Array ( 500 ) , ( ) => Array ( 500 ) ) ; function CountSets ( x , pos ) { if ( x <= 0 ) { if ( pos == 0 ) return 1 ; else return 0 ; } if ( pos == 0 ) return 1 ; if ( dp [ x ] [ pos ] != - 1 ) return dp [ x ] [ pos ] ; var answer = CountSets ( x - 1 , pos ) + CountSets ( x - 2 , pos - 1 ) ; return dp [ x ] [ pos ] = answer ; } function CountOrderedSets ( n ) { var factorial = Array ( 10000 ) . fill ( 0 ) ; factorial [ 0 ] = 1 ; for ( var i = 1 ; i < 10000 ; i ++ ) factorial [ i ] = factorial [ i - 1 ] * i ; var answer = 0 ; dp = Array . from ( Array ( 500 ) , ( ) => Array ( 500 ) . fill ( - 1 ) ) ; for ( var i = 1 ; i <= n ; i ++ ) { var sets = CountSets ( n , i ) * factorial [ i ] ; answer = answer + sets ; } return answer ; } var N = 3 ; document . write ( CountOrderedSets ( N ) ) ;
Count the Arithmetic sequences in the Array of size at least 3 | Function to find all arithmetic sequences of size atleast 3 ; If array size is less than 3 ; Finding arithmetic subarray length ; To store all arithmetic subarray of length at least 3 ; Check if current element makes arithmetic sequence with previous two elements ; Begin with a new element for new arithmetic sequences ; Accumulate result in till i . ; Return final count ; Driver code ; Function to find arithmetic sequences
function numberOfArithmeticSequences ( L , N ) { if ( N <= 2 ) return 0 ; var count = 0 ; var res = 0 ; for ( var i = 2 ; i < N ; ++ i ) { if ( L [ i ] - L [ i - 1 ] == L [ i - 1 ] - L [ i - 2 ] ) { ++ count ; } else { count = 0 ; } res += count ; } return res ; } var L = [ 1 , 3 , 5 , 6 , 7 , 8 ] ; var N = L . length ; document . write ( numberOfArithmeticSequences ( L , N ) ) ;
Count triplet of indices ( i , j , k ) such that XOR of elements between [ i , j ) equals [ j , k ] | Function return the count of triplets having subarray XOR equal ; XOR value till i ; Count and ways array as defined above ; Using the formula stated ; Increase the frequency of x ; Add i + 1 to ways [ x ] for upcoming indices ; Driver code
function CountOfTriplets ( a , n ) { let answer = 0 ; let x = 0 ; let count = new Array ( 100005 ) ; let ways = new Array ( 100005 ) ; count . fill ( 0 ) ; ways . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { x ^= a [ i ] ; answer += count [ x ] * i - ways [ x ] ; count [ x ] ++ ; ways [ x ] = ways [ x ] + i + 1 ; } return answer ; } let Arr = [ 3 , 6 , 12 , 8 , 6 , 2 , 1 , 5 ] ; let N = Arr . length ; document . write ( CountOfTriplets ( Arr , N ) ) ;
Count of subarrays of an Array having all unique digits | Dynamic programming table ; Function to obtain the mask for any integer ; Function to count the number of ways ; Subarray must not be empty ; If subproblem has been solved ; Excluding this element in the subarray ; If there are no common digits then only this element can be included ; Calculate the new mask if this element is included ; Store and return the answer ; Function to find the count of subarray with all digits unique ; initializing dp ; Driver code
var dp = Array . from ( Array ( 5000 ) , ( ) => Array ( ( 1 << 10 ) + 5 ) . fill ( - 1 ) ) ; function getmask ( val ) { var mask = 0 ; if ( val == 0 ) return 1 ; while ( val ) { var d = val % 10 ; mask |= ( 1 << d ) ; val = parseInt ( val / 10 ) ; } return mask ; } function countWays ( pos , mask , a , n ) { if ( pos == n ) return ( mask > 0 ? 1 : 0 ) ; if ( dp [ pos ] [ mask ] != - 1 ) return dp [ pos ] [ mask ] ; var count = 0 ; count = count + countWays ( pos + 1 , mask , a , n ) ; if ( ( getmask ( a [ pos ] ) & mask ) == 0 ) { var new_mask = ( mask | ( getmask ( a [ pos ] ) ) ) ; count = count + countWays ( pos + 1 , new_mask , a , n ) ; } return dp [ pos ] [ mask ] = count ; } function numberOfSubarrays ( a , n ) { dp = Array . from ( Array ( 5000 ) , ( ) => Array ( ( 1 << 10 ) + 5 ) . fill ( - 1 ) ) ; return countWays ( 0 , 0 , a , n ) ; } var N = 4 ; var A = [ 1 , 12 , 23 , 34 ] ; document . write ( numberOfSubarrays ( A , N ) ) ;
Count of Fibonacci paths in a Binary tree | Vector to store the fibonacci series ; Binary Tree Node ; Function to create a new tree node ; Function to find the height of the given tree ; Function to make fibonacci series upto n terms ; Preorder Utility function to count exponent path in a given Binary tree ; Base Condition , when node pointer becomes null or node value is not a number of Math . pow ( x , y ) ; Increment count when encounter leaf node ; Left recursive call save the value of count ; Right recursive call and return value of count ; Function to find whether fibonacci path exists or not ; To find the height ; Making fibonacci series upto ht terms ; Create binary tree ; Function Call
let fib = [ ] ; class node { constructor ( data ) { this . left = null ; this . right = null ; this . data = data ; } } ; function newNode ( data ) { let temp = new node ( data ) ; return temp ; } function height ( root ) { if ( root == null ) return 0 ; return ( Math . max ( height ( root . left ) , height ( root . right ) ) + 1 ) ; } function FibonacciSeries ( n ) { fib . push ( 0 ) ; fib . push ( 1 ) ; for ( let i = 2 ; i < n ; i ++ ) fib . push ( fib [ i - 1 ] + fib [ i - 2 ] ) ; } function CountPathUtil ( root , i , count ) { if ( root == null || ! ( fib [ i ] == root . data ) ) { return count ; } if ( root . left != null && root . right != null ) { count ++ ; } count = CountPathUtil ( root . left , i + 1 , count ) ; return CountPathUtil ( root . right , i + 1 , count ) ; } function CountPath ( root ) { let ht = height ( root ) ; FibonacciSeries ( ht ) ; document . write ( CountPathUtil ( root , 0 , 0 ) ) ; } let root = newNode ( 0 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 1 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 4 ) ; root . right . right = newNode ( 1 ) ; root . right . right . left = newNode ( 2 ) ; CountPath ( root ) ;
Count maximum occurrence of subsequence in string such that indices in subsequence is in A . P . | Function to find the maximum occurrence of the subsequence such that the indices of characters are in arithmetic progression ; Frequency for characters ; Loop to count the occurrence of ith character before jth character in the given String ; Increase the frequency of s [ i ] or c of String ; Maximum occurrence of subsequence of length 1 in given String ; Maximum occurrence of subsequence of length 2 in given String ; Driver Code
function maximumOccurrence ( s ) { var n = s . length ; var freq = Array ( 26 ) . fill ( 0 ) ; var dp = Array ( 26 ) . fill ( ) . map ( ( ) => Array ( 26 ) . fill ( 0 ) ) ; for ( var i = 0 ; i < n ; i ++ ) { var c = ( s . charCodeAt ( i ) - ' ' . charCodeAt ( 0 ) ) ; for ( var j = 0 ; j < 26 ; j ++ ) dp [ j ] += freq [ j ] ; freq ++ ; } var answer = Number . MIN_VALUE ; for ( var i = 0 ; i < 26 ; i ++ ) answer = Math . max ( answer , freq [ i ] ) ; for ( var i = 0 ; i < 26 ; i ++ ) { for ( var j = 0 ; j < 26 ; j ++ ) { answer = Math . max ( answer , dp [ i ] [ j ] ) ; } } return answer ; } var s = " " ; document . write ( maximumOccurrence ( s ) ) ;
Count the numbers with N digits and whose suffix is divisible by K | Javascript implementation to Count the numbers with N digits and whose suffix is divisible by K ; Suffix of length pos with remainder rem and Z representing whether the suffix has a non zero digit until now ; Base case ; If count of digits is less than n ; Placing all possible digits in remaining positions ; If remainder non zero and suffix has n digits ; If the subproblem is already solved ; Placing all digits at MSB of suffix and increasing it 's length by 1 ; Non zero digit is placed ; Store and return the solution to this subproblem ; Function to Count the numbers with N digits and whose suffix is divisible by K ; Since we need powers of 10 for counting , it 's better to  pre store them along with their  modulo with 1e9 + 7 for counting ; Since at each recursive step we increase the suffix length by 1 by placing digits at its leftmost position , we need powers of 10 modded with k , in order to fpos the new remainder efficiently ; Driver Code
var mod = 1000000007 ; var dp = Array . from ( Array ( 1005 ) , ( ) => Array ( 105 ) ) ; for ( var i = 0 ; i < 1005 ; i ++ ) { for ( var j = 0 ; j < 105 ; j ++ ) { dp [ i ] [ j ] = Array ( 2 ) . fill ( - 1 ) ; } } var powers = Array ( 1005 ) ; var powersModk = Array ( 1005 ) ; function calculate ( pos , rem , z , k , n ) { if ( rem == 0 && z ) { if ( pos != n ) return ( powers [ n - pos - 1 ] * 9 ) % mod ; else return 1 ; } if ( pos == n ) return 0 ; if ( dp [ pos ] [ rem ] [ z ] != - 1 ) return dp [ pos ] [ rem ] [ z ] ; var count = 0 ; for ( var i = 0 ; i < 10 ; i ++ ) { if ( i == 0 ) count = ( count + ( calculate ( pos + 1 , ( rem + ( i * powersModk [ pos ] ) % k ) % k , z , k , n ) ) ) % mod ; else count = ( count + ( calculate ( pos + 1 , ( rem + ( i * powersModk [ pos ] ) % k ) % k , 1 , k , n ) ) ) % mod ; } return dp [ pos ] [ rem ] [ z ] = count ; } function countNumbers ( n , k ) { var st = 1 ; for ( var i = 0 ; i <= n ; i ++ ) { powers [ i ] = st ; st *= 10 ; st %= mod ; } st = 1 ; for ( var i = 0 ; i <= n ; i ++ ) { powersModk [ i ] = st ; st *= 10 ; st %= mod ; } return calculate ( 0 , 0 , 0 , k , n ) ; } var N = 2 ; var K = 2 ; document . write ( countNumbers ( N , K ) ) ;
Shortest path with exactly k edges in a directed and weighted graph | Set 2 | Javascript implementation of the above approach ; Function to find the smallest path with exactly K edges ; Array to store dp ; Initialising the array ; Loop to solve DP ; Initialising next state ; Recurrence relation ; Returning final answer ; Driver code ; Input edges ; Source and Destination ; Number of edges in path ; Calling the function
let inf = 100000000 ; function smPath ( s , d , ed , n , k ) { let dis = new Array ( n + 1 ) ; for ( let i = 0 ; i < ( n + 1 ) ; i ++ ) { dis [ i ] = inf ; } dis [ s ] = 0 ; for ( let i = 0 ; i < k ; i ++ ) { let dis1 = new Array ( n + 1 ) ; for ( let i = 0 ; i < ( n + 1 ) ; i ++ ) { dis1 [ i ] = inf ; } for ( let it = 0 ; it < ed . length ; it ++ ) dis1 [ ed [ it ] [ 1 ] ] = Math . min ( dis1 [ ed [ it ] [ 1 ] ] , dis [ ed [ it ] [ 0 ] ] + ed [ it ] [ 2 ] ) ; document . write ( ) for ( let j = 0 ; j <= n ; j ++ ) dis [ j ] = dis1 [ j ] ; } if ( dis [ d ] == inf ) return - 1 ; else return dis [ d ] ; } let n = 4 ; let ed = [ [ 0 , 1 , 10 ] , [ 0 , 2 , 3 ] , [ 0 , 3 , 2 ] , [ 1 , 3 , 7 ] , [ 2 , 3 , 7 ] ] ; let s = 0 , d = 3 ; let k = 2 ; document . write ( smPath ( s , d , ed , n , k ) ) ;
Path with smallest product of edges with weight > 0 | Javascript implementation of the approach . ; Function to return the smallest product of edges ; If the source is equal to the destination ; Initialising the array ; Bellman ford algorithm ; Loop to detect cycle ; Returning final answer ; Driver code ; Input edges ; Source and Destination ; Bellman ford
var inf = 1000000000 ; function bellman ( s , d , ed , n ) { if ( s == d ) return 0 ; var dis = Array ( n + 1 ) . fill ( inf ) ; dis [ s ] = 1 ; for ( var i = 0 ; i < n - 1 ; i ++ ) { for ( var j = 0 ; j < ed . length ; j ++ ) { dis [ ed [ j ] [ 0 ] [ 1 ] ] = Math . min ( dis [ ed [ j ] [ 0 ] [ 1 ] ] , dis [ ed [ j ] [ 0 ] [ 0 ] ] * ed [ j ] [ 1 ] ) ; } } for ( var it in ed ) { if ( dis [ it [ 0 ] [ 1 ] ] > dis [ it [ 0 ] [ 0 ] ] * it [ 1 ] ) return - 2 ; } if ( dis [ d ] == inf ) return - 1 ; else return dis [ d ] ; } var n = 3 ; var ed ; ed = [ [ [ 1 , 2 ] , 0.5 ] , [ [ 1 , 3 ] , 1.9 ] , [ [ 2 , 3 ] , 3 ] ] ; var s = 1 , d = 3 ; var get = bellman ( s , d , ed , n ) ; if ( get == - 2 ) document . write ( " " ) ; else document . write ( get ) ;
Find Maximum Length Of A Square Submatrix Having Sum Of Elements At | Function to return maximum length of square submatrix having sum of elements at - most K ; Matrix to store prefix sum ; Current maximum length ; Variable for storing maximum length of square ; Calculating prefix sum ; Checking whether there exits square with length cur_max + 1 or not ; Returning the maximum length ; Driver code
function maxLengthSquare ( row , column , arr , k ) { let sum = new Array ( ) ; [ row + 1 ] [ column + 1 ] ; for ( let i = 0 ; i < row + 1 ; i ++ ) { let temp = new Array ( ) ; for ( let j = 0 ; j < column + 1 ; j ++ ) { temp . push ( [ ] ) } sum . push ( temp ) } for ( let i = 1 ; i <= row ; i ++ ) for ( let j = 0 ; j <= column ; j ++ ) sum [ i ] [ j ] = 0 ; let cur_max = 1 ; let max = 0 ; for ( let i = 1 ; i <= row ; i ++ ) { for ( let j = 1 ; j <= column ; j ++ ) { sum [ i ] [ j ] = sum [ i - 1 ] [ j ] + sum [ i ] [ j - 1 ] + arr [ i - 1 ] [ j - 1 ] - sum [ i - 1 ] [ j - 1 ] ; if ( i >= cur_max && j >= cur_max && sum [ i ] [ j ] - sum [ i - cur_max ] [ j ] - sum [ i ] [ j - cur_max ] + sum [ i - cur_max ] [ j - cur_max ] <= k ) { max = cur_max ++ ; } } } return max ; } let row = 4 , column = 4 ; let matrix = [ [ 1 , 1 , 1 , 1 ] , [ 1 , 0 , 0 , 0 ] , [ 1 , 0 , 0 , 0 ] , [ 1 , 0 , 0 , 0 ] ] ; let k = 6 ; let ans = maxLengthSquare ( row , column , matrix , k ) ; document . write ( ans ) ;
Sum of all numbers formed having 4 atmost X times , 5 atmost Y times and 6 atmost Z times | Javascript program to find sum of all numbers formed having 4 atmost X times , 5 atmost Y times and 6 atmost Z times ; exactsum [ i ] [ j ] [ k ] stores the sum of all the numbers having exact i 4 ' s , ▁ j ▁ 5' s and k 6 's ; exactnum [ i ] [ j ] [ k ] stores numbers of numbers having exact i 4 ' s , ▁ j ▁ 5' s and k 6 's ; Utility function to calculate the sum for x 4 ' s , ▁ y ▁ 5' s and z 6 's ; Computing exactsum [ i ] [ j ] [ k ] as explained above ; Driver code
let N = 101 ; let mod = 1e9 + 7 ; let exactsum = new Array ( N ) ; let exactnum = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { exactsum [ i ] = new Array ( N ) ; exactnum [ i ] = new Array ( N ) ; for ( let j = 0 ; j < N ; j ++ ) { exactsum [ i ] [ j ] = new Array ( N ) ; exactnum [ i ] [ j ] = new Array ( N ) ; for ( let k = 0 ; k < N ; k ++ ) { exactsum [ i ] [ j ] [ k ] = 0 ; exactnum [ i ] [ j ] [ k ] = 0 ; } } } function getSum ( x , y , z ) { let ans = 0 ; exactnum [ 0 ] [ 0 ] [ 0 ] = 1 ; for ( let i = 0 ; i <= x ; ++ i ) { for ( let j = 0 ; j <= y ; ++ j ) { for ( let k = 0 ; k <= z ; ++ k ) { if ( i > 0 ) { exactsum [ i ] [ j ] [ k ] += ( exactsum [ i - 1 ] [ j ] [ k ] * 10 + 4 * exactnum [ i - 1 ] [ j ] [ k ] ) % mod ; exactnum [ i ] [ j ] [ k ] += exactnum [ i - 1 ] [ j ] [ k ] % mod ; } if ( j > 0 ) { exactsum [ i ] [ j ] [ k ] += ( exactsum [ i ] [ j - 1 ] [ k ] * 10 + 5 * exactnum [ i ] [ j - 1 ] [ k ] ) % mod ; exactnum [ i ] [ j ] [ k ] += exactnum [ i ] [ j - 1 ] [ k ] % mod ; } if ( k > 0 ) { exactsum [ i ] [ j ] [ k ] += ( exactsum [ i ] [ j ] [ k - 1 ] * 10 + 6 * exactnum [ i ] [ j ] [ k - 1 ] ) % mod ; exactnum [ i ] [ j ] [ k ] += exactnum [ i ] [ j ] [ k - 1 ] % mod ; } ans += exactsum [ i ] [ j ] [ k ] % mod ; ans %= mod ; } } } return ans ; } let x = 1 , y = 1 , z = 1 ; document . write ( getSum ( x , y , z ) % mod ) ;
Maximum value obtained by performing given operations in an Array | A function to calculate the maximum value ; basecases ; Loop to iterate and add the max value in the dp array ; Driver Code
function findMax ( a , n ) { var dp = Array ( n ) . fill ( ) . map ( ( ) => Array ( 2 ) . fill ( 0 ) ) ; var i , j ; for ( i = 0 ; i < n ; i ++ ) for ( j = 0 ; j < 2 ; j ++ ) dp [ i ] [ j ] = 0 ; dp [ 0 ] [ 0 ] = a [ 0 ] + a [ 1 ] ; dp [ 0 ] [ 1 ] = a [ 0 ] * a [ 1 ] ; for ( i = 1 ; i <= n - 2 ; i ++ ) { dp [ i ] [ 0 ] = Math . max ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) + a [ i + 1 ] ; dp [ i ] [ 1 ] = dp [ i - 1 ] [ 0 ] - a [ i ] + a [ i ] * a [ i + 1 ] ; } document . write ( Math . max ( dp [ n - 2 ] [ 0 ] , dp [ n - 2 ] [ 1 ] ) ) ; } var arr = [ 5 , - 1 , - 5 , - 3 , 2 , 9 , - 4 ] ; findMax ( arr , 7 ) ;
Optimal strategy for a Game with modifications | Function to return sum of subarray from l to r ; calculate sum by a loop from l to r ; dp to store the values of sub problems ; if length of the array is less than k return the sum ; if the value is previously calculated ; else calculate the value ; select all the sub array of length len_r ; get the sum of that sub array ; check if it is the maximum or not ; store it in the table ; Driver code
function sum ( arr , l , r ) { let s = 0 ; for ( let i = l ; i <= r ; i ++ ) { s += arr [ i ] ; } return s ; } let dp = new Array ( 101 ) ; for ( let i = 0 ; i < 101 ; i ++ ) { dp [ i ] = new Array ( 101 ) ; for ( let j = 0 ; j < 101 ; j ++ ) { dp [ i ] [ j ] = new Array ( 101 ) ; for ( let k = 0 ; k < 101 ; k ++ ) { dp [ i ] [ j ] [ k ] = 0 ; } } } function solve ( arr , l , r , k ) { if ( r - l + 1 <= k ) return sum ( arr , l , r ) ; if ( dp [ l ] [ r ] [ k ] != 0 ) return dp [ l ] [ r ] [ k ] ; let sum_ = sum ( arr , l , r ) ; let len_r = ( r - l + 1 ) - k ; let len = ( r - l + 1 ) ; let ans = 0 ; for ( let i = 0 ; i < len - len_r + 1 ; i ++ ) { let sum_sub = sum ( arr , i + l , i + l + len_r - 1 ) ; ans = Math . max ( ans , ( sum_ - sum_sub ) + ( sum_sub - solve ( arr , i + l , i + l + len_r - 1 , k ) ) ) ; } dp [ l ] [ r ] [ k ] = ans ; return ans ; } let arr = [ 10 , 15 , 20 , 9 , 2 , 5 ] , k = 2 ; let n = arr . length ; document . write ( solve ( arr , 0 , n - 1 , k ) ) ;
Find the minimum difference path from ( 0 , 0 ) to ( N | JavaScript implementation of the approach ; Function to return the minimum difference path from ( 0 , 0 ) to ( N - 1 , M - 1 ) ; Terminating case ; Base case ; If it is already visited ; Recursive calls ; Return the value ; Driver code ; Function call
let MAXI = 50 ; let dp = new Array ( MAXI ) ; let n , m ; let INT_MAX = Number . MAX_VALUE ; function minDifference ( x , y , k , b , c ) { if ( x >= n y >= m ) return INT_MAX ; if ( x == n - 1 && y == m - 1 ) { let diff = b [ x ] [ y ] - c [ x ] [ y ] ; return Math . min ( Math . abs ( k - diff ) , Math . abs ( k + diff ) ) ; } let ans = dp [ x ] [ y ] [ k ] ; if ( ans != - 1 ) return ans ; ans = INT_MAX ; let diff = b [ x ] [ y ] - c [ x ] [ y ] ; ans = Math . min ( ans , minDifference ( x + 1 , y , Math . abs ( k + diff ) , b , c ) ) ; ans = Math . min ( ans , minDifference ( x , y + 1 , Math . abs ( k + diff ) , b , c ) ) ; ans = Math . min ( ans , minDifference ( x + 1 , y , Math . abs ( k - diff ) , b , c ) ) ; ans = Math . min ( ans , minDifference ( x , y + 1 , Math . abs ( k - diff ) , b , c ) ) ; return ans ; } n = 2 ; m = 2 ; let b = [ [ 1 , 4 ] , [ 2 , 4 ] ] ; let c = [ [ 3 , 2 ] , [ 3 , 1 ] ] ; for ( let i = 0 ; i < MAXI ; i ++ ) { dp [ i ] = new Array ( MAXI ) ; for ( let j = 0 ; j < MAXI ; j ++ ) { dp [ i ] [ j ] = new Array ( MAXI * MAXI ) ; for ( let k = 0 ; k < MAXI * MAXI ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } document . write ( minDifference ( 0 , 0 , 0 , b , c ) ) ;
Longest subsequence having difference atmost K | Function to find the longest Special Sequence ; Creating a list with all 0 's of size  equal to the length of String ; Supporting list with all 0 's of size 26 since  the given String consists  of only lower case alphabets ; Converting the ascii value to list indices ; Determining the lower bound ; Determining the upper bound ; Filling the dp array with values ; Filling the max_length array with max length of subsequence till now ; return the max length of subsequence ; Driver Code
function longest_subseq ( n , k , s ) { let dp = new Array ( n ) ; let max_length = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { max_length [ i ] = 0 ; dp [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { let curr = s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; let lower = Math . max ( 0 , curr - k ) ; let upper = Math . min ( 25 , curr + k ) ; for ( let j = lower ; j < upper + 1 ; j ++ ) { dp [ i ] = Math . max ( dp [ i ] , max_length [ j ] + 1 ) ; } max_length [ curr ] = Math . max ( dp [ i ] , max_length [ curr ] ) ; } let ans = 0 ; ans = Math . max ( ... dp ) return ans ; } let s = " " ; let n = s . length ; let k = 3 ; document . write ( longest_subseq ( n , k , s ) ) ;