codeparrot/xlcost-text-to-code huggingface dataset:
Find the equal pairs of subsequence of S and subsequence of T | JavaScript implementation of the approach ; Function to return the pairs of subsequences from S [ ] and subsequences from T [ ] such that both have the same content ; Create dp array ; Base values ; Base values ; Keep previous dp value ; If both elements are same ; Return the required answer ; Driver code
let mod = 1e9 + 7 ; function subsequence ( S , T , n , m ) { let dp = new Array ( ) for ( let i = 0 ; i < n + 1 ; i ++ ) { let temp = [ ] ; for ( let j = 0 ; j < m + 1 ; j ++ ) { temp . push ( [ ] ) } dp . push ( temp ) } for ( let i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = 1 ; for ( let j = 0 ; j <= m ; j ++ ) dp [ 0 ] [ j ] = 1 ; for ( let i = 1 ; i <= n ; ++ i ) { for ( let j = 1 ; j <= m ; ++ j ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] - dp [ i - 1 ] [ j - 1 ] ; if ( S [ i - 1 ] == T [ j - 1 ] ) dp [ i ] [ j ] += dp [ i - 1 ] [ j - 1 ] ; dp [ i ] [ j ] += mod ; dp [ i ] [ j ] %= mod ; } } return dp [ n ] [ m ] ; } let S = [ 1 , 1 ] ; let n = S . length ; let T = [ 1 , 1 ] ; let m = T . length ; document . write ( subsequence ( S , T , n , m ) ) ;
Maximum count of elements divisible on the left for any element | Function to return the maximum count of required elements ; For every element in the array starting from the second element ; Check all the elements on the left of current element which are divisible by the current element ; Driver code
function findMax ( arr , n ) { var res = 0 ; var i , j ; for ( i = 0 ; i < n ; i ++ ) { var count = 0 ; for ( j = 0 ; j < i ; j ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) count += 1 ; } res = Math . max ( count , res ) ; } return res ; } var arr = [ 8 , 1 , 28 , 4 , 2 , 6 , 7 ] ; var n = arr . length ; document . write ( findMax ( arr , n ) ) ;
Maximum count of elements divisible on the left for any element | Function to return the maximum count of required elements ; divisible [ i ] will store true if arr [ i ] is divisible by any element on its right ; To store the maximum required count ; For every element of the array ; If the current element is divisible by any element on its right ; Find the count of element on the left which are divisible by the current element ; If arr [ j ] is divisible then set divisible [ j ] to true ; Update the maximum required count ; Driver code
function findMax ( arr , n ) { var divisible = Array ( n ) . fill ( false ) ; var res = 0 ; for ( var i = n - 1 ; i > 0 ; i -- ) { if ( divisible [ i ] ) continue ; var cnt = 0 ; for ( var j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] % arr [ i ] ) == 0 ) { divisible [ j ] = true ; cnt ++ ; } } res = Math . max ( res , cnt ) ; } return res ; } var arr = [ 8 , 1 , 28 , 4 , 2 , 6 , 7 ] ; var n = arr . length ; document . write ( findMax ( arr , n ) ) ;
Number of subsets with sum divisible by M | Set 2 | Javascript implementation of the approach ; To store the states of DP ; Function to find the required count ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code
var maxN = 20 var maxM = 10 var dp = Array . from ( Array ( maxN ) , ( ) => Array ( maxM ) ) ; var v = Array . from ( Array ( maxN ) , ( ) => Array ( maxM ) ) ; function findCnt ( arr , i , curr , n , m ) { if ( i == n ) { if ( curr == 0 ) return 1 ; else return 0 ; } if ( v [ i ] [ curr ] ) return dp [ i ] [ curr ] ; v [ i ] [ curr ] = 1 ; dp [ i ] [ curr ] = findCnt ( arr , i + 1 , curr , n , m ) + findCnt ( arr , i + 1 , ( curr + arr [ i ] ) % m , n , m ) ; return dp [ i ] [ curr ] ; } var arr = [ 3 , 3 , 3 , 3 ] ; var n = arr . length ; var m = 6 ; document . write ( findCnt ( arr , 0 , 0 , n , m ) - 1 ) ;
Longest subsequence with a given OR value : Dynamic Programming Approach | Javascript implementation of the approach ; To store the states of DP ; Function to return the required length ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code
var maxN = 20 var maxM = 64 var dp = Array . from ( Array ( maxN ) , ( ) => Array ( maxM ) ) ; var v = Array . from ( Array ( maxN ) , ( ) => Array ( maxM ) ) ; function findLen ( arr , i , curr , n , m ) { if ( i == n ) { if ( curr == m ) return 0 ; else return - 1 ; } if ( v [ i ] [ curr ] ) return dp [ i ] [ curr ] ; v [ i ] [ curr ] = 1 ; var l = findLen ( arr , i + 1 , curr , n , m ) ; var r = findLen ( arr , i + 1 , curr arr [ i ] , n , m ) ; dp [ i ] [ curr ] = l ; if ( r != - 1 ) dp [ i ] [ curr ] = Math . max ( dp [ i ] [ curr ] , r + 1 ) ; return dp [ i ] [ curr ] ; } var arr = [ 3 , 7 , 2 , 3 ] ; var n = arr . length ; var m = 3 ; var ans = findLen ( arr , 0 , 0 , n , m ) ; if ( ans == - 1 ) document . write ( 0 ) ; else document . write ( ans ) ;
Longest subsequence with a given AND value | Javascript implementation of the approach ; To store the states of DP ; Function to return the required length ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code
var maxN = 20 var maxM = 64 var dp = Array . from ( Array ( maxN ) , ( ) => Array ( maxM ) ) ; var v = Array . from ( Array ( maxN ) , ( ) => Array ( maxM ) ) ; function findLen ( arr , i , curr , n , m ) { if ( i == n ) { if ( curr == m ) return 0 ; else return - 1 ; } if ( v [ i ] [ curr ] ) return dp [ i ] [ curr ] ; v [ i ] [ curr ] = 1 ; var l = findLen ( arr , i + 1 , curr , n , m ) ; var r = findLen ( arr , i + 1 , curr & arr [ i ] , n , m ) ; dp [ i ] [ curr ] = l ; if ( r != - 1 ) dp [ i ] [ curr ] = Math . max ( dp [ i ] [ curr ] , r + 1 ) ; return dp [ i ] [ curr ] ; } var arr = [ 3 , 7 , 2 , 3 ] ; var n = arr . length ; var m = 3 ; var ans = findLen ( arr , 0 , ( ( 1 << 8 ) - 1 ) , n , m ) ; if ( ans == - 1 ) document . write ( 0 ) ; else document . write ( ans ) ;
Longest subsequence whose sum is divisible by a given number | Javascript implementation of the approach ; To store the states of DP ; Function to return the length of the longest subsequence whose sum is divisible by m ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code
var maxN = 20 var maxM = 64 var dp = Array . from ( Array ( maxN ) , ( ) => Array ( maxM ) . fill ( 0 ) ) ; var v = Array . from ( Array ( maxN ) , ( ) => Array ( maxM ) . fill ( false ) ) ; function findLen ( arr , i , curr , n , m ) { if ( i == n ) { if ( ! curr ) return 0 ; else return - 1 ; } if ( v [ i ] [ curr ] ) return dp [ i ] [ curr ] ; v [ i ] [ curr ] = 1 ; var l = findLen ( arr , i + 1 , curr , n , m ) ; var r = findLen ( arr , i + 1 , ( curr + arr [ i ] ) % m , n , m ) ; dp [ i ] [ curr ] = l ; if ( r != - 1 ) dp [ i ] [ curr ] = Math . max ( dp [ i ] [ curr ] , r + 1 ) ; return dp [ i ] [ curr ] ; } var arr = [ 3 , 2 , 2 , 1 ] ; var n = arr . length ; var m = 3 ; document . write ( findLen ( arr , 0 , 0 , n , m ) ) ;
Count of subsets with sum equal to X | Javascript implementation of the approach ; To store the states of DP ; Function to return the required count ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code
var maxN = 20 var maxSum = 50 var minSum = 50 var base = 50 var dp = Array . from ( Array ( maxN ) , ( ) => Array ( maxSum + minSum ) ) ; var v = Array . from ( Array ( maxN ) , ( ) => Array ( maxSum + minSum ) ) ; function findCnt ( arr , i , required_sum , n ) { if ( i == n ) { if ( required_sum == 0 ) return 1 ; else return 0 ; } if ( v [ i ] [ required_sum + base ] ) return dp [ i ] [ required_sum + base ] ; v [ i ] [ required_sum + base ] = 1 ; dp [ i ] [ required_sum + base ] = findCnt ( arr , i + 1 , required_sum , n ) + findCnt ( arr , i + 1 , required_sum - arr [ i ] , n ) ; return dp [ i ] [ required_sum + base ] ; } var arr = [ 3 , 3 , 3 , 3 ] ; var n = arr . length ; var x = 6 ; document . write ( findCnt ( arr , 0 , x , n ) ) ;
Number of subsets with a given OR value | Javascript implementation of the approach ; To store states of DP ; Function to return the required count ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code
var maxN = 20 var maxM = 64 var dp = Array . from ( Array ( maxN ) , ( ) => Array ( maxM ) ) ; var v = Array . from ( Array ( maxN ) , ( ) => Array ( maxM ) ) ; function findCnt ( arr , i , curr , n , m ) { if ( i == n ) { return ( curr == m ) ; } if ( v [ i ] [ curr ] ) return dp [ i ] [ curr ] ; v [ i ] [ curr ] = 1 ; dp [ i ] [ curr ] = findCnt ( arr , i + 1 , curr , n , m ) + findCnt ( arr , i + 1 , ( curr arr [ i ] ) , n , m ) ; return dp [ i ] [ curr ] ; } var arr = [ 2 , 3 , 2 ] ; var n = arr . length ; var m = 3 ; document . write ( findCnt ( arr , 0 , 0 , n , m ) ) ;
Number of subsets with a given AND value | Javascript implementation of the approach ; To store states of DP ; Function to return the required count ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code
var maxN = 20 ; var maxM = 64 ; var dp1 = Array . from ( Array ( maxN ) , ( ) => Array ( maxM ) ) ; var v1 = Array . from ( Array ( maxN ) , ( ) => Array ( maxM ) ) ; function findCnt ( arr , i , curr , n , m ) { if ( i == n ) { return ( curr == m ) ; } if ( v1 [ i ] [ curr ] ) return dp1 [ i ] [ curr ] ; v1 [ i ] [ curr ] = 1 ; dp1 [ i ] [ curr ] = findCnt ( arr , i + 1 , curr , n , m ) + findCnt ( arr , i + 1 , ( curr & arr [ i ] ) , n , m ) ; return dp1 [ i ] [ curr ] ; } var arr = [ 0 , 0 , 0 ] ; var n = arr . length ; var m = 0 ; document . write ( findCnt ( arr , 0 , ( ( 1 << 6 ) - 1 ) , n , m ) ) ;
Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13 | javascript implementation of the approach ; Function to find the count of integers obtained by replacing ' ? ' in a given string such that formed integer gives remainder 5 when it is divided by 13 ; Initialise ; Place digit j at ? position ; Get the remainder ; Return the required answer ; Driver code
var MOD = parseInt ( 1e9 + 7 ) ; function modulo_13 ( s , n ) { var dp = Array ( n + 1 ) . fill ( ) . map ( ( ) => Array ( 13 ) . fill ( 0 ) ) ; dp [ 0 ] [ 0 ] = 1 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < 10 ; j ++ ) { var nxt = s . charAt ( i ) - ' ' ; if ( s . charAt ( i ) == ' ' ) nxt = j ; for ( k = 0 ; k < 13 ; k ++ ) { var rem = ( 10 * k + nxt ) % 13 ; dp [ i + 1 ] [ rem ] += dp [ i ] [ k ] ; dp [ i + 1 ] [ rem ] %= MOD ; } if ( s . charAt ( i ) != ' ' ) break ; } } return parseInt ( dp [ n ] [ 5 ] ) ; } var s = " " ; var n = s . length ; document . write ( modulo_13 ( s , n ) ) ;
Longest Increasing Subsequence using Longest Common Subsequence Algorithm | Function to return the size of the longest increasing subsequence ; Create an 2D array of integer for tabulation ; Take the second sequence as the sorted sequence of the given sequence ; Classical Dynamic Programming algorithm for Longest Common Subsequence ; Return the ans ; Driver code
function LISusingLCS ( seq ) { let n = seq . length ; let L = new Array ( n + 1 ) ; for ( let i = 0 ; i < ( n + 1 ) ; i ++ ) { L [ i ] = new Array ( n + 1 ) ; for ( let j = 0 ; j < ( n + 1 ) ; j ++ ) { L [ i ] [ j ] = 0 ; } } let sortedseq = [ ... seq ] ; sortedseq . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i <= n ; i ++ ) { for ( let j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i ] [ j ] = 0 ; else if ( seq [ i - 1 ] == sortedseq [ j - 1 ] ) L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; else L [ i ] [ j ] = Math . max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } return L [ n ] [ n ] ; } let sequence = [ 12 , 34 , 1 , 5 , 40 , 80 ] ; document . write ( LISusingLCS ( sequence ) ) ;
Count of N | Function to return the count of n - digit numbers that satisfy the given conditions ; Base case ; If 0 wasn 't chosen previously ; If 0 wasn 't chosen previously ; Driver code
function count_numbers ( k , n , flag ) { if ( n == 1 ) { if ( flag ) { return ( k - 1 ) ; } else { return 1 ; } } if ( flag ) return ( k - 1 ) * ( count_numbers ( k , n - 1 , 0 ) + count_numbers ( k , n - 1 , 1 ) ) ; else return count_numbers ( k , n - 1 , 1 ) ; } let n = 3 ; let k = 10 ; document . write ( count_numbers ( k , n , true ) ) ;
Count of N | Function to return the count of n - digit numbers that satisfy the given conditions ; DP array to store the pre - caluclated states ; Base cases ; i - digit numbers ending with 0 can be formed by concatenating 0 in the end of all the ( i - 1 ) - digit number ending at a non - zero digit ; i - digit numbers ending with non - zero can be formed by concatenating any non - zero digit in the end of all the ( i - 1 ) - digit number ending with any digit ; n - digit number ending with and ending with non - zero ; Driver code
function count_numbers ( k , n ) { let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) dp [ i ] = new Array ( 2 ) ; dp [ 1 ] [ 0 ] = 0 ; dp [ 1 ] [ 1 ] = k - 1 ; for ( let i = 2 ; i <= n ; i ++ ) { dp [ i ] [ 0 ] = dp [ i - 1 ] [ 1 ] ; dp [ i ] [ 1 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] ) * ( k - 1 ) ; } return dp [ n ] [ 0 ] + dp [ n ] [ 1 ] ; } let k = 10 ; let n = 3 ; document . write ( count_numbers ( k , n ) ) ;
Divide an array into K subarray with the given condition | Function to divide an array into k parts such that the sum of difference of every element with the maximum element of that part is minimum ; Dp to store the values ; Fill up the dp table ; Intitilize maximum value ; Max element and the sum ; Run a loop from i to n ; Find the maximum number from i to l and the sum from i to l ; Find the sum of difference of every element with the maximum element ; If the array can be divided ; Returns the minimum sum in K parts ; Driver code
function divideArray ( arr , n , k ) { var dp = Array . from ( Array ( 500 ) , ( ) => Array ( 500 ) . fill ( 0 ) ) ; k -= 1 ; for ( var i = n - 1 ; i >= 0 ; i -- ) { for ( var j = 0 ; j <= k ; j ++ ) { dp [ i ] [ j ] = 1000000000 ; var max_ = - 1 , sum = 0 ; for ( var l = i ; l < n ; l ++ ) { max_ = Math . max ( max_ , arr [ l ] ) ; sum += arr [ l ] ; var diff = ( l - i + 1 ) * max_ - sum ; if ( j > 0 ) dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , diff + dp [ l + 1 ] [ j - 1 ] ) ; else dp [ i ] [ j ] = diff ; } } } return dp [ 0 ] [ k ] ; } var arr = [ 2 , 9 , 5 , 4 , 8 , 3 , 6 ] ; var n = arr . length ; var k = 2 ; document . write ( divideArray ( arr , n , k ) + " " ) ;
Count of subsets not containing adjacent elements | Function to return the count of possible subsets ; Total possible subsets of n sized array is ( 2 ^ n - 1 ) ; To store the required count of subsets ; Run from i 000. .0 to 111. .1 ; If current subset has consecutive elements from the array ; Driver code
function cntSubsets ( arr , n ) { var max = Math . pow ( 2 , n ) ; var result = 0 ; for ( var i = 0 ; i < max ; i ++ ) { var counter = i ; if ( counter & ( counter >> 1 ) ) continue ; result ++ ; } return result ; } var arr = [ 3 , 5 , 7 ] ; var n = arr . length ; document . write ( cntSubsets ( arr , n ) ) ;
Count of subsets not containing adjacent elements | Function to return the count of possible subsets ; If previous element was 0 then 0 as well as 1 can be appended ; If previous element was 1 then only 0 can be appended ; Store the count of all possible subsets ; Driver code
function cntSubsets ( arr , n ) { var a = Array ( n ) ; var b = Array ( n ) ; a [ 0 ] = b [ 0 ] = 1 ; for ( var i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; b [ i ] = a [ i - 1 ] ; } var result = a [ n - 1 ] + b [ n - 1 ] ; return result ; } var arr = [ 3 , 5 , 7 ] ; var n = arr . length ; document . write ( cntSubsets ( arr , n ) ) ;
Maximum perimeter of a square in a 2D grid | Function to calculate the perfix sum of the rows and the columns of the given matrix ; Number of rows and cols ; First column of the row prefix array ; Update the prefix sum for the rows ; First row of the column prefix array ; Update the prefix sum for the columns ; Function to return the perimeter of the square having top - left corner at ( i , j ) and size k ; i and j represent the top left corner of the square and k is the size ; Get the upper row sum ; Get the left column sum ; At the distance of k in both direction ; The perimeter will be sum of all the values ; Since all the corners are included twice , they need to be subtract from the sum ; Function to return the maximum perimeter of a square in the given matrix ; Number of rows and cols ; Function call to calculate the prefix sum of rows and cols ; To store the maximum perimeter ; Nested loops to choose the top - left corner of the square ; Loop for the size of the square ; Get the perimeter of the current square ; Update the maximum perimeter so far ; Driver code
function perfix_calculate ( A , row , col ) { let n = A . length ; let m = A [ 0 ] . length ; for ( let i = 0 ; i < n ; ++ i ) { row [ i ] [ 0 ] = A [ i ] [ 0 ] ; } for ( let i = 0 ; i < n ; ++ i ) { for ( let j = 1 ; j < m ; ++ j ) { row [ i ] [ j ] = row [ i ] [ j - 1 ] + A [ i ] [ j ] ; } } for ( let i = 0 ; i < m ; ++ i ) { col [ 0 ] [ i ] = A [ 0 ] [ i ] ; } for ( let i = 0 ; i < m ; ++ i ) { for ( let j = 1 ; j < n ; ++ j ) { col [ j ] [ i ] = A [ j ] [ i ] + col [ j - 1 ] [ i ] ; } } } function perimeter ( i , j , k , row , col , A ) { let row_s , col_s ; if ( j == 0 ) row_s = 0 ; else row_s = row [ i ] [ j - 1 ] ; if ( i == 0 ) col_s = 0 ; else col_s = col [ i - 1 ] [ j ] ; let upper_row = row [ i ] [ j + k ] - row_s ; let left_col = col [ i + k ] [ j ] - col_s ; if ( j == 0 ) row_s = 0 ; else row_s = row [ i + k ] [ j - 1 ] ; if ( i == 0 ) col_s = 0 ; else col_s = col [ i - 1 ] [ j + k ] ; let lower_row = row [ i + k ] [ j + k ] - row_s ; let right_col = col [ i + k ] [ j + k ] - col_s ; let sum = upper_row + lower_row + left_col + right_col ; sum -= ( A [ i ] [ j ] + A [ i + k ] [ j ] + A [ i ] [ j + k ] + A [ i + k ] [ j + k ] ) ; return sum ; } function maxPerimeter ( A ) { let n = A . length ; let m = A [ 0 ] . length ; let row = new Array ( n ) ; let col = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { row [ i ] = new Array ( m ) ; col [ i ] = new Array ( m ) ; } perfix_calculate ( A , row , col ) ; let maxPer = 0 ; for ( let i = 0 ; i < n ; ++ i ) { for ( let j = 0 ; j < m ; ++ j ) { for ( let k = 0 ; k < Math . min ( n - i , m - j ) ; ++ k ) { let perimtr = perimeter ( i , j , k , row , col , A ) ; maxPer = Math . max ( maxPer , perimtr ) ; } } } return maxPer ; } let A = [ [ 1 , 1 , 0 ] , [ 1 , 1 , 1 ] , [ 0 , 1 , 1 ] ] ; document . write ( maxPerimeter ( A ) ) ;
Optimal Strategy for a Game | Set 3 | Calculates the maximum score possible for P1 If only the bags from beg to ed were available ; Length of the game ; Which turn is being played ; Base case i . e last turn ; if it is P1 's turn ; if P2 's turn ; Player picks money from the left end ; Player picks money from the right end ; if it is player 1 's turn then  current picked score added to  his total. choose maximum of  the two scores as P1 tries to  maximize his score. ; if player 2 ' s ▁ turn ▁ don ' t add current picked bag score to total . choose minimum of the two scores as P2 tries to minimize P1 's score. ; Input array ; Function Calling
function maxScore ( money , beg , ed ) { let totalTurns = money . length ; let turnsTillNow = beg + ( ( totalTurns - 1 ) - ed ) ; if ( beg == ed ) { if ( turnsTillNow % 2 == 0 ) return [ money [ beg ] , " " ] ; else return [ 0 , " " ] ; } let scoreOne = maxScore ( money , beg + 1 , ed ) ; let scoreTwo = maxScore ( money , beg , ed - 1 ) ; if ( turnsTillNow % 2 == 0 ) { if ( money [ beg ] + scoreOne [ 0 ] > money [ ed ] + scoreTwo [ 0 ] ) { return [ money [ beg ] + scoreOne [ 0 ] , " " + scoreOne [ 1 ] ] ; } else return [ money [ ed ] + scoreTwo [ [ 0 ] ] , " " + scoreTwo [ 1 ] ] ; } else { if ( scoreOne . first < scoreTwo . first ) return [ scoreOne [ 0 ] , " " + scoreOne [ 1 ] ] ; else return [ scoreTwo [ 0 ] , " " + scoreTwo [ 1 ] ] ; } } let ar = [ 10 , 80 , 90 , 30 ] ; let arraySize = ar . length ; let bags = ar ; let ans = maxScore ( bags , 0 , bags . length - 1 ) ; document . write ( ans [ 0 ] + " " + ans [ 1 ] + " " ) ;
Minimum number of Fibonacci jumps to reach end | A Dynamic Programming based Javascript program to find minimum number of jumps to reach Destination ; Function that returns the min number of jump to reach the destination ; We consider only those Fibonacci numbers which are less than n , where we can consider fib [ 30 ] to be the upper bound as this will cross 10 ^ 5 ; DP [ i ] will be storing the minimum number of jumps required for the position i . So DP [ N + 1 ] will have the result we consider 0 as source and N + 1 as the destination ; Base case ( Steps to reach source is ) ; Initialize all table values as Infinite ; Go through each positions till destination . ; Calculate the minimum of that position if all the Fibonacci numbers are considered ; If the position is safe or the position is the destination then only we calculate the minimum otherwise the cost is MAX as default ; - 1 denotes if there is no path possible ; Driver program to test above function
const MAX = 1000000000 ; function minJumps ( arr , N ) { let fib = new Array ( 30 ) ; fib [ 0 ] = 0 ; fib [ 1 ] = 1 ; for ( let i = 2 ; i < 30 ; i ++ ) fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; let DP = new Array ( N + 2 ) ; DP [ 0 ] = 0 ; for ( let i = 1 ; i <= N + 1 ; i ++ ) DP [ i ] = MAX ; for ( let i = 1 ; i <= N + 1 ; i ++ ) { for ( let j = 1 ; j < 30 ; j ++ ) { if ( ( arr [ i - 1 ] == 1 i == N + 1 ) && i - fib [ j ] >= 0 ) DP [ i ] = Math . min ( DP [ i ] , 1 + DP [ i - fib [ j ] ] ) ; } } if ( DP [ N + 1 ] != MAX ) return DP [ N + 1 ] ; else return - 1 ; } let arr = [ 0 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 , 0 , 0 ] ; let n = arr . length ; document . write ( minJumps ( arr , n ) ) ;
Subsequence X of length K such that gcd ( X [ 0 ] , X [ 1 ] ) + ( X [ 2 ] , X [ 3 ] ) + ... is maximized | Javascript program to find the sum of the addition of all possible subsets ; Recursive function to find the maximum value of the given recurrence ; If we get K elements ; If we have reached the end and K elements are not there ; If the state has been visited ; Iterate for every element as the next possible element and take the element which gives the maximum answer ; If this element is the first element in the individual pair in the subsequence then simply recurrence with the last element as i - th index ; If this element is the second element in the individual pair , the find gcd with the previous element and add to the answer and recur for the next element ; Driver Code
let MAX = 100 ; function recur ( ind , cnt , last , a , n , k , dp ) { if ( cnt == k ) return 0 ; if ( ind == n ) return - 1e9 ; if ( dp [ ind ] [ cnt ] != - 1 ) return dp [ ind ] [ cnt ] ; let ans = 0 ; for ( let i = ind ; i < n ; i ++ ) { if ( cnt % 2 == 0 ) ans = Math . max ( ans , recur ( i + 1 , cnt + 1 , i , a , n , k , dp ) ) ; else ans = Math . max ( ans , __gcd ( a [ last ] , a [ i ] ) + recur ( i + 1 , cnt + 1 , 0 , a , n , k , dp ) ) ; } return dp [ ind ] [ cnt ] = ans ; } function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } let a = [ 4 , 5 , 3 , 7 , 8 , 10 , 9 , 8 ] ; let n = a . length ; let k = 4 ; let dp = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { dp [ i ] = new Array ( MAX ) ; for ( let j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } document . write ( recur ( 0 , 0 , 0 , a , n , k , dp ) ) ;