codeparrot/xlcost-text-to-code huggingface dataset:
Minimize operations to reduce N to 2 by repeatedly reducing by 3 or dividing by 5 | Function to find the minimum number of operations to reduce N to 2 by dividing N by 5 or decrementing by 3 ; Initialize the dp array ; Initialize the array dp [ ] ; For N = 2 number of operations needed is zero ; Iterating over the range [ 1 , N ] ; If it 's not possible to  create current N ; Multiply with 5 ; Adding the value 3 ; Checking if not possible to make the number as 2 ; Return the minimum number of operations ; Driver Code
function minimumOperations ( N ) { let dp = new Array ( N + 1 ) ; let i ; for ( i = 0 ; i <= N ; i ++ ) { dp [ i ] = 1e9 ; } dp [ 2 ] = 0 ; for ( i = 2 ; i <= N ; i ++ ) { if ( dp [ i ] == 1e9 ) continue ; if ( i * 5 <= N ) { dp [ i * 5 ] = Math . min ( dp [ i * 5 ] , dp [ i ] + 1 ) ; } if ( i + 3 <= N ) { dp [ i + 3 ] = Math . min ( dp [ i + 3 ] , dp [ i ] + 1 ) ; } } if ( dp [ N ] == 1e9 ) return - 1 ; return dp [ N ] ; } let N = 25 ; document . write ( minimumOperations ( N ) ) ;
Maximum profit after buying and selling the stocks with transaction fees | Set 2 | Function to find the maximum profit with transaction fee ; Traversing the stocks for each day ; Update buy and sell ; Return the maximum profit ; Given Input ; Function Call
function MaxProfit ( arr , n , transactionFee ) { let buy = - arr [ 0 ] ; let sell = 0 ; for ( let i = 1 ; i < n ; i ++ ) { let temp = buy ; buy = Math . max ( buy , sell - arr [ i ] ) ; sell = Math . max ( sell , temp + arr [ i ] - transactionFee ) ; } return Math . max ( sell , buy ) ; } let arr = [ 6 , 1 , 7 , 2 , 8 , 4 ] ; let n = arr . length ; let transactionFee = 2 ; document . write ( MaxProfit ( arr , n , transactionFee ) ) ;
Maximum path sum from top left to bottom right of a matrix passing through one of the given cells | Stores the maximum path sum from the cell ( 1 , 1 ) to ( N , M ) ; Stores the maximum path sum from the cell ( j , j ) to ( N , M ) ; Function to find the maximum path sum from the cell ( 1 , 1 ) to ( N , M ) ; Traverse the first row ; Traverse the first column ; Traverse the matrix ; Update the value of start [ i ] [ j ] ; Function to find the maximum path sum from the cell ( j , j ) to ( N , M ) ; Traverse the last row ; Traverse the last column ; Traverse the matrix ; Update the value of ending [ i ] [ j ] ; Function to find the maximum path sum from the top - left to the bottom right cell such that path contains one of the cells in the array coordinates [ ] [ ] ; Initialize the start and the end matrices ; Calculate the start matrix ; Calculate the end matrix ; Stores the maximum path sum ; Traverse the coordinates ; Update the value of ans ; Print the resultant maximum sum path value ; Drive Code
var start = Array . from ( Array ( 3 ) , ( ) => Array ( 3 ) ) ; var ending = Array . from ( Array ( 3 ) , ( ) => Array ( 3 ) ) ; function calculateStart ( n , m ) { for ( var i = 1 ; i < m ; ++ i ) { start [ 0 ] [ i ] += start [ 0 ] [ i - 1 ] ; } for ( var i = 1 ; i < n ; ++ i ) { start [ i ] [ 0 ] += start [ i - 1 ] [ 0 ] ; } for ( var i = 1 ; i < n ; ++ i ) { for ( var j = 1 ; j < m ; ++ j ) { start [ i ] [ j ] += Math . max ( start [ i - 1 ] [ j ] , start [ i ] [ j - 1 ] ) ; } } } function calculateEnd ( n , m ) { for ( var i = n - 2 ; i >= 0 ; -- i ) { ending [ i ] [ m - 1 ] += ending [ i + 1 ] [ m - 1 ] ; } for ( var i = m - 2 ; i >= 0 ; -- i ) { ending [ n - 1 ] [ i ] += ending [ n - 1 ] [ i + 1 ] ; } for ( var i = n - 2 ; i >= 0 ; -- i ) { for ( var j = m - 2 ; j >= 0 ; -- j ) { ending [ i ] [ j ] += Math . max ( ending [ i + 1 ] [ j ] , ending [ i ] [ j + 1 ] ) ; } } } function maximumPathSum ( mat , n , m , q , coordinates ) { for ( var i = 0 ; i < n ; ++ i ) { for ( var j = 0 ; j < m ; ++ j ) { start [ i ] [ j ] = mat [ i ] [ j ] ; ending [ i ] [ j ] = mat [ i ] [ j ] ; } } calculateStart ( n , m ) ; calculateEnd ( n , m ) ; var ans = 0 ; for ( var i = 0 ; i < q ; ++ i ) { var X = coordinates [ i ] [ 0 ] - 1 ; var Y = coordinates [ i ] [ 1 ] - 1 ; ans = Math . max ( ans , start [ X ] [ Y ] + ending [ X ] [ Y ] - mat [ X ] [ Y ] ) ; } document . write ( ans ) ; } var mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; var N = 3 ; var M = 3 ; var Q = 2 ; var coordinates = [ [ 1 , 2 ] , [ 2 , 2 ] ] ; maximumPathSum ( mat , N , M , Q , coordinates ) ;
Length of longest subset consisting of A 0 s and B 1 s from an array of strings | Set 2 | Function to find the length of the longest subset of an array of strings with at most A 0 s and B 1 s ; Initialize a 2D array with its entries as 0 ; Traverse the given array ; Store the count of 0 s and 1 s in the current string ; Iterate in the range [ A , zeros ] ; Iterate in the range [ B , ones ] ; Update the value of dp [ i ] [ j ] ; Print the result ; Driver Code
function MaxSubsetlength ( arr , A , B ) { var dp = Array . from ( Array ( A + 1 ) , ( ) => Array ( B + 1 ) . fill ( 0 ) ) ; arr . forEach ( str => { var zeros = [ ... str ] . filter ( x => x == ' ' ) . length ; var ones = [ ... str ] . filter ( x => x == ' ' ) . length ; for ( var i = A ; i >= zeros ; i -- ) for ( var j = B ; j >= ones ; j -- ) dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i - zeros ] [ j - ones ] + 1 ) ; } ) ; return dp [ A ] [ B ] ; } var arr = [ " " , " " , " " , " " , " " ] ; var A = 5 , B = 3 ; document . write ( MaxSubsetlength ( arr , A , B ) ) ;
Count ways to select N pairs of candies of distinct colors ( Dynamic Programming + Bitmasking ) | Function to count ways to select N distinct pairs of candies with different colours ; If n pairs are selected ; Stores count of ways to select the i - th pair ; Iterate over the range [ 0 , n ] ; If pair ( i , j ) is not included ; Driver Code
function numOfWays ( a , n , i , blue ) { if ( i == n ) return 1 ; let count = 0 ; for ( let j = 0 ; j < n ; j ++ ) { if ( a [ i ] [ j ] == 1 && ! blue . has ( j ) ) { blue . add ( j ) ; count += numOfWays ( a , n , i + 1 , blue ) ; blue . delete ( j ) ; } } return count ; } let n = 3 ; let mat = [ [ 0 , 1 , 1 ] , [ 1 , 0 , 1 ] , [ 1 , 1 , 1 ] ] ; let mpp = new Set ( ) ; document . write ( numOfWays ( mat , n , 0 , mpp ) ) ;
Minimize cost to reach end of an array by two forward jumps or one backward jump in each move | Function to find the minimum cost to reach the end of an array ; Base Case : When N < 3 ; Store the results in table ; Initialize base cases ; Iterate over the range [ 2 , N - 2 ] to construct the dp array ; Handle case for the last index , i . e . N - 1 ; Prlet the answer ; Driver Code
function minCost ( arr , n ) { if ( n < 3 ) { document . write ( arr [ 0 ] ) ; return ; } let dp = [ ] ; dp [ 0 ] = arr [ 0 ] ; dp [ 1 ] = dp [ 0 ] + arr [ 1 ] + arr [ 2 ] ; for ( let i = 2 ; i < n - 1 ; i ++ ) dp [ i ] = Math . min ( dp [ i - 2 ] + arr [ i ] , dp [ i - 1 ] + arr [ i ] + arr [ i + 1 ] ) ; dp [ n - 1 ] = Math . min ( dp [ n - 2 ] , dp [ n - 3 ] + arr [ n - 1 ] ) ; document . write ( dp [ n - 1 ] ) ; } let arr = [ 9 , 4 , 6 , 8 , 5 ] ; let N = arr . length ; minCost ( arr , N ) ;
Calculate the value of 2 raised to the power of twice the binary representation of N | javascript program to implement the above approach ; Function to find the value of power ( X , Y ) in O ( log Y ) ; Stores power ( X , Y ) ; Update X ; Base Case ; Calculate power ( X , Y ) ; If Y is an odd number ; Update res ; Update Y ; Update X ; Function to calculate ( 2 ^ ( 2 * x ) ) % ( 10 ^ 9 + 7 ) ; Stores binary representation of n ; Stores power of 10 ; Calculate the binary representation of n ; If n is an odd number ; Update X ; Update pow_10 ; Update n ; Double the value of X ; Stores the value of ( 2 ^ ( 2 * x ) ) % ( 10 ^ 9 + 7 ) ; Driver code
M = 1000000007 ; function power ( X , Y ) { var res = 1 ; X = X % M ; if ( X == 0 ) return 0 ; while ( Y > 0 ) { if ( ( Y & 1 ) != 0 ) { res = ( res * X ) % M ; } Y = Y >> 1 ; X = ( X * X ) % M ; } return res ; } function findValue ( n ) { var X = 0 ; var pow_10 = 1 ; while ( n != 0 ) { if ( ( n & 1 ) != 0 ) { X += pow_10 ; } pow_10 *= 10 ; n /= 2 ; } X = ( X * 2 ) % M ; var res = power ( 2 , X ) ; return res ; } var n = 2 ; document . write ( findValue ( n ) ) ;
Count ways to obtain given sum by repeated throws of a dice | Function to find the number of ways to get the sum N with throw of dice ; Base Case ; Stores the count of total number of ways to get sum N ; Recur for all 6 states ; Return answer ; Driver Code ; Function call
function findWays ( N ) { if ( N == 0 ) { return 1 ; } var cnt = 0 ; for ( var i = 1 ; i <= 6 ; i ++ ) { if ( N - i >= 0 ) { cnt = cnt + findWays ( N - i ) ; } } return cnt ; } var N = 4 ; document . write ( findWays ( N ) ) ;
Check if an array can be split into 3 subsequences of equal sum or not | Utility function to check array can be partition to 3 subsequences of equal sum or not ; Base Case ; When element at index j is added to sm1 ; When element at index j is added to sm2 ; When element at index j is added to sm3 ; Return maximum value among all above 3 recursive call ; Function to check array can be partition to 3 subsequences of equal sum or not ; Initialise 3 sums to 0 ; Function Call ; Given array arr [ ] ; Function Call
function checkEqualSumUtil ( arr , N , sm1 , sm2 , sm3 , j ) { if ( j == N ) { if ( sm1 == sm2 && sm2 == sm3 ) return 1 ; else return 0 ; } else { let l = checkEqualSumUtil ( arr , N , sm1 + arr [ j ] , sm2 , sm3 , j + 1 ) ; let m = checkEqualSumUtil ( arr , N , sm1 , sm2 + arr [ j ] , sm3 , j + 1 ) ; let r = checkEqualSumUtil ( arr , N , sm1 , sm2 , sm3 + arr [ j ] , j + 1 ) ; return Math . max ( Math . max ( l , m ) , r ) ; } } function checkEqualSum ( arr , N ) { let sum1 , sum2 , sum3 ; sum1 = sum2 = sum3 = 0 ; if ( checkEqualSumUtil ( arr , N , sum1 , sum2 , sum3 , 0 ) == 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let arr = [ 17 , 34 , 59 , 23 , 17 , 67 , 57 , 2 , 18 , 59 , 1 ] ; let N = arr . length ; checkEqualSum ( arr , N ) ;
Check if an array can be split into 3 subsequences of equal sum or not | JavaScript program for the above approach ; Function to check array can be partition into sum of 3 equal ; Base Case ; If value at particular index is not - 1 then return value at that index which ensure no more further calls ; When element at index j is added to sm1 ; When element at index j is added to sm2 ; When element at index j is added to sm3 ; Update the current state and return that value ; Function to check array can be partition to 3 subsequences of equal sum or not ; Initialise 3 sums to 0 ; Function Call ; Given array arr [ ] ; Function Call
var dp = new Map ( ) ; function checkEqualSumUtil ( arr , N , sm1 , sm2 , sm3 , j ) { var s = ( sm1 . toString ( ) ) + " " + ( sm2 . toString ( ) ) + ( j . toString ( ) ) ; if ( j == N ) { if ( sm1 == sm2 && sm2 == sm3 ) return 1 ; else return 0 ; } if ( dp . has ( s ) ) return dp [ s ] ; else { var l = checkEqualSumUtil ( arr , N , sm1 + arr [ j ] , sm2 , sm3 , j + 1 ) ; var m = checkEqualSumUtil ( arr , N , sm1 , sm2 + arr [ j ] , sm3 , j + 1 ) ; var r = checkEqualSumUtil ( arr , N , sm1 , sm2 , sm3 + arr [ j ] , j + 1 ) ; return dp [ s ] = Math . max ( Math . max ( l , m ) , r ) ; } } function checkEqualSum ( arr , N ) { var sum1 , sum2 , sum3 ; sum1 = sum2 = sum3 = 0 ; if ( checkEqualSumUtil ( arr , N , sum1 , sum2 , sum3 , 0 ) == 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var arr = [ 17 , 34 , 59 , 23 , 17 , 67 , 57 , 2 , 18 , 59 , 1 ] ; var N = arr . length ; checkEqualSum ( arr , N ) ;
Smallest index in given range of indices which is not equal to X | Precompute the index of next different element in the array for every array element ; Default value ; Compute nextpos [ i ] using nextpos [ i + 1 ] ; Function to return the smallest index ; nextpos [ i ] will store the next position p where arr [ p ] != arr [ i ] ; If X is not present at l ; Otherwise ; Find the index which stores a value different from X ; If that index is within the range ; Driver Code
function precompute ( nextpos , arr , N ) { nextpos [ N - 1 ] = N ; for ( var i = N - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] == arr [ i + 1 ] ) nextpos [ i ] = nextpos [ i + 1 ] ; else nextpos [ i ] = i + 1 ; } } function findIndex ( query , arr , N , Q ) { var nextpos = Array ( N ) ; precompute ( nextpos , arr , N ) ; for ( var i = 0 ; i < Q ; i ++ ) { var l , r , x ; l = query [ i ] [ 0 ] ; r = query [ i ] [ 1 ] ; x = query [ i ] [ 2 ] ; var ans = - 1 ; if ( arr [ l ] != x ) ans = l ; else { var d = nextpos [ l ] ; if ( d <= r ) ans = d ; } document . write ( ans + " " ) ; } } var N , Q ; N = 6 ; Q = 3 ; var arr = [ 1 , 2 , 1 , 1 , 3 , 5 ] ; var query = [ [ 0 , 3 , 1 ] , [ 1 , 5 , 2 ] , [ 2 , 3 , 1 ] ] ; findIndex ( query , arr , N , Q ) ;
Count number of ways to convert string S to T by performing K cyclic shifts | JavaScript program for the above approach ; Function to count number of ways to convert string S to string T by performing K cyclic shifts ; Calculate length of string ; ' a ' is no of good cyclic shifts ' b ' is no of bad cyclic shifts ; Iterate in the string ; Precompute the number of good and bad cyclic shifts ; dp2 [ i ] to store the no of ways to get to a bad shift in i moves ; Calculate good and bad shifts ; Return the required number of ways ; Given Strings ; Given K shifts required ; Function Call
let mod = 10000000007 ; function countWays ( s , t , k ) { let n = s . length ; let a = 0 , b = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let p = s . substr ( i , n - i ) + s . substr ( 0 , i ) ; if ( p == t ) a ++ ; else b ++ ; } let dp1 = Array . from ( { length : k + 1 } , ( _ , i ) => 0 ) ; let dp2 = Array . from ( { length : k + 1 } , ( _ , i ) => 0 ) ; if ( s == t ) { dp1 [ 0 ] = 1 ; dp2 [ 0 ] = 0 ; } else { dp1 [ 0 ] = 0 ; dp2 [ 0 ] = 1 ; } for ( let i = 1 ; i <= k ; i ++ ) { dp1 [ i ] = ( ( dp1 [ i - 1 ] * ( a - 1 ) ) % mod + ( dp2 [ i - 1 ] * a ) % mod ) % mod ; dp2 [ i ] = ( ( dp1 [ i - 1 ] * ( b ) ) % mod + ( dp2 [ i - 1 ] * ( b - 1 ) ) % mod ) % mod ; } return dp1 [ k ] ; } let S = " " , T = " " ; let K = 2 ; document . write ( countWays ( S , T , K ) ) ;
Minimize steps to reach K from 0 by adding 1 or doubling at each step | Function to find minimum operations ; dp is initialised to store the steps ; For all even numbers ; Driver Code
function minOperation ( k ) { let dp = Array . from ( { length : k + 1 } , ( _ , i ) => 0 ) ; for ( let i = 1 ; i <= k ; i ++ ) { dp [ i ] = dp [ i - 1 ] + 1 ; if ( i % 2 == 0 ) { dp [ i ] = Math . min ( dp [ i ] , dp [ i / 2 ] + 1 ) ; } } return dp [ k ] ; } let K = 12 ; document . write ( minOperation ( K ) ) ;
Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum | Function to find the maximum subset sum ; Ignore the current element ; Including element in partition 1 ; Including element in partition 2 ; Size of the array
function maxSum ( p0 , p1 , a , pos , n ) { if ( pos == n ) { if ( p0 == p1 ) return p0 ; else return 0 ; } var ans = maxSum ( p0 , p1 , a , pos + 1 , n ) ; ans = Math . max ( ans , maxSum ( p0 + a [ pos ] , p1 , a , pos + 1 , n ) ) ; ans = Math . max ( ans , maxSum ( p0 , p1 + a [ pos ] , a , pos + 1 , n ) ) ; return ans ; } var n = 4 ; var a = [ 1 , 2 , 3 , 6 ] ; document . write ( maxSum ( 0 , 0 , a , 0 , n ) ) ;
Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum | Function to find the maximum subset sum ; sum of all elements ; bottom up lookup table ; ; initialising dp table with - 1000000000 where , - 1000000000 means no solution ; Case when diff is 0 ; Putting ith element in g0 ; Putting ith element in g1 ; Ignoring ith element ; Driver code
function maxSum ( a , n ) { var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) sum += a [ i ] ; var limit = 2 * sum + 1 ; var dp = Array . from ( Array ( n + 1 ) , ( ) => Array ( limit ) ) ; for ( var i = 0 ; i < n + 1 ; i ++ ) { for ( var j = 0 ; j < limit ; j ++ ) dp [ i ] [ j ] = - 1000000000 ; } dp [ 0 ] [ sum ] = 0 ; for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = 0 ; j < limit ; j ++ ) { if ( ( j - a [ i - 1 ] ) >= 0 && dp [ i - 1 ] [ j - a [ i - 1 ] ] != - 1000000000 ) dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - a [ i - 1 ] ] + a [ i - 1 ] ) ; if ( ( j + a [ i - 1 ] ) < limit && dp [ i - 1 ] [ j + a [ i - 1 ] ] != - 1000000000 ) dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j + a [ i - 1 ] ] ) ; if ( dp [ i - 1 ] [ j ] != - 1000000000 ) dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j ] ) ; } } return dp [ n ] [ sum ] ; } var n = 4 ; var a = [ 1 , 2 , 3 , 6 ] ; document . write ( maxSum ( a , n ) ) ;
Count of strings possible by replacing two consecutive same character with new character | Array to find the fibonacci sequence ; Function to find the fibonacci sequence ; Function to count all possible strings ; Initialize ans = 1 ; If two consecutive char are same increase cnt ; Else multiply the fib [ cnt ] to ans and initialize ans to 1 ; If str = abcdeeee , then for last " eeee " the count munst be updated ; Return the total count ; Driver 's Code ; Function to precompute all the fibonacci number ; Function call to find the count
fib = Array ( 100005 ) . fill ( 0 ) ; function computeFibonacci ( ) { fib [ 0 ] = 1 ; fib [ 1 ] = 1 ; for ( i = 2 ; i < 100005 ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } } function countString ( str ) { var ans = 1 ; var cnt = 1 ; for ( i = 1 ; i < str . length ; i ++ ) { if ( str . charAt ( i ) == str . charAt ( i - 1 ) ) { cnt ++ ; } else { ans = ans * fib [ cnt ] ; cnt = 1 ; } } ans = ans * fib [ cnt ] ; return ans ; } var str = " " ; computeFibonacci ( ) ; document . write ( countString ( str ) ) ;
Golomb Sequence | Set 2 | Javascript program to find the first N terms of Golomb Sequence ; Function to print the Golomb Sequence ; Initialise the array ; Initialise the cnt to 0 ; First and second element of Golomb Sequence is 0 , 1 ; Map to store the count of current element in Golomb Sequence ; Store the count of 2 ; Iterate over 2 to N ; If cnt is equals to 0 then we have new number for Golomb Sequence which is 1 + previous element ; Else the current element is the previous element in this Sequence ; Map the current index to current value in arr [ ] ; Print the Golomb Sequence ; Driver Code
var MAX = 100001 ; function printGolombSequence ( N ) { var arr = Array ( MAX ) ; var cnt = 0 ; arr [ 0 ] = 0 ; arr [ 1 ] = 1 ; var M = new Map ( ) ; M . set ( 2 , 2 ) ; for ( var i = 2 ; i <= N ; i ++ ) { if ( cnt == 0 ) { arr [ i ] = 1 + arr [ i - 1 ] ; cnt = M . get ( arr [ i ] ) ; cnt -- ; } else { arr [ i ] = arr [ i - 1 ] ; cnt -- ; } M . set ( i , arr [ i ] ) ; } for ( var i = 1 ; i <= N ; i ++ ) { document . write ( arr [ i ] + ' ' ) ; } } var N = 11 ; printGolombSequence ( N ) ;
Count ways to reach Nth Stairs by taking 1 and 2 steps with exactly one 3 step | Function to find the number the number of ways to reach Nth stair ; Array including number of ways that includes 3 ; Array including number of ways that doesn 't includes 3 ; Initially to reach 3 stairs by taking 3 steps can be reached by 1 way ; Loop to find the number the number of ways to reach Nth stair ; Driver Code
function number_of_ways ( n ) { let includes_3 = new Uint8Array ( n + 1 ) ; let not_includes_3 = new Uint8Array ( n + 1 ) ; includes_3 [ 3 ] = 1 ; not_includes_3 [ 1 ] = 1 ; not_includes_3 [ 2 ] = 2 ; not_includes_3 [ 3 ] = 3 ; for ( let i = 4 ; i <= n ; i ++ ) { includes_3 [ i ] = includes_3 [ i - 1 ] + includes_3 [ i - 2 ] + not_includes_3 [ i - 3 ] ; not_includes_3 [ i ] = not_includes_3 [ i - 1 ] + not_includes_3 [ i - 2 ] ; } return includes_3 [ n ] ; } let n = 7 ; document . write ( number_of_ways ( n ) ) ;
Maximum number of multiples in an array before any element | JavaScript implementation of the approach ; Map to store the divisor count ; Function to generate the divisors of all the array elements ; Function to find the maximum number of multiples in an array before it ; To store the maximum divisor count ; Update ans if more number of divisors are found ; Generating all the divisors of the next element of the array ; Driver code
const MAX = 100000 ; var divisors = new Array ( MAX ) . fill ( 0 ) ; function generateDivisors ( n ) { for ( var i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { divisors [ i ] ++ ; } else { divisors [ i ] ++ ; divisors [ n / i ] ++ ; } } } } function findMaxMultiples ( arr , n ) { var ans = 0 ; for ( var i = 0 ; i < n ; i ++ ) { ans = Math . max ( divisors [ arr [ i ] ] , ans ) ; generateDivisors ( arr [ i ] ) ; } return ans ; } var arr = [ 8 , 1 , 28 , 4 , 2 , 6 , 7 ] ; var n = arr . length ; document . write ( findMaxMultiples ( arr , n ) ) ;
Number of ways to reach the end of matrix with non | Javascript implementation of the approach ; 3d array to store states of dp ; Array to determine whether a state has been solved before ; Function to return the count of required paths ; Base cases ; If a state has been solved before it won 't be evaluated again ; Recurrence relation ; Driver code
var n = 3 ; var maxV = 20 ; var dp = new Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { dp [ i ] = new Array ( n ) ; for ( var j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] = new Array ( maxV ) ; } } var v = new Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { v [ i ] = new Array ( n ) ; for ( var j = 0 ; j < n ; j ++ ) { v [ i ] [ j ] = new Array ( maxV ) ; } } function countWays ( i , j , x , arr ) { if ( i == n j == n ) return 0 ; x = ( x & arr [ i ] [ j ] ) ; if ( x == 0 ) return 0 ; if ( i == n - 1 && j == n - 1 ) return 1 ; if ( v [ i ] [ j ] [ x ] ) return dp [ i ] [ j ] [ x ] ; v [ i ] [ j ] [ x ] = 1 ; dp [ i ] [ j ] [ x ] = countWays ( i + 1 , j , x , arr ) + countWays ( i , j + 1 , x , arr ) ; return dp [ i ] [ j ] [ x ] ; } var arr = [ [ 1 , 2 , 1 ] , [ 1 , 1 , 0 ] , [ 2 , 1 , 1 ] ] ; document . write ( countWays ( 0 , 0 , arr [ 0 ] [ 0 ] , arr ) ) ;
Maximum sum from three arrays such that picking elements consecutively from same is not allowed | JavaScript implementation of the approach ; Function to return the maximum sum ; Base case ; Already visited ; If the element has been taken from first array in previous step ; If the element has been taken from second array in previous step ; If the element has been taken from third array in previous step ; Driver code ; Pick element from first array ; Pick element from second array ; Pick element from third array ; Print the maximum of them
var N = 3 ; function FindMaximumSum ( ind , kon , a , b , c , n , dp ) { if ( ind == n ) return 0 ; if ( dp [ ind ] [ kon ] != - 1 ) return dp [ ind ] [ kon ] ; var ans = - 1000000005 ; if ( kon == 0 ) { ans = Math . max ( ans , b [ ind ] + FindMaximumSum ( ind + 1 , 1 , a , b , c , n , dp ) ) ; ans = Math . max ( ans , c [ ind ] + FindMaximumSum ( ind + 1 , 2 , a , b , c , n , dp ) ) ; } else if ( kon == 1 ) { ans = Math . max ( ans , a [ ind ] + FindMaximumSum ( ind + 1 , 0 , a , b , c , n , dp ) ) ; ans = Math . max ( ans , c [ ind ] + FindMaximumSum ( ind + 1 , 2 , a , b , c , n , dp ) ) ; } else if ( kon == 2 ) { ans = Math . max ( ans , a [ ind ] + FindMaximumSum ( ind + 1 , 1 , a , b , c , n , dp ) ) ; ans = Math . max ( ans , b [ ind ] + FindMaximumSum ( ind + 1 , 0 , a , b , c , n , dp ) ) ; } return dp [ ind ] [ kon ] = ans ; } var a = [ 6 , 8 , 2 , 7 , 4 , 2 , 7 ] ; var b = [ 7 , 8 , 5 , 8 , 6 , 3 , 5 ] ; var c = [ 8 , 3 , 2 , 6 , 8 , 4 , 1 ] ; var n = a . length ; var dp = Array . from ( Array ( n ) , ( ) => Array ( n ) . fill ( - 1 ) ) ; var x = FindMaximumSum ( 0 , 0 , a , b , c , n , dp ) ; var y = FindMaximumSum ( 0 , 1 , a , b , c , n , dp ) ; var z = FindMaximumSum ( 0 , 2 , a , b , c , n , dp ) ; document . write ( Math . max ( x , Math . max ( y , z ) ) ) ;
Number of ways to make binary string of length N such that 0 s always occur together in groups of size K | Javascript implementation of the approach ; Function to return no of ways to build a binary string of length N such that 0 s always occur in groups of size K ; Driver Code
let mod = 1000000007 ; function noOfBinaryStrings ( N , k ) { let dp = new Array ( 100002 ) ; for ( let i = 1 ; i <= k - 1 ; i ++ ) { dp [ i ] = 1 ; } dp [ k ] = 2 ; for ( let i = k + 1 ; i <= N ; i ++ ) { dp [ i ] = ( dp [ i - 1 ] + dp [ i - k ] ) % mod ; } return dp [ N ] ; } let N = 4 ; let K = 2 ; document . write ( noOfBinaryStrings ( N , K ) ) ;