codeparrot/xlcost-text-to-code huggingface dataset:
Find minimum number of steps to reach the end of String | Function to return the minimum number of steps to reach the end ; If the end can 't be reached ; Already at the end ; If the length is 2 or 3 then the end can be reached in a single step ; For the other cases , solve the problem using dynamic programming ; It requires no move from the end to reach the end ; From the 2 nd last and the 3 rd last index , only a single move is required ; Update the answer for every index ; If the current index is not reachable ; To store the minimum steps required from the current index ; If it is a valid move then update the minimum steps required ; Update the minimum steps required starting from the current index ; Cannot reach the end starting from str [ 0 ] ; Return the minimum steps required ; Driver code
function minSteps ( str , n , k ) { if ( str [ n - 1 ] == ' ' ) return - 1 ; if ( n == 1 ) return 0 ; if ( n < 4 ) return 1 ; var dp = Array ( n ) ; dp [ n - 1 ] = 0 ; dp [ n - 2 ] = 1 ; dp [ n - 3 ] = 1 ; for ( var i = n - 4 ; i >= 0 ; i -- ) { if ( str [ i ] == ' ' ) continue ; var steps = 1000000000 ; if ( i + k < n && str [ i + k ] == ' ' ) steps = Math . min ( steps , dp [ i + k ] ) ; if ( str [ i + 1 ] == ' ' ) steps = Math . min ( steps , dp [ i + 1 ] ) ; if ( str [ i + 2 ] == ' ' ) steps = Math . min ( steps , dp [ i + 2 ] ) ; dp [ i ] = ( steps == 1000000000 ) ? steps : 1 + steps ; } if ( dp [ 0 ] == 1000000000 ) return - 1 ; return dp [ 0 ] ; } var str = " " ; var n = str . length ; var k = 5 ; document . write ( minSteps ( str , n , k ) ) ;
Length of longest Palindromic Subsequence of even length with no two adjacent characters same | To store the values of subproblems ; Function to find the Longest Palindromic subsequence of even length with no two adjacent characters same ; Base cases If the string length is 1 return 0 ; If the string length is 2 ; Check if the characters match ; If the value with given parameters is previously calculated ; If the first and last character of the string matches with the given character ; Remove the first and last character and call the function for all characters ; If it does not match ; Then find the first and last index of given character in the given string ; Take the substring from i to j and call the function with substring and the given character ; Store the answer for future use ; Driver code ; Check for all starting characters
var dp = new Map ( ) ; function solve ( s , c ) { if ( s . length == 1 ) return 0 ; if ( s . length == 2 ) { if ( s [ 0 ] == s [ 1 ] && s [ 0 ] == c ) return 1 ; else return 0 ; } if ( dp . has ( s + " " + c ) ) return dp . get ( s + " " + c ) ; var ans = 0 ; if ( s [ 0 ] == s [ s . length - 1 ] && s [ 0 ] == c ) { for ( var c1 = ' ' . charCodeAt ( 0 ) ; c1 <= ' ' . charCodeAt ( 0 ) ; c1 ++ ) if ( String . fromCharCode ( c1 ) != c ) ans = Math . max ( ans , 1 + solve ( s . substring ( 1 , s . length - 1 ) , String . fromCharCode ( c1 ) ) ) ; } else { for ( var i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == c ) { for ( var j = s . length - 1 ; j > i ; j -- ) if ( s [ j ] == c ) { if ( j == i ) break ; ans = solve ( s . substring ( i , j + 1 ) , c ) ; break ; } break ; } } } dp . set ( s + " " + c , ans ) ; return ans ; } var s = " " ; var ma = 0 ; for ( var c1 = ' ' . charCodeAt ( 0 ) ; c1 <= ' ' . charCodeAt ( 0 ) ; c1 ++ ) ma = Math . max ( ma , solve ( s , String . fromCharCode ( c1 ) ) * 2 ) ; document . write ( ma ) ;
Eggs dropping puzzle | Set 2 | Function to return the minimum number of trials needed in the worst case with n eggs and k floors ; Fill all the entries in table using optimal substructure property ; Return the minimum number of moves ; Driver code
function eggDrop ( n , k ) { let dp = new Array ( ) ; for ( let i = 0 ; i < k + 1 ; i ++ ) { dp . push ( new Array ( n + 1 ) . fill ( 0 ) ) } let x = 0 ; while ( dp [ x ] [ n ] < k ) { x ++ ; for ( let i = 1 ; i <= n ; i ++ ) dp [ x ] [ i ] = dp [ x - 1 ] [ i - 1 ] + dp [ x - 1 ] [ i ] + 1 ; } return x ; } let n = 2 , k = 36 ; document . write ( eggDrop ( n , k ) ) ;
Maximum length of Strictly Increasing Sub | Function to return the maximum length of strictly increasing subarray after removing atmost one element ; Create two arrays pre and pos ; Find out the contribution of the current element in array [ 0 , i ] and update pre [ i ] ; Find out the contribution of the current element in array [ N - 1 , i ] and update pos [ i ] ; Calculate the maximum length of the stricly increasing subarray without removing any element ; Calculate the maximum length of the strictly increasing subarray after removing the current element ; Driver code
function maxIncSubarr ( a , n ) { let pre = new Array ( n ) ; let pos = new Array ( n ) ; pre . fill ( 0 ) ; pos . fill ( 0 ) ; pre [ 0 ] = 1 ; pos [ n - 1 ] = 1 ; let l = 0 ; for ( let i = 1 ; i < n ; i ++ ) { if ( a [ i ] > a [ i - 1 ] ) pre [ i ] = pre [ i - 1 ] + 1 ; else pre [ i ] = 1 ; } l = 1 ; for ( let i = n - 2 ; i >= 0 ; i -- ) { if ( a [ i ] < a [ i + 1 ] ) pos [ i ] = pos [ i + 1 ] + 1 ; else pos [ i ] = 1 ; } let ans = 0 ; l = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( a [ i ] > a [ i - 1 ] ) l ++ ; else l = 1 ; ans = Math . max ( ans , l ) ; } for ( let i = 1 ; i <= n - 2 ; i ++ ) { if ( a [ i - 1 ] < a [ i + 1 ] ) ans = Math . max ( pre [ i - 1 ] + pos [ i + 1 ] , ans ) ; } return ans ; } let arr = [ 1 , 2 ] ; let n = arr . length ; document . write ( maxIncSubarr ( arr , n ) ) ;
Number of square matrices with all 1 s | Javascript program to return the number of square submatrices with all 1 s ; Function to return the number of square submatrices with all 1 s ; Initialize count variable ; If a [ i ] [ j ] is equal to 0 ; Calculate number of square submatrices ending at ( i , j ) ; Calculate the sum of the array ; Driver code
var n = 3 var m = 3 function countSquareMatrices ( a , N , M ) { var count = 0 ; for ( var i = 1 ; i < N ; i ++ ) { for ( var j = 1 ; j < M ; j ++ ) { if ( a [ i ] [ j ] == 0 ) continue ; a [ i ] [ j ] = Math . min ( Math . min ( a [ i - 1 ] [ j ] , a [ i ] [ j - 1 ] ) , a [ i - 1 ] [ j - 1 ] ) + 1 ; } } for ( var i = 0 ; i < N ; i ++ ) for ( var j = 0 ; j < M ; j ++ ) count += a [ i ] [ j ] ; return count ; } var arr = [ [ 1 , 0 , 1 ] , [ 1 , 1 , 0 ] , [ 1 , 1 , 0 ] ] ; document . write ( countSquareMatrices ( arr , n , m ) ) ;
Count number of increasing sub | Javascript implementation of the approach ; Segment tree array ; Function for point update inVal segment tree ; Base case ; If l == r == up ; Mid element ; Updating the segment tree ; Function for the range sum - query ; Base case ; Mid element ; Calling for the left and the right subtree ; Function to return the count ; Copying array arr to sort it ; Sorting array brr ; Map to store the rank of each element ; dp array ; To store the final answer ; Updating the dp array ; Rank of the element ; Solving the dp - states using segment tree ; Updating the final answer ; Updating the segment tree ; Returning the final answer ; Driver code
var N = 10000 ; var seg = Array ( 3 * N ) . fill ( 0 ) ; function update ( inVal , l , r , up_in , val ) { if ( r < up_in l > up_in ) return seg [ inVal ] ; if ( l == up_in && r == up_in ) return seg [ inVal ] = val ; var m = parseInt ( ( l + r ) / 2 ) ; seg [ inVal ] = update ( 2 * inVal + 1 , l , m , up_in , val ) + update ( 2 * inVal + 2 , m + 1 , r , up_in , val ) ; return seg [ inVal ] ; } function query ( inVal , l , r , l1 , r1 ) { if ( l > r ) return 0 ; if ( r < l1 l > r1 ) return 0 ; if ( l1 <= l && r <= r1 ) return seg [ inVal ] ; var m = parseInt ( ( l + r ) / 2 ) ; return query ( 2 * inVal + 1 , l , m , l1 , r1 ) + query ( 2 * inVal + 2 , m + 1 , r , l1 , r1 ) ; } function findCnt ( arr , n ) { var brr = Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; brr . sort ( ( a , b ) => a - b ) ; var r = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) r [ brr [ i ] ] = i + 1 ; var dp = Array ( n ) . fill ( 0 ) ; var ans = 0 ; for ( var i = n - 1 ; i >= 0 ; i -- ) { var rank = r [ arr [ i ] ] ; dp [ i ] = 1 + query ( 0 , 0 , n - 1 , rank , n - 1 ) ; ans += dp [ i ] ; update ( 0 , 0 , n - 1 , rank - 1 , dp [ i ] + query ( 0 , 0 , n - 1 , rank - 1 , rank - 1 ) ) ; } return ans ; } var arr = [ 1 , 2 , 10 , 9 ] ; var n = arr . length ; document . write ( findCnt ( arr , n ) ) ;
Size of the largest divisible subset in an Array | Javascript implementation of the above approach ; Function to find the size of the largest divisible subarray ; Mark all elements of the array ; Traverse reverse ; For all multiples of i ; Return the required answer ; Driver code ; Function call
let N = 1000005 function maximum_set ( a , n ) { let dp = new Array ( N ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) dp [ a [ i ] ] = 1 ; let ans = 1 ; for ( let i = N - 1 ; i >= 1 ; i -- ) { if ( dp [ i ] != 0 ) { for ( let j = 2 * i ; j < N ; j += i ) { dp [ i ] = Math . max ( dp [ i ] , 1 + dp [ j ] ) ; ans = Math . max ( ans , dp [ i ] ) ; } } } return ans ; } let arr = [ 3 , 4 , 6 , 8 , 10 , 18 , 21 , 24 ] ; let n = arr . length ; document . write ( maximum_set ( arr , n ) ) ;
Longest sub | Javascript implementation of the approach ; Function to return the length of the largest sub - string divisible by 3 ; Base - case ; If the state has been solved before then return its value ; Marking the state as solved ; Recurrence relation ; Driver code
var N = 100 var dp = Array . from ( Array ( N ) , ( ) => Array ( 3 ) ) ; var v = Array . from ( Array ( N ) , ( ) => Array ( 3 ) ) ; function findLargestString ( s , i , r ) { if ( i == s . length ) { if ( r == 0 ) return 0 ; else return - 1000000000 ; } if ( v [ i ] [ r ] ) return dp [ i ] [ r ] ; v [ i ] [ r ] = 1 ; dp [ i ] [ r ] = Math . max ( 1 + findLargestString ( s , i + 1 , ( r * 2 + ( s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) % 3 ) , findLargestString ( s , i + 1 , r ) ) ; return dp [ i ] [ r ] ; } var s = " " ; document . write ( findLargestString ( s , 0 , 0 ) ) ;
Number of sub | Javascript implementation of th approach ; Function to return the number of sub - sequences divisible by 3 ; Base - cases ; If the state has been solved before then return its value ; Marking the state as solved ; Recurrence relation ; Driver code
var N = 100 var dp = Array . from ( Array ( N ) , ( ) => Array ( 3 ) ) ; var v = Array . from ( Array ( N ) , ( ) => Array ( 3 ) ) ; function findCnt ( s , i , r ) { if ( i == s . length ) { if ( r == 0 ) return 1 ; else return 0 ; } if ( v [ i ] [ r ] ) return dp [ i ] [ r ] ; v [ i ] [ r ] = 1 ; dp [ i ] [ r ] = findCnt ( s , i + 1 , ( r * 2 + ( s [ i ] - ' ' ) ) % 3 ) + findCnt ( s , i + 1 , r ) ; return dp [ i ] [ r ] ; } var s = " " ; document . write ( ( findCnt ( s , 0 , 0 ) - 1 ) ) ;
Remove minimum elements from ends of array so that sum decreases by at least K | O ( N ) | Function to return the count of minimum elements to be removed from the ends of the array such that the sum of the array decrease by at least K ; To store the final answer ; Maximum possible sum required ; Left point ; Right pointer ; Total current sum ; Two pointer loop ; If the sum fits ; Update the answer ; Update the total sum ; Increment the left pointer ; Driver code
function minCount ( arr , n , k ) { var ans = 0 ; var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; sum -= k ; var l = 0 ; var r = 0 ; var tot = 0 ; while ( l < n ) { if ( tot <= sum ) { ans = Math . max ( ans , r - l ) ; if ( r == n ) break ; tot += arr [ r ++ ] ; } else { tot -= arr [ l ++ ] ; } } return ( n - ans ) ; } var arr = [ 1 , 11 , 5 , 5 ] ; var n = arr . length ; var k = 11 ; document . write ( minCount ( arr , n , k ) ) ;
Minimum cost to merge all elements of List | Javascript implementation of the approach ; To store the states of DP ; Function to return the minimum merge cost ; Base case ; If the state has been solved before ; Marking the state as solved ; Reference to dp [ i ] [ j ] ; To store the sum of all the elements in the subarray arr [ i ... j ] ; Loop to iterate the recurrence ; Returning the solved value ; Driver code
var N = 401 var dp = Array . from ( Array ( N ) , ( ) => Array ( N ) ) ; var v = Array . from ( Array ( N ) , ( ) => Array ( N ) ) ; function minMergeCost ( i , j , arr ) { if ( i == j ) return 0 ; if ( v [ i ] [ j ] ) return dp [ i ] [ j ] ; v [ i ] [ j ] = 1 ; var x = dp [ i ] [ j ] ; x = 1000000000 ; var tot = 0 ; for ( var k = i ; k <= j ; k ++ ) tot += arr [ k ] ; for ( var k = i + 1 ; k <= j ; k ++ ) { x = Math . min ( x , tot + minMergeCost ( i , k - 1 , arr ) + minMergeCost ( k , j , arr ) ) ; } return x ; } var arr = [ 1 , 3 , 7 ] ; var n = arr . length ; document . write ( minMergeCost ( 0 , n - 1 , arr ) ) ;
Expected number of moves to reach the end of a board | Dynamic programming | Javascript implementation of the approach ; To store the states of dp ; To determine whether a state has been solved before ; Function to return the count ; Base cases ; If a state has been solved before it won 't be evaluated again ; Recurrence relation ; Driver code
var maxSize = 50 ; var dp = Array ( maxSize ) ; var v = Array ( maxSize ) ; function expectedSteps ( x ) { if ( x == 0 ) return 0 ; if ( x <= 5 ) return 6 ; if ( v [ x ] ) return dp [ x ] ; v [ x ] = 1 ; dp [ x ] = 1 + ( expectedSteps ( x - 1 ) + expectedSteps ( x - 2 ) + expectedSteps ( x - 3 ) + expectedSteps ( x - 4 ) + expectedSteps ( x - 5 ) + expectedSteps ( x - 6 ) ) / 6 ; return dp [ x ] ; } var n = 10 ; document . write ( expectedSteps ( n - 1 ) . toFixed ( 5 ) ) ;
Number of subsequences in a given binary string divisible by 2 | Function to return the count of the required subsequences ; To store the final answer ; Multiplier ; Loop to find the answer ; Condition to update the answer ; updating multiplier ; Driver code
function countSubSeq ( str , len ) { var ans = 0 ; var mul = 1 ; for ( var i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' ' ) ans += mul ; mul *= 2 ; } return ans ; } var str = " " ; var len = str . length ; document . write ( countSubSeq ( str , len ) ) ;
Knapsack with large Weights | Javascript implementation of the approach ; To store the states of DP ; Function to solve the recurrence relation ; Base cases ; Marking state as solved ; Recurrence relation ; Function to return the maximum weight ; Iterating through all possible values to find the the largest value that can be represented by the given weights ; Driver code
var V_SUM_MAX = 1000 var N_MAX = 100 var W_MAX = 10000000 var dp = Array . from ( Array ( V_SUM_MAX + 1 ) , ( ) => Array ( N_MAX ) ) ; var v = Array . from ( Array ( V_SUM_MAX + 1 ) , ( ) => Array ( N_MAX ) ) ; function solveDp ( r , i , w , val , n ) { if ( r <= 0 ) return 0 ; if ( i == n ) return W_MAX ; if ( v [ r ] [ i ] ) return dp [ r ] [ i ] ; v [ r ] [ i ] = 1 ; dp [ r ] [ i ] = Math . min ( solveDp ( r , i + 1 , w , val , n ) , w [ i ] + solveDp ( r - val [ i ] , i + 1 , w , val , n ) ) ; return dp [ r ] [ i ] ; } function maxWeight ( w , val , n , c ) { for ( var i = V_SUM_MAX ; i >= 0 ; i -- ) { if ( solveDp ( i , 0 , w , val , n ) <= c ) { return i ; } } return 0 ; } var w = [ 3 , 4 , 5 ] ; var val = [ 30 , 50 , 60 ] ; var n = w . length ; var C = 8 ; document . write ( maxWeight ( w , val , n , C ) ) ;
Sum of lengths of all paths possible in a given tree | Javascript implementation of the approach ; Number of vertices ; Adjacency list representation of the tree ; Array that stores the subtree size ; Array to mark all the vertices which are visited ; Utility function to create an edge between two vertices ; Add a to b 's list ; Add b to a 's list ; Function to calculate the subtree size ; Mark visited ; For every adjacent node ; If not already visited ; Recursive call for the child ; Function to calculate the contribution of each edge ; Mark current node as visited ; For every adjacent node ; If it is not already visisted ; Function to return the required sum ; First pass of the dfs ; Second pass ; Driver code
let sz = 100005 ; let n ; let tree = new Array ( sz ) ; let subtree_size = new Array ( sz ) ; let vis = new Array ( sz ) ; function AddEdge ( a , b ) { tree [ a ] . push ( b ) ; tree [ b ] . push ( a ) ; } function dfs ( node ) { vis [ node ] = 1 ; subtree_size [ node ] = 1 ; for ( let child = 0 ; child < tree [ node ] . length ; child ++ ) { if ( vis [ tree [ node ] [ child ] ] == 0 ) { subtree_size [ node ] += dfs ( tree [ node ] [ child ] ) ; } } return subtree_size [ node ] ; } function contribution ( node , ans ) { vis [ node ] = 1 ; for ( let child = 0 ; child < tree [ node ] . length ; child ++ ) { if ( vis [ tree [ node ] [ child ] ] == 0 ) { ans += ( subtree_size [ tree [ node ] [ child ] ] * ( n - subtree_size [ tree [ node ] [ child ] ] ) ) ; ans = contribution ( tree [ node ] [ child ] , ans ) ; } } return ans ; } function getSum ( ) { for ( let i = 0 ; i < vis . length ; i ++ ) { vis [ i ] = 0 ; } dfs ( 0 ) ; let ans = 0 ; for ( let i = 0 ; i < vis . length ; i ++ ) { vis [ i ] = 0 ; } ans = contribution ( 0 , ans ) ; return ans ; } n = 5 ; for ( let i = 0 ; i < sz ; i ++ ) { tree [ i ] = [ ] ; } AddEdge ( 0 , 1 ) ; AddEdge ( 0 , 2 ) ; AddEdge ( 1 , 3 ) ; AddEdge ( 1 , 4 ) ; document . write ( getSum ( ) ) ;
Number of cells in a matrix that satisfy the given condition | Javascript implementation of the approach ; Function to return the number of cells in which mirror can be placed ; Update the row array where row [ i ] [ j ] will store whether the current row i contains all 1 s in the columns starting from j ; Update the column array where col [ i ] [ j ] will store whether the current column j contains all 1 s in the rows starting from i ; To store the required result ; For every cell except the last row and the last column ; If the current cell is not blocked and the light can travel from the next row and the next column then the current cell is valid ; For the last column ; For the last row , note that the last column is not taken into consideration as the bottom right element has already been considered in the last column previously ; Driver code
let N = 3 ; function numberOfCells ( mat ) { let row = new Array ( N ) ; let col = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { row [ i ] = new Array ( N ) ; col [ i ] = new Array ( N ) ; for ( let j = 0 ; j < N ; j ++ ) { row [ i ] [ j ] = 0 ; col [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = N - 1 ; j >= 0 ; j -- ) { if ( mat [ i ] [ j ] == 1 ) { row [ i ] [ j ] = ( j + 1 < N ) ? row [ i ] [ j + 1 ] : true ; } else { row [ i ] [ j ] = false ; } } } for ( let j = 0 ; j < N ; j ++ ) { for ( let i = N - 1 ; i >= 0 ; i -- ) { if ( mat [ i ] [ j ] == 1 ) { col [ i ] [ j ] = ( i + 1 < N ) ? col [ i + 1 ] [ j ] : true ; } else { col [ i ] [ j ] = false ; } } } let cnt = 0 ; for ( let i = 0 ; i < N - 1 ; i ++ ) { for ( let j = 0 ; j < N - 1 ; j ++ ) { if ( row [ i ] [ j ] && col [ i ] [ j ] ) { cnt ++ ; } } } for ( let i = 0 ; i < N ; i ++ ) { if ( col [ i ] [ N - 1 ] ) cnt ++ ; } for ( let j = 0 ; j < N - 1 ; j ++ ) { if ( row [ N - 1 ] [ j ] ) cnt ++ ; } return cnt ; } let mat = [ [ 0 , 1 , 1 ] , [ 0 , 1 , 1 ] , [ 0 , 1 , 1 ] ] ; document . write ( numberOfCells ( mat ) ) ;
Maximum sum subarray after altering the array | Function to return the maximum subarray sum ; Function to reverse the subarray arr [ 0. . . i ] ; Function to return the maximum subarray sum after performing the given operation at most once ; To store the result ; When no operation is performed ; Find the maximum subarray sum after reversing the subarray arr [ 0. . . i ] for all possible values of i ; The complete array is reversed so that the subarray can be processed as arr [ 0. . . i ] instead of arr [ i ... N - 1 ] ; Driver code
function maxSumSubarray ( arr , size ) { let max_so_far = Number . MIN_SAFE_INTEGER , max_ending_here = 0 ; for ( let i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + arr [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } function getUpdatedArray ( arr , copy , i ) { for ( let j = 0 ; j <= ( i / 2 ) ; j ++ ) { copy [ j ] = arr [ i - j ] ; copy [ i - j ] = arr [ j ] ; } return ; } function maxSum ( arr , size ) { let resSum = Number . MIN_SAFE_INTEGER ; resSum = Math . max ( resSum , maxSumSubarray ( arr , size ) ) ; let copyArr = arr ; for ( let i = 1 ; i < size ; i ++ ) { getUpdatedArray ( arr , copyArr , i ) ; resSum = Math . max ( resSum , maxSumSubarray ( copyArr , size ) ) ; } arr . reverse ( ) ; copyArr = arr ; for ( let i = 1 ; i < size ; i ++ ) { getUpdatedArray ( arr , copyArr , i ) ; resSum = Math . max ( resSum , maxSumSubarray ( copyArr , size ) ) ; } resSum += 6 return resSum ; } let arr = [ - 9 , 21 , 24 , 24 , - 51 , - 6 , 17 , - 42 , - 39 , 33 ] ; let size = arr . length ; document . write ( maxSum ( arr , size ) ) ;
Maximum possible GCD after replacing at most one element in the given array | Javascript implementation of the approach ; Function to return the maximum possible gcd after replacing a single element ; Prefix and Suffix arrays ; Single state dynamic programming relation for storing gcd of first i elements from the left in Prefix [ i ] ; Initializing Suffix array ; Single state dynamic programming relation for storing gcd of all the elements having index greater than or equal to i in Suffix [ i ] ; If first or last element of the array has to be replaced ; If any other element is replaced ; Return the maximized gcd ; Driver code
function gcd ( a , b ) { if ( b == 0 ) { return a ; } return gcd ( b , a % b ) ; } function MaxGCD ( a , n ) { var Prefix = Array ( n + 2 ) . fill ( 0 ) ; var Suffix = Array ( n + 2 ) . fill ( 0 ) ; var i ; Prefix [ 1 ] = a [ 0 ] ; for ( i = 2 ; i <= n ; i ++ ) { Prefix [ i ] = gcd ( Prefix [ i - 1 ] , a [ i - 1 ] ) ; } Suffix [ n ] = a [ n - 1 ] ; for ( i = n - 1 ; i >= 1 ; i -- ) { Suffix [ i ] = gcd ( Suffix [ i + 1 ] , a [ i - 1 ] ) ; } var ans = Math . max ( Suffix [ 2 ] , Prefix [ n - 1 ] ) ; for ( i = 2 ; i < n ; i ++ ) { ans = Math . max ( ans , gcd ( Prefix [ i - 1 ] , Suffix [ i + 1 ] ) ) ; } return ans ; } var a = [ 6 , 7 , 8 ] ; n = a . length ; document . write ( MaxGCD ( a , n ) ) ;
Sum of products of all possible K size subsets of the given array | Function to return the sum of products of all the possible k size subsets ; Initialising all the values to 0 ; To store the answer for current value of k ; For k = 1 , the answer will simply be the sum of all the elements ; Filling the table in bottom up manner ; To store the elements of the current row so that we will be able to use this sum for subsequent values of k ; We will subtract previously computed value so as to get the sum of elements from j + 1 to n in the ( i - 1 ) th row ; Driver code
function sumOfProduct ( arr , n , k ) { let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( n + 1 ) ; for ( let j = 0 ; j < dp [ i ] . length ; j ++ ) { dp [ i ] [ j ] = 0 ; } } let cur_sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { dp [ 1 ] [ i ] = arr [ i - 1 ] ; cur_sum += arr [ i - 1 ] ; } for ( let i = 2 ; i <= k ; i ++ ) { let temp_sum = 0 ; for ( let j = 1 ; j <= n ; j ++ ) { cur_sum -= dp [ i - 1 ] [ j ] ; dp [ i ] [ j ] = arr [ j - 1 ] * cur_sum ; temp_sum += dp [ i ] [ j ] ; } cur_sum = temp_sum ; } return cur_sum ; } let arr = [ 1 , 2 , 3 , 4 ] ; let n = arr . length ; let k = 2 ; document . write ( sumOfProduct ( arr , n , k ) ) ;