dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
var N = 4 function getSum ( r1 , r2 , c1 , c2 , dp ) { return dp [ r2 ] [ c2 ] - dp [ r2 ] [ c1 ] - dp [ r1 ] [ c2 ] + dp [ r1 ] [ c1 ] ; } function sumFound ( K , S , grid ) { var dp = Array . from ( Array ( N + 1 ) , ( ) => Array ( N + 1 ) . fill ( 0 ) ) ; for ( var i = 0 ; i < N ; i ++ ) for ( var j = 0 ; j < N ; j ++ ) dp [ i + 1 ] [ j + 1 ] = dp [ i + 1 ] [ j ] + dp [ i ] [ j + 1 ] - dp [ i ] [ j ] + grid [ i ] [ j ] ; for ( var i = 0 ; i < N ; i ++ ) for ( var j = 0 ; j < N ; j ++ ) { var sum = getSum ( i , i + K , j , j + K , dp ) ; if ( sum == S ) return true ; } return false ; } var grid = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] ; var K = 2 ; var S = 14 ; if ( sumFound ( K , S , grid ) ) document . write ( " " ) ; else document . write ( " " ) ;

Index/key variables: c1, c2, i, j, r1, r2
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function bfs ( n , m , num ) { let q = [ ] ; q . push ( num ) ; while ( q . length != 0 ) { let stepNum = q . shift ( ) ; if ( stepNum <= m && stepNum >= n ) { document . write ( stepNum + " " ) ; } if ( stepNum == 0 stepNum > m ) continue ; let lastDigit = stepNum % 10 ; let stepNumA = stepNum * 10 + ( lastDigit - 1 ) ; let stepNumB = stepNum * 10 + ( lastDigit + 1 ) ; if ( lastDigit == 0 ) q . push ( stepNumB ) ; else if ( lastDigit == 9 ) q . push ( stepNumA ) ; else { q . push ( stepNumA ) ; q . push ( stepNumB ) ; } } } function displaySteppingNumbers ( n , m ) { for ( let i = 0 ; i <= 9 ; i ++ ) bfs ( n , m , i ) ; } let n = 0 , m = 21 ; displaySteppingNumbers ( n , m ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
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 ) + " " ) ;

Index/key variables: i, j, left, right
Accumulator variables: i, j, left, right
Iterator variables: i, j, left
Javascript program:
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 ) ;

Index/key variables: N, i
Iterator variables: i
Javascript program:
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 ] ) ;

Index/key variables: i, index, j, openbrk
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
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 ) + " " ) ;

Index/key variables: N, i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
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 ) ) ;

Index/key variables: i, j, k, m, n
Iterator variables: i, j, k
Javascript program:
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 ) ) ;

Index/key variables: k, start
Accumulator variables: k, one, zero
Iterator variables: k
Javascript program:
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 ) ) ;

Index/key variables: i, j, m, n
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
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 ( " " ) ; }

Index/key variables: i, n
Accumulator variables: i
Iterator variables: i
Javascript program:
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 ) ;

Index/key variables: i, j
Accumulator variables: i, j, maxSum
Iterator variables: i, j
Javascript program:
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 ) ) ;

Index/key variables: m, n
Javascript program:
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 ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
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 ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
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 ) ) ;

Index/key variables: i, j
Accumulator variables: i, j, val
Iterator variables: i, j
Javascript program:
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 ) + " " ) ;

Index/key variables: i, k, n
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
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 ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
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 ) ;

Index/key variables: i, j, k, n
Accumulator variables: i, j, k, sum
Iterator variables: i, j, k
Javascript program:
var MAX = 1000 ; function isPossible ( n , index , sum , M , arr , dp ) { if ( index == n ) { if ( ( sum % M ) == 0 ) return true ; return false ; } else if ( sum < 0 sum >= MAX ) return false ; if ( dp [ index ] [ sum ] != - 1 ) { if ( dp [ index ] [ sum ] == 0 ) return false ; return true ; } var placeAdd = isPossible ( n , index + 1 , sum + arr [ index ] , M , arr , dp ) ; var placeMinus = isPossible ( n , index + 1 , sum - arr [ index ] , M , arr , dp ) ; var res = ( placeAdd placeMinus ) ; dp [ index ] [ sum ] = ( res ) ? 1 : 0 ; return res ; } var arr = [ 1 , 2 , 3 , 4 , 6 ] ; var n = arr . length ; var M = 4 ; var dp = Array ( n + 1 ) . fill ( - 1 ) . map ( x => Array ( MAX ) . fill ( - 1 ) ) ; res = isPossible ( n , 0 , 0 , M , arr , dp ) ; document . write ( ( res ? " " : " " ) ) ;

