codeparrot/xlcost-text-to-code huggingface dataset:
Maximum number of given operations to remove the entire string | Function to return the maximum number of given operations required to remove the given string entirely ; If length of the string is zero ; Single operation can delete the entire string ; To store the prefix of the string which is to be deleted ; Prefix s [ 0. . i ] ; To store the substring s [ i + 1. . .2 * i + 1 ] ; If the prefix s [ 0. . . i ] can be deleted ; 1 operation to remove the current prefix and then recursively find the count of operations for the substring s [ i + 1. . . n - 1 ] ; Entire string has to be deleted ; Driver code
function find ( s ) { if ( s . length == 0 ) return 0 ; var c = 1 ; var d = " " ; for ( var i = 0 ; i < s . length ; i ++ ) { d += s [ i ] ; var s2 = s . substring ( i + 1 , i + 1 + d . length ) ; if ( s2 == d ) { c = 1 + find ( s . substring ( i + 1 ) ) ; break ; } } return c ; } var s = " " ; document . write ( find ( s ) ) ;
Divide the array in K segments such that the sum of minimums is maximized | JavaScript program to find the sum of the minimum of all the segments ; Function to maximize the sum of the minimums ; If k segments have been divided ; If we are at the end ; If we donot reach the end then return a negative number that cannot be the sum ; If at the end but k segments are not formed ; If the state has not been visited yet ; If the state has not been visited ; Get the minimum element in the segment ; Iterate and try to break at every index and create a segment ; Find the minimum element in the segment ; Find the sum of all the segments trying all the possible combinations ; Return the answer by memoizing it ; Driver Code ; Initialize dp array with - 1
var MAX = 10 ; function maximizeSum ( a , n , ind , k , dp ) { if ( k == 0 ) { if ( ind == n ) return 0 ; else return - 1000000000 ; } else if ( ind == n ) return - 1000000000 ; else if ( dp [ ind ] [ k ] != - 1 ) return dp [ ind ] [ k ] ; else { var ans = 0 ; var mini = a [ ind ] ; for ( i = ind ; i < n ; i ++ ) { mini = Math . min ( mini , a [ i ] ) ; ans = Math . max ( ans , maximizeSum ( a , n , i + 1 , k - 1 , dp ) + mini ) ; } return dp [ ind ] [ k ] = ans ; } } var a = [ 5 , 7 , 4 , 2 , 8 , 1 , 6 ] ; var k = 3 ; var n = a . length ; var dp = Array ( MAX ) . fill ( ) . map ( ( ) => Array ( MAX ) . fill ( 0 ) ) ; for ( var i = 0 ; i < MAX ; i ++ ) { for ( j = 0 ; j < MAX ; j ++ ) dp [ i ] [ j ] = - 1 ; } document . write ( maximizeSum ( a , n , 0 , k , dp ) ) ;
Optimally accommodate 0 s and 1 s from a Binary String into K buckets | 2 - D dp array saving different states dp [ i ] [ j ] = minimum value of accommodation till i 'th index of the string using j+1 number of buckets. ; Function to find the minimum required sum using dynamic programming ; If both start and bucket reached end then return 0 else that arrangement is not possible so return INT_MAX ; Corner case ; If state if already calculated then return its answer ; Start filling zeroes and ones which to be accommodated in jth bucket then ans for current arrangement will be ones * zeroes + recur ( i + 1 , bucket + 1 ) ; If this arrangement is not possible then don 't calculate further ; Function to initialize the dp and call solveUtil ( ) method to get the answer ; Start with 0 - th index and 1 bucket ; Driver code ; K buckets
let dp ; function solveUtil ( start , bucket , str , K ) { let N = str . length ; if ( start == N ) { if ( bucket == K ) { return 0 ; } return Number . MAX_VALUE ; } if ( bucket == K ) { return Number . MAX_VALUE ; } if ( dp [ start ] [ bucket ] != - 1 ) { return dp [ start ] [ bucket ] ; } let zeroes = 0 ; let ones = 0 ; let ans = Number . MAX_VALUE ; for ( let i = start ; i < N ; ++ i ) { if ( str [ i ] == ' ' ) { ones ++ ; } else { zeroes ++ ; } if ( ones * zeroes > ans ) { break ; } let temp = solveUtil ( i + 1 , bucket + 1 , str , K ) ; if ( temp != Number . MAX_VALUE ) { ans = Math . min ( ans , temp + ( ones * zeroes ) ) ; } } return dp [ start ] [ bucket ] = ans ; } function solve ( str , K ) { let N = str . length ; dp = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { dp [ i ] = new Array ( K ) ; for ( let j = 0 ; j < K ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } let ans = solveUtil ( 0 , 0 , str , K ) ; return ans == Number . MAX_VALUE ? - 1 : ans ; } let S = " " ; let K = 2 ; document . write ( solve ( S , K ) ) ;
Probability of getting more heads than tails when N biased coins are tossed | Function to return the probability when number of heads is greater than the number of tails ; Declaring the DP table ; Base case ; Iterating for every coin ; j represents the numbers of heads ; If number of heads is equal to zero there there is only one possibility ; When the number of heads is greater than ( n + 1 ) / 2 it means that heads are greater than tails as no of tails + no of heads is equal to n for any permutation of heads and tails ; 1 based indexing ; Number of coins ; Function call
function Probability ( p , n ) { var dp = Array ( n + 1 ) . fill ( 0 ) . map ( x => Array ( n + 1 ) . fill ( 0 ) ) ; dp [ 0 ] [ 0 ] = 1.0 ; for ( var i = 1 ; i <= n ; i += 1 ) { for ( var j = 0 ; j <= i ; j += 1 ) { if ( j == 0 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] * ( 1.0 - p [ i ] ) ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j ] * ( 1.0 - p [ i ] ) + dp [ i - 1 ] [ j - 1 ] * p [ i ] ; } } var ans = 0.0 ; for ( var i = parseInt ( ( n + 1 ) / 2 ) ; i <= n ; i += 1 ) ans += dp [ n ] [ i ] ; return ans ; } var p = [ 0.0 , 0.3 , 0.4 , 0.7 ] ; var n = p . length - 1 ; document . write ( Probability ( p , n ) ) ;
Number of ways to get a given sum with n number of m | Javascript function to calculate the number of ways to achieve sum x in n no of throws ; Function to calculate recursively the number of ways to get sum in given throws and [ 1. . m ] values ; Base condition 1 ; Base condition 2 ; If value already calculated dont move into re - computation ; Recursively moving for sum - i in throws - 1 no of throws left ; Inserting present values in dp ; Driver function
const mod = 1000000007 ; let dp = new Array ( 55 ) ; for ( let i = 0 ; i < 55 ; i ++ ) dp [ i ] = new Array ( 55 ) . fill ( - 1 ) ; function NoofWays ( face , throws , sum ) { if ( sum == 0 && throws == 0 ) return 1 ; if ( sum < 0 throws == 0 ) return 0 ; if ( dp [ throws ] [ sum ] != - 1 ) return dp [ throws ] [ sum ] ; let ans = 0 ; for ( let i = 1 ; i <= face ; i ++ ) { ans += NoofWays ( face , throws - 1 , sum - i ) ; } return dp [ throws ] [ sum ] = ans ; } let faces = 6 , throws = 3 , sum = 12 ; document . write ( NoofWays ( faces , throws , sum ) ) ;
Maximize the happiness of the groups on the Trip | Function to return the maximized happiness ; Two arrays similar to 0 1 knapsack problem ; To store the happiness of the current goup ; Current person is a child ; Woman ; Man ; Old Person ; Group 's happiness is the sum of happiness  of the people in the group multiplie  the number of people ; Solution using 0 1 knapsack ; Number of seats ; Groups
function maxHappiness ( A , N , v ) { let str ; let val = Array . from ( { length : N } , ( _ , i ) => 0 ) ; let wt = Array . from ( { length : N } , ( _ , i ) => 0 ) ; let c = 0 ; for ( let i = 0 ; i < N ; i ++ ) { str = v [ i ] ; c = 0 ; for ( let j = 0 ; j < str . length ; j ++ ) { if ( str [ j ] == ' ' ) c += 4 ; else if ( str [ j ] == ' ' ) c += 3 ; else if ( str [ j ] == ' ' ) c += 2 ; else c ++ ; } c *= str . length ; val [ i ] = c ; wt [ i ] = str . length ; } let k = new Array ( N + 1 ) ; for ( var i = 0 ; i < k . length ; i ++ ) { k [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i <= N ; i ++ ) { for ( let w = 0 ; w <= A ; w ++ ) { if ( i == 0 w == 0 ) k [ i ] [ w ] = 0 ; else if ( wt [ i - 1 ] <= w ) { k [ i ] [ w ] = Math . max ( val [ i - 1 ] + k [ i - 1 ] [ w - wt [ i - 1 ] ] , k [ i - 1 ] [ w ] ) ; } else { k [ i ] [ w ] = k [ i - 1 ] [ w ] ; } } } return k [ N ] [ A ] ; } let A = 5 ; let v = [ " " , " " , " " , " " , " " ] ; let N = v . length ; document . write ( maxHappiness ( A , N , v ) ) ;
0 | Javascript implementation of the approach ; To store states of DP ; To check if a state has been solved ; Function to compute the states ; Base case ; Check if a state has been solved ; Setting a state as solved ; Returning the solved state ; Function to pre - compute the states dp [ 0 ] [ 0 ] , dp [ 0 ] [ 1 ] , . . , dp [ 0 ] [ C_MAX ] ; Function to answer a query in O ( 1 ) ; Driver code ; Performing required pre - computation ; Perform queries
var C_MAX = 30 var max_arr_len = 10 var dp = Array . from ( Array ( max_arr_len ) , ( ) => Array ( C_MAX + 1 ) ) ; var v = Array . from ( Array ( max_arr_len ) , ( ) => Array ( C_MAX + 1 ) ) ; function findMax ( i , r , w , n ) { if ( r < 0 ) return - 1000000000 ; if ( i == n ) return 0 ; if ( v [ i ] [ r ] ) return dp [ i ] [ r ] ; v [ i ] [ r ] = 1 ; dp [ i ] [ r ] = Math . max ( w [ i ] + findMax ( i + 1 , r - w [ i ] , w , n ) , findMax ( i + 1 , r , w , n ) ) ; return dp [ i ] [ r ] ; } function preCompute ( w , n ) { for ( var i = C_MAX ; i >= 0 ; i -- ) findMax ( 0 , i , w , n ) ; } function ansQuery ( w ) { return dp [ 0 ] [ w ] ; } var w = [ 3 , 8 , 9 ] ; var n = w . length ; preCompute ( w , n ) ; var queries = [ 11 , 10 , 4 ] ; var q = queries . length ; for ( var i = 0 ; i < q ; i ++ ) document . write ( ansQuery ( queries [ i ] ) + " " ) ;
Maximise array sum after taking non | Javascript implementation of the approach ; To store the states of dp ; To check if a given state has been solved ; To store the prefix - sum ; Function to fill the prefix_sum [ ] with the prefix sum of the given array ; Function to find the maximum sum subsequence such that no two elements are adjacent ; Base case ; To check if a state has been solved ; Required recurrence relation ; Returning the value ; Driver code ; Finding prefix - sum ; Finding the maximum possible sum
var maxLen = 10 var dp = Array ( maxLen ) ; var v = Array ( maxLen ) ; var prefix_sum = Array ( maxLen ) ; ; function findPrefixSum ( arr , n ) { prefix_sum [ 0 ] = arr [ 0 ] ; for ( var i = 1 ; i < n ; i ++ ) prefix_sum [ i ] = arr [ i ] + prefix_sum [ i - 1 ] ; } function maxSum ( arr , i , n , k ) { if ( i + k > n ) return 0 ; if ( v [ i ] ) return dp [ i ] ; v [ i ] = 1 ; var x ; if ( i == 0 ) x = prefix_sum [ k - 1 ] ; else x = prefix_sum [ i + k - 1 ] - prefix_sum [ i - 1 ] ; dp [ i ] = Math . max ( maxSum ( arr , i + 1 , n , k ) , x + maxSum ( arr , i + k + 1 , n , k ) ) ; return dp [ i ] ; } var arr = [ 1 , 3 , 7 , 6 ] ; var n = arr . length ; var k = 1 ; findPrefixSum ( arr , n ) ; document . write ( maxSum ( arr , 0 , n , k ) ) ;
Number of ways to divide an array into K equal sum sub | Javascript implementation of the approach ; Array to store the states of DP ; Array to check if a state has been solved before ; To store the sum of the array elements ; Function to find the sum of all the array elements ; Function to return the number of ways ; If sum is not divisible by k answer will be zero ; Base case ; To check if a state has been solved before ; Sum of all the numbers from the beginning of the array ; Setting the current state as solved ; Recurrence relation ; Returning solved state ; Driver code ; Function call to find the sum of the array elements ; Print the number of ways
var max_size = 20 ; var max_k = 20 var dp = Array . from ( Array ( max_size ) , ( ) => Array ( max_k ) ) ; var v = Array . from ( Array ( max_size ) , ( ) => Array ( max_k ) ) ; var sum = 0 ; function findSum ( arr , n ) { for ( var i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; } function cntWays ( arr , i , ck , k , n , curr_sum ) { if ( sum % k != 0 ) return 0 ; if ( i != n && ck == k + 1 ) return 0 ; if ( i == n ) { if ( ck == k + 1 ) return 1 ; else return 0 ; } if ( v [ i ] [ ck ] ) return dp [ i ] [ ck ] ; curr_sum += arr [ i ] ; v [ i ] [ ck ] = 1 ; dp [ i ] [ ck ] = cntWays ( arr , i + 1 , ck , k , n , curr_sum ) ; if ( curr_sum == ( sum / k ) * ck ) dp [ i ] [ ck ] += cntWays ( arr , i + 1 , ck + 1 , k , n , curr_sum ) ; return dp [ i ] [ ck ] ; } var arr = [ 1 , - 1 , 1 , - 1 , 1 , - 1 ] ; var n = arr . length ; var k = 2 ; findSum ( arr , n ) ; document . write ( cntWays ( arr , 0 , 1 , k , n , 0 ) ) ;
Maximum sum in an array such that every element has exactly one adjacent element to it | Javascript implementation of the approach ; To store the states of dp ; Function to return the maximized sum ; Base case ; Checks if a state is already solved ; Recurrence relation ; Return the result ; Driver code
var arrSize = 51 ; var dp = Array ( arrSize ) ; var v = Array ( arrSize ) ; function sumMax ( i , arr , n ) { if ( i >= n - 1 ) return 0 ; if ( v [ i ] ) return dp [ i ] ; v [ i ] = true ; dp [ i ] = Math . max ( arr [ i ] + arr [ i + 1 ] + sumMax ( i + 3 , arr , n ) , sumMax ( i + 1 , arr , n ) ) ; return dp [ i ] ; } var arr = [ 1 , 1 , 1 , 1 ] ; var n = arr . length ; document . write ( sumMax ( 0 , arr , n ) ) ;
Maximum Sum Subsequence of length k | Javascript program to calculate the maximum sum of increasing subsequence of length k ; In the implementation dp [ n ] [ k ] represents maximum sum subsequence of length k and the subsequence is ending at index n . ; Initializing whole multidimensional dp array with value - 1 ; For each ith position increasing subsequence of length 1 is equal to that array ith value so initializing dp [ i ] [ 1 ] with that array value ; Starting from 1 st index as we have calculated for 0 th index . Computing optimized dp values in bottom - up manner ; Check for increasing subsequence ; Proceed if value is pre calculated ; Check for all the subsequences ending at any j < i and try including element at index i in them for some length l . Update the maximum value for every length . ; The final result would be the maximum value of dp [ i ] [ k ] for all different i . ; When no subsequence of length k is possible sum would be considered zero ; Driver Code
function MaxIncreasingSub ( arr , n , k ) { let dp = new Array ( n ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } let ans = - 1 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < k + 1 ; j ++ ) dp [ i ] [ j ] = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { dp [ i ] [ 1 ] = arr [ i ] ; } for ( let i = 1 ; i < n ; i ++ ) { for ( let j = 0 ; j < i ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { for ( let l = 1 ; l <= k - 1 ; l ++ ) { if ( dp [ j ] [ l ] != - 1 ) { dp [ i ] [ l + 1 ] = Math . max ( dp [ i ] [ l + 1 ] , dp [ j ] [ l ] + arr [ i ] ) ; } } } } } for ( let i = 0 ; i < n ; i ++ ) { if ( ans < dp [ i ] [ k ] ) ans = dp [ i ] [ k ] ; } return ( ans == - 1 ) ? 0 : ans ; } let n = 8 , k = 3 ; let arr = [ 8 , 5 , 9 , 10 , 5 , 6 , 21 , 8 ] ; let ans = MaxIncreasingSub ( arr , n , k ) ; document . write ( ans ) ;
Generate all unique partitions of an integer | Set 2 | Array to store the numbers used to form the required sum ; Function to print the array which contains the unique partitions which are used to form the required sum ; Function to find all the unique partitions remSum = remaining sum to form maxVal is the maximum number that can be used to make the partition ; If remSum == 0 that means the sum is achieved so prvar the array ; i will begin from maxVal which is the maximum value which can be used to form the sum ; Store the number used in forming sum gradually in the array ; Since i used the rest of partition cant have any number greater than i hence second parameter is i ; Driver code
var dp = Array . from ( { length : 200 } , ( _ , i ) => 0 ) ; var count = 0 ; function print ( idx ) { for ( var i = 1 ; i < idx ; i ++ ) { document . write ( dp [ i ] + " " ) ; } document . write ( ' ' ) ; } function solve ( remSum , maxVal , idx , count ) { if ( remSum == 0 ) { print ( idx ) ; count ++ ; return ; } for ( var i = maxVal ; i >= 1 ; i -- ) { if ( i > remSum ) { continue ; } else if ( i <= remSum ) { dp [ idx ] = i ; solve ( remSum - i , i , idx + 1 , count ) ; } } } var n = 4 , count = 0 ; solve ( n , n , 1 , count ) ;
Queries for bitwise OR in the given matrix | Javascript implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix - count for each row ; Finding column - wise prefix count ; Function to return the result for a query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; If count of variables with ith bit set is greater than 0 ; Driver code
const bitscount = 32 ; const n = 3 ; let prefix_count = new Array ( bitscount ) ; for ( let i = 0 ; i < bitscount ; i ++ ) { prefix_count [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) prefix_count [ i ] [ j ] = new Array ( n ) ; } function findPrefixCount ( arr ) { for ( let i = 0 ; i < bitscount ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { prefix_count [ i ] [ j ] [ 0 ] = ( ( arr [ j ] [ 0 ] >> i ) & 1 ) ; for ( let k = 1 ; k < n ; k ++ ) { prefix_count [ i ] [ j ] [ k ] = ( ( arr [ j ] [ k ] >> i ) & 1 ) ; prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j ] [ k - 1 ] ; } } } for ( let i = 0 ; i < bitscount ; i ++ ) for ( let j = 1 ; j < n ; j ++ ) for ( let k = 0 ; k < n ; k ++ ) prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j - 1 ] [ k ] ; } function rangeOr ( x1 , y1 , x2 , y2 ) { let ans = 0 ; for ( let i = 0 ; i < bitscount ; i ++ ) { let p ; if ( x1 == 0 && y1 == 0 ) p = prefix_count [ i ] [ x2 ] [ y2 ] ; else if ( x1 == 0 ) p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] ; else if ( y1 == 0 ) p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] ; else p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] + prefix_count [ i ] [ x1 - 1 ] [ y1 - 1 ] ; if ( p != 0 ) ans = ( ans | ( 1 << i ) ) ; } return ans ; } let arr = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; findPrefixCount ( arr ) ; let queries = [ [ 1 , 1 , 1 , 1 ] , [ 1 , 2 , 2 , 2 ] ] ; let q = queries . length ; for ( let i = 0 ; i < q ; i ++ ) document . write ( rangeOr ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , queries [ i ] [ 2 ] , queries [ i ] [ 3 ] ) + " " ) ;
Queries for bitwise AND in the given matrix | Javascript implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix - count for each row ; Finding column - wise prefix count ; Function to return the result for a query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; If count of variables whose ith bit is set equals to the total elements in the sub - matrix ; Driver code
let bitscount = 32 ; let n = 3 ; let prefix_count = new Array ( bitscount ) ; for ( let i = 0 ; i < bitscount ; i ++ ) { prefix_count [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) { prefix_count [ i ] [ j ] = new Array ( n ) ; for ( let k = 0 ; k < n ; k ++ ) { prefix_count [ i ] [ j ] [ k ] = 0 ; } } } function findPrefixCount ( arr ) { for ( let i = 0 ; i < bitscount ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { prefix_count [ i ] [ j ] [ 0 ] = ( ( arr [ j ] [ 0 ] >> i ) & 1 ) ; for ( let k = 1 ; k < n ; k ++ ) { prefix_count [ i ] [ j ] [ k ] = ( ( arr [ j ] [ k ] >> i ) & 1 ) ; prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j ] [ k - 1 ] ; } } } for ( let i = 0 ; i < bitscount ; i ++ ) for ( let j = 1 ; j < n ; j ++ ) for ( let k = 0 ; k < n ; k ++ ) prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j - 1 ] [ k ] ; } function rangeAnd ( x1 , y1 , x2 , y2 ) { let ans = 0 ; for ( let i = 0 ; i < bitscount ; i ++ ) { let p ; if ( x1 == 0 && y1 == 0 ) p = prefix_count [ i ] [ x2 ] [ y2 ] ; else if ( x1 == 0 ) p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] ; else if ( y1 == 0 ) p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] ; else p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] + prefix_count [ i ] [ x1 - 1 ] [ y1 - 1 ] ; if ( p == ( x2 - x1 + 1 ) * ( y2 - y1 + 1 ) ) ans = ( ans | ( 1 << i ) ) ; } return ans ; } let arr = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; findPrefixCount ( arr ) ; let queries = [ [ 1 , 1 , 1 , 1 ] , [ 1 , 2 , 2 , 2 ] ] ; let q = queries . length ; for ( let i = 0 ; i < q ; i ++ ) document . write ( rangeAnd ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , queries [ i ] [ 2 ] , queries [ i ] [ 3 ] ) + " " ) ;
Maximum sum such that no two elements are adjacent | Set 2 | Javascript program to implement above approach ; variable to store states of dp ; variable to check if a given state has been solved ; Function to find the maximum sum subsequence such that no two elements are adjacent ; Base case ; To check if a state has been solved ; Required recurrence relation ; Returning the value ; Driver code
var maxLen = 10 ; var dp = Array ( maxLen ) ; var v = Array ( maxLen ) ; function maxSum ( arr , i , n ) { if ( i >= n ) return 0 ; if ( v [ i ] ) return dp [ i ] ; v [ i ] = 1 ; dp [ i ] = Math . max ( maxSum ( arr , i + 1 , n ) , arr [ i ] + maxSum ( arr , i + 2 , n ) ) ; return dp [ i ] ; } var arr = [ 12 , 9 , 7 , 33 ] ; var n = arr . length ; document . write ( maxSum ( arr , 0 , n ) ) ;
Optimal Strategy for a Game | Set 2 | Javascript program to find out maximum value from a given sequence of coins ; For both of your choices , the opponent gives you total sum minus maximum of his value ; Returns optimal value possible that a player can collect from an array of coins of size n . Note than n must be even ; Compute sum of elements ; Initialize memoization table ; Driver Code
let MAX = 100 ; let memo = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { memo [ i ] = new Array ( MAX ) ; for ( let j = 0 ; j < MAX ; j ++ ) { memo [ i ] [ j ] = 0 ; } } function oSRec ( arr , i , j , sum ) { if ( j == i + 1 ) return Math . max ( arr [ i ] , arr [ j ] ) ; if ( memo [ i ] [ j ] != - 1 ) return memo [ i ] [ j ] ; memo [ i ] [ j ] = Math . max ( ( sum - oSRec ( arr , i + 1 , j , sum - arr [ i ] ) ) , ( sum - oSRec ( arr , i , j - 1 , sum - arr [ j ] ) ) ) ; return memo [ i ] [ j ] ; } function accumulate ( arr , start , end ) { let sum = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) sum += arr [ i ] ; return sum ; } function optimalStrategyOfGame ( arr , n ) { let sum = 0 ; sum = accumulate ( arr , 0 , n ) ; for ( let j = 0 ; j < MAX ; j ++ ) { for ( let k = 0 ; k < MAX ; k ++ ) memo [ j ] [ k ] = - 1 ; } return oSRec ( arr , 0 , n - 1 , sum ) ; } let arr1 = [ 8 , 15 , 3 , 7 ] ; let n = arr1 . length ; document . write ( optimalStrategyOfGame ( arr1 , n ) + " " ) ; let arr2 = [ 2 , 2 , 2 , 2 ] ; n = arr2 . length ; document . write ( optimalStrategyOfGame ( arr2 , n ) + " " ) ; let arr3 = [ 20 , 30 , 2 , 2 , 2 , 10 ] ; n = arr3 . length ; document . write ( optimalStrategyOfGame ( arr3 , n ) + " " ) ;