codeparrot/xlcost-text-to-code huggingface dataset:
Maximum sum by picking elements from two arrays in order | Set 2 | Function to calculate maximum sum ; Maximum elements that can be chosen from array A ; Maximum elements that can be chosen from array B ; Stores the maximum sum possible ; Fill the dp [ ] [ ] for base case when all elements are selected from A [ ] ; Fill the dp [ ] [ ] for base case when all elements are selected from B [ ] ; Return the final answer ; Driver Program
function maximumSum ( A , B , length , X , Y ) { var l = length ; var l1 = Math . min ( length , X ) ; var l2 = Math . min ( length , Y ) ; var dp = Array . from ( Array ( l1 + 1 ) , ( ) => Array ( l2 + 1 ) . fill ( 0 ) ) ; dp [ 0 ] [ 0 ] = 0 ; var max_sum = - 1000000000 ; for ( var i = 1 ; i <= l1 ; i ++ ) { dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + A [ i - 1 ] ; max_sum = Math . max ( max_sum , dp [ i ] [ 0 ] ) ; } for ( var i = 1 ; i <= l2 ; i ++ ) { dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + B [ i - 1 ] ; max_sum = Math . max ( max_sum , dp [ 0 ] [ i ] ) ; } for ( var i = 1 ; i <= l1 ; i ++ ) { for ( var j = 1 ; j <= l2 ; j ++ ) { if ( i + j <= l ) dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] + A [ i + j - 1 ] , dp [ i ] [ j - 1 ] + B [ i + j - 1 ] ) ; max_sum = Math . max ( dp [ i ] [ j ] , max_sum ) ; } } return max_sum ; } var A = [ 1 , 2 , 3 , 4 , 5 ] ; var B = [ 5 , 4 , 3 , 2 , 1 ] ; var X = 3 , Y = 2 ; var N = A . length ; document . write ( maximumSum ( A , B , N , X , Y ) ) ;
Min number of operations to reduce N to 0 by subtracting any digits from N | Function to reduce an integer N to Zero in minimum operations by removing digits from N ; Initialise dp [ ] to steps ; Iterate for all elements ; For each digit in number i ; Either select the number or do not select it ; dp [ N ] will give minimum step for N ; Given Number ; Function Call
function reduceZero ( N ) { var dp = Array ( N + 1 ) . fill ( 1000000000 ) ; dp [ 0 ] = 0 ; for ( var i = 0 ; i <= N ; i ++ ) { for ( var j = 0 ; j < i . toString ( ) . length ; j ++ ) { dp [ i ] = Math . min ( dp [ i ] , dp [ i - ( i . toString ( ) [ j ] - ' ' ) ] + 1 ) ; } } return dp [ N ] ; } var N = 25 ; document . write ( reduceZero ( N ) ) ;
Pentanacci Numbers | Function to print Nth Pentanacci number ; Initialize first five numbers to base cases ; Declare a current variable ; Loop to add previous five numbers for each number starting from 5 and then assign first , second , third , fourth to second , third , fourth and curr to fifth respectively ; Driver code
function printpenta ( n ) { if ( n < 0 ) return ; let first = 0 ; let second = 0 ; let third = 0 ; let fourth = 0 ; let fifth = 1 ; let curr = 0 ; if ( n == 0 n == 1 n == 2 n == 3 ) document . write ( first + " " ) ; else if ( n == 5 ) document . write ( fifth + " " ) ; else { for ( let i = 5 ; i < n ; i ++ ) { curr = first + second + third + fourth + fifth ; first = second ; second = third ; third = fourth ; fourth = fifth ; fifth = curr ; } document . write ( curr + " " ) ; } } let n = 10 ; printpenta ( n ) ;
Count of binary strings of length N with even set bit count and at most K consecutive 1 s | Table to store solution of each subproblem ; Function to calculate the possible binary strings ; If number of ones is equal to K ; pos : current position Base Case : When n length is traversed ; sum : count of 1 ' s ▁ ▁ Return ▁ the ▁ count ▁ ▁ of ▁ 1' s obtained ; If the subproblem has already been solved ; Return the answer ; Recursive call when current position is filled with 1 ; Recursive call when current position is filled with 0 ; Store the solution to this subproblem ; Driver Code ; Initialising the table with - 1
let dp = new Array ( 100001 ) . fill ( - 1 ) . map ( ( t ) => new Array ( 20 ) . fill ( - 1 ) . map ( ( r ) => new Array ( 2 ) . fill ( - 1 ) ) ) ; function possibleBinaries ( pos , ones , sum , k ) { if ( ones == k ) return 0 ; if ( pos == 0 ) return ( sum == 0 ) ? 1 : 0 ; if ( dp [ pos ] [ ones ] [ sum ] != - 1 ) return dp [ pos ] [ ones ] [ sum ] ; let ret = possibleBinaries ( pos - 1 , ones + 1 , ( sum + 1 ) % 2 , k ) + possibleBinaries ( pos - 1 , 0 , sum , k ) ; dp [ pos ] [ ones ] [ sum ] = ret ; return dp [ pos ] [ ones ] [ sum ] ; } let N = 3 ; let K = 2 ; document . write ( possibleBinaries ( N , 0 , 0 , K ) ) ;
Count of ways to traverse a Matrix and return to origin in K steps | JavaScript program to count total number of ways to return to origin after completing given number of steps . ; Initialize [ , ] dp [ ] array with - 1 ; Function returns the total count ; Driver Program
var MOD = 1000000007 ; var dp = Array . from ( Array ( 101 ) , ( ) => Array ( 101 ) ) ; var N , M , K ; for ( var i = 0 ; i < 101 ; i ++ ) for ( var j = 0 ; j < 101 ; j ++ ) dp [ i ] [ j ] = new Array ( 101 ) . fill ( - 1 ) ; function CountWays ( i , j , k ) { if ( i >= N i < 0 j >= M j < 0 k < 0 ) return 0 ; if ( i == 0 && j == 0 && k == 0 ) return 1 ; if ( dp [ i ] [ j ] [ k ] != - 1 ) return dp [ i ] [ j ] [ k ] ; else dp [ i ] [ j ] [ k ] = ( CountWays ( i + 1 , j , k - 1 ) % MOD + CountWays ( i - 1 , j , k - 1 ) % MOD + CountWays ( i , j - 1 , k - 1 ) % MOD + CountWays ( i , j + 1 , k - 1 ) % MOD + CountWays ( i , j , k - 1 ) % MOD ) % MOD ; return dp [ i ] [ j ] [ k ] ; } N = 3 ; M = 3 ; K = 4 ; document . write ( CountWays ( 0 , 0 , K ) )
Count of subsequences of length atmost K containing distinct prime elements | Javascript Program to find the count of distinct prime subsequences at most of of length K from a given array ; Initialize all indices as true ; A value in prime [ i ] will finally be false if i is not a prime , else true ; If prime [ p ] is true , then it is a prime ; Update all multiples of p as false , i . e . non - prime ; Returns number of subsequences of maximum length k and contains distinct primes ; Store the primes in the given array ; Sort the primes ; Store the frequencies of all the distinct primes ; Store the frequency of primes ; Store the sum of all frequencies ; Store the length of subsequence at every instant ; Store the frequency ; Store the previous count of updated DP ; Calculate total subsequences of current of_length ; Add the number of subsequences to the answer ; Update the value in dp [ i ] ; Store the updated dp [ i ] ; Driver Code
let prime = new Array ( 100001 ) ; function SieveOfEratosthenes ( ) { prime . fill ( true ) prime [ 0 ] = prime [ 1 ] = false ; for ( let p = 2 ; p * p <= 100000 ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * p ; i <= 100000 ; i += p ) prime [ i ] = false ; } } } function distinctPrimeSubSeq ( a , n , k ) { SieveOfEratosthenes ( ) ; let primes = new Array ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( prime [ a [ i ] ] ) primes . push ( a [ i ] ) ; } let l = primes . length ; primes . sort ( ( a , b ) => a - b ) let b = new Array ( ) ; let dp = new Array ( ) ; let sum = 0 ; for ( let i = 0 ; i < l ; ) { let count = 1 , x = a [ i ] ; i ++ ; while ( i < l && a [ i ] == x ) { count ++ ; i ++ ; } b . push ( count ) ; dp . push ( count ) ; sum += count ; } let of_length = 2 ; let len = dp . length ; let ans = 0 ; while ( of_length <= k ) { let freq = 0 ; let prev = 0 ; for ( let i = 0 ; i < ( len - 1 ) ; i ++ ) { freq += dp [ i ] ; let j = sum - freq ; let subseq = b [ i ] * j ; ans += subseq ; dp [ i ] = subseq ; prev += dp [ i ] ; } len -- ; sum = prev ; of_length ++ ; } ans += ( l + 1 ) ; return ans ; } let a = [ 1 , 2 , 2 , 3 , 3 , 4 , 5 ] ; let n = a . length ; let k = 3 ; document . write ( distinctPrimeSubSeq ( a , n , k ) ) ;
Minimize prize count required such that smaller value gets less prize in an adjacent pair | Function to find the minimum number of required such that adjacent smaller elements gets less number of prizes ; Loop to iterate over every elements of the array ; Loop to find the consecutive smaller elements at left ; Loop to find the consecutive smaller elements at right ; Driver Code
function findMinPrizes ( arr , n ) { let totalPrizes = 0 , j , x , y ; for ( let i = 0 ; i < n ; i ++ ) { x = 1 ; j = i ; while ( j > 0 && arr [ j ] > arr [ j - 1 ] ) { x ++ ; j -- ; } j = i ; y = 1 ; while ( j < n - 1 && arr [ j ] > arr [ j + 1 ] ) { y ++ ; j ++ ; } totalPrizes += Math . max ( x , y ) ; } document . write ( totalPrizes ) ; return 0 ; } let arr = [ 1 , 2 , 2 , 3 ] ; let n = arr . length ; findMinPrizes ( arr , n ) ;
Number of ways to split N as sum of K numbers from the given range | JavaScript implementation to count the number of ways to divide N in K groups such that each group has elements in range [ L , R ] ; DP Table ; Function to count the number of ways to divide the number N in K groups such that each group has number of elements in range [ L , R ] ; Base Case ; if N is divides completely into less than k groups ; If the subproblem has been solved , use the value ; put all possible values greater equal to prev ; Function to count the number of ways to divide the number N ; Initialize DP Table as - 1 ; Driver Code
var mod = 1000000007 ; var dp = Array . from ( Array ( 1000 ) , ( ) => Array ( 1000 ) ) ; function calculate ( pos , left , k , L , R ) { if ( pos == k ) { if ( left == 0 ) return 1 ; else return 0 ; } if ( left == 0 ) return 0 ; if ( dp [ pos ] [ left ] != - 1 ) return dp [ pos ] [ left ] ; var answer = 0 ; for ( var i = L ; i <= R ; i ++ ) { if ( i > left ) break ; answer = ( answer + calculate ( pos + 1 , left - i , k , L , R ) ) % mod ; } return dp [ pos ] [ left ] = answer ; } function countWaystoDivide ( n , k , L , R ) { dp = Array . from ( Array ( 1000 ) , ( ) => Array ( 1000 ) . fill ( - 1 ) ) ; return calculate ( 0 , n , k , L , R ) ; } var N = 12 ; var K = 3 ; var L = 1 ; var R = 5 ; document . write ( countWaystoDivide ( N , K , L , R ) ) ;
Minimum number of squares whose sum equals to given number N | set 2 | Function for finding minimum square numbers ; A [ i ] of array arr store minimum count of square number to get i ; sqrNum [ i ] store last square number to get i ; Initialize ; Find minimum count of square number for all value 1 to n ; In worst case it will be arr [ i - 1 ] + 1 we use all combination of a [ i - 1 ] and add 1 ; Check for all square number less or equal to i ; if it gives less count then update it ; Vector v stores optimum square number whose sum give N ; Driver code ; Calling function ; Printing vector
function minSqrNum ( n ) { var arr = Array ( n + 1 ) , k ; var sqrNum = Array ( n + 1 ) ; var v = [ ] ; arr [ 0 ] = 0 ; sqrNum [ 0 ] = 0 ; for ( var i = 1 ; i <= n ; i ++ ) { arr [ i ] = arr [ i - 1 ] + 1 ; sqrNum [ i ] = 1 ; k = 1 ; while ( k * k <= i ) { if ( arr [ i ] > arr [ i - k * k ] + 1 ) { arr [ i ] = arr [ i - k * k ] + 1 ; sqrNum [ i ] = k * k ; } k ++ ; } } while ( n > 0 ) { v . push ( sqrNum [ n ] ) ; n -= sqrNum [ n ] ; } return v ; } var n = 10 ; var v = [ ] ; v = minSqrNum ( n ) ; for ( var i = 0 ; i < v . length ; i ++ ) { document . write ( v [ i ] ) ; if ( i + 1 != v . length ) document . write ( " " ) ; }