Index/key variables: index, sum
Javascript program:
let MAX_NODES = 100 ; let In = new Array ( MAX_NODES ) ; let out = new Array ( MAX_NODES ) ; for ( let i = 0 ; i < MAX_NODES ; i ++ ) { In [ i ] = 0 ; out [ i ] = 0 ; } function dfs1 ( v , u , parent ) { In [ u ] = 0 ; for ( let j = 0 ; j < v [ u ] . length ; j ++ ) { let child = v [ u ] [ j ] ; if ( child == parent ) continue ; dfs1 ( v , child , u ) ; In [ u ] = Math . max ( In [ u ] , 1 + In [ child ] ) ; } } function dfs2 ( v , u , parent ) { let mx1 = - 1 , mx2 = - 1 ; for ( let j = 0 ; j < v [ u ] . length ; j ++ ) { let child = v [ u ] [ j ] ; if ( child == parent ) continue ; if ( In [ child ] >= mx1 ) { mx2 = mx1 ; mx1 = In [ child ] ; } else if ( In [ child ] > mx2 ) mx2 = In [ child ] ; } for ( let j = 0 ; j < v [ u ] . length ; j ++ ) { let child = v [ u ] [ j ] ; if ( child == parent ) continue ; let longest = mx1 ; if ( mx1 == In [ child ] ) longest = mx2 ; out [ child ] = 1 + Math . max ( out [ u ] , 1 + longest ) ; dfs2 ( v , child , u ) ; } } function addEdge ( adj , u , v ) { adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } function printHeights ( v , n ) { dfs1 ( v , 1 , 0 ) ; dfs2 ( v , 1 , 0 ) ; for ( let i = 1 ; i < n ; i ++ ) document . write ( " " + i + " " + Math . max ( In [ i ] , out [ i ] ) + " " ) ; } let V = 12 ; let adj = new Array ( V + 1 ) ; for ( let i = 0 ; i <= V ; i ++ ) adj [ i ] = [ ] ; addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 1 , 3 ) ; addEdge ( adj , 1 , 4 ) ; addEdge ( adj , 2 , 5 ) ; addEdge ( adj , 2 , 6 ) ; addEdge ( adj , 3 , 7 ) ; addEdge ( adj , 7 , 10 ) ; addEdge ( adj , 7 , 11 ) ; addEdge ( adj , 4 , 8 ) ; addEdge ( adj , 4 , 9 ) ; printHeights ( adj , V ) ;

Index/key variables: child, i, j, u, v
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function findGolomb ( n ) { if ( n == 1 ) return 1 ; return 1 + findGolomb ( n - findGolomb ( findGolomb ( n - 1 ) ) ) ; } function printGolomb ( n ) { for ( let i = 1 ; i <= n ; i ++ ) document . write ( findGolomb ( i ) + " " ) ; } var n = 9 ; printGolomb ( n ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function max ( a , b ) { return ( a > b ) ? a : b ; } function printknapSack ( W , wt , val , n ) { let i , w ; let K = new Array ( n + 1 ) ; for ( i = 0 ; i < K . length ; i ++ ) { K [ i ] = new Array ( W + 1 ) ; for ( let j = 0 ; j < W + 1 ; j ++ ) { K [ i ] [ j ] = 0 ; } } for ( i = 0 ; i <= n ; i ++ ) { for ( w = 0 ; w <= W ; w ++ ) { if ( i == 0 w == 0 ) K [ i ] [ w ] = 0 ; else if ( wt [ i - 1 ] <= w ) K [ i ] [ w ] = Math . max ( val [ i - 1 ] + K [ i - 1 ] [ w - wt [ i - 1 ] ] , K [ i - 1 ] [ w ] ) ; else K [ i ] [ w ] = K [ i - 1 ] [ w ] ; } } let res = K [ n ] [ W ] ; document . write ( res + " " ) ; w = W ; for ( i = n ; i > 0 && res > 0 ; i -- ) { if ( res == K [ i - 1 ] [ w ] ) continue ; else { document . write ( wt [ i - 1 ] + " " ) ; res = res - val [ i - 1 ] ; w = w - wt [ i - 1 ] ; } } } let val = [ 60 , 100 , 120 ] ; let wt = [ 10 , 20 , 30 ] ; let W = 50 ; let n = val . length ; printknapSack ( W , wt , val , n ) ;

Index/key variables: W, i, j, n, w
Accumulator variables: i, j, w
Iterator variables: i, j, w
Javascript program:
function removals ( arr , n , k ) { arr . sort ( ( a , b ) => a - b ) ; var dp = Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) dp [ i ] = - 1 ; var ans = n - 1 ; dp [ 0 ] = 0 ; for ( var i = 1 ; i < n ; i ++ ) { dp [ i ] = i ; var j = dp [ i - 1 ] ; while ( j != i && arr [ i ] - arr [ j ] > k ) { j ++ ; } dp [ i ] = Math . min ( dp [ i ] , j ) ; ans = Math . min ( ans , ( n - ( i - j + 1 ) ) ) ; } return ans ; } var a = [ 1 , 3 , 4 , 9 , 10 , 11 , 12 , 17 , 20 ] ; var n = a . length ; var k = 4 ; document . write ( removals ( a , n , k ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i
Javascript program:
function maximumSegments ( n , a , b , c ) { let dp = [ ] ; for ( let i = 0 ; i < n + 10 ; i ++ ) dp [ i ] = - 1 ; dp [ 0 ] = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( dp [ i ] != - 1 ) { dp [ i + a ] = Math . max ( dp [ i ] + 1 , dp [ i + a ] ) ; dp [ i + b ] = Math . max ( dp [ i ] + 1 , dp [ i + b ] ) ; dp [ i + c ] = Math . max ( dp [ i ] + 1 , dp [ i + c ] ) ; } } return dp [ n ] ; } let n = 7 , a = 5 , b = 2 , c = 5 ; document . write ( maximumSegments ( n , a , b , c ) ) ;

Index/key variables: i, n
Accumulator variables: i
Iterator variables: i
Javascript program:
function getMaximumSum ( arr ) { let n = arr . length ; let max = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { max = Math . max ( max , arr [ i ] ) ; } let freq = new Array ( max + 1 ) ; freq . fill ( 0 ) ; for ( let j = 0 ; j < n ; j ++ ) { freq [ arr [ j ] ] ++ ; } let ans = 0 , i = max ; while ( i > 0 ) { if ( freq [ i ] > 0 ) { ans += i ; freq [ i - 1 ] -- ; freq [ i ] -- ; } else { i -- ; } } return ans ; } let a = [ 1 , 2 , 3 ] ; document . write ( getMaximumSum ( a ) ) ;

Index/key variables: i, j
Accumulator variables: ans, i, j
Iterator variables: i, j
Javascript program:
function sequence ( n ) { let f = [ ] ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; f [ 2 ] = 1 ; document . write ( f [ 1 ] + " " + f [ 2 ] + " " ) ; for ( let i = 3 ; i <= n ; i ++ ) { f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ; document . write ( f [ i ] + " " ) ; } } let n = 13 ; sequence ( n ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function longestSubsequenceCommonSegment ( k , s1 , s2 ) { var n = s1 . length ; var m = s2 . length ; var lcs = Array . from ( Array ( n + 1 ) , ( ) => Array ( m + 1 ) . fill ( 0 ) ) ; var cnt = Array . from ( Array ( n + 1 ) , ( ) => Array ( m + 1 ) . fill ( 0 ) ) ; for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = 1 ; j <= m ; j ++ ) { lcs [ i ] [ j ] = Math . max ( lcs [ i - 1 ] [ j ] , lcs [ i ] [ j - 1 ] ) ; if ( s1 [ i - 1 ] == s2 [ j - 1 ] ) cnt [ i ] [ j ] = cnt [ i - 1 ] [ j - 1 ] + 1 ; if ( cnt [ i ] [ j ] >= k ) { for ( var a = k ; a <= cnt [ i ] [ j ] ; a ++ ) lcs [ i ] [ j ] = Math . max ( lcs [ i ] [ j ] , lcs [ i - a ] [ j - a ] + a ) ; } } } return lcs [ n ] [ m ] ; } var k = 4 ; var s1 = " " ; var s2 = " " ; document . write ( longestSubsequenceCommonSegment ( k , s1 , s2 ) ) ;

Index/key variables: i, j, m, n
Accumulator variables: a, i, j
Iterator variables: a, i, j
Javascript program:
var row = 5 ; var col = 5 ; function isPath ( arr ) { var dir = [ [ 0 , 1 ] , [ 0 , - 1 ] , [ 1 , 0 ] , [ - 1 , 0 ] ] ; var q = [ ] ; q . push ( [ 0 , 0 ] ) ; while ( q . length > 0 ) { var p = q [ 0 ] ; q . shift ( ) ; arr [ p [ 0 ] ] [ p [ 1 ] ] = - 1 ; if ( p == [ row - 1 ] [ col - 1 ] ) return true ; for ( var i = 0 ; i < 4 ; i ++ ) { var a = p [ 0 ] + dir [ i ] [ 0 ] ; var b = p [ 1 ] + dir [ i ] [ 1 ] ; if ( a >= 0 && b >= 0 && a < row && b < col && arr [ a ] [ b ] != - 1 ) { q . push ( [ a , b ] ) ; } } } return false ; } var arr = [ [ 0 , 0 , 0 , - 1 , 0 ] , [ - 1 , 0 , 0 , - 1 , - 1 ] , [ 0 , 0 , 0 , - 1 , 0 ] , [ - 1 , 0 , - 1 , 0 , - 1 ] , [ 0 , 0 , - 1 , 0 , 0 ] ] ; if ( isPath ( arr ) ) document . write ( " " ) ; else document . write ( " " ) ;

Index/key variables: a, b, i
Accumulator variables: i
Iterator variables: i
Javascript program:
function zigzag ( n , k ) { let dp = new Array ( n + 1 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } dp [ 0 ] [ 0 ] = 1 ; for ( let i = 1 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { for ( let j = 1 ; j <= Math . min ( i , k ) ; j ++ ) dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ i - j ] ; } return dp [ n ] [ k ] ; } let n = 4 , k = 3 ; document . write ( zigzag ( n , k ) ) ;

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