codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of operations required to sum to binary string S | Function to return the minimum operations required to sum to a number reprented by the binary string S ; Reverse the string to consider it from LSB to MSB ; initialise the dp table ; If S [ 0 ] = '0' , there is no need to perform any operation ; If S [ 0 ] = '1' , just perform a single operation ( i . e Add 2 ^ 0 ) ; Irrespective of the LSB , dp [ 0 ] [ 1 ] is always 1 as there is always the need of making the suffix of the binary string of the form "11 . . . . 1" as suggested by the definition of dp [ i ] [ 1 ] ; Transition from dp [ i - 1 ] [ 0 ] ; 1. Transition from dp [ i - 1 ] [ 1 ] by just doing 1 extra operation of subtracting 2 ^ i 2. Transition from dp [ i - 1 ] [ 0 ] by just doing 1 extra operation of subtracting 2 ^ ( i + 1 ) ; Transition from dp [ i - 1 ] [ 1 ] ; 1. Transition from dp [ i - 1 ] [ 1 ] by just doing 1 extra operation of adding 2 ^ ( i + 1 ) 2. Transition from dp [ i - 1 ] [ 0 ] by just doing 1 extra operation of adding 2 ^ i ; Swap values of left and right ; Driver Code
function findMinOperations ( S ) { S = reverse ( S ) ; let n = S . length ; let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; for ( let j = 0 ; j < 2 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } if ( S [ 0 ] == ' ' ) { dp [ 0 ] [ 0 ] = 0 ; } else { dp [ 0 ] [ 0 ] = 1 ; } dp [ 0 ] [ 1 ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( S [ i ] == ' ' ) { dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] ; dp [ i ] [ 1 ] = 1 + Math . min ( dp [ i - 1 ] [ 1 ] , dp [ i - 1 ] [ 0 ] ) ; } else { dp [ i ] [ 1 ] = dp [ i - 1 ] [ 1 ] ; dp [ i ] [ 0 ] = 1 + Math . min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) ; } } return dp [ n - 1 ] [ 0 ] ; } function reverse ( input ) { let temparray = input . split ( " " ) ; let left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { let temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return ( temparray ) . join ( " " ) ; } let S = " " ; document . write ( findMinOperations ( S ) + " " ) ; S = " " ; document . write ( findMinOperations ( S ) + " " ) ;
K | Function that finds the Nth element of K - Fibonacci series ; If N is less than K then the element is '1' ; first k elements are 1 ; ( K + 1 ) th element is K ; find the elements of the K - Fibonacci series ; subtract the element at index i - k - 1 and add the element at index i - i from the sum ( sum contains the sum of previous ' K ' elements ) ; set the new sum ; Driver code ; get the Nth value of K - Fibonacci series
function solve ( N , K ) { var Arr = new Array ( N + 1 ) ; if ( N <= K ) { document . write ( " " + " " ) ; return ; } var i = 0 , sum = K ; for ( i = 1 ; i <= K ; ++ i ) { Arr [ i ] = 1 ; } Arr [ i ] = sum ; for ( var i = K + 2 ; i <= N ; ++ i ) { Arr [ i ] = sum - Arr [ i - K - 1 ] + Arr [ i - 1 ] ; sum = Arr [ i ] ; } document . write ( Arr [ N ] + " " ) ; } var N = 4 , K = 2 ; solve ( N , K ) ;
Minimum sum possible of any bracket sequence of length N | Javascript program to find the Minimum sum possible of any bracket sequence of length N using the given values for brackets ; DP array ; Recursive function to check for correct bracket expression ; / Not a proper bracket expression ; If reaches at end ; / If proper bracket expression ; else if not , return max ; If already visited ; To find out minimum sum ; Driver Code
let MAX_VAL = 10000000 ; let dp = new Array ( 100 ) ; for ( let i = 0 ; i < 100 ; i ++ ) { dp [ i ] = new Array ( 100 ) ; for ( let j = 0 ; j < 100 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } function find ( index , openbrk , n , adj ) { if ( openbrk < 0 ) return MAX_VAL ; if ( index == n ) { if ( openbrk == 0 ) { return 0 ; } return MAX_VAL ; } if ( dp [ index ] [ openbrk ] != - 1 ) return dp [ index ] [ openbrk ] ; dp [ index ] [ openbrk ] = Math . min ( adj [ index ] [ 1 ] + find ( index + 1 , openbrk + 1 , n , adj ) , adj [ index ] [ 0 ] + find ( index + 1 , openbrk - 1 , n , adj ) ) ; return dp [ index ] [ openbrk ] ; } let n = 4 ; let adj = [ [ 5000 , 3000 ] , [ 6000 , 2000 ] , [ 8000 , 1000 ] , [ 9000 , 6000 ] ] ; for ( let i = 0 ; i < dp . length ; i ++ ) for ( let j = 0 ; j < dp . length ; j ++ ) dp [ i ] [ j ] = - 1 ; document . write ( find ( 1 , 1 , n , adj ) + adj [ 0 ] [ 1 ] ) ;
Understanding The Coin Change Problem With Dynamic Programming | We have input values of N and an array Coins that holds all of the coins . We use data type of long because we want to be able to test large values without integer overflow ; Create the ways array to 1 plus the amount to stop overflow ; Set the first way to 1 because its 0 and there is 1 way to make 0 with 0 coins ; Go through all of the coins ; Make a comparison to each index value of ways with the coin value . ; Update the ways array ; return the value at the Nth position of the ways array . ; Driver code
function getNumberOfWays ( N , Coins ) { let ways = new Array ( N + 1 ) ; for ( let i = 0 ; i < N + 1 ; i ++ ) { ways [ i ] = 0 ; } ways [ 0 ] = 1 ; for ( let i = 0 ; i < Coins . length ; i ++ ) { for ( let j = 0 ; j < ways . length ; j ++ ) { if ( Coins [ i ] <= j ) { ways [ j ] += ways [ ( j - Coins [ i ] ) ] ; } } } return ways [ N ] ; } function printArray ( coins ) { for ( let i = 0 ; i < coins . length ; i ++ ) { document . write ( coins [ i ] + " " ) ; } } let Coins = [ 1 , 5 , 10 ] ; document . write ( " " ) ; printArray ( Coins ) ; document . write ( " " ) ; document . write ( getNumberOfWays ( 12 , Coins ) + " " ) ;
Minimum cost to buy N kilograms of sweet for M persons | Function to find the minimum cost of sweets ; Defining the sweet array ; DP array to store the values ; Since index starts from 1 we reassign the array into sweet ; Assigning base cases for dp array ; At 0 it is free ; Package not available for desirable amount of sweets ; Buying the ' k ' kg package and assigning it to dp array ; If no solution , select from previous k - 1 packages ; If solution does not exist ; Print the solution ; Driver code ; Calling the desired function
function find ( m , n , adj ) { let sweet = new Array ( n + 1 ) ; let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; ++ i ) { dp [ i ] = new Array ( n + 1 ) ; for ( let j = 0 ; j <= n ; ++ j ) { dp [ i ] [ j ] = new Array ( n + 1 ) ; for ( let k = 0 ; k <= n ; ++ k ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } sweet [ 0 ] = 0 ; for ( let i = 1 ; i <= m ; ++ i ) sweet [ i ] = adj [ i - 1 ] ; for ( let i = 0 ; i <= m ; ++ i ) { for ( let k = 0 ; k <= n ; ++ k ) dp [ i ] [ 0 ] [ k ] = 0 ; for ( let k = 1 ; k <= n ; ++ k ) dp [ i ] [ k ] [ 0 ] = - 1 ; } for ( let i = 0 ; i <= m ; ++ i ) { for ( let j = 1 ; j <= n ; ++ j ) { for ( let k = 1 ; k <= n ; ++ k ) { dp [ i ] [ j ] [ k ] = - 1 ; if ( i > 0 && j >= k && sweet [ k ] > 0 && dp [ i - 1 ] [ j - k ] [ k ] != - 1 ) dp [ i ] [ j ] [ k ] = dp [ i - 1 ] [ j - k ] [ k ] + sweet [ k ] ; if ( dp [ i ] [ j ] [ k ] == - 1 || ( dp [ i ] [ j ] [ k - 1 ] != - 1 && dp [ i ] [ j ] [ k ] > dp [ i ] [ j ] [ k - 1 ] ) ) dp [ i ] [ j ] [ k ] = dp [ i ] [ j ] [ k - 1 ] ; } } } if ( dp [ m ] [ n ] [ n ] == - 1 ) return 0 ; else return dp [ m ] [ n ] [ n ] ; } let m = 3 ; let adj = [ 2 , 1 , 3 , 0 , 4 , 10 ] ; let n = adj . length ; document . write ( find ( m , n , adj ) ) ;
Maximum length of segments of 0 ' s ▁ and ▁ 1' s | Recursive Function to find total length of the array Where 1 is greater than zero ; If reaches till end ; If dp is saved ; Finding for each length ; If the character scanned is 1 ; If one is greater than zero , add total length scanned till now ; Continue with next length ; Return the value for start index ; Driver code ; Size of string ; Calling the function to find the value of function
function find ( start , adj , n , dp ) { if ( start == n ) return 0 ; if ( dp [ start ] != - 1 ) return dp [ start ] ; dp [ start ] = 0 ; var one = 0 , zero = 0 , k ; for ( k = start ; k < n ; k ++ ) { if ( adj [ k ] == ' ' ) one ++ ; else zero ++ ; if ( one > zero ) dp [ start ] = Math . max ( dp [ start ] , find ( k + 1 , adj , n , dp ) + k - start + 1 ) ; else dp [ start ] = Math . max ( dp [ start ] , find ( k + 1 , adj , n , dp ) ) ; } return dp [ start ] ; } var adj = " " ; var n = adj . length ; var dp = Array ( n + 1 ) . fill ( - 1 ) ; document . write ( find ( 0 , adj , n , dp ) ) ;
Length of longest common subsequence containing vowels | Function to check whether ' ch ' is a vowel or not ; Function to find the length of longest common subsequence which contains all vowel characters ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; L [ m ] [ n ] contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ] which contains all vowel characters ; Driver Code
function isVowel ( ch ) { if ( ch == ' ' ch == ' ' ch == ' ' ch == ' ' ch == ' ' ) return true ; return false ; } function lcs ( X , Y , m , n ) { let L = new Array ( m + 1 ) ; let i , j ; for ( i = 0 ; i <= m ; i ++ ) { L [ i ] = new Array ( n + 1 ) ; for ( j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i ] [ j ] = 0 ; else if ( ( X [ i - 1 ] == Y [ j - 1 ] ) && isVowel ( X [ i - 1 ] ) ) L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; else L [ i ] [ j ] = Math . max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } return L [ m ] [ n ] ; } let X = " " ; let Y = " " ; let m = X . length ; let n = Y . length ; document . write ( " " + lcs ( X , Y , m , n ) ) ;
Minimum number of single digit primes required whose sum is equal to N | function to check if i - th index is valid or not ; function to find the minimum number of single digit prime numbers required which when summed up equals to a given number N . ; Not possible ; Driver Code
function check ( i , val ) { if ( i - val < 0 ) return false ; return true ; } function MinimumPrimes ( n ) { let dp = new Array ( n + 1 ) for ( let i = 1 ; i <= n ; i ++ ) dp [ i ] = 1e9 ; dp [ 0 ] = dp [ 2 ] = dp [ 3 ] = dp [ 5 ] = dp [ 7 ] = 1 ; for ( let i = 1 ; i <= n ; i ++ ) { if ( check ( i , 2 ) ) dp [ i ] = Math . min ( dp [ i ] , 1 + dp [ i - 2 ] ) ; if ( check ( i , 3 ) ) dp [ i ] = Math . min ( dp [ i ] , 1 + dp [ i - 3 ] ) ; if ( check ( i , 5 ) ) dp [ i ] = Math . min ( dp [ i ] , 1 + dp [ i - 5 ] ) ; if ( check ( i , 7 ) ) dp [ i ] = Math . min ( dp [ i ] , 1 + dp [ i - 7 ] ) ; } if ( dp [ n ] == ( 1e9 ) ) return - 1 ; else return dp [ n ] ; } let n = 12 ; let minimal = MinimumPrimes ( n ) ; if ( minimal != - 1 ) { document . write ( " " + " " ) ; document . write ( minimal ) ; } else { document . write ( " " ) ; }
Find all distinct subset ( or subsequence ) sums of an array | Set | Function to print all th distinct sum ; Declare a boolean array of size equal to total sum of the array ; Fill the first row beforehand ; dp [ j ] will be true only if sum j can be formed by any possible addition of numbers in given array upto index i , otherwise false ; Iterate from maxSum to 1 and avoid lookup on any other row ; Do not change the dp array for j less than arr [ i ] ; If dp [ j ] is true then print ; Function to find the total sum and print the distinct sum ; Find the sum of array elements ; Function to print all the distinct sum ; Driver Code
function subsetSum ( arr , n , maxSum ) { var dp = Array ( maxSum + 1 ) . fill ( false ) dp [ arr [ 0 ] ] = true ; for ( var i = 1 ; i < n ; i ++ ) { for ( var j = maxSum ; j >= 1 ; j -- ) { if ( arr [ i ] <= j ) { if ( arr [ i ] == j || dp [ j ] || dp [ ( j - arr [ i ] ) ] ) dp [ j ] = true ; else dp [ j ] = false ; } } } document . write ( 0 + " " ) ; for ( var j = 0 ; j < maxSum + 1 ; j ++ ) { if ( dp [ j ] == true ) document . write ( j + " " ) ; } document . write ( " " ) ; } function printDistinct ( a , n ) { var maxSum = 0 ; for ( var i = 0 ; i < n ; i ++ ) { maxSum += a [ i ] ; } subsetSum ( a , n , maxSum ) ; } var arr = [ 2 , 3 , 4 , 5 , 6 ] ; var n = arr . length ; printDistinct ( arr , n ) ;
Sudo Placement [ 1.5 ] | Wolfish | Javascript program for SP - Wolfish ; Function to find the maxCost of path from ( n - 1 , n - 1 ) to ( 0 , 0 ) | recursive approach ; base condition ; reaches the point ; i + j ; check if it is a power of 2 , then only move diagonally ; if not a power of 2 then move side - wise ; Function to return the maximum cost ; calling dp function to get the answer ; Driver Code ; Function calling to get the answer
let size = 1000 ; function maxCost ( a , m , n ) { if ( n < 0 m < 0 ) { return - 1 ; } else if ( m == 0 && n == 0 ) { return 0 ; } else { let num = m + n ; if ( ( num & ( num - 1 ) ) == 0 ) { return a [ m ] [ n ] + maxCost ( a , m - 1 , n - 1 ) ; } else { return a [ m ] [ n ] + Math . max ( maxCost ( a , m - 1 , n ) , maxCost ( a , m , n - 1 ) ) ; } } } function answer ( a , n ) { return maxCost ( a , n - 1 , n - 1 ) ; } let a = [ [ 1 , 2 , 3 , 1 ] , [ 4 , 5 , 6 , 1 ] , [ 7 , 8 , 9 , 1 ] , [ 1 , 1 , 1 , 1 ] ] ; let n = 4 ; document . write ( answer ( a , n ) ) ;
Longest Common Subsequence | DP using Memoization | Javascript program to memoize recursive implementation of LCS problem ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] memoization applied in recursive solution ; base case ; if the same state has already been computed ; if equal , then we store the value of the function call ; store it in arr to avoid further repetitive work in future function calls ; store it in arr to avoid further repetitive work in future function calls ; Driver Code ; assign - 1 to all positions
let maximum = 1000 ; function lcs ( X , Y , m , n , dp ) { if ( m == 0 n == 0 ) { return 0 ; } if ( dp [ m - 1 ] [ n - 1 ] != - 1 ) { return dp [ m - 1 ] [ n - 1 ] ; } if ( X [ m - 1 ] == Y [ n - 1 ] ) { dp [ m - 1 ] [ n - 1 ] = 1 + lcs ( X , Y , m - 1 , n - 1 , dp ) ; return dp [ m - 1 ] [ n - 1 ] ; } else { dp [ m - 1 ] [ n - 1 ] = Math . max ( lcs ( X , Y , m , n - 1 , dp ) , lcs ( X , Y , m - 1 , n , dp ) ) ; return dp [ m - 1 ] [ n - 1 ] ; } } let X = " " ; let Y = " " ; let m = X . length ; let n = Y . length ; let dp = new Array ( m ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( maximum ) ; for ( let j = 0 ; j < dp [ i ] . length ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } document . write ( " " + lcs ( X , Y , m , n , dp ) ) ;
Memoization ( 1D , 2D and 3D ) | Javascript program to memoize recursive implementation of LCS problem ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] memoization applied in recursive solution ; base case ; if the same state has already been computed ; if equal , then we store the value of the function call ; store it in arr to avoid further repetitive work in future function calls ; store it in arr to avoid further repetitive work in future function calls ; Driver code
let arr = new Array ( 1000 ) ; for ( let i = 0 ; i < 1000 ; i ++ ) { arr [ i ] = new Array ( 1000 ) ; for ( let j = 0 ; j < 1000 ; j ++ ) { arr [ i ] [ j ] = - 1 ; } } function lcs ( X , Y , m , n ) { if ( m == 0 n == 0 ) return 0 ; if ( arr [ m - 1 ] [ n - 1 ] != - 1 ) return arr [ m - 1 ] [ n - 1 ] ; if ( X [ m - 1 ] == Y [ n - 1 ] ) { arr [ m - 1 ] [ n - 1 ] = 1 + lcs ( X , Y , m - 1 , n - 1 ) ; return arr [ m - 1 ] [ n - 1 ] ; } else { arr [ m - 1 ] [ n - 1 ] = Math . max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ; return arr [ m - 1 ] [ n - 1 ] ; } } let X = " " ; let Y = " " ; let m = X . length ; let n = Y . length ; document . write ( " " + lcs ( X , Y , m , n ) ) ;
Minimum splits in a binary string such that every substring is a power of 4 or 6. | Function to find if given number is power of another number or not . ; Divide given number repeatedly by base value . ; return false ; not a power ; Function to find minimum number of partitions of given binary string so that each partition is power of 4 or 6. ; Variable to store integer value of given binary string partition . ; DP table to store results of partitioning done at differentindices . ; If the last digit is 1 , hence 4 ^ 0 = 1 and 6 ^ 0 = 1 ; Fix starting position for partition ; Binary representation with leading zeroes is not allowed . ; Iterate for all different partitions starting from i ; Find integer value of current binary partition . ; Check if the value is a power of 4 or 6 or not apply recurrence relation ; If no partitions are possible , then make dp [ i ] = - 1 to represent this . ; Driver code
function isPowerOf ( val , base ) { while ( val > 1 ) { if ( val % base != 0 ) val /= base ; } return true ; } function numberOfPartitions ( binaryNo ) { var i , j , n = binaryNo . length ; var val ; var dp = Array ( n ) ; dp [ n - 1 ] = ( ( binaryNo [ n - 1 ] - ' ' ) == 0 ) ? - 1 : 1 ; for ( i = n - 2 ; i >= 0 ; i -- ) { val = 0 ; if ( ( binaryNo [ i ] - ' ' ) == 0 ) { dp [ i ] = - 1 ; continue ; } dp [ i ] = 1000000000 ; for ( j = i ; j < n ; j ++ ) { val = ( val * 2 ) + ( binaryNo [ j ] - ' ' ) ; if ( isPowerOf ( val , 4 ) || isPowerOf ( val , 6 ) ) { if ( j == n - 1 ) { dp [ i ] = 1 ; } else { if ( dp [ j + 1 ] != - 1 ) dp [ i ] = Math . min ( dp [ i ] , dp [ j + 1 ] + 1 ) ; } } } if ( dp [ i ] == 1000000000 ) dp [ i ] = - 1 ; } return dp [ 0 ] ; } var binaryNo = " " ; document . write ( numberOfPartitions ( binaryNo ) ) ;
Sum of product of r and rth Binomial Coefficient ( r * nCr ) | Javascript Program to find sum of product of r and rth Binomial Coefficient i . e summation r * nCr ; Return summation of r * nCr ; Driven Program
var MAX = 100 function summation ( n ) { return n << ( n - 1 ) ; } var n = 2 ; document . write ( summation ( n ) ) ;
Integers from the range that are composed of a single distinct digit | Function to return the count of digits of a number ; Function to return a number that contains only digit ' d ' repeated exactly count times ; Function to return the count of integers that are composed of a single distinct digit only ; Count of digits in L and R ; First digits of L and R ; If L has lesser number of digits than R ; If the number that starts with firstDigitL and has number of digits = countDigitsL is within the range include the number ; Exclude the number ; If the number that starts with firstDigitR and has number of digits = countDigitsR is within the range include the number ; Exclude the number ; If both L and R have equal number of digits ; Include the number greater than L upto the maximum number whose digit = coutDigitsL ; Exclude the numbers which are greater than R ; Return the count ; Driver code
function countDigits ( n ) { let count = 0 ; while ( n > 0 ) { count += 1 ; n = parseInt ( n / 10 , 10 ) ; } return count ; } function getDistinct ( d , count ) { let num = 0 ; count = parseInt ( Math . pow ( 10 , count - 1 ) , 10 ) ; while ( count > 0 ) { num += ( count * d ) ; count = parseInt ( count / 10 , 10 ) ; } return num ; } function findCount ( L , R ) { let count = 0 ; let countDigitsL = countDigits ( L ) ; let countDigitsR = countDigits ( R ) ; let firstDigitL = parseInt ( L / parseInt ( Math . pow ( 10 , countDigitsL - 1 ) , 10 ) , 10 ) ; let firstDigitR = parseInt ( R / parseInt ( Math . pow ( 10 , countDigitsR - 1 ) , 10 ) , 10 ) ; if ( countDigitsL < countDigitsR ) { count += ( 9 * ( countDigitsR - countDigitsL - 1 ) ) ; if ( getDistinct ( firstDigitL , countDigitsL ) >= L ) count += ( 9 - firstDigitL + 1 ) ; else count += ( 9 - firstDigitL ) ; if ( getDistinct ( firstDigitR , countDigitsR ) <= R ) count += firstDigitR ; else count += ( firstDigitR - 1 ) ; } else { if ( getDistinct ( firstDigitL , countDigitsL ) >= L ) count += ( 9 - firstDigitL + 1 ) ; else count += ( 9 - firstDigitL ) ; if ( getDistinct ( firstDigitR , countDigitsR ) <= R ) count -= ( 9 - firstDigitR ) ; else count -= ( 9 - firstDigitR + 1 ) ; } return count ; } let L = 10 , R = 50 ; document . write ( findCount ( L , R ) ) ;
Maximum average sum partition of an array | JavaScript program for maximum average sum partition ; bottom up approach to calculate score ; storing averages from starting to each i ; ; Driver code ; atmost partitioning size
let MAX = 1000 ; let memo = new Array ( MAX ) . fill ( 0 ) . map ( ( ) => new Array ( MAX ) . fill ( 0 ) ) ; function score ( n , A , k ) { if ( memo [ n ] [ k ] > 0 ) return memo [ n ] [ k ] ; let sum = 0 ; for ( let i = n - 1 ; i > 0 ; i -- ) { sum += A [ i ] ; memo [ n ] [ k ] = Math . max ( memo [ n ] [ k ] , score ( i , A , k - 1 ) + sum / ( n - i ) ) ; } return memo [ n ] [ k ] ; } function largestSumOfAverages ( A , K ) { let n = A . length ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += A [ i ] ; memo [ i + 1 ] [ 1 ] = sum / ( i + 1 ) ; } return score ( n , A , K ) ; } let A = [ 9 , 1 , 2 , 3 , 9 ] ; let K = 3 ; document . write ( largestSumOfAverages ( A , K ) + " " ) ;
Maximum Product Subarray | Added negative product case | Function to find maximum subarray product . ; As maximum product can be negative , so initialize ans with minimum integer value . ; Variable to store maximum product until current value . ; Variable to store minimum product until current value . ; Variable used during updation of maximum product and minimum product . ; If current element is positive , update maxval . Update minval if it is negative . ; If current element is zero , maximum product cannot end at current element . Update minval with 1 and maxval with 0. maxval is updated to 0 as in case all other elements are negative , then maxval is 0. ; If current element is negative , then new value of maxval is previous minval * arr [ i ] and new value of minval is previous maxval * arr [ i ] . Before updating maxval , store its previous value in prevMax to be used to update minval . ; Update ans if necessary . ; If maxval is zero , then to calculate product for next iteration , it should be set to 1 as maximum product subarray does not include 0. The minimum possible value to be considered in maximum product subarray is already stored in minval , so when maxval is negative it is set to 1. ; Driver code
function findMaxProduct ( arr , n ) { let i ; let ans = - 1 ; let maxval = 1 ; let minval = 1 ; let prevMax ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) { maxval = maxval * arr [ i ] ; minval = Math . min ( 1 , minval * arr [ i ] ) ; } else if ( arr [ i ] == 0 ) { minval = 1 ; maxval = 0 ; } else if ( arr [ i ] < 0 ) { prevMax = maxval ; maxval = minval * arr [ i ] ; minval = prevMax * arr [ i ] ; } ans = Math . max ( ans , maxval ) ; if ( maxval <= 0 ) { maxval = 1 ; } } return ans ; } let arr = [ 0 , - 4 , 0 , - 2 ] ; let n = arr . length ; document . write ( findMaxProduct ( arr , n ) ) ;
Maximum and Minimum Values of an Algebraic Expression | Javascript program to find the maximum and minimum values of an Algebraic expression of given form ; Finding sum of array elements ; shifting the integers by 50 so that they become positive ; dp [ i ] [ j ] represents true if sum j can be reachable by choosing i numbers ; if dp [ i ] [ j ] is true , that means it is possible to select i numbers from ( n + m ) numbers to sum upto j ; k can be at max n because the left expression has n numbers ; checking if a particular sum can be reachable by choosing n numbers ; getting the actual sum as we shifted the numbers by / 50 to avoid negative indexing in array ; Driver Code
var INF = 1000000000 var MAX = 50 function minMaxValues ( arr , n , m ) { var sum = 0 ; for ( var i = 0 ; i < ( n + m ) ; i ++ ) { sum += arr [ i ] ; arr [ i ] += 50 ; } var dp = Array . from ( Array ( MAX + 1 ) , ( ) => Array ( MAX * MAX + 1 ) . fill ( 0 ) ) ; dp [ 0 ] [ 0 ] = 1 ; for ( var i = 0 ; i < ( n + m ) ; i ++ ) { for ( var k = Math . min ( n , i + 1 ) ; k >= 1 ; k -- ) { for ( var j = 0 ; j < MAX * MAX + 1 ; j ++ ) { if ( dp [ k - 1 ] [ j ] ) dp [ k ] [ j + arr [ i ] ] = 1 ; } } } var max_value = - INF , min_value = INF ; for ( var i = 0 ; i < MAX * MAX + 1 ; i ++ ) { if ( dp [ n ] [ i ] ) { var temp = i - 50 * n ; max_value = Math . max ( max_value , temp * ( sum - temp ) ) ; min_value = Math . min ( min_value , temp * ( sum - temp ) ) ; } } document . write ( " " + max_value + " " + " " + min_value ) ; } var n = 2 , m = 2 ; var arr = [ 1 , 2 , 3 , 4 ] ; minMaxValues ( arr , n , m ) ;