dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
const N = 1e5 + 5 ; let adj = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { adj . push ( [ ] ) } let answer = new Array ( N ) ; function addEdgesToGraph ( Edges , N ) { for ( let i = 0 ; i < N - 1 ; i ++ ) { let u = Edges [ i ] [ 0 ] ; let v = Edges [ i ] [ 1 ] ; adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } } function DFS ( node , parent , Val ) { answer [ node ] = Val [ node ] ; for ( let child of adj [ node ] ) { if ( child == parent ) continue ; DFS ( child , node , Val ) ; answer [ node ] = ( answer [ node ] answer [ child ] ) ; } } function preprocess ( Val ) { DFS ( 1 , - 1 , Val ) ; } function findSubtreeOR ( Queries , Q , Val ) { preprocess ( Val ) ; for ( let i = 0 ; i < Q ; i ++ ) { document . write ( answer [ Queries [ i ] ] + ' ' ) ; } } function findSubtreeORUtil ( N , Edges , Val , Queries , Q ) { addEdgesToGraph ( Edges , N ) ; findSubtreeOR ( Queries , Q , Val ) ; } let n = 5 ; let Edges = [ [ 1 , 2 ] , [ 1 , 3 ] , [ 3 , 4 ] , [ 3 , 5 ] ] ; let Val = [ 0 , 2 , 3 , 4 , 8 , 16 ] ; let Queries = [ 2 , 3 , 1 ] ; let Q = Queries . length ; findSubtreeORUtil ( n , Edges , Val , Queries , Q ) ;

