codeparrot/xlcost-text-to-code huggingface dataset:
Maximum possible array sum after performing the given operation | Function to return the maximum possible sum after performing the given operation ; Dp vector to store the answer ; Base value ; Return the maximum sum ; Driver code
function max_sum ( a , n ) { var dp = Array ( n + 1 ) . fill ( ) . map ( ( ) => Array ( 2 ) . fill ( 0 ) ) ; dp [ 0 ] [ 0 ] = 0 ; dp [ 0 ] [ 1 ] = - 999999 ; for ( i = 0 ; i <= n - 1 ; i ++ ) { dp [ i + 1 ] [ 0 ] = Math . max ( dp [ i ] [ 0 ] + a [ i ] , dp [ i ] [ 1 ] - a [ i ] ) ; dp [ i + 1 ] [ 1 ] = Math . max ( dp [ i ] [ 0 ] - a [ i ] , dp [ i ] [ 1 ] + a [ i ] ) ; } return dp [ n ] [ 0 ] ; } var a = [ - 10 , 5 , - 4 ] ; var n = a . length ; document . write ( max_sum ( a , n ) ) ;
Find the number of ways to reach Kth step in stair case | JavaScript implementation of the approach ; Function to return the number of ways to reach the kth step ; Create the dp array ; Broken steps ; Calculate the number of ways for the rest of the positions ; If it is a blocked position ; Number of ways to get to the ith step ; Return the required answer ; Driver code
let MOD = 1000000007 ; function number_of_ways ( arr , n , k ) { if ( k == 1 ) return 1 ; let dp = new Array ( k + 1 ) ; let i ; for ( i = 0 ; i < k + 1 ; i ++ ) dp [ i ] = - 1 ; for ( i = 0 ; i < n ; i ++ ) dp [ arr [ i ] ] = 0 ; dp [ 0 ] = 1 ; dp [ 1 ] = ( dp [ 1 ] == - 1 ) ? 1 : dp [ 1 ] ; for ( i = 2 ; i <= k ; ++ i ) { if ( dp [ i ] == 0 ) continue ; dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] ; dp [ i ] %= MOD ; } return dp [ k ] ; } let arr = [ 3 ] ; let n = arr . length ; let k = 6 ; document . write ( number_of_ways ( arr , n , k ) ) ;
Minimum number of coins that can generate all the values in the given range | Function to find minimum number of coins ; Driver code
function findCount ( n ) { return parseInt ( Math . log ( n ) / Math . log ( 2 ) , 10 ) + 1 ; } let N = 10 ; document . write ( findCount ( N ) ) ;
Calculate the number of set bits for every number from 0 to N | Javascript implementation of the approach ; Function to find the count of set bits in all the integers from 0 to n ; Driver code
function count ( n ) { let count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } function findSetBits ( n ) { for ( let i = 0 ; i <= n ; i ++ ) document . write ( count ( i ) + " " ) ; } let n = 5 ; findSetBits ( n ) ;
Count number of ways to arrange first N numbers | Function to return the count of required arrangements ; Create a vector ; Store numbers from 1 to n ; To store the count of ways ; Generate all the permutations using next_permutation in STL ; Initialize flag to true if first element is 1 else false ; Checking if the current permutation satisfies the given conditions ; If the current permutation is invalid then set the flag to false ; If valid arrangement ; Generate the next permutation ;  ; Driver code
function countWays ( n ) { let a = [ ] ; let i = 1 ; while ( i <= n ) a . push ( i ++ ) ; let ways = 0 ; do { let flag = ( a [ 0 ] == 1 ) ; for ( let j = 1 ; j < n ; j ++ ) { if ( Math . abs ( a [ j ] - a [ j - 1 ] ) > 2 ) flag = false ; } if ( flag ) ways ++ ; } while ( next_permutation ( a ) ) ; return ways ; } function next_permutation ( p ) { for ( let a = p . length - 2 ; a >= 0 ; -- a ) { if ( p [ a ] < p [ a + 1 ] ) { for ( let b = p . length - 1 ; ; -- b ) { if ( p [ b ] > p [ a ] ) { let t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } } } } return false ; } let n = 6 ; document . write ( countWays ( n ) ) ;
Count number of ways to arrange first N numbers | Function to return the count of required arrangements ; Create the dp array ; Initialize the base cases as explained above ; ( 12 ) as the only possibility ; Generate answer for greater values ; dp [ n ] contains the desired answer ; Driver code
function countWays ( n ) { let dp = new Array ( n + 1 ) ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; dp [ 2 ] = 1 ; for ( let i = 3 ; i <= n ; i ++ ) { dp [ i ] = dp [ i - 1 ] + dp [ i - 3 ] + 1 ; } return dp [ n ] ; } let n = 6 ; document . write ( countWays ( n ) ) ;
Number of shortest paths to reach every cell from bottom | Function to find number of shortest paths ; Compute the grid starting from the bottom - left corner ; Prvar the grid ; Driver code ; Function call
function NumberOfShortestPaths ( n , m ) { var a = Array ( n ) . fill ( ) . map ( ( ) => Array ( m ) . fill ( 0 ) ) ; for ( var i = n - 1 ; i >= 0 ; i -- ) { for ( j = 0 ; j < m ; j ++ ) { if ( j == 0 i == n - 1 ) a [ i ] [ j ] = 1 ; else a [ i ] [ j ] = a [ i ] [ j - 1 ] + a [ i + 1 ] [ j ] ; } } for ( var i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { document . write ( a [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } var n = 5 , m = 2 ; NumberOfShortestPaths ( n , m ) ;
Maximum sum combination from two arrays | Function to maximum sum combination from two arrays ; To store dp value ; For loop to calculate the value of dp ; Return the required answer ; Driver code ; Function call
function Max_Sum ( arr1 , arr2 , n ) { let dp = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { dp [ i ] = new Array ( 2 ) ; for ( let j = 0 ; j < 2 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { dp [ i ] [ 0 ] = arr1 [ i ] ; dp [ i ] [ 1 ] = arr2 [ i ] ; continue ; } dp [ i ] [ 0 ] = Math . max ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] + arr1 [ i ] ) ; dp [ i ] [ 1 ] = Math . max ( dp [ i - 1 ] [ 1 ] , dp [ i - 1 ] [ 0 ] + arr2 [ i ] ) ; } return Math . max ( dp [ n - 1 ] [ 0 ] , dp [ n - 1 ] [ 1 ] ) ; } let arr1 = [ 9 , 3 , 5 , 7 , 3 ] ; let arr2 = [ 5 , 8 , 1 , 4 , 5 ] ; let n = arr1 . length ; document . write ( Max_Sum ( arr1 , arr2 , n ) ) ;
Partition the array in K segments such that bitwise AND of individual segment sum is maximized | Function to check whether a k segment partition is possible such that bitwise AND is ' mask ' ; dp [ i ] [ j ] stores whether it is possible to partition first i elements into j segments such that all j segments are ' good ' ; Initialising dp ; Filling dp in bottom - up manner ; Finding a cut such that first l elements can be partitioned into j - 1 ' good ' segments and arr [ l + 1 ] + ... + arr [ i ] is a ' good ' segment ; Function to find maximum possible AND ; Array to store prefix sums ; Maximum no of bits in the possible answer ; This will store the final answer ; Constructing answer greedily selecting from the higher most bit ; Checking if array can be partitioned such that the bitwise AND is ans | ( 1 << i ) ; if possible , update the answer ; Return the final answer ; Driver code ; n = 11 , first element is zero to make array 1 based indexing . So , number of elements are 10 ; Function call
function checkpossible ( mask , arr , prefix , n , k ) { let i , j ; let dp = new Array ( n + 1 ) ; for ( i = 0 ; i < n + 1 ; i ++ ) { dp [ i ] = new Array ( k + 1 ) ; for ( j = 0 ; j < k + 1 ; j ++ ) { dp [ i ] [ j ] = false ; } } dp [ 0 ] [ 0 ] = true ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= k ; j ++ ) { for ( let l = i - 1 ; l >= 0 ; -- l ) { if ( dp [ l ] [ j - 1 ] && ( ( ( prefix [ i ] - prefix [ l ] ) & mask ) == mask ) ) { dp [ i ] [ j ] = true ; break ; } } } } return dp [ n ] [ k ] ; } function Partition ( arr , n , k ) { let prefix = new Array ( n + 1 ) ; prefix . fill ( 0 ) ; for ( let i = 1 ; i <= n ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + arr [ i ] ; } let LOGS = 20 ; let ans = 0 ; for ( let i = LOGS ; i >= 0 ; -- i ) { if ( checkpossible ( ans | ( 1 << i ) , arr , prefix , n , k ) ) { ans = ans | ( 1 << i ) ; } } return ans ; } let arr = [ 0 , 1 , 2 , 7 , 10 , 23 , 21 , 6 , 8 , 7 , 3 ] , k = 2 ; let n = arr . length - 1 ; document . write ( Partition ( arr , n , k ) ) ;
Cost Based Tower of Hanoi | Javascript implementation of the approach ; Function to initialize the dp table ; Initialize with maximum value ; Function to return the minimum cost ; Base case ; If problem is already solved , return the pre - calculated answer ; Number of the auxiliary disk ; Initialize the minimum cost as Infinity ; Calculationg the cost for first case ; Calculating the cost for second case ; Minimum of both the above cases ; Store it in the dp table ; Return the minimum cost ; Driver code
let RODS = 3 ; let N = 3 ; let dp = new Array ( N + 1 ) ; function initialize ( ) { for ( let i = 0 ; i <= N ; i += 1 ) { dp [ i ] = new Array ( RODS + 1 ) ; for ( let j = 1 ; j <= RODS ; j ++ ) { dp [ i ] [ j ] = new Array ( RODS + 1 ) ; for ( let k = 1 ; k <= RODS ; k += 1 ) { dp [ i ] [ j ] [ k ] = Number . MAX_VALUE ; } } } } function mincost ( idx , src , dest , costs ) { if ( idx > N ) return 0 ; if ( dp [ idx ] [ src ] [ dest ] != Number . MAX_VALUE ) return dp [ idx ] [ src ] [ dest ] ; let rem = 6 - ( src + dest ) ; let ans = Number . MAX_VALUE ; let case1 = costs [ src - 1 ] [ dest - 1 ] + mincost ( idx + 1 , src , rem , costs ) + mincost ( idx + 1 , rem , dest , costs ) ; let case2 = costs [ src - 1 ] [ rem - 1 ] + mincost ( idx + 1 , src , dest , costs ) + mincost ( idx + 1 , dest , src , costs ) + costs [ rem - 1 ] [ dest - 1 ] + mincost ( idx + 1 , src , dest , costs ) ; ans = Math . min ( case1 , case2 ) ; dp [ idx ] [ src ] [ dest ] = ans ; return ans ; } let costs = [ [ 0 , 1 , 2 ] , [ 2 , 0 , 1 ] , [ 3 , 2 , 0 ] ] ; initialize ( ) ; document . write ( mincost ( 1 , 1 , 3 , costs ) ) ;
Minimum time required to rot all oranges | Dynamic Programming | Javascript implementation of the approach ; DP table to memoize the values ; Visited array to keep track of visited nodes in order to avoid infinite loops ; Function to return the minimum of four numbers ; Function to return the minimum distance to any rotten orange from [ i , j ] ; If i , j lie outside the array ; If 0 then it can 't lead to  any path so return INT_MAX ; If 2 then we have reached our rotten oranges so return from here ; If this node is already visited then return to avoid infinite loops ; Mark the current node as visited ; Check in all four possible directions ; Take the minimum of all ; If result already exists in the table check if min_value is less than existing value ; Function to return the minimum time required to rot all the oranges ; Calculate the minimum distances to any rotten orange from all the fresh oranges ; Pick the maximum distance of fresh orange to some rotten orange ; If all oranges can be rotten ; Driver Code
let C = 5 ; let R = 3 ; let INT_MAX = 10000000 ; let table = new Array ( R ) ; let visited = new Array ( R ) ; for ( let i = 0 ; i < R ; i ++ ) { table [ i ] = new Array ( C ) ; visited [ i ] = new Array ( C ) ; } function min ( p , q , r , s ) { let temp1 = p < q ? p : q ; let temp2 = r < s ? r : s ; if ( temp1 < temp2 ) return temp1 ; return temp2 ; } function Distance ( arr , i , j ) { if ( i >= R j >= C i < 0 j < 0 ) return INT_MAX ; else if ( arr [ i ] [ j ] == 0 ) { table [ i ] [ j ] = INT_MAX ; return INT_MAX ; } else if ( arr [ i ] [ j ] == 2 ) { table [ i ] [ j ] = 0 ; return 0 ; } else if ( visited [ i ] [ j ] == 1 ) { return INT_MAX ; } else { visited [ i ] [ j ] = 1 ; let temp1 = Distance ( arr , i + 1 , j ) ; let temp2 = Distance ( arr , i - 1 , j ) ; let temp3 = Distance ( arr , i , j + 1 ) ; let temp4 = Distance ( arr , i , j - 1 ) ; let min_value = 1 + min ( temp1 , temp2 , temp3 , temp4 ) ; if ( table [ i ] [ j ] > 0 && table [ i ] [ j ] < INT_MAX ) { if ( min_value < table [ i ] [ j ] ) table [ i ] [ j ] = min_value ; } else table [ i ] [ j ] = min_value ; visited [ i ] [ j ] = 0 ; } return table [ i ] [ j ] ; } function minTime ( arr ) { let max = 0 ; for ( let i = 0 ; i < R ; i ++ ) { for ( let j = 0 ; j < C ; j ++ ) { if ( arr [ i ] [ j ] == 1 ) Distance ( arr , i , j ) ; } } for ( let i = 0 ; i < R ; i ++ ) { for ( let j = 0 ; j < C ; j ++ ) { if ( arr [ i ] [ j ] == 1 && table [ i ] [ j ] > max ) max = table [ i ] [ j ] ; } } if ( max < INT_MAX ) return max ; return - 1 ; } let arr = [ [ 2 , 1 , 0 , 2 , 1 ] , [ 0 , 0 , 1 , 2 , 1 ] , [ 1 , 0 , 0 , 2 , 1 ] ] document . write ( minTime ( arr ) ) ;
Maximum sum of non | 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 ; Variable to store prefix sum for sub - array { i , j } ; Required recurrence relation ; Returning the value ; Input array
let maxLen = 10 ; let dp = new Array ( maxLen ) ; let visit = new Array ( maxLen ) ; function maxSum ( arr , i , n , k ) { if ( i >= n ) return 0 ; if ( visit [ i ] ) return dp [ i ] ; visit [ i ] = true ; let tot = 0 ; dp [ i ] = maxSum ( arr , i + 1 , n , k ) ; for ( let j = i ; j < ( i + k ) && ( j < n ) ; j ++ ) { tot += arr [ j ] ; dp [ i ] = Math . max ( dp [ i ] , tot + maxSum ( arr , j + 2 , n , k ) ) ; } return dp [ i ] ; } let arr = [ - 1 , 2 , - 3 , 4 , 5 ] ; let k = 2 ; let n = arr . length ; document . write ( maxSum ( arr , 0 , n , k ) ) ;
Maximum subset sum such that no two elements in set have same digit in them | Javascript implementation of above approach ; Function to create mask for every number ; Recursion for Filling DP array ; Base Condition ; Recurrence Relation ; Function to find Maximum Subset Sum ; Initialize DP array ; Iterate over all possible masks of 10 bit number ; Driver Code
let dp = new Array ( 1024 ) ; dp . fill ( - 1 ) ; function get_binary ( u ) { let ans = 0 ; while ( u > 0 ) { let rem = u % 10 ; ans |= ( 1 << rem ) ; u = parseInt ( u / 10 , 10 ) ; } return ans ; } function recur ( u , array , n ) { if ( u == 0 ) return 0 ; if ( dp [ u ] != - 1 ) return dp [ u ] ; for ( let i = 0 ; i < n ; i ++ ) { let mask = get_binary ( array [ i ] ) ; if ( ( mask u ) == u ) { dp [ u ] = Math . max ( Math . max ( 0 , dp [ u ^ mask ] ) + array [ i ] , dp [ u ] ) ; } } return dp [ u ] ; } function solve ( array , n ) { for ( let i = 0 ; i < ( 1 << 10 ) ; i ++ ) { dp [ i ] = - 1 ; } let ans = 0 ; for ( let i = 0 ; i < ( 1 << 10 ) ; i ++ ) { ans = Math . max ( ans , recur ( i , array , n ) ) ; } return ans ; } let array = [ 22 , 132 , 4 , 45 , 12 , 223 ] ; let n = array . length ; document . write ( solve ( array , n ) ) ;
Minimize the sum after choosing elements from the given three arrays | Javascript implementation of the above approach ; Function to return the minimized sum ; If all the indices have been used ; If this value is pre - calculated then return its value from dp array instead of re - computing it ; If A [ i - 1 ] was chosen previously then only B [ i ] or C [ i ] can chosen now choose the one which leads to the minimum sum ; If B [ i - 1 ] was chosen previously then only A [ i ] or C [ i ] can chosen now choose the one which leads to the minimum sum ; If C [ i - 1 ] was chosen previously then only A [ i ] or B [ i ] can chosen now choose the one which leads to the minimum sum ; Driver code ; Initialize the dp [ ] [ ] array ; min ( start with A [ 0 ] , start with B [ 0 ] , start with C [ 0 ] )
let SIZE = 3 ; let N = 3 ; function minSum ( A , B , C , i , n , curr , dp ) { if ( n <= 0 ) return 0 ; if ( dp [ n ] [ curr ] != - 1 ) return dp [ n ] [ curr ] ; if ( curr == 0 ) { return dp [ n ] [ curr ] = Math . min ( B [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 1 , dp ) , C [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 2 , dp ) ) ; } if ( curr == 1 ) return dp [ n ] [ curr ] = Math . min ( A [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 0 , dp ) , C [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 2 , dp ) ) ; return dp [ n ] [ curr ] = Math . min ( A [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 0 , dp ) , B [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 1 , dp ) ) ; } let A = [ 1 , 50 , 1 ] ; let B = [ 50 , 50 , 50 ] ; let C = [ 50 , 50 , 50 ] ; let dp = new Array ( SIZE ) ; for ( let i = 0 ; i < SIZE ; i ++ ) { dp [ i ] = new Array ( N ) ; for ( let j = 0 ; j < N ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } document . write ( Math . min ( A [ 0 ] + minSum ( A , B , C , 1 , SIZE - 1 , 0 , dp ) , Math . min ( B [ 0 ] + minSum ( A , B , C , 1 , SIZE - 1 , 1 , dp ) , C [ 0 ] + minSum ( A , B , C , 1 , SIZE - 1 , 2 , dp ) ) ) ) ;
Maximise matrix sum by following the given Path | Javascript implementation of the approach ; To store the states of the DP ; Function to return the maximum of the three integers ; Function to return the maximum score ; Base cases ; If the state has already been solved then return it ; Marking the state as solved ; Growing phase ; } Shrinking phase ; Returning the solved state ; Driver code
let n = 3 ; let dp = new Array ( n ) ; let v = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { dp [ i ] = new Array ( n ) ; v [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] = new Array ( 2 ) ; v [ i ] [ j ] = new Array ( 2 ) ; for ( let k = 0 ; k < 2 ; k ++ ) { dp [ i ] [ j ] [ k ] = 0 ; v [ i ] [ j ] [ k ] = 0 ; } } } function max ( a , b , c ) { let m = a ; if ( m < b ) { m = b ; } if ( m < c ) { m = c ; } return m ; } function maxScore ( arr , i , j , s ) { if ( i > n - 1 i < 0 j > n - 1 ) { return 0 ; } if ( i == 0 && j == n - 1 ) { return arr [ i ] [ j ] ; } if ( v [ i ] [ j ] [ s ] ) { return dp [ i ] [ j ] [ s ] ; } v [ i ] [ j ] [ s ] = true ; if ( s != 1 ) { dp [ i ] [ j ] [ s ] = arr [ i ] [ j ] + Math . max ( maxScore ( arr , i + 1 , j , s ) , Math . max ( maxScore ( arr , i , j + 1 , s ) , maxScore ( arr , i - 1 , j , ( s == 1 ) ? 0 : 1 ) ) ) ; else { dp [ i ] [ j ] [ s ] = arr [ i ] [ j ] + Math . max ( maxScore ( arr , i - 1 , j , s ) , maxScore ( arr , i , j + 1 , s ) ) ; } return dp [ i ] [ j ] [ s ] ; } let arr = [ [ 1 , 1 , 1 ] , [ 1 , 5 , 1 ] , [ 1 , 1 , 1 ] ] ; document . write ( maxScore ( arr , 0 , 0 , 0 ) ) ;
Find maximum topics to prepare in order to pass the exam | Function to return the maximum marks by considering topics which can be completed in the given time duration ; If we are given 0 time then nothing can be done So all values are 0 ; If we are given 0 topics then the time required will be 0 for sure ; Calculating the maximum marks that can be achieved under the given time constraints ; If time taken to read that topic is more than the time left now at position j then do no read that topic ; Two cases arise : 1 ) Considering current topic 2 ) Ignoring current topic We are finding maximum of ( current topic weightage + topics which can be done in leftover time - current topic time ) and ignoring current topic weightage sum ; Moving upwards in table from bottom right to calculate the total time taken to read the topics which can be done in given time and have highest weightage sum ; It means we have not considered reading this topic for max weightage sum ; Adding the topic time ; Evaluating the left over time after considering this current topic ; One topic completed ; It contains the maximum weightage sum formed by considering the topics ; Condition when exam cannot be passed ; Return the marks that can be obtained after passing the exam ; Number of topics , hours left and the passing marks ; n + 1 is taken for simplicity in loops Array will be indexed starting from 1
function MaximumMarks ( marksarr , timearr , h , n , p ) { let no_of_topics = n + 1 ; let total_time = h + 1 ; let T = new Array ( no_of_topics ) ; for ( let i = 0 ; i < no_of_topics ; i ++ ) { T [ i ] = new Array ( total_time ) ; for ( let j = 0 ; j < total_time ; j ++ ) { T [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < no_of_topics ; i ++ ) { T [ i ] [ 0 ] = 0 ; } for ( let j = 0 ; j < total_time ; j ++ ) { T [ 0 ] [ j ] = 0 ; } for ( let i = 1 ; i < no_of_topics ; i ++ ) { for ( let j = 1 ; j < total_time ; j ++ ) { if ( j < timearr [ i ] ) { T [ i ] [ j ] = T [ i - 1 ] [ j ] ; } else { T [ i ] [ j ] = Math . max ( marksarr [ i ] + T [ i - 1 ] [ j - timearr [ i ] ] , T [ i - 1 ] [ j ] ) ; } } } let i = no_of_topics - 1 , j = total_time - 1 ; let sum = 0 ; while ( i > 0 && j > 0 ) { if ( T [ i ] [ j ] == T [ i - 1 ] [ j ] ) { i -- ; } else { sum += timearr [ i ] ; j -= timearr [ i ] ; i -- ; } } let marks = T [ no_of_topics - 1 ] [ total_time - 1 ] ; if ( marks < p ) return - 1 ; return sum ; } let n = 4 , h = 10 , p = 10 ; let marksarr = [ 0 , 6 , 4 , 2 , 8 ] ; let timearr = [ 0 , 4 , 6 , 2 , 7 ] ; document . write ( MaximumMarks ( marksarr , timearr , h , n , p ) ) ;
Minimize the number of steps required to reach the end of the array | Javascript implementation of the above approach ; variable to store states of dp ; variable to check if a given state has been solved ; Function to find the minimum number of steps required to reach the end of the array ; base case ; to check if a state has been solved ; required recurrence relation ; returning the value ; Driver code
let maxLen = 10 ; let maskLen = 130 ; let dp = new Array ( maxLen ) ; for ( let i = 0 ; i < maxLen ; i ++ ) { dp [ i ] = new Array ( maskLen ) ; } let v = new Array ( maxLen ) ; for ( let i = 0 ; i < maxLen ; i ++ ) { v [ i ] = new Array ( maskLen ) ; } function minSteps ( arr , i , mask , n ) { if ( i == n - 1 ) { return 0 ; } if ( i > n - 1 i < 0 ) { return 9999999 ; } if ( ( mask >> i ) % 2 == 1 ) { return 9999999 ; } if ( v [ i ] [ mask ] ) { return dp [ i ] [ mask ] ; } v [ i ] [ mask ] = true ; dp [ i ] [ mask ] = 1 + Math . min ( minSteps ( arr , i - arr [ i ] , ( mask | ( 1 << i ) ) , n ) , minSteps ( arr , i + arr [ i ] , ( mask | ( 1 << i ) ) , n ) ) ; return dp [ i ] [ mask ] ; } let arr = [ 1 , 2 , 2 , 2 , 1 , 1 ] ; let n = arr . length ; let ans = minSteps ( arr , 0 , 0 , n ) ; if ( ans >= 9999999 ) { document . write ( - 1 ) ; } else { document . write ( ans ) ; }
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 ; Driver code
function oSRec ( arr , i , j , sum ) { if ( j == i + 1 ) return Math . max ( arr [ i ] , arr [ j ] ) ; return Math . max ( ( sum - oSRec ( arr , i + 1 , j , sum - arr [ i ] ) ) , ( sum - oSRec ( arr , i , j - 1 , sum - arr [ j ] ) ) ) ; } function optimalStrategyOfGame ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } 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 ) + " " ) ;
Minimum number of sub | Function that returns true if n is a power of 5 ; Function to return the decimal value of binary equivalent ; Function to return the minimum cuts required ; Allocating memory for dp [ ] array ; From length 1 to n ; If previous character is '0' then ignore to avoid number with leading 0 s . ; Ignore s [ j ] = '0' starting numbers ; Number formed from s [ j ... . i ] ; Check for power of 5 ; Assigning min value to get min cut possible ; ( n + 1 ) to check if all the strings are traversed and no divisible by 5 is obtained like 000000 ; Driver code
function ispower ( n ) { if ( n < 125 ) return ( n == 1 n == 5 n == 25 ) ; if ( n % 125 != 0 ) return false ; else return ispower ( parseInt ( n / 125 ) ) ; } function number ( s , i , j ) { var ans = 0 ; for ( var x = i ; x < j ; x ++ ) { ans = ans * 2 + ( s [ x ] - ' ' ) ; } return ans ; } function minCuts ( s , n ) { var dp = Array ( n + 1 ) . fill ( n + 1 ) ; dp [ 0 ] = 0 ; for ( var i = 1 ; i <= n ; i ++ ) { if ( s [ i - 1 ] == ' ' ) continue ; for ( var j = 0 ; j < i ; j ++ ) { if ( s [ j ] == ' ' ) continue ; var num = number ( s , j , i ) ; if ( ! ispower ( num ) ) continue ; dp [ i ] = Math . min ( dp [ i ] , dp [ j ] + 1 ) ; } } return ( ( dp [ n ] < n + 1 ) ? dp [ n ] : - 1 ) ; } var s = " " ; var n = s . length ; document . write ( minCuts ( s , n ) ) ;