codeparrot/xlcost-text-to-code huggingface dataset:
Maximize cost to empty an array by removing contiguous subarrays of equal elements | Initialize dp array ; Function that removes elements from array to maximize the cost ; Base case ; Check if an answer is stored ; Deleting count + 1 i . e . including the first element and starting a new sequence ; Removing [ left + 1 , i - 1 ] elements to continue with previous sequence ; Store the result ; Return answer ; Function to remove the elements ; Function call ; Given array ; Function call
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 ] = - 1 ; } } } function helper ( arr , left , right , count , m ) { if ( left > right ) return 0 ; if ( dp [ left ] [ right ] [ count ] != - 1 ) { return dp [ left ] [ right ] [ count ] ; } let ans = ( count + 1 ) * m + helper ( arr , left + 1 , right , 0 , m ) ; for ( let i = left + 1 ; i <= right ; ++ i ) { if ( arr [ i ] == arr [ left ] ) { ans = Math . max ( ans , helper ( arr , left + 1 , i - 1 , 0 , m ) + helper ( arr , i , right , count + 1 , m ) ) ; } } dp [ left ] [ right ] [ count ] = ans ; return ans ; } function maxPoints ( arr , n , m ) { let len = arr . length ; return helper ( arr , 0 , len - 1 , 0 , m ) ; } let arr = [ 1 , 3 , 2 , 2 , 2 , 3 , 4 , 3 , 1 ] ; let M = 3 ; let N = arr . length ; document . write ( maxPoints ( arr , N , M ) ) ;
Minimum cost to convert M to N by repeated addition of its even divisors | Javascript program to implement the above approach ; Function to find the value of minimum steps to convert m to n ; Base Case ; If n exceeds m ; Iterate through all possible even divisors of m ; If m is divisible by i , then find the minimum cost ; Add the cost to convert m to m + i and recursively call next state ; Return min_cost ; Driver Code ; Function Call ; If conversion is not possible ; Print the cost
let inf = 1000000008 ; function minSteps ( m , n ) { if ( n == m ) return 0 ; if ( m > n ) return inf ; let min_cost = inf ; for ( let i = 2 ; i < m ; i += 2 ) { if ( m % i == 0 ) { min_cost = Math . min ( min_cost , m / i + minSteps ( m + i , n ) ) ; } } return min_cost ; } let M = 6 ; let N = 24 ; let minimum_cost = minSteps ( M , N ) ; minimum_cost = minimum_cost == inf ? - 1 : minimum_cost ; document . write ( minimum_cost ) ;
Represent a number as the sum of positive numbers ending with 9 | Function to find the minimum count of numbers ending with 9 to form N ; Extract last digit of N ; Calculate the last digit ; If the last digit satisfies the condition ; Driver Code
function minCountOfNumbers ( N ) { let k = N % 10 ; let z = N - ( 9 * ( 9 - k ) ) ; if ( z >= 9 && z % 10 == 9 ) { return 10 - k ; } else { return - 1 ; } } let N = 156 ; document . write ( minCountOfNumbers ( N ) ) ;
Binary Matrix after flipping submatrices in given range for Q queries | Function to flip a submatrices ; Boundaries of the submatrix ; Iterate over the submatrix ; Check for 1 or 0 and flip accordingly ; Function to perform the queries ; Driver code ; Function call
function manipulation ( matrix , q ) { let x1 = q [ 0 ] , y1 = q [ 1 ] , x2 = q [ 2 ] , y2 = q [ 3 ] ; for ( let i = x1 - 1 ; i < x2 ; i ++ ) { for ( let j = y1 - 1 ; j < y2 ; j ++ ) { if ( matrix [ i ] [ j ] == 1 ) matrix [ i ] [ j ] = 0 ; else matrix [ i ] [ j ] = 1 ; } } } function queries_fxn ( matrix , queries ) { for ( let q of queries ) manipulation ( matrix , q ) ; } let matrix = [ [ 0 , 1 , 0 ] , [ 1 , 1 , 0 ] ] ; let queries = [ [ 1 , 1 , 2 , 3 ] , [ 1 , 1 , 1 , 1 ] , [ 1 , 2 , 2 , 3 ] ] ; queries_fxn ( matrix , queries ) ; document . write ( " " ) ; for ( let i = 0 ; i < matrix . length ; i ++ ) { document . write ( " " ) ; for ( let j = 0 ; j < matrix [ i ] . length ; j ++ ) { if ( j < matrix [ i ] . length - 1 ) { document . write ( matrix [ i ] [ j ] + " " ) ; } else { document . write ( matrix [ i ] [ j ] + " " ) ; } } if ( i == matrix . length - 1 ) document . write ( " " ) ; else document . write ( " " ) ; } document . write ( " " ) ;
Minimum repeated addition of even divisors of N required to convert N to M | INF is the maximum value which indicates Impossible state ; Recursive Function that considers all possible even divisors of cur ; Indicates impossible state ; Return 0 if cur == M ; Initially it is set to INF that means we can 't transform cur to M ; Loop to find even divisors of cur ; if i is divisor of cur ; if i is even ; Finding divisors recursively for cur + i ; Check another divisor ; Find recursively for cur + ( cur / i ) ; Return the answer ; Function that finds the minimum operation to reduce N to M ; INF indicates impossible state ; Given N and M ; Function Call
let INF = 1e7 ; function min_op ( cur , M ) { if ( cur > M ) return INF ; if ( cur == M ) return 0 ; let op = INF ; for ( let i = 2 ; i * i <= cur ; i ++ ) { if ( cur % i == 0 ) { if ( i % 2 == 0 ) { op = Math . min ( op , 1 + min_op ( cur + i , M ) ) ; } if ( ( cur / i ) != i && ( cur / i ) % 2 == 0 ) { op = Math . min ( op , 1 + min_op ( cur + ( cur / i ) , M ) ) ; } } } return op ; } function min_operations ( N , M ) { let op = min_op ( N , M ) ; if ( op >= INF ) document . write ( " " ) ; else document . write ( op + " " ) ; } let N = 6 , M = 24 ; min_operations ( N , M ) ;
Minimum length of subsequence having unit GCD | Function that finds the prime factors of a number ; To store the prime factor ; 2 s that divide n ; N must be odd at this point Skip one element ; Update the prime factor ; If n is a prime number greater than 2 ; Function that finds the shortest subsequence ; Check if the prime factor of first number , is also the prime factor of the rest numbers in array ; Set corresponding bit of prime factor to 1 , it means both these numbers have the same prime factor ; If no states encountered so far continue for this combination of bits ; Update this state with minimum ways to reach this state ; Function that print the minimum length of subsequence ; Find the prime factors of the first number ; Initialize the array with maximum steps , size of the array + 1 for instance ; Total number of set bits is equal to the total number of prime factors ; Indicates there is one way to reach the number under consideration ; State 0 corresponds to gcd of 1 ; If not found such subsequence then print " - 1" ; Else print the length ; Given array arr [ ] ; Function Call
function findPrimeFactors ( n ) { let primeFactors = new Array ( 9 ) . fill ( 0 ) ; let j = 0 ; if ( n % 2 == 0 ) { primeFactors [ j ++ ] = 2 ; while ( n % 2 == 0 ) n >>= 1 ; } for ( let i = 3 ; i * i <= n ; i += 2 ) { if ( n % i == 0 ) { primeFactors [ j ++ ] = i ; while ( n % i == 0 ) n /= i ; } } if ( n > 2 ) primeFactors [ j ++ ] = n ; let PrimeFactors = new Array ( j ) ; for ( let i = 0 ; i < j ; i ++ ) { PrimeFactors [ i ] = primeFactors [ i ] ; } return PrimeFactors ; } function findShortestSubsequence ( dp , a , index , primeFactors ) { let n = a . length ; for ( let j = index ; j < n ; j ++ ) { let bitmask = 0 ; for ( let p = 0 ; p < primeFactors . length ; p ++ ) { if ( ( a [ j ] % primeFactors [ p ] ) == 0 ) { bitmask ^= ( 1 << p ) ; } } for ( let i = 0 ; i < dp . length ; i ++ ) { if ( dp [ i ] == n + 1 ) continue ; dp [ bitmask & i ] = Math . min ( dp [ bitmask & i ] , dp [ i ] + 1 ) ; } } } function prletMinimumLength ( a ) { let Min = a . length + 1 ; for ( let i = 0 ; i < a . length - 1 ; i ++ ) { let primeFactors = findPrimeFactors ( a [ i ] ) ; let n = primeFactors . length ; let dp = new Array ( 1 << n ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = a . length + 1 } let setBits = ( 1 << n ) - 1 ; dp [ setBits ] = 1 ; findShortestSubsequence ( dp , a , i + 1 , primeFactors ) ; Min = Math . min ( dp [ 0 ] , Min ) ; } if ( Min == ( a . length + 1 ) ) document . write ( - 1 + " " ) ; else document . write ( Min + " " ) ; } let arr = [ 2 , 6 , 12 , 3 ] ; prletMinimumLength ( arr ) ;
Maximum sum of Bitwise XOR of all elements of two equal length subsets | Function that finds the maximum Bitwise XOR sum of the two subset ; Check if the current state is already computed ; Initialize answer to minimum value ; Iterate through all possible pairs ; Check whether ith bit and jth bit of mask is not set then pick the pair ; For all possible pairs find maximum value pick current a [ i ] , a [ j ] and set i , j th bits in mask ; Store the maximum value and return the answer ; Driver Code ; Given array arr [ ] ; Declare Initialize the dp states ; Function Call
function xorSum ( a , n , mask , dp ) { if ( dp [ mask ] != - 1 ) { return dp [ mask ] ; } var max_value = 0 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i + 1 ; j < n ; j ++ ) { if ( i != j && ( mask & ( 1 << i ) ) == 0 && ( mask & ( 1 << j ) ) == 0 ) { max_value = Math . max ( max_value , ( a [ i ] ^ a [ j ] ) + xorSum ( a , n , ( mask | ( 1 << i ) | ( 1 << j ) ) , dp ) ) ; } } } return dp [ mask ] = max_value ; } var n = 4 ; var arr = [ 1 , 2 , 3 , 4 ] ; var dp = Array ( ( 1 << n ) + 5 ) . fill ( - 1 ) ; document . write ( xorSum ( arr , n , 0 , dp ) ) ;
Count of ways in which N can be represented as sum of Fibonacci numbers without repetition | Javascript program for the above approach ; Function to generate the fibonacci number ; First two number of fibonacci sqequence ; Function to find maximum ways to represent num as the sum of fibonacci number ; Generate the Canonical form of given number ; Reverse the number ; Base condition of dp1 and dp2 ; Iterate from 1 to cnt ; Calculate dp1 ; Calculate dp2 ; Return final ans ; Function call to generate the fibonacci numbers ; Given number ; Function call
var fib = Array ( 101 ) . fill ( 0 ) ; var dp1 = Array ( 101 ) . fill ( 0 ) ; var dp2 = Array ( 101 ) . fill ( 0 ) ; var v = Array ( 101 ) . fill ( 0 ) ; function fibonacci ( ) { fib [ 1 ] = 1 ; fib [ 2 ] = 2 ; for ( i = 3 ; i <= 87 ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } } function find ( num ) { var cnt = 0 ; for ( i = 87 ; i > 0 ; i -- ) { if ( num >= fib [ i ] ) { v [ cnt ++ ] = i ; num -= fib [ i ] ; } } for ( i = 0 ; i < cnt / 2 ; i ++ ) { var t = v [ i ] ; v [ i ] = v [ cnt - i - 1 ] ; v [ cnt - i - 1 ] = t ; } dp1 [ 0 ] = 1 ; dp2 [ 0 ] = parseInt ( ( v [ 0 ] - 1 ) / 2 ) ; for ( i = 1 ; i < cnt ; i ++ ) { dp1 [ i ] = dp1 [ i - 1 ] + dp2 [ i - 1 ] ; dp2 [ i ] = parseInt ( ( v [ i ] - v [ i - 1 ] ) / 2 ) * dp2 [ i - 1 ] + parseInt ( ( v [ i ] - v [ i - 1 ] - 1 ) / 2 ) * dp1 [ i - 1 ] ; } return ( dp1 [ cnt - 1 ] + dp2 [ cnt - 1 ] ) ; } fibonacci ( ) ; var num = 13 ; document . write ( find ( num ) ) ;
Count of N | Function to find count of N - digit numbers with single digit XOR ; Range of numbers ; Calculate XOR of digits ; If XOR <= 9 , then increment count ; Print the count ; Given number ; Function call
function countNums ( N ) { let l = Math . floor ( Math . pow ( 10 , N - 1 ) ) ; let r = Math . floor ( Math . pow ( 10 , N ) ) - 1 ; let count = 0 ; for ( let i = l ; i <= r ; i ++ ) { let xorr = 0 , temp = i ; while ( temp > 0 ) { xorr = xorr ^ ( temp % 10 ) ; temp = Math . floor ( temp / 10 ) ; } if ( xorr <= 9 ) count ++ ; } document . write ( count ) ; } let N = 2 ; countNums ( N ) ;
Maximize length of Non | Function to find the maximum length non decreasing subarray by reversing at most one subarray ; dp [ i ] [ j ] be the longest subsequence of a [ 0. . . i ] with first j parts ; Maximum length sub - sequence of ( 0. . ) ; Maximum length sub - sequence of ( 0. . 1. . ) ; Maximum length sub - sequence of ( 0. . 1. .0 . . ) ; Maximum length sub - sequence of ( 0. . 1. .0 . .1 . . ) ; Find the max length subsequence ; Print the answer ; Driver Code
function main_fun ( arr , n ) { var dp = Array . from ( Array ( 4 ) , ( ) => Array ( n ) . fill ( 0 ) ) ; if ( arr [ 0 ] == 0 ) dp [ 0 ] [ 0 ] = 1 ; else dp [ 1 ] [ 0 ] = 1 ; for ( var i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + 1 ; else dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] ; } for ( var i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) dp [ 1 ] [ i ] = Math . max ( dp [ 1 ] [ i - 1 ] + 1 , dp [ 0 ] [ i - 1 ] + 1 ) ; else dp [ 1 ] [ i ] = dp [ 1 ] [ i - 1 ] ; } for ( var i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { dp [ 2 ] [ i ] = Math . max ( dp [ 2 ] [ i - 1 ] + 1 , Math . max ( dp [ 1 ] [ i - 1 ] + 1 , dp [ 0 ] [ i - 1 ] + 1 ) ) ; } else dp [ 2 ] [ i ] = dp [ 2 ] [ i - 1 ] ; } for ( var i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { dp [ 3 ] [ i ] = Math . max ( dp [ 3 ] [ i - 1 ] + 1 , Math . max ( dp [ 2 ] [ i - 1 ] + 1 , Math . max ( dp [ 1 ] [ i - 1 ] + 1 , dp [ 0 ] [ i - 1 ] + 1 ) ) ) ; } else dp [ 3 ] [ i ] = dp [ 3 ] [ i - 1 ] ; } var ans = Math . max ( dp [ 2 ] [ n - 1 ] , Math . max ( dp [ 1 ] [ n - 1 ] , Math . max ( dp [ 0 ] [ n - 1 ] , dp [ 3 ] [ n - 1 ] ) ) ) ; document . write ( ans ) ; } var n = 4 ; var arr = [ 0 , 1 , 0 , 1 ] ; main_fun ( arr , n ) ;
Print the Maximum Subarray Sum | Function to print the elements of Subarray with maximum sum ; Initialize currMax and globalMax with first value of nums ; Iterate for all the elemensts of the array ; Update currMax ; Check if currMax is greater than globalMax ; Traverse in left direction to find start Index of subarray ; Decrement the start index ; Printing the elements of subarray with max sum ; Given array arr [ ] ; Function call
function SubarrayWithMaxSum ( nums ) { var endIndex , currMax = nums [ 0 ] ; var globalMax = nums [ 0 ] ; for ( var i = 1 ; i < nums . length ; ++ i ) { currMax = Math . max ( nums [ i ] , nums [ i ] + currMax ) ; if ( currMax > globalMax ) { globalMax = currMax ; endIndex = i ; } } var startIndex = endIndex ; while ( startIndex >= 0 ) { globalMax -= nums [ startIndex ] ; if ( globalMax == 0 ) break ; startIndex -- ; } for ( var i = startIndex ; i <= endIndex ; ++ i ) { document . write ( nums [ i ] + " " ) ; } } var arr = [ - 2 , - 5 , 6 , - 2 , - 3 , 1 , 5 , - 6 ] ; SubarrayWithMaxSum ( arr ) ;
Count of distinct Primonacci Numbers in a given range [ L , R ] | Stores list of all primes ; Function to find all primes ; To mark the prime ones ; Initially all indices as prime ; If i is prime ; Set all multiples of i as non - prime ; Adding all primes to a list ; Function to return the count of Primonacci Numbers in the range [ l , r ] ; dp [ i ] contains ith Primonacci Number ; Stores the Primonacci Numbers ; Iterate over all smaller primes ; If Primonacci number lies within the range [ L , R ] ; Count of Primonacci Numbers ; Driver Code
let primes = new Array ( ) ; let M = 100005 ; function sieve ( ) { let mark = new Array ( M ) ; for ( let i = 0 ; i < M ; i ++ ) mark [ i ] = false ; for ( let i = 2 ; i < M ; i ++ ) mark [ i ] = true ; for ( let i = 2 ; i * i < M ; i ++ ) { if ( mark [ i ] ) { for ( let j = i * i ; j < M ; j += i ) mark [ j ] = false ; } } for ( let i = 2 ; i < M ; i ++ ) if ( mark [ i ] ) primes . push ( i ) ; } function countPrimonacci ( l , r ) { let dp = new Array ( ) ; dp . push ( 1 ) ; dp . push ( 1 ) ; let i = 2 ; let s = new Set ( ) ; while ( true ) { let x = 0 ; for ( let j = 0 ; j < primes . length ; j ++ ) { let p = primes [ j ] ; if ( p >= i ) break ; x += dp [ i - p ] ; } if ( x >= l && x <= r ) s . add ( x ) ; if ( x > r ) break ; dp . push ( x ) ; i ++ ; } document . write ( s . size ) ; } sieve ( ) ; let L = 1 , R = 10 ; countPrimonacci ( L , R ) ;
Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties | Javascript program to implement the above approach ; Function to calculate the count of possible Strings ; Initialize dp [ 0 ] ; dp [ i ] represents count of Strings of length i ; Add dp [ i - k ] if i >= k ; Update Prefix Sum Array ; Driver Code
let N = parseInt ( ( 1e5 + 5 ) ) ; let MOD = 1000000007 ; let dp = Array ( N ) . fill ( 0 ) ; function countStrings ( K , Q ) { dp [ 0 ] = 1 ; for ( let i = 1 ; i < N ; i ++ ) { dp [ i ] = dp [ i - 1 ] ; if ( i >= K ) dp [ i ] = ( dp [ i ] + dp [ i - K ] ) % MOD ; } for ( let i = 1 ; i < N ; i ++ ) { dp [ i ] = ( dp [ i ] + dp [ i - 1 ] ) % MOD ; } for ( let i = 0 ; i < Q . length ; i ++ ) { var ans = dp [ Q [ i ] [ 1 ] ] - dp [ Q [ i ] [ 0 ] - 1 ] ; if ( ans < 0 ) ans = ans + MOD ; document . write ( ans + " " ) ; } } var K = 3 ; let Q = [ [ 1 , 4 ] , [ 3 , 7 ] ] ; countStrings ( K , Q ) ;
Minimum possible sum of prices of a Triplet from the given Array | Function to minimize the sum of price by taking a triplet ; Initialize a dp [ ] array ; Stores the final result ; Iterate for all values till N ; Check if num [ j ] > num [ i ] ; Update dp [ j ] if it is greater than stored value ; Update the minimum sum as ans ; If there is no minimum sum exist then print - 1 else print the ans ; Driver Code
function minSum ( n , num , price ) { let dp = Array . from ( { length : n } , ( _ , i ) => Number . MAX_VALUE ) ; let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( num [ j ] > num [ i ] ) { dp [ j ] = Math . min ( dp [ j ] , price [ i ] + price [ j ] ) ; ans = Math . min ( ans , dp [ i ] + price [ j ] ) ; } } } return ans != Number . MAX_VALUE ? ans : - 1 ; } let num = [ 2 , 4 , 6 , 7 , 8 ] ; let price = [ 10 , 20 , 100 , 20 , 40 ] ; let n = price . length ; document . write ( minSum ( n , num , price ) ) ;
Count of binary strings of length N having equal count of 0 ' s ▁ and ▁ 1' s and count of 1 ' s ▁ â ‰ ¥ ▁ count ▁ of ▁ 0' s in each prefix substring | Function to calculate and returns the value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate the value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; Function to return the count of all binary strings having equal count of 0 ' s ▁ ▁ and ▁ 1' s and each prefix substring having frequency of 1 ' s ▁ > = ▁ frequencies ▁ of ▁ 0' s ; If N is odd ; No such strings possible ; Otherwise ; Calculate value of 2 nCn ; Return 2 nCn / ( n + 1 ) ; Driver code
function binomialCoeff ( n , k ) { var res = 1 ; if ( k > n - k ) k = n - k ; for ( var i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } function countStrings ( N ) { if ( N % 2 == 1 ) return 0 ; else { N /= 2 ; var c = binomialCoeff ( 2 * N , N ) ; return c / ( N + 1 ) ; } } var N = 6 ; document . write ( countStrings ( N ) + " " ) ;
Number of ways to cut a stick of length N into in even length at most K units long pieces | Recursive Function to count the total number of ways ; Base case if no - solution exist ; Condition if a solution exist ; Check if already calculated ; Initialize counter ; Recursive call ; Store the answer ; Return the answer ; Driver Code
function solve ( n , k , mod , dp ) { if ( n < 0 ) return 0 ; if ( n == 0 ) return 1 ; if ( dp [ n ] != - 1 ) return dp [ n ] ; let cnt = 0 ; for ( let i = 2 ; i <= k ; i += 2 ) { cnt = ( cnt % mod + solve ( n - i , k , mod , dp ) % mod ) % mod ; } dp [ n ] = cnt ; return cnt ; } let mod = ( 1e9 + 7 ) ; let n = 4 , k = 2 ; let dp = new Array ( n + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) dp [ i ] = - 1 ; let ans = solve ( n , k , mod , dp ) ; document . write ( ans ) ;
Number of ways to select exactly K even numbers from given Array | Javascript program for the above approach ; Function for calculating factorial ; Factorial of n defined as : n ! = n * ( n - 1 ) * ... * 1 ; Function to find the number of ways to select exactly K even numbers from the given array ; Count even numbers ; Check if the current number is even ; Check if the even numbers to be chosen is greater than n . Then , there is no way to pick it . ; The number of ways will be nCk ; Given array arr [ ] ; Given count of even elements ; Function Call
var f = Array ( 12 ) . fill ( 0 ) ; function fact ( ) { f [ 0 ] = f [ 1 ] = 1 ; for ( var i = 2 ; i <= 10 ; i ++ ) f [ i ] = i * 1 * f [ i - 1 ] ; } function solve ( arr , n , k ) { fact ( ) ; var even = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; } if ( k > even ) document . write ( 0 ) ; else { document . write ( f [ even ] / ( f [ k ] * f [ even - k ] ) ) ; } } var arr = [ 1 , 2 , 3 , 4 ] ; var n = arr . length ; var k = 1 ; solve ( arr , n , k ) ;
Count of binary strings of length N having equal count of 0 ' s ▁ and ▁ 1' s | Javascript program for the above approach ; Function to calculate C ( n , r ) % MOD DP based approach ; Corner case ; Stores the last row of Pascal 's Triangle ; Initialize top row of pascal triangle ; Construct Pascal 's Triangle  from top to bottom ; Fill current row with the help of previous row ; C ( n , j ) = C ( n - 1 , j ) + C ( n - 1 , j - 1 ) ; Driver Code
var MOD = 1000000007 ; function nCrModp ( n , r ) { if ( n % 2 == 1 ) { return - 1 ; } var C = Array ( r + 1 ) . fill ( 0 ) ; C [ 0 ] = 1 ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = Math . min ( i , r ) ; j > 0 ; j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % MOD ; } return C [ r ] ; } var N = 6 ; document . write ( nCrModp ( N , N / 2 ) ) ;
Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1 | Function to print the Minimum number of operations required to convert 1 into X by using three given operations ; Initialize a DP array to store min operations for sub - problems ; Base Condition : No operation required when N is 1 ; Multiply the number by 2 ; Multiply the number by 3 ; Add 1 to the number . ; Print the minimum operations ; Initialize a list to store the sequence ; Backtrack to find the sequence ; If add by 1 ; If multiply by 2 ; If multiply by 3 ; Print the sequence of operation ; Given number X ; Function call
function printMinOperations ( N ) { var dp = Array ( N + 1 ) for ( var i = 0 ; i < N + 1 ; i ++ ) dp [ i ] = N ; dp [ 1 ] = 0 ; for ( var i = 2 ; i < N + 1 ; i ++ ) { if ( i % 2 == 0 && dp [ i ] > dp [ i / 2 ] + 1 ) dp [ i ] = dp [ i / 2 ] + 1 ; if ( i % 3 == 0 && dp [ i ] > dp [ i / 3 ] + 1 ) dp [ i ] = dp [ i / 3 ] + 1 ; if ( dp [ i ] > dp [ i - 1 ] + 1 ) dp [ i ] = dp [ i - 1 ] + 1 ; } document . write ( dp [ N ] + " " ) ; var seq = [ ] ; while ( N > 1 ) { seq . push ( N ) ; if ( dp [ N - 1 ] == dp [ N ] - 1 ) N = N - 1 ; else if ( N % 2 == 0 && dp [ N / 2 ] == dp [ N ] - 1 ) N = N / 2 ; else N = N / 3 ; } seq . push ( 1 ) ; for ( var i = seq . length - 1 ; i >= 0 ; i -- ) document . write ( seq [ i ] + " " ) ; document . write ( " " ) ; } var X = 96234 ; printMinOperations ( X ) ;
Largest subsequence such that all indices and all values are multiples individually | Function that prlet maximum length of array ; dp [ ] array to store the maximum length ; Find all divisors of i ; If the current value is greater than the divisor 's value ; If current value is greater than the divisor 's value  and s is not equal  to current index ; Condition if current value is greater than the divisor 's value ; Computing the greatest value ; Prleting maximum length of array ; Given array arr [ ] ; Function call
function maxLength ( arr , n ) { let dp = [ ] ; for ( let i = 1 ; i < n ; i ++ ) { dp [ i ] = 1 ; } for ( let i = n - 1 ; i > 1 ; i -- ) { for ( let j = 1 ; j <= Math . sqrt ( i ) ; j ++ ) { if ( i % j == 0 ) { let s = i / j ; if ( s == j ) { if ( arr [ i ] > arr [ s ] ) { dp [ s ] = Math . max ( dp [ i ] + 1 , dp [ s ] ) ; } } else { if ( s != i && arr [ i ] > arr [ s ] ) dp [ s ] = Math . max ( dp [ i ] + 1 , dp [ s ] ) ; if ( arr [ i ] > arr [ j ] ) { dp [ j ] = Math . max ( dp [ i ] + 1 , dp [ j ] ) ; } } } } } let max = 0 ; for ( let i = 1 ; i < n ; i ++ ) { if ( dp [ i ] > max ) max = dp [ i ] ; } document . write ( max ) ; } let arr = [ 0 , 1 , 4 , 2 , 3 , 6 , 4 , 9 ] ; let size = arr . length ; maxLength ( arr , size ) ;