Index/key variables: child, i, node, u, v
Accumulator variables: i
Iterator variables: child, i
Javascript program:
function max ( a , b ) { return a > b ? a : b ; } function maximumsum ( arr , K , N , M ) { let sum = 0 , maxSum ; let i , j , k ; let dp = new Array ( K + 1 ) ; for ( i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( i = 0 ; i <= N ; i ++ ) dp [ 0 ] [ i ] = 0 ; for ( i = 0 ; i <= K ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( i = 1 ; i <= K ; i ++ ) { for ( j = 0 ; j < N ; j ++ ) { sum = 0 ; maxSum = dp [ i ] [ j ] ; for ( k = 1 ; k <= M && k <= i ; k ++ ) { sum += arr [ j ] [ k - 1 ] ; maxSum = Math . max ( maxSum , sum + dp [ i - k ] [ j ] ) ; } dp [ i ] [ j + 1 ] = maxSum ; } } return dp [ K ] [ N ] ; } let arr = [ [ 10 , 10 , 100 , 30 ] , [ 80 , 50 , 10 , 50 ] ] ; let N = 2 , M = 4 ; let K = 5 ; document . write ( maximumsum ( arr , K , N , M ) ) ;

Index/key variables: K, N, i, j
Accumulator variables: i, j, k, sum
Iterator variables: i, j, k
Javascript program:
function waysToArrangeKLengthStrings ( N , K ) { let column_sum = [ ] ; let i , j ; for ( i = 1 ; i < N + 1 ; i ++ ) { column_sum [ i ] = 0 ; } let dp = new Array ( K + 1 ) ; for ( i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( i = 1 ; i < K + 1 ; i ++ ) { for ( j = 1 ; j < N + 1 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( i = 0 ; i <= N ; i ++ ) { dp [ 0 ] [ i ] = 1 ; column_sum [ i ] = 1 ; } for ( i = 1 ; i <= K ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { dp [ i ] [ j ] += column_sum [ j - 1 ] ; column_sum [ j ] += dp [ i ] [ j ] ; } } document . write ( dp [ K ] [ N ] ) ; } let N = 5 , K = 2 ; waysToArrangeKLengthStrings ( N , K ) ;

Index/key variables: K, N, i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function findNumberOfStrings ( n ) { let DP = new Array ( n + 1 ) ; for ( var i = 0 ; i < DP . length ; i ++ ) { DP [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < DP . length ; i ++ ) { for ( var j = 0 ; j < DP . length ; j ++ ) { DP [ i ] [ j ] = 0 ; } } DP [ 1 ] [ 1 ] = 1 ; for ( let i = 1 ; i < n + 1 ; i ++ ) { for ( let j = 1 ; j < 6 ; j ++ ) { if ( i == 1 ) { DP [ i ] [ j ] = DP [ i ] [ j - 1 ] + 1 ; } else { DP [ i ] [ j ] = DP [ i ] [ j - 1 ] + DP [ i - 1 ] [ j ] ; } } } return DP [ n ] [ 5 ] ; } let N = 2 ; document . write ( findNumberOfStrings ( N ) ) ;

Index/key variables: i, j, n
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function dfs ( v , p , adj , ans , vals ) { let isLeaf = true ; ans [ v ] = Number . MIN_VALUE ; for ( let u = 0 ; u < adj [ v ] . length ; u ++ ) { if ( adj [ v ] [ u ] == p ) continue ; isLeaf = false ; dfs ( adj [ v ] [ u ] , v , adj , ans , vals ) ; ans [ v ] = Math . max ( ans [ adj [ v ] [ u ] ] + vals [ v ] , Math . max ( ans [ adj [ v ] [ u ] ] , vals [ adj [ v ] [ u ] ] ) ) ; } if ( isLeaf ) { ans [ v ] = vals [ v ] ; } } function printAnswer ( n , edges , values ) { let adj = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) adj [ i ] = [ ] ; for ( let i = 0 ; i < n - 1 ; i ++ ) { let u = edges [ i ] [ 0 ] - 1 ; let v = edges [ i ] [ 1 ] - 1 ; adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } let ans = new Array ( n ) ; dfs ( 0 , - 1 , adj , ans , values ) ; for ( let x = 0 ; x < ans . length ; x ++ ) { document . write ( ans [ x ] + " " ) ; } } let N = 4 ; let edges = [ [ 1 , 2 ] , [ 1 , 3 ] , [ 3 , 4 ] ] ; let values = [ 1 , - 1 , 0 , 1 ] ; printAnswer ( N , edges , values ) ;

Index/key variables: i, u, v, x
Accumulator variables: i, u, x
Iterator variables: i, u, x
Boolean variables: isLeaf
Javascript program:
function noOfChars ( s , queries ) { var n = s . length ; var dp = Array ( n + 1 ) . fill ( 0 ) . map ( x => Array ( 26 ) . fill ( 0 ) ) ; for ( var i = 0 ; i < n ; i ++ ) { dp [ i + 1 ] [ s . charAt ( i ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( var j = 0 ; j < 26 ; j ++ ) { dp [ i + 1 ] [ j ] += dp [ i ] [ j ] ; } } var q = queries . length ; for ( var i = 0 ; i < q ; i ++ ) { var l = String . fromCharCode ( queries [ i ] [ 0 ] ) . charCodeAt ( 0 ) ; var r = String . fromCharCode ( queries [ i ] [ 1 ] ) . charCodeAt ( 0 ) ; var c = queries [ i ] [ 2 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; document . write ( dp [ r ] - dp [ l - 1 ] + " " ) ; } } var S = " " ; var queries = [ [ 1 , 2 , ' ' ] , [ 2 , 3 , ' ' ] ] ; noOfChars ( S , queries ) ;

Index/key variables: i, j, r
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function LowerBound ( a , k , x ) { var l = - 1 ; var r = k ; while ( l + 1 < r ) { var m = ( l + r ) >>> 1 ; if ( a [ m ] >= x ) { r = m ; } else { l = m ; } } return r ; } function findLCS ( nums , N ) { var k = 0 ; for ( i = 0 ; i < N ; i ++ ) { var pos = LowerBound ( nums , k , nums [ i ] ) ; nums [ pos ] = nums [ i ] ; if ( k == pos ) { k = pos + 1 ; } } return k ; } function minimumCost ( A , B , M , N , C ) { var nums = Array ( 100000 ) . fill ( 0 ) ; var index = Array ( 100000 ) . fill ( 0 ) ; for ( i = 0 ; i < 100000 ; i ++ ) index [ i ] = - 1 ; for ( i = 0 ; i < N ; i ++ ) { index [ B [ i ] ] = i ; } var k = 0 ; for ( i = 0 ; i < M ; i ++ ) { if ( index [ A [ i ] ] != - 1 ) { nums [ k ++ ] = index [ A [ i ] ] ; } } var lcs_length = findLCS ( nums , k ) ; var elements_to_be_added = N - lcs_length ; var min_cost = elements_to_be_added * C ; document . write ( min_cost ) ; return 0 ; } var A = [ 1 , 6 , 3 , 5 , 10 ] ; var B = [ 3 , 1 , 5 ] ; var C = 2 ; var M = A . length ; var N = B . length ; minimumCost ( A , B , M , N , C ) ;

Index/key variables: i, m, pos
Accumulator variables: i, k
Iterator variables: i
Javascript program:
function maxCount ( arr , m , V ) { let table = [ ] ; table [ 0 ] = 0 ; for ( let i = 1 ; i <= V ; i ++ ) table [ i ] = - 1 ; for ( let i = 1 ; i <= V ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { if ( arr [ j ] <= i ) { let sub_res = table [ i - arr [ j ] ] ; if ( sub_res != - 1 && sub_res + 1 > table [ i ] ) table [ i ] = sub_res + 1 ; } } } return table [ V ] ; } let arr = [ 25 , 10 , 5 ] ; let m = arr . length ; let V = 30 ; document . write ( maxCount ( arr , m , V ) ) ;

Index/key variables: V, i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
let SZ = 100 ; function pathSum ( mat , N , M ) { let dp = new Array ( N ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] ; } for ( let j = 1 ; j < M ; j ++ ) { dp [ 0 ] [ j ] = mat [ 0 ] [ j ] + dp [ 0 ] [ j - 1 ] ; } for ( let i = 1 ; i < N ; i ++ ) { for ( let j = 1 ; j < M ; j ++ ) { dp [ i ] [ j ] = mat [ i ] [ j ] + Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { document . write ( dp [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let mat = [ [ 3 , 2 , 1 ] , [ 6 , 5 , 4 ] , [ 7 , 8 , 9 ] ] ; let N = 3 ; let M = 3 ; pathSum ( mat , N , M ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
var INF = 10000000 ; var max_size = 100007 ; var dp = Array ( max_size ) ; function min_op ( cur , M ) { if ( cur > M ) return INF ; if ( cur == M ) return 0 ; if ( dp [ cur ] != - 1 ) return dp [ cur ] ; var op = INF ; for ( var i = 2 ; i * i <= cur ; i ++ ) { if ( cur % i == 0 ) { if ( i % 2 == 0 ) { op = Math . min ( op , 1 + min_op ( cur + i , M ) ) ; } if ( ( cur / i ) != i && ( cur / i ) % 2 == 0 ) { op = Math . min ( op , 1 + min_op ( cur + ( cur / i ) , M ) ) ; } } } return dp [ cur ] = op ; } function min_operations ( N , M ) { for ( i = N ; i <= M ; i ++ ) { dp [ i ] = - 1 ; } return min_op ( N , M ) ; } var N = 6 , M = 24 ; var op = min_operations ( N , M ) ; if ( op >= INF ) document . write ( " " ) ; else document . write ( op + " " ) ;

Index/key variables: cur, i
Accumulator variables: i
Iterator variables: i
Javascript program:
function maxValue ( a , n , pos , moves , left , dp ) { if ( moves == 0 || ( pos > n - 1 pos < 0 ) ) return 0 ; if ( dp [ pos ] [ left ] != - 1 ) return dp [ pos ] [ left ] ; let value = 0 ; if ( left > 0 && pos >= 1 ) value = Math . max ( value , a [ pos ] + maxValue ( a , n , pos - 1 , moves - 1 , left - 1 , dp ) ) ; if ( pos <= n - 1 ) value = Math . max ( value , a [ pos ] + maxValue ( a , n , pos + 1 , moves - 1 , left , dp ) ) ; return dp [ pos ] [ left ] = value ; } let n = 5 ; let a = [ 1 , 5 , 4 , 3 , 2 ] ; let k = 1 ; let m = 4 ; let dp = new Array ( n + 1 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < dp . length ; i ++ ) { for ( var j = 0 ; j < dp . length ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } document . write ( ( a [ 0 ] + maxValue ( a , n , 1 , m , k , dp ) ) ) ;

Index/key variables: i, j, left, pos
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
var N = 6 ; function maximumSum ( a , count , index , n , dp ) { if ( index == n ) return 0 ; if ( dp [ index ] [ count ] != - 1 ) return dp [ index ] [ count ] ; var take_element = a [ index ] * count + maximumSum ( a , count + 1 , index + 1 , n , dp ) ; var dont_take = maximumSum ( a , count , index + 1 , n , dp ) ; return dp [ index ] [ count ] = Math . max ( take_element , dont_take ) ; } var n = 5 ; var a = [ - 1 , - 9 , 0 , 5 , - 7 ] ; var dp = Array . from ( Array ( N ) , ( ) => Array ( N ) . fill ( - 1 ) ) ; document . write ( maximumSum ( a , 1 , 0 , n , dp ) ) ;

Index/key variables: count, index
Javascript program:
let N = 20 ; let visited = new Array ( N ) ; let K ; let removals = 0 ; let removed_nodes = [ ] ; let adj = [ ] ; function dfs ( s ) { visited [ s ] = true ; let nodes = 1 ; for ( let child = 0 ; child < adj [ s ] . length ; child ++ ) { if ( visited [ adj [ s ] [ child ] ] ) continue ; nodes += dfs ( adj [ s ] [ child ] ) ; } if ( nodes > K ) { removals ++ ; removed_nodes . push ( s ) ; nodes = 0 ; } return nodes ; } function addEdge ( a , b ) { adj [ a ] . push ( b ) ; adj [ b ] . push ( a ) ; } function findRemovedNodes ( K ) { dfs ( 1 ) ; document . write ( " " + " " + removals + " " ) ; document . write ( " " ) ; for ( let node = 0 ; node < removed_nodes . length ; node ++ ) document . write ( removed_nodes [ node ] + " " ) ; } for ( let i = 0 ; i < N ; i ++ ) adj . push ( [ ] ) ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 2 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 3 , 6 ) ; K = 3 ; findRemovedNodes ( K ) ;

Index/key variables: a, b, child, node, s
Accumulator variables: child, i, node, nodes, removals
Iterator variables: child, i, node
Javascript program:
class pair { constructor ( f , s ) { this . first = f ; this . second = s ; } } let N = 1e5 + 5 ; let v = [ ] ; let dp = new Array ( N ) ; let leaves = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { dp [ i ] = 0 ; leaves [ i ] = 0 ; } let n ; function dfs ( a , par ) { let leaf = 1 ; for ( let i = 0 ; i < v [ a ] . length ; i ++ ) { if ( v [ a ] [ i ] . first == par ) continue ; leaf = 0 ; dfs ( v [ a ] [ i ] . first , a ) ; } if ( leaf == 1 ) { leaves [ a ] += 1 ; } else { for ( let i = 0 ; i < v [ a ] . length ; i ++ ) { if ( v [ a ] [ i ] . first == par ) continue ; leaves [ a ] += leaves [ v [ a ] [ i ] . first ] ; dp [ a ] = dp [ a ] + dp [ v [ a ] [ i ] . first ] + leaves [ v [ a ] [ i ] . first ] * v [ a ] [ i ] . second ; } } } function dfs2 ( a , par ) { for ( let i = 0 ; i < v [ a ] . length ; i ++ ) { if ( v [ a ] [ i ] . first == par ) continue ; let leafOutside = leaves [ a ] - leaves [ v [ a ] [ i ] . first ] ; dp [ v [ a ] [ i ] . first ] += ( dp [ a ] - dp [ v [ a ] [ i ] . first ] ) ; dp [ v [ a ] [ i ] . first ] += v [ a ] [ i ] . second * ( leafOutside - leaves [ v [ a ] [ i ] . first ] ) ; leaves [ v [ a ] [ i ] . first ] += leafOutside ; dfs2 ( v [ a ] [ i ] . first , a ) ; } } function answerQueries ( queries ) { dfs ( 1 , 0 ) ; dfs2 ( 1 , 0 ) ; for ( let i = 0 ; i < queries . length ; i ++ ) { document . write ( dp [ queries [ i ] ] + " " ) ; } } n = 5 ; v = [ ] ; for ( let i = 0 ; i <= n ; i ++ ) v . push ( [ ] ) ; v [ 1 ] . push ( new pair ( 4 , 4 ) ) ; v [ 4 ] . push ( new pair ( 1 , 4 ) ) ; v [ 1 ] . push ( new pair ( 2 , 2 ) ) ; v [ 2 ] . push ( new pair ( 1 , 2 ) ) ; v [ 2 ] . push ( new pair ( 3 , 3 ) ) ; v [ 3 ] . push ( new pair ( 2 , 3 ) ) ; v [ 2 ] . push ( new pair ( 5 , 5 ) ) ; v [ 5 ] . push ( new pair ( 2 , 5 ) ) ; let queries = [ 1 , 3 , 5 ] ; answerQueries ( queries ) ;

Index/key variables: a, i
Accumulator variables: i
Iterator variables: i
Class/struct names: pair
Javascript program:
function countChanges ( matrix , n , m ) { let i , j , dist = n + m - 1 ; let Max_element = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { Max_element = Math . max ( Max_element , matrix [ i ] [ j ] ) ; } } let freq = new Array ( dist ) ; for ( i = 0 ; i < freq . length ; i ++ ) { freq [ i ] = new Array ( 2 ) ; } for ( i = 0 ; i < dist ; i ++ ) { for ( j = 0 ; j < Max_element + 1 ; j ++ ) freq [ i ] [ j ] = 0 ; } for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { freq [ i + j ] [ matrix [ i ] [ j ] ] ++ ; } } let min_changes_sum = 0 ; for ( i = 0 ; i < Math . floor ( dist / 2 ) ; i ++ ) { let maximum = 0 ; let total_values = 0 ; for ( j = 0 ; j < Max_element + 1 ; j ++ ) { maximum = Math . max ( maximum , freq [ i ] [ j ] + freq [ n + m - 2 - i ] [ j ] ) ; total_values += freq [ i ] [ j ] + freq [ n + m - 2 - i ] [ j ] ; } min_changes_sum += total_values - maximum ; } return min_changes_sum ; } let mat = [ [ 7 , 0 , 3 , 1 , 8 , 1 , 3 ] , [ 0 , 4 , 0 , 1 , 0 , 4 , 0 ] , [ 3 , 1 , 8 , 3 , 1 , 0 , 7 ] ] ; let minChanges = countChanges ( mat , 3 , 7 ) ; document . write ( minChanges ) ;

Index/key variables: i, j
Accumulator variables: i, j, min_changes_sum, total_values
Iterator variables: i, j
Javascript program:
let dp = new Array ( 1002 ) ; function possibleNumbers ( pos , previous , tight , start , N , K ) { if ( pos == N . length ) return 1 ; if ( dp [ pos ] [ previous ] [ tight ] [ start ] != - 1 ) return dp [ pos ] [ previous ] [ tight ] [ start ] ; let res = 0 ; let upper_limit = ( tight == 1 ) ? ( N [ pos ] - ' ' ) : 9 ; let new_tight ; if ( start == 0 ) { res = possibleNumbers ( pos + 1 , previous , 0 , 0 , N , K ) ; for ( let i = 1 ; i <= upper_limit ; i ++ ) { new_tight = ( tight > 0 && i == upper_limit ) ? 1 : 0 ; res += possibleNumbers ( pos + 1 , i , new_tight , 1 , N , K ) ; } } else { for ( let i = 0 ; i <= upper_limit ; i ++ ) { new_tight = ( tight > 0 && i == upper_limit ) ? 1 : 0 ; if ( Math . abs ( i - previous ) <= K ) res += possibleNumbers ( pos + 1 , i , new_tight , 1 , N , K ) ; } } dp [ pos ] [ previous ] [ tight ] [ start ] = res ; return dp [ pos ] [ previous ] [ tight ] [ start ] ; } let N = " " ; let K = 2 ; for ( let i = 0 ; i < 1002 ; i ++ ) { dp [ i ] = new Array ( 10 ) ; for ( let j = 0 ; j < 10 ; j ++ ) { dp [ i ] [ j ] = new Array ( 2 ) ; for ( let k = 0 ; k < 2 ; k ++ ) { dp [ i ] [ j ] [ k ] = new Array ( 2 ) ; for ( let l = 0 ; l < 2 ; l ++ ) { dp [ i ] [ j ] [ k ] [ l ] = - 1 ; } } } } document . write ( possibleNumbers ( 0 , 0 , 1 , 0 , N , K ) + " " ) ;

Index/key variables: i, j, k, l, pos, previous, start, tight
Accumulator variables: i, j, k, l, res
Iterator variables: i, j, k, l
Javascript program:
function mergeTwoNumbers ( numbers ) { var len , i , j , k ; var n = numbers . length ; if ( numbers . length == 0 ) { return 0 ; } var prefixSum = Array ( n + 1 ) . fill ( 0 ) ; for ( var i = 1 ; i <= n ; i ++ ) { prefixSum [ i ] = prefixSum [ i - 1 ] + numbers [ i - 1 ] ; } var dp = Array . from ( Array ( n + 1 ) , ( ) => Array ( n + 1 ) ) ; for ( var i = 1 ; i <= n ; i ++ ) { dp [ i ] [ i ] = 0 ; } for ( len = 2 ; len <= n ; len ++ ) { for ( i = 1 ; i <= n - len + 1 ; i ++ ) { j = i + len - 1 ; var sum = prefixSum [ j ] - prefixSum [ i - 1 ] ; dp [ i ] [ j ] = 1000000000 ; for ( k = i ; k < j ; k ++ ) { dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , dp [ i ] [ k ] + dp [ k + 1 ] [ j ] + sum ) ; } } } return dp [ 1 ] [ n ] ; } var arr1 = [ 6 , 4 , 4 , 6 ] ; document . write ( mergeTwoNumbers ( arr1 ) )

Index/key variables: i, j, k, n
Accumulator variables: i, k, len
Iterator variables: i, k, len
Javascript program:
let dp1 = [ ] ; let dp2 = [ ] ; for ( let i = 0 ; i < 100005 ; i ++ ) { dp1 . push ( 0 ) ; dp2 . push ( 0 ) ; } function dfs1 ( u , par , g , weight ) { dp1 [ u ] = weight [ u ] ; for ( let c = 0 ; c < g [ u ] . length ; c ++ ) { if ( g [ u ] != par ) { dfs1 ( g [ u ] , u , g , weight ) ; dp1 [ u ] += Math . max ( 0 , dp1 [ g [ u ] ] ) ; } } } function dfs2 ( u , par , g , weight ) { if ( par != 0 ) { maxSumAncestors = dp2 [ par ] - Math . max ( 0 , dp1 [ u ] ) ; dp2 [ u ] = dp1 [ u ] + Math . max ( 0 , maxSumAncestors ) ; } for ( let c = 0 ; c < g [ u ] . length ; c ++ ) { if ( g [ u ] != par ) { dfs2 ( g [ u ] , u , g , weight ) ; } } } function addEdge ( u , v , g ) { g [ u ] . push ( v ) ; g [ v ] . push ( u ) ; } function maxSumSegments ( g , weight , n ) { dfs1 ( 1 , 0 , g , weight ) ; for ( let i = 1 ; i < n + 1 ; i ++ ) dp2 [ i ] = dp1 [ i ] ; dfs2 ( 1 , 0 , g , weight ) ; } function printAns ( n ) { for ( let i = 1 ; i < n ; i ++ ) document . write ( dp2 [ i ] + " " ) ; } let n = 7 , u = 0 , v = 0 ; let g = [ ] ; for ( let i = 0 ; i < 100005 ; i ++ ) { g . push ( [ ] ) ; } addEdge ( 1 , 2 , g ) ; addEdge ( 1 , 3 , g ) ; addEdge ( 2 , 4 , g ) ; addEdge ( 2 , 5 , g ) ; addEdge ( 3 , 6 , g ) ; addEdge ( 4 , 7 , g ) ; let weight = new Array ( n + 1 ) ; weight . fill ( 0 ) ; weight [ 1 ] = - 8 ; weight [ 2 ] = 9 ; weight [ 3 ] = 7 ; weight [ 4 ] = - 4 ; weight [ 5 ] = 5 ; weight [ 6 ] = - 10 ; weight [ 7 ] = - 6 ; maxSumSegments ( g , weight , n ) ; printAns ( n ) ;

Index/key variables: i, par, u, v
Accumulator variables: c, i
Iterator variables: c, i
Javascript program:
function countPairs ( arr , N ) { var maxm = arr . reduce ( ( a , b ) => Math . max ( a , b ) ) ; var i , k ; var bitscount = Array ( maxm + 1 ) . fill ( 0 ) ; for ( i = 1 ; i <= maxm ; i *= 2 ) bitscount [ i ] = 1 ; for ( i = 1 ; i <= maxm ; i ++ ) { if ( bitscount [ i ] == 1 ) k = i ; if ( bitscount [ i ] == 0 ) { bitscount [ i ] = bitscount [ k ] + bitscount [ i - k ] ; } } var setbits = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( setbits . has ( bitscount [ arr [ i ] ] ) ) setbits . set ( bitscount [ arr [ i ] ] , setbits . get ( bitscount [ arr [ i ] ] ) + 1 ) else setbits . set ( bitscount [ arr [ i ] ] , 1 ) } var ans = 0 ; setbits . forEach ( ( value , key ) => { ans += value * ( value - 1 ) / 2 ; } ) ; return ans ; } var N = 12 ; var arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ] ; document . write ( countPairs ( arr , N ) ) ;

Index/key variables: i, k
Accumulator variables: ans, i
Iterator variables: i
Javascript program:
function depthFirst ( v , graph , visited , storeChain ) { visited [ v ] = true ; storeChain . push ( v ) ; for ( let i = 0 ; i < graph [ v ] . length ; i ++ ) { if ( visited [ graph [ v ] [ i ] ] == false ) { depthFirst ( graph [ v ] [ i ] , graph , visited , storeChain ) ; } } } function subarraySum ( arr , n ) { let maxSubarraySum = arr [ 0 ] ; let currentMax = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { currentMax = Math . max ( arr [ i ] , arr [ i ] + currentMax ) ; maxSubarraySum = Math . max ( maxSubarraySum , currentMax ) ; } return maxSubarraySum ; } function maxSubarraySum ( graph , vertices , values ) { let visited = new Array ( 1001 ) ; visited . fill ( false ) ; let maxSum = - 1000000 ; for ( let i = 1 ; i <= vertices ; i ++ ) { if ( visited [ i ] == false ) { let sizeChain ; let tempSum ; let storeChain = [ ] ; depthFirst ( i , graph , visited , storeChain ) ; sizeChain = storeChain . length ; let chainValues = new Array ( sizeChain + 1 ) ; for ( let j = 0 ; j < sizeChain ; j ++ ) { let temp = values [ storeChain [ j ] - 1 ] ; chainValues [ j ] = temp ; } tempSum = subarraySum ( chainValues , sizeChain ) ; if ( tempSum > maxSum ) { maxSum = tempSum ; } } } document . write ( " " ) ; document . write ( " " ) ; document . write ( maxSum ) ; } let graph = [ ] ; for ( let i = 0 ; i < 1001 ; i ++ ) graph . push ( [ ] ) ; let V = 7 ; let values = [ ] ; values . push ( 3 ) ; values . push ( 2 ) ; values . push ( 4 ) ; values . push ( - 2 ) ; values . push ( 0 ) ; values . push ( - 1 ) ; values . push ( - 5 ) ; graph [ 1 ] . push ( 2 ) ; graph [ 2 ] . push ( 1 ) ; graph [ 3 ] . push ( 4 ) ; graph [ 4 ] . push ( 3 ) ; graph [ 4 ] . push ( 5 ) ; graph [ 5 ] . push ( 4 ) ; graph [ 6 ] . push ( 7 ) ; graph [ 7 ] . push ( 6 ) ; maxSubarraySum ( graph , V , values ) ;

Index/key variables: i, j, v
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
dp = Array ( 1000 ) ; for ( i = 0 ; i < 1000 ; i ++ ) dp [ i ] = Array ( 1000 ) . fill ( 0 ) ; function solve ( n , k , m ) { if ( n == 0 && k == 0 ) return 1 ; if ( n == 0 && k != 0 ) return 0 ; if ( n != 0 && k == 0 ) { return 0 ; } if ( n < 0 k < 0 ) return 0 ; if ( dp [ n ] [ k ] > 0 ) return dp [ n ] [ k ] ; var ans = 0 ; for ( var j = 1 ; j <= m ; j ++ ) { ans += solve ( n - j , k - 1 , m ) ; } return dp [ n ] [ k ] = ans ; } var N = 7 , K = 4 , M = 3 ; document . write ( solve ( N , K , M ) ) ;

Index/key variables: i, k, n
Accumulator variables: ans, i, j
Iterator variables: i, j
Javascript program:
var prime = [ ] ; var isprime = Array ( 1000 ) . fill ( true ) ; var dp = Array . from ( Array ( 200 ) , ( ) => Array ( 20 ) ) ; for ( var i = 0 ; i < 200 ; i ++ ) for ( var j = 0 ; j < 20 ; j ++ ) dp [ i ] [ j ] = new Array ( 1000 ) . fill ( 0 ) ; function sieve ( ) { for ( var i = 2 ; i * i <= 1000 ; i ++ ) { if ( isprime [ i ] ) { for ( var j = i * i ; j <= 1000 ; j += i ) { isprime [ j ] = false ; } } } for ( var i = 2 ; i <= 1000 ; i ++ ) { if ( isprime [ i ] ) { prime . push ( i ) ; } } } function CountWays ( i , j , sum , n , k ) { if ( i > prime . length sum > n ) { return 0 ; } if ( sum == n ) { if ( j == k ) { return 1 ; } return 0 ; } if ( j == k ) return 0 ; if ( dp [ i ] [ j ] [ sum ] ) return dp [ i ] [ j ] [ sum ] ; var inc = 0 , exc = 0 ; inc = CountWays ( i + 1 , j + 1 , sum + prime [ i ] , n , k ) ; exc = CountWays ( i + 1 , j , sum , n , k ) ; return dp [ i ] [ j ] [ sum ] = inc + exc ; } sieve ( ) ; var N = 100 , K = 5 ; document . write ( CountWays ( 0 , 0 , 0 , N , K ) ) ;

Index/key variables: i, j, sum
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
var factors = Array . from ( Array ( 100005 ) , ( ) => Array ( ) ) ; var dp = Array ( 100005 ) ; function precompute ( ) { for ( var i = 1 ; i <= 100000 ; i ++ ) { for ( var j = i ; j <= 100000 ; j += i ) factors [ j ] . push ( i ) ; } } function solve ( arr , n ) { for ( var i = 0 ; i <= 100005 ; i ++ ) { dp [ i ] = 1000000000 ; } dp [ 0 ] = 0 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j of factors [ arr [ i ] ] ) { if ( i + j < n ) dp [ i + j ] = Math . min ( dp [ i + j ] , 1 + dp [ i ] ) ; } } return dp [ n - 1 ] ; } precompute ( ) ; var arr = [ 2 , 8 , 16 , 55 , 99 , 100 ] ; var n = arr . length document . write ( solve ( arr , n ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j