codeparrot/xlcost-text-to-code huggingface dataset:
Bin Packing Problem ( Minimize number of used Bins ) | Returns number of bins required using first fit online algorithm ; Initialize result ( Count of bins ) ; Create an array to store remaining space in bins there can be at most n bins ; Place items one by one ; Find the first bin that can accommodate weight [ i ] ; If no bin could accommodate weight [ i ] ; Driver program
function firstFit ( weight , n , c ) { let res = 0 ; let bin_rem = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { let j ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] ) { bin_rem [ j ] = bin_rem [ j ] - weight [ i ] ; break ; } } if ( j == res ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } } return res ; } let weight = [ 2 , 5 , 4 , 7 , 1 , 3 , 8 ] ; let c = 10 ; let n = weight . length ; document . write ( " " + firstFit ( weight , n , c ) ) ;
Minimum cost to reduce A and B to 0 using square root or divide by 2 | Function to return the minimum cost of converting A and B to 0 ; If both A and B doesn 't change in  this recusrive call, then return INT_MAX  to save the code from going into  infinite loop ; Base Case ; If the answer of this recursive call is already memoised ; If A is reduced to A / 2 ; If B is reduced to B / 2 ; If both A and B is reduced to sqrt ( A * B ) ; Return the minimum of the value given by the above three subproblems , also memoize the value while returning ; Driver Code
function getMinOperations ( A , B , prevA , prevB , dp ) { if ( A == prevA && B == prevB ) { return Number . MAX_SAFE_INTEGER ; } if ( A == 0 && B == 0 ) { return 0 ; } if ( dp [ A ] [ B ] != - 1 ) { return dp [ A ] [ B ] ; } let ans1 = getMinOperations ( Math . floor ( A / 2 ) , B , A , B , dp ) ; if ( ans1 != Number . MAX_SAFE_INTEGER ) { ans1 += 1 ; } let ans2 = getMinOperations ( A , Math . floor ( B / 2 ) , A , B , dp ) ; if ( ans2 != Number . MAX_SAFE_INTEGER ) { ans2 += 1 ; } let ans3 = getMinOperations ( Math . floor ( Math . sqrt ( A * B ) ) , Math . floor ( Math . sqrt ( A * B ) ) , A , B , dp ) ; if ( ans3 != Number . MAX_SAFE_INTEGER ) { ans3 += 2 ; } return ( dp [ A ] [ B ] = Math . min ( ans1 , Math . min ( ans2 , ans3 ) ) ) ; } let A = 53 , B = 16 ; let mx = Math . max ( A , B ) ; let dp = new Array ( mx + 1 ) . fill ( 0 ) . map ( ( ) => new Array ( mx + 1 ) . fill ( - 1 ) ) ; document . write ( getMinOperations ( A , B , - 1 , - 1 , dp ) ) ;
Maximize count of indices with same element by pairing rows from given Matrices | JavaScript Program to implement the above approach ; Function to find the maximum score ; Stores the maximum sum of scores ; For each permutation of pos vector calculate the score ; If values at current indexes are same then increment the current score ; Print the maximum score ; Driver Code
function next_permutation ( array ) { var i = array . length - 1 ; while ( i > 0 && array [ i - 1 ] >= array [ i ] ) { i -- ; } if ( i <= 0 ) { return false ; } var j = array . length - 1 ; while ( array [ j ] <= array [ i - 1 ] ) { j -- ; } var temp = array [ i - 1 ] ; array [ i - 1 ] = array [ j ] ; array [ j ] = temp ; j = array . length - 1 ; while ( i < j ) { temp = array [ i ] ; array [ i ] = array [ j ] ; array [ j ] = temp ; i ++ ; j -- ; } return array ; } function maxScoreSum ( a , b ) { let maxSum = 0 ; let pos = [ ] ; for ( let i = 0 ; i < a . length ; i ++ ) { pos . push ( i ) ; } do { let curSum = 0 ; for ( let i = 0 ; i < a . length ; i ++ ) { for ( let j = 0 ; j < a [ pos [ i ] ] . length ; j ++ ) { curSum += ( a [ pos [ i ] ] [ j ] == b [ i ] [ j ] ) ; maxSum = Math . max ( maxSum , curSum ) ; } } } while ( next_permutation ( pos ) ) ; document . write ( maxSum ) ; } let N = 3 , M = 3 ; let a = [ [ 1 , 1 , 0 ] , [ 1 , 0 , 1 ] , [ 0 , 0 , 1 ] ] ; let b = [ [ 1 , 0 , 0 ] , [ 0 , 0 , 1 ] , [ 1 , 1 , 0 ] ] ; maxScoreSum ( a , b ) ;
Split given arrays into subarrays to maximize the sum of maximum and minimum in each subarrays | Function to find the maximum sum of differences of subarrays by splitting array into non - overlapping subarrays ; Stores the answer for prefix and initialize with zero ; Assume i - th index as right endpoint ; Choose the current value as the maximum and minimum ; Find the left endpoint and update the array dp [ ] ; Return answer ; Driver Code
function maxDiffSum ( arr , n ) { let dp = new Array ( n ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { let maxVal = arr [ i ] , minVal = arr [ i ] ; for ( let j = i ; j >= 0 ; j -- ) { minVal = Math . min ( minVal , arr [ j ] ) ; maxVal = Math . max ( maxVal , arr [ j ] ) ; if ( j - 1 >= 0 ) dp [ i ] = Math . max ( dp [ i ] , maxVal - minVal + dp [ j - 1 ] ) ; else dp [ i ] = Math . max ( dp [ i ] , maxVal - minVal ) ; } } return dp [ n - 1 ] ; } let arr = [ 8 , 1 , 7 , 9 , 2 ] ; let N = arr . length ; document . write ( maxDiffSum ( arr , N ) ) ;
Count of N | JavaScript Program to implement the above approach ; Find the value of x raised to the yth power modulo MOD ; Stores the value of x ^ y ; Iterate until y is positive ; Function to perform the Modular Multiplicative Inverse using the Fermat 's little theorem ; Modular division x / y , find modular multiplicative inverse of y and multiply by x ; Function to find Binomial Coefficient C ( n , k ) in O ( k ) time ; Base Case ; Update the value of p and q ; Function to find the count of arrays having K as the first element satisfying the given criteria ; Stores the resultant count of arrays ; Find the factorization of K ; Stores the count of the exponent of the currentprime factor ; N is one last prime factor , for c = 1 -> C ( N - 1 + 1 , 1 ) = N ; Return the totol count ; Driver Code
let MOD = 1000000007 ; function modPow ( x , y ) { let r = 1 , a = x ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) { r = ( r * a ) % MOD ; } a = ( a * a ) % MOD ; y /= 2 ; } return r ; } function modInverse ( x ) { return modPow ( x , MOD - 2 ) ; } function modDivision ( p , q ) { return ( p * modInverse ( q ) ) % MOD ; } function C ( n , k ) { if ( k > n ) { return 0 ; } let p = 1 , q = 1 ; for ( let i = 1 ; i <= k ; i ++ ) { q = ( q * i ) % MOD ; p = ( p * ( n - i + 1 ) ) % MOD ; } return modDivision ( p , q ) ; } function countArrays ( N , K ) { let res = 1 ; for ( let p = 2 ; p <= K / p ; p ++ ) { let c = 0 ; while ( K % p == 0 ) { K /= p ; c ++ ; } res = ( res * C ( N - 1 + c , c ) ) % MOD ; } if ( N > 1 ) { res = ( res * N ) % MOD ; } return res ; } let N = 3 , K = 5 ; document . write ( countArrays ( N , K ) ) ;
Minimum number of days to debug all programs | Function to calculate the minimum work sessions ; Break condition ; All bits are set ; Check if already calculated ; Store the answer ; Check if ith bit is set or unset ; Including in current work session ; Including in next work session ; Resultant answer will be minimum of both ; Function to initialize DP array and solve the problem ; Initialize dp table with - 1 ; Resultant mask ; no . of minimum work sessions is even ; no . of minimum work sessions is odd ; Driver code
function minSessions ( codeTime , dp , ones , n , mask , currTime , WorkingSessionTime ) { if ( currTime > WorkingSessionTime ) return Number . MAX_VALUE ; if ( mask == ones ) return 1 ; if ( dp [ mask ] [ currTime ] != - 1 ) return dp [ mask ] [ currTime ] ; let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { if ( ( mask & ( 1 << i ) ) == 0 ) { let inc = minSessions ( codeTime , dp , ones , n , mask | ( 1 << i ) , currTime + codeTime [ i ] , WorkingSessionTime ) ; let inc_next = 1 + minSessions ( codeTime , dp , ones , n , mask | ( 1 << i ) , codeTime [ i ] , WorkingSessionTime ) ; ans = Math . min ( ans , Math . min ( inc , inc_next ) ) ; } } return dp [ mask ] [ currTime ] = ans ; } function solve ( codeTime , n , WorkingSessionTime ) { let dp = new Array ( 1 << 14 ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 15 ) . fill ( - 1 ) ; } let ones = ( 1 << n ) - 1 ; let ans = minSessions ( codeTime , dp , ones , n , 0 , 0 , WorkingSessionTime ) ; if ( WorkingSessionTime < 6 ) { if ( ans % 2 == 0 ) ans = Math . floor ( ans / 2 ) ; else ans = Math . floor ( ans / 2 ) + 1 ; } return ans ; } let codeTime = [ 1 , 2 , 3 , 1 , 1 , 3 ] ; let n = codeTime . length ; let WorkingSessionTime = 4 ; document . write ( solve ( codeTime , n , WorkingSessionTime ) )
Count of non | JavaScript Program to implement the above approach ; Define the dp table globally ; Recursive function to calculate total number of valid non - decreasing strings ; If already calculated state ; Base Case ; Stores the total count of strings formed ; Fill the value in dp matrix ; Function to find the total number of non - decreasing string formed by replacing the ' ? ' ; Initialize all value of dp table with - 1 ; Left and Right limits ; Iterate through all the characters of the string S ; Change R to the current character ; Call the recursive function ; Change L to R and R to 9 ; Reinitialize the length of ? to 0 ; Increment the length of the segment ; Update the ans ; Return the total count ; Driver Code
let MAXN = 100005 let dp = new Array ( MAXN ) . fill ( new Array ( 10 ) ) ; function solve ( len , gap ) { if ( dp [ len ] [ gap ] != - 1 ) { return dp [ len ] [ gap ] ; } if ( len == 0 gap == 0 ) { return 1 ; } if ( gap < 0 ) { return 0 ; } let ans = 0 ; for ( let i = 0 ; i <= gap ; i ++ ) { ans += solve ( len - 1 , gap - i ) ; } return dp [ len ] [ gap ] = ans ; } function countValidStrings ( S ) { for ( let i = 0 ; i < dp . length ; i ++ ) { for ( let j = 0 ; j < dp [ i ] . length ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } let N = S . length ; let L = 1 , R = 9 ; let cnt = 0 ; let ans = 1 ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] != ' ' ) { R = S . charCodeAt ( i ) - ' ' . charCodeAt ( 0 ) ; ans *= solve ( cnt , R - L ) ; L = R ; R = 9 ; cnt = 0 ; } else { cnt ++ ; } } ans *= solve ( cnt , R - L ) ; return ans ; } let S = " " ; document . write ( countValidStrings ( S ) ) ;
K | Recursive function to find the Kth smallest binary string ; Base Case ; Return string of all 1 's  of length B ; Return string of all 0 's  of length A ; Function to find the Kth lexicographically smallest binary string with exactly A zeroes and B ones ; Stores the recurring states ; Calculate the dp values iteratively ; The last character was '0' ; The last character was '1' ; Print the binary string obtained ; Driver Code
function KthString ( A , B , K , dp ) { if ( A == 0 ) { let str = " " ; for ( let i = 0 ; i < B ; i ++ ) { str += ' ' ; } return str ; } if ( B == 0 ) { let str = " " ; for ( let i = 0 ; i < A ; i ++ ) { str += ' ' ; } return str ; } if ( K <= dp [ A - 1 ] [ B ] ) { return " " + KthString ( A - 1 , B , K , dp ) ; } else { return " " + KthString ( A , B - 1 , K - dp [ A - 1 ] [ B ] , dp ) ; } } function KthStringUtil ( A , B , K ) { let dp = new Array ( A + 1 ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( B + 1 ) . fill ( 0 ) ; } dp [ 0 ] [ 0 ] = 1 ; for ( let i = 0 ; i <= A ; ++ i ) { for ( let j = 0 ; j <= B ; ++ j ) { if ( i > 0 ) { dp [ i ] [ j ] += dp [ i - 1 ] [ j ] ; } if ( j > 0 ) { dp [ i ] [ j ] += dp [ i ] [ j - 1 ] ; } } } document . write ( KthString ( A , B , K , dp ) ) ; return 0 ; } let A = 3 , B = 3 , K = 7 ; KthStringUtil ( A , B , K ) ;
Count of binary arrays of size N with sum of product of adjacent pairs equal to K | Function to return the number of total possible combinations of 0 and 1 to form an array of size N having sum of product of consecutive elements K ; If value is greater than K , then return 0 as no combination is possible to have sum K ; Check if the result of this recursive call is memoised already , if it is then just return the previously calculated result ; Check if the value is equal to K at N , if it is then return 1 as this combination is possible . Otherwise return 0. ; If previous element is 1 ; If current element is 1 as well , then add 1 to value ; If current element is 0 , then value will remain same ; If previous element is 0 , then value will remain same irrespective of the current element ; Memoise and return the ans ; Driver Code ; As the array can be started by 0 or 1 , so take both cases while calculating the total possible combinations
function combinationsPossible ( N , idx , prev , val , K , dp ) { if ( val > K ) { return 0 ; } if ( dp [ val ] [ idx ] [ prev ] != - 1 ) { return dp [ val ] [ idx ] [ prev ] ; } if ( idx == N - 1 ) { if ( val == K ) { return 1 ; } return 0 ; } let ans = 0 ; if ( prev == 1 ) { ans += combinationsPossible ( N , idx + 1 , 1 , val + 1 , K , dp ) ; ans += combinationsPossible ( N , idx + 1 , 0 , val , K , dp ) ; } else { ans += combinationsPossible ( N , idx + 1 , 1 , val , K , dp ) ; ans += combinationsPossible ( N , idx + 1 , 0 , val , K , dp ) ; } return dp [ val ] [ idx ] [ prev ] = ans ; } let N = 5 ; let K = 3 ; let dp = new Array ( K + 1 ) ; for ( i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( N + 1 ) ; } for ( i = 0 ; i < dp . length ; i ++ ) { for ( j = 0 ; j < dp [ 0 ] . length ; j ++ ) { dp [ i ] [ j ] = new Array ( 2 ) . fill ( - 1 ) ; } } document . write ( combinationsPossible ( N , 0 , 0 , 0 , K , dp ) + combinationsPossible ( N , 0 , 1 , 0 , K , dp ) ) ;
Count ways to split an array into subarrays such that sum of the i | Function to count ways to split an array into subarrays such that sum of the i - th subarray is divisible by i ; Stores the prefix sum of array ; Find the prefix sum ; Initialize dp [ ] [ ] array ; Stores the count of splitting ; Iterate over the range [ 0 , N ] ; Update the dp table ; If the last index is reached , then add it to the variable ans ; Return the possible count of splitting of array into subarrays ; Driver Code
function countOfWays ( arr , N ) { let pre = new Array ( N + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { pre [ i + 1 ] = pre [ i ] + arr [ i ] ; } let dp = new Array ( N + 2 ) . fill ( 0 ) . map ( ( ) => new Array ( N + 2 ) . fill ( 0 ) ) ; dp [ 1 ] [ 0 ] ++ ; let ans = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = N ; j >= 1 ; j -- ) { dp [ j + 1 ] [ pre [ i + 1 ] % ( j + 1 ) ] += dp [ j ] [ pre [ i + 1 ] % j ] ; if ( i == N - 1 ) { ans += dp [ j ] [ pre [ i + 1 ] % j ] ; } } } return ans ; } let arr = [ 1 , 2 , 3 , 4 ] ; let N = arr . length ; document . write ( countOfWays ( arr , N ) ) ;
Queries to find minimum absolute difference between adjacent array elements in given ranges | Javascript program for the above approach ; Stores the index for the minimum value in the subarray arr [ i , j ] ; Structure for query range ; Function to fill the lookup array lookup [ ] [ ] in the bottom up manner ; Initialize M for the intervals with length 1 ; Find the values from smaller to bigger intervals ; Compute minimum value for all intervals with size 2 ^ j ; For arr [ 2 ] [ 10 ] , compare arr [ lookup [ 0 ] [ 3 ] ] and arr [ lookup [ 3 ] [ 3 ] ] ; Otherwise ; Function find minimum of absolute difference of all adjacent element in subarray arr [ L . . R ] ; For [ 2 , 10 ] , j = 3 ; For [ 2 , 10 ] , compare arr [ lookup [ 0 ] [ 3 ] ] and arr [ lookup [ 3 ] [ 3 ] ] , ; Function to find the minimum of the ranges for M queries ; Fills table lookup [ n ] [ Log n ] ; Compute sum of all queries ; Left and right boundaries of current range ; let sum of current query range ; Function to find the minimum absolute difference in a range ; diff [ ] is to stores the absolute difference of adjacent elements ; Call Min_difference to get minimum difference of adjacent elements ; Driver Code
let MAX = 500 ; let lookup = new Array ( MAX ) . fill ( 0 ) . map ( ( ) => new Array ( MAX ) . fill ( 0 ) ) ; class Query { constructor ( l , r ) { this . L = l ; this . R = r ; } } function preprocess ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) lookup [ i ] [ 0 ] = i ; for ( let j = 1 ; 1 << j <= n ; j ++ ) { for ( let i = 0 ; i + ( 1 << j ) - 1 < n ; i ++ ) { if ( arr [ lookup [ i ] [ j - 1 ] ] < arr [ lookup [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] ] ) lookup [ i ] [ j ] = lookup [ i ] [ j - 1 ] ; else lookup [ i ] [ j ] = lookup [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] ; } } } function query ( arr , L , R ) { let j = Math . floor ( Math . log ( R - L + 1 ) ) ; if ( arr [ lookup [ L ] [ j ] ] <= arr [ lookup [ R - ( 1 << j ) + 1 ] [ j ] ] ) return arr [ lookup [ L ] [ j ] ] ; else return arr [ lookup [ R - ( 1 << j ) + 1 ] [ j ] ] ; } function Min_difference ( arr , n , q , m ) { preprocess ( arr , n ) ; for ( let i = 0 ; i < m ; i ++ ) { let L = q [ i ] . L , R = q [ i ] . R ; document . write ( query ( arr , L , R - 1 ) + " " ) ; } } function minimumDifference ( arr , q , N , m ) { let diff = new Array ( N ) ; for ( let i = 0 ; i < N - 1 ; i ++ ) diff [ i ] = Math . abs ( arr [ i ] - arr [ i + 1 ] ) ; Min_difference ( diff , N - 1 , q , m ) ; } let arr = [ 2 , 6 , 1 , 8 , 3 , 4 ] ; let N = arr . length ; let Q = [ new Query ( 0 , 3 ) , new Query ( 1 , 5 ) , new Query ( 4 , 5 ) ] ; let M = Q . length ; minimumDifference ( arr , Q , N , M ) ;
Domino and Tromino tiling problem | Function to find the total number of ways to tile a 2 * N board using the given types of tile ; If N is less than 3 ; Store all dp - states ; Base Case ; Traverse the range [ 2 , N ] ; Update the value of dp [ i ] [ 0 ] ; Update the value of dp [ i ] [ 1 ] ; Update the value of dp [ i ] [ 2 ] ; Return the number of ways as the value of dp [ N ] [ 0 ] ; Driver Code
const MOD = 1e9 + 7 ; function numTilings ( N ) { if ( N < 3 ) { return N ; } let dp = Array ( N + 1 ) . fill ( ) . map ( ( ) => Array ( 3 ) . fill ( 0 ) ) dp [ 0 ] [ 0 ] = dp [ 1 ] [ 0 ] = 1 ; dp [ 1 ] [ 1 ] = dp [ 1 ] [ 2 ] = 1 ; for ( let i = 2 ; i <= N ; i ++ ) { dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 2 ] [ 0 ] + dp [ i - 2 ] [ 1 ] + dp [ i - 2 ] [ 2 ] ) % MOD ; dp [ i ] [ 1 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 2 ] ) % MOD ; dp [ i ] [ 2 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] ) % MOD ; } return dp [ N ] [ 0 ] ; } let N = 3 ; document . write ( numTilings ( N ) ) ;
Count of N | Store the recurring recursive states ; Function to find the number of strings of length N such that it is a concatenation it substrings ; Single character cant be repeated ; Check if this state has been already calculated ; Stores the resultant count for the current recursive calls ; Iterate over all divisors ; Non - Repeated = Total - Repeated ; Non - Repeated = Total - Repeated ; Store the result for the further calculation ; Return resultant count ; Driver code ; Function Call
let dp = new Map ( ) ; function countStrings ( N ) { if ( N == 1 ) return 0 ; if ( dp . has ( N ) ) return dp . get ( N ) ; let ret = 0 ; for ( let div = 1 ; div <= Math . sqrt ( N ) ; div ++ ) { if ( N % div == 0 ) { ret += ( 1 << div ) - countStrings ( div ) ; let div2 = N / div ; if ( div2 != div && div != 1 ) ret += ( 1 << div2 ) - countStrings ( div2 ) ; } } dp [ N ] = ret ; return ret ; } let N = 6 ; document . write ( countStrings ( N ) + " " ) ;
Count N | JavaScript program for the above approach ; Function to count N - digit numbers such that each position is divisible by the digit occurring at that position ; Stores the answer . ; Iterate from indices 1 to N ; Stores count of digits that can be placed at the current index ; Iterate from digit 1 to 9 ; If index is divisible by digit ; Multiply answer with possible choices ; Given Input ; Function call
const mod = 1e9 + 7 ; function countOfNumbers ( N ) { let ans = 1 ; for ( let index = 1 ; index <= N ; ++ index ) { let choices = 0 ; for ( let digit = 1 ; digit <= 9 ; ++ digit ) { if ( index % digit == 0 ) { ++ choices ; } } ans = ( ans * 1 * choices ) % mod ; } document . write ( ans ) ; } let N = 5 ; countOfNumbers ( N ) ;
Maximum number of groups that can receive fresh donuts distributed in batches of size K | Recursive function to find the maximum number of groups that will receive fresh donuts ; Store the result for the current state ; Check if the leftover donuts from the previous batch is 0 ; If true , then one by one give the fresh donuts to each group ; Decrement arr [ i ] ; Update the maximum number of groups ; Increment arr [ i ] ; Otherwise , traverse the given array , arr [ ] ; Decrement arr [ i ] ; Update the maximum number of groups ; Increment arr [ i ] ; Return the value of q ; Function to find the maximum number of groups that will receive fresh donuts ; Stores count of remainder by K ; Traverse the array arr [ ] ; Stores maximum number of groups ; Return the answer ; Driver Code
function dfs ( arr , left , K ) { let q = 0 ; if ( left == 0 ) { for ( let i = 1 ; i < K ; ++ i ) { if ( arr [ i ] > 0 ) { arr [ i ] -- ; q = Math . max ( q , 1 + dfs ( arr , K - i , K ) ) ; arr [ i ] ++ ; } } } else { for ( let i = 1 ; i < K ; ++ i ) { if ( arr [ i ] > 0 ) { arr [ i ] -- ; let nleft = ( i <= left ? left - i : K + left - i ) ; q = Math . max ( q , dfs ( arr , nleft , K ) ) ; arr [ i ] ++ ; } } } return q ; } function maxGroups ( K , arr , n ) { let V = new Array ( K ) . fill ( 0 ) ; for ( let x = 0 ; x < n ; x ++ ) V [ arr [ x ] % K ] ++ ; let ans = V [ 0 ] + dfs ( V , 0 , K ) ; return ans ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; let n = arr . length ; let K = 3 ; document . write ( maxGroups ( K , arr , n ) )
Maximum jumps to reach end of Array with condition that index i can make arr [ i ] jumps | Function to find the maximum jumps to reach end of array ; Stores the jumps needed to reach end from each index ; Traverse the array ; Check if j is less than N ; Add dp [ j ] to the value of dp [ i ] ; Update the value of ans ; Print the value of ans ; Driver Code
function findMaxJumps ( arr , N ) { let dp = new Array ( N ) . fill ( 0 ) ; let ans = 0 ; for ( let i = N - 1 ; i >= 0 ; i -- ) { dp [ i ] = arr [ i ] ; let j = i + arr [ i ] ; if ( j < N ) { dp [ i ] = dp [ i ] + dp [ j ] ; } ans = Math . max ( ans , dp [ i ] ) ; } document . write ( ans ) ; } let arr = [ 2 , 3 , 5 , 7 , 9 ] ; let N = arr . length ; findMaxJumps ( arr , N ) ;
Longest subsequence with non negative prefix sum at each position | Function to find the length of the longest subsequence with non negative prefix sum at each position ; Initialize dp array with - 1 ; Maximum subsequence sum by including no elements till position ' i ' ; Maximum subsequence sum by including first element at first position ; Iterate over all the remaining positions ; If the current element is excluded ; If current element is included and if total sum is positive or not ; Select the maximum j by which a non negative prefix sum subsequence can be obtained ; Print the answer ; Driver Code
function longestSubsequence ( arr , N ) { let dp = Array ( N ) . fill ( ) . map ( ( ) => Array ( N + 1 ) . fill ( - 1 ) ) ; for ( let i = 0 ; i < N ; ++ i ) { dp [ i ] [ 0 ] = 0 ; } dp [ 0 ] [ 1 ] = ( arr [ 0 ] >= 0 ? arr [ 0 ] : - 1 ) ; for ( let i = 1 ; i < N ; ++ i ) { for ( let j = 1 ; j <= ( i + 1 ) ; ++ j ) { if ( dp [ i - 1 ] [ j ] != - 1 ) { dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j ] ) ; } if ( dp [ i - 1 ] [ j - 1 ] >= 0 && dp [ i - 1 ] [ j - 1 ] + arr [ i ] >= 0 ) { dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - 1 ] + arr [ i ] ) ; } } } let ans = 0 ; for ( let j = 0 ; j <= N ; ++ j ) { if ( dp [ N - 1 ] [ j ] >= 0 ) { ans = j ; } } document . write ( ans ) ; } let arr = [ 4 , - 4 , 1 , - 3 , 1 , - 3 ] ; let N = arr . length ; longestSubsequence ( arr , N ) ;
Total count of sorted numbers upto N digits in range [ L , R ] ( Magnificent necklace combinatorics problem ) | Function to count total number of ways ; Stores all DP - states ; Stores the result ; Traverse the range [ 0 , N ] ; Traverse the range [ 1 , R - L ] ; Update dp [ i ] [ j ] ; Assign dp [ 0 ] [ R - L ] to ans ; Traverse the range [ 1 , N ] ; Traverse the range [ 1 , R - L ] ; Update dp [ i ] [ j ] ; Increment ans by dp [ i - 1 ] [ j ] ; Return ans ; Input ; Function call
function Count ( N , L , R ) { let dp = new Array ( N ) . fill ( 0 ) . map ( ( ) => new Array ( R - L + 1 ) . fill ( 0 ) ) ; let ans = 0 ; for ( let i = 0 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = 1 ; } for ( let i = 1 ; i < dp [ 0 ] . length ; i ++ ) { dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + 1 ; } ans = dp [ 0 ] [ R - L ] ; for ( let i = 1 ; i < N ; i ++ ) { for ( let j = 1 ; j < dp [ 0 ] . length ; j ++ ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] ; } ans += dp [ i ] [ R - L ] ; } return ans ; } let N = 3 ; let L = 6 ; let R = 9 ; document . write ( Count ( N , L , R ) ) ;