dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
function binomialCoeff ( n , k ) { let C = new Array ( n + 1 ) ; for ( var i = 0 ; i < C . length ; i ++ ) { C [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i <= n ; i ++ ) { for ( let j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } function lobb ( n , m ) { return ( ( 2 * m + 1 ) * binomialCoeff ( 2 * n , m + n ) ) / ( m + n + 1 ) ; } let n = 5 , m = 3 ; document . write ( lobb ( n , m ) ) ;

Index/key variables: i, j, k, n
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
let MAXN = 109 ; function countarray ( n , k , x ) { let dp = [ ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( let i = 2 ; i < n ; i ++ ) dp [ i ] = ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] ; return ( x == 1 ? ( k - 1 ) * dp [ n - 2 ] : dp [ n - 1 ] ) ; } let n = 4 , k = 3 , x = 2 ; document . write ( countarray ( n , k , x ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
let MAX = 100 ; let MAX_CHAR = 26 ; function precompute ( s , n , l , r ) { l [ s [ 0 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] [ 0 ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) { for ( let j = 0 ; j < MAX_CHAR ; j ++ ) l [ j ] [ i ] += l [ j ] [ i - 1 ] ; l [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] [ i ] ++ ; } r [ s [ n - 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] [ n - 1 ] = 1 ; for ( let i = n - 2 ; i >= 0 ; i -- ) { for ( let j = 0 ; j < MAX_CHAR ; j ++ ) r [ j ] [ i ] += r [ j ] [ i + 1 ] ; r [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] [ i ] ++ ; } } function countPalindromes ( k , n , l , r ) { let ans = 0 ; if ( k == 1 ) { for ( let i = 0 ; i < MAX_CHAR ; i ++ ) ans += l [ i ] [ n - 1 ] ; return ans ; } if ( k == 2 ) { for ( let i = 0 ; i < MAX_CHAR ; i ++ ) ans += ( ( l [ i ] [ n - 1 ] * ( l [ i ] [ n - 1 ] - 1 ) ) / 2 ) ; return ans ; } for ( let i = 1 ; i < n - 1 ; i ++ ) for ( let j = 0 ; j < MAX_CHAR ; j ++ ) ans += l [ j ] [ i - 1 ] * r [ j ] [ i + 1 ] ; return ans ; } let s = " " ; let k = 2 ; let n = s . length ; let l = new Array ( MAX_CHAR ) ; let r = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) { l [ i ] = new Array ( MAX ) ; r [ i ] = new Array ( MAX ) ; for ( let j = 0 ; j < MAX ; j ++ ) { l [ i ] [ j ] = 0 ; r [ i ] [ j ] = 0 ; } } precompute ( s , n , l , r ) ; document . write ( countPalindromes ( k , n , l , r ) ) ;

Index/key variables: i, j
Accumulator variables: ans, i, j
Iterator variables: i, j
Javascript program:
function maxSum ( mat , n ) { if ( n == 1 ) return mat [ 0 ] [ 0 ] ; let dp = new Array ( n ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } let maxSum = Number . MIN_VALUE , max ; for ( let j = 0 ; j < n ; j ++ ) dp [ n - 1 ] [ j ] = mat [ n - 1 ] [ j ] ; for ( let i = n - 2 ; i >= 0 ; i -- ) { for ( let j = 0 ; j < n ; j ++ ) { max = Number . MIN_VALUE ; if ( ( ( j - 1 ) >= 0 ) && ( max < dp [ i + 1 ] [ j - 1 ] ) ) max = dp [ i + 1 ] [ j - 1 ] ; if ( ( ( j + 1 ) < n ) && ( max < dp [ i + 1 ] [ j + 1 ] ) ) max = dp [ i + 1 ] [ j + 1 ] ; dp [ i ] [ j ] = mat [ i ] [ j ] + max ; } } for ( let j = 0 ; j < n ; j ++ ) if ( maxSum < dp [ 0 ] [ j ] ) maxSum = dp [ 0 ] [ j ] ; return maxSum ; } let mat = [ [ 5 , 6 , 1 , 7 ] , [ - 2 , 10 , 8 , - 1 ] , [ 3 , - 7 , - 9 , 11 ] , [ 12 , - 4 , 2 , 6 ] ] ; let n = 4 ; document . write ( " " + maxSum ( mat , n ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function longestRepeatedSubSeq ( str ) { subsequence / let n = str . length ; let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i <= n ; i ++ ) { dp [ i ] = new Array ( n + 1 ) ; for ( let j = 0 ; j <= n ; j ++ ) dp [ i ] [ j ] = 0 ; } for ( let i = 1 ; i <= n ; i ++ ) for ( let j = 1 ; j <= n ; j ++ ) if ( str [ i - 1 ] == str [ j - 1 ] && i != j ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = Math . max ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] ) ; let res = " " ; let i = n , j = n ; while ( i > 0 && j > 0 ) { if ( dp [ i ] [ j ] == dp [ i - 1 ] [ j - 1 ] + 1 ) { res = res + str [ i - 1 ] ; i -- ; j -- ; } else if ( dp [ i ] [ j ] == dp [ i - 1 ] [ j ] ) i -- ; else j -- ; } let reverse = " " ; for ( let k = res . length - 1 ; k >= 0 ; k -- ) { reverse = reverse + res [ k ] ; } return reverse ; } let str = " " ; document . write ( longestRepeatedSubSeq ( str ) ) ;

Index/key variables: i, j, k
Accumulator variables: i, j, k
Iterator variables: i, j, k
Javascript program:
function dfs ( node , parent , tree , colour , answer ) { answer [ node ] = colour [ node ] ; for ( var u of tree [ node ] ) { if ( u == parent ) continue ; dfs ( u , node , tree , colour , answer ) ; answer [ node ] += Math . max ( answer [ u ] , 0 ) ; } } function maxDiff ( tree , colour , N ) { var answer = Array ( N + 1 ) . fill ( 0 ) ; dfs ( 1 , 0 , tree , colour , answer ) ; var high = 0 ; for ( var i = 1 ; i <= N ; i ++ ) { high = Math . max ( high , answer [ i ] ) ; answer [ i ] = 0 ; } for ( var i = 1 ; i <= N ; i ++ ) { if ( colour [ i ] == - 1 ) colour [ i ] = 1 ; else colour [ i ] = - 1 ; } dfs ( 1 , 0 , tree , colour , answer ) ; for ( var i = 1 ; i < N ; i ++ ) high = Math . max ( high , answer [ i ] ) ; return high ; } var N = 5 ; var tree = Array . from ( Array ( N + 1 ) , ( ) => Array ( ) ) ; tree [ 1 ] . push ( 2 ) ; tree [ 2 ] . push ( 1 ) ; tree [ 1 ] . push ( 3 ) ; tree [ 3 ] . push ( 1 ) ; tree [ 2 ] . push ( 4 ) ; tree [ 4 ] . push ( 2 ) ; tree [ 3 ] . push ( 5 ) ; tree [ 5 ] . push ( 3 ) ; var colour = [ 0 , 1 , 1 , - 1 , - 1 , 1 ] ; document . write ( maxDiff ( tree , colour , N ) ) ;

Index/key variables: i, node, u
Accumulator variables: i
Iterator variables: i, u
Javascript program:
let MAX = 50 ; function solve ( dp , a , low , high , turn ) { if ( low == high ) { return Math . floor ( a [ low ] * turn ) ; } if ( dp [ low ] [ high ] != 0 ) { return dp [ low ] [ high ] ; } dp [ low ] [ high ] = Math . max ( Math . floor ( a [ low ] * turn ) + solve ( dp , a , low + 1 , high , turn + 1 ) , Math . floor ( a [ high ] * turn ) + solve ( dp , a , low , high - 1 , turn + 1 ) ) ; return dp [ low ] [ high ] ; } let arr = [ 1 , 3 , 1 , 5 , 2 ] ; let n = arr . length ; let dp = new Array ( MAX ) ; 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 ; } } document . write ( solve ( dp , arr , 0 , n - 1 , 1 ) ) ;

Index/key variables: high, i, j, low
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function maxSumBitonicSubArr ( arr , n ) { let msis = new Array ( n ) ; msis . fill ( 0 ) ; let msds = new Array ( n ) ; msds . fill ( 0 ) ; let max_sum = Number . MIN_VALUE ; msis [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) if ( arr [ i ] > arr [ i - 1 ] ) msis [ i ] = msis [ i - 1 ] + arr [ i ] ; else msis [ i ] = arr [ i ] ; msds [ n - 1 ] = arr [ n - 1 ] ; for ( let i = n - 2 ; i >= 0 ; i -- ) if ( arr [ i ] > arr [ i + 1 ] ) msds [ i ] = msds [ i + 1 ] + arr [ i ] ; else msds [ i ] = arr [ i ] ; for ( let i = 0 ; i < n ; i ++ ) if ( max_sum < ( msis [ i ] + msds [ i ] - arr [ i ] ) ) max_sum = msis [ i ] + msds [ i ] - arr [ i ] ; return max_sum ; } let arr = [ 5 , 3 , 9 , 2 , 7 , 6 , 4 ] ; let n = arr . length ; document . write ( " " + maxSumBitonicSubArr ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function printSuperSeq ( a , b ) { let m = a . length , n = b . length ; let dp = new Array ( m + 1 ) ; for ( let i = 0 ; i < m + 1 ; i ++ ) dp [ i ] = new Array ( n + 1 ) ; for ( let i = 0 ; i <= m ; i ++ ) { for ( let j = 0 ; j <= n ; j ++ ) { if ( i == 0 ) dp [ i ] [ j ] = j ; else if ( j == 0 ) dp [ i ] [ j ] = i ; else if ( a [ i - 1 ] == b [ j - 1 ] ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = 1 + Math . min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } let res = " " ; let i = m , j = n ; while ( i > 0 && j > 0 ) { if ( a [ i - 1 ] == b [ j - 1 ] ) { res = a [ i - 1 ] + res ; i -- ; j -- ; } else if ( dp [ i - 1 ] [ j ] < dp [ i ] [ j - 1 ] ) { res = a [ i - 1 ] + res ; i -- ; } else { res = b [ j - 1 ] + res ; j -- ; } } while ( i > 0 ) { res = a [ i - 1 ] + res ; i -- ; } while ( j > 0 ) { res = b [ j - 1 ] + res ; j -- ; } document . write ( res ) ; } let a = " " ; let b = " " ; printSuperSeq ( a , b ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function minTimeForWritingChars ( N , insert , remove , copy ) { if ( N == 0 ) return 0 ; if ( N == 1 ) return insert ; let dp = new Array ( N + 1 ) ; for ( let i = 0 ; i < N + 1 ; i ++ ) dp [ i ] = 0 ; dp [ 1 ] = insert ; for ( let i = 2 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) dp [ i ] = Math . min ( dp [ i - 1 ] + insert , dp [ parseInt ( i / 2 , 10 ) ] + copy ) ; else dp [ i ] = Math . min ( dp [ i - 1 ] + insert , dp [ parseInt ( ( i + 1 ) / 2 , 10 ) ] + copy + remove ) ; } return dp [ N ] ; } let N = 9 ; let insert = 1 , remove = 2 , copy = 1 ; document . write ( minTimeForWritingChars ( N , insert , remove , copy ) ) ;

Index/key variables: N, i
Accumulator variables: i
Iterator variables: i
Javascript program:
function countWays ( n , m ) { let count = new Array ( n + 1 ) ; count [ 0 ] = 0 ; let i ; for ( i = 1 ; i <= n ; i ++ ) { if ( i > m ) count [ i ] = count [ i - 1 ] + count [ i - m ] ; else if ( i < m i == 1 ) count [ i ] = 1 ; else count [ i ] = 2 ; } return count [ n ] ; } let n = 7 ; let m = 4 ; document . write ( " " + countWays ( n , m ) ) ;

Index/key variables: i, n
Accumulator variables: i
Iterator variables: i
Javascript program:
function LAS ( arr , n ) { let inc = 1 ; let dec = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) { inc = dec + 1 ; } else if ( arr [ i ] < arr [ i - 1 ] ) { dec = inc + 1 ; } } return Math . max ( inc , dec ) ; } let arr = [ 10 , 22 , 9 , 33 , 49 , 50 , 31 , 60 ] ; let n = arr . length ; document . write ( LAS ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function min ( x , y ) { return ( x < y ) ? x : y ; } function utility_fun_for_del ( str , i , j ) { if ( i >= j ) return 0 ; if ( str [ i ] == str [ j ] ) { return utility_fun_for_del ( str , i + 1 , j - 1 ) ; } return 1 + Math . min ( utility_fun_for_del ( str , i + 1 , j ) , utility_fun_for_del ( str , i , j - 1 ) ) ; } function min_ele_del ( str ) { return utility_fun_for_del ( str , 0 , str . length - 1 ) ; } let str = " " ; document . write ( " " + " " + min_ele_del ( str ) ) ;

Index/key variables: i, j
Javascript program:
let dp = new Array ( 2000 ) ; function transformation ( s1 , s2 , i , j ) { if ( i >= s1 . length j >= s2 . length ) { return 0 ; } if ( s1 [ i ] == s2 [ j ] ) { dp [ i ] [ j ] = 1 + transformation ( s1 , s2 , i + 1 , j + 1 ) ; } if ( dp [ i ] [ j ] != - 1 ) { return dp [ i ] [ j ] ; } else { dp [ i ] [ j ] = Math . max ( transformation ( s1 , s2 , i , j + i ) , transformation ( s1 , s2 , i + 1 , j ) ) ; } return dp [ s1 . length - 1 ] [ s2 . length - 1 ] ; } let s1 = " " ; let s2 = " " ; let i = 0 ; let j = 0 ; for ( let row = 0 ; row < dp . length ; row ++ ) { dp [ row ] = new Array ( dp . length ) ; for ( let column = 0 ; column < dp . length ; column ++ ) { dp [ row ] [ column ] = - 1 ; } } document . write ( " " + ( s1 . length - transformation ( s1 , s2 , 0 , 0 ) ) ) ; document . write ( " " + ( s2 . length - transformation ( s1 , s2 , 0 , 0 ) ) ) ; document . write ( " " + transformation ( s1 , s2 , 0 , 0 ) ) ;

Index/key variables: column, i, j, row
Accumulator variables: column, row
Iterator variables: column, row
Javascript program:
let MAX = 100 ; function largestZigZagSumRec ( mat , i , j , n ) { if ( i == n - 1 ) return mat [ i ] [ j ] ; let zzs = 0 ; for ( let k = 0 ; k < n ; k ++ ) if ( k != j ) zzs = Math . max ( zzs , largestZigZagSumRec ( mat , i + 1 , k , n ) ) ; return zzs + mat [ i ] [ j ] ; } function largestZigZag ( mat , n ) { let res = 0 ; for ( let j = 0 ; j < n ; j ++ ) res = Math . max ( res , largestZigZagSumRec ( mat , 0 , j , n ) ) ; return res ; } let n = 3 ; let mat = [ [ 4 , 2 , 1 ] , [ 3 , 9 , 6 ] , [ 11 , 3 , 15 ] ] ; document . write ( " " + largestZigZag ( mat , n ) )

Index/key variables: i, j
Accumulator variables: j, k
Iterator variables: j, k
Javascript program:
function countStr ( n , bCount , cCount ) { if ( bCount < 0 cCount < 0 ) return 0 ; if ( n == 0 ) return 1 ; if ( bCount == 0 && cCount == 0 ) return 1 ; let res = countStr ( n - 1 , bCount , cCount ) ; res += countStr ( n - 1 , bCount - 1 , cCount ) ; res += countStr ( n - 1 , bCount , cCount - 1 ) ; return res ; } let n = 3 ; document . write ( countStr ( n , 1 , 2 ) ) ;

Accumulator variables: res
Javascript program:
let N = 4 ; function MaximumPath ( Mat ) { let result = 0 ; let dp = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { dp [ i ] = new Array ( N + 2 ) ; for ( let j = 0 ; j < N + 2 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < N ; i ++ ) dp [ 0 ] [ i + 1 ] = Mat [ 0 ] [ i ] ; for ( let i = 1 ; i < N ; i ++ ) for ( let j = 1 ; j <= N ; j ++ ) dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j - 1 ] , Math . max ( dp [ i - 1 ] [ j ] , dp [ i - 1 ] [ j + 1 ] ) ) + Mat [ i ] [ j - 1 ] ; for ( let i = 0 ; i <= N ; i ++ ) result = Math . max ( result , dp [ N - 1 ] [ i ] ) ; return result ; } let Mat = [ [ 4 , 2 , 3 , 4 ] , [ 2 , 9 , 1 , 10 ] , [ 15 , 1 , 3 , 0 ] , [ 16 , 92 , 41 , 44 ] ] document . write ( MaximumPath ( Mat ) )

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
let MAX = 100001 ; let dp = new Array ( MAX ) . fill ( 0 ) ; function probability ( k , n ) { var ans = 0.0 ; for ( let i = k ; i <= n ; ++ i ) { var res = dp [ n ] - dp [ i ] - dp [ n - i ] - n ; ans += Math . pow ( 2.0 , res ) ; } return ans ; } function precompute ( ) { for ( let i = 2 ; i < MAX ; ++ i ) dp [ i ] = ( Math . log ( i ) / Math . log ( 2 ) ) + dp [ i - 1 ] ; } precompute ( ) ; document . write ( probability ( 2 , 3 ) . toFixed ( 2 ) + " " ) ; document . write ( probability ( 3 , 6 ) . toFixed ( 5 ) + " " ) ; document . write ( probability ( 500 , 1000 ) . toFixed ( 6 ) + " " ) ;

Index/key variables: i, n
Accumulator variables: ans
Iterator variables: i
Javascript program:
function canVote ( a , n , x ) { let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) { dp [ i ] = new Array ( x + 1 ) ; for ( let j = 0 ; j < x + 1 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += a [ i ] ; for ( let i = 1 ; i <= n ; i ++ ) for ( let j = 1 ; j <= x ; j ++ ) if ( a [ i ] <= j ) dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , a [ i ] + dp [ i - 1 ] [ j - a [ i ] ] ) ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; return ( sum - dp [ n ] [ x ] <= x ) ; } let n = 3 , x = 4 ; let a = [ 2 , 4 , 2 ] ; if ( canVote ( a , n , x ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }

Index/key variables: i, j, n, x
Accumulator variables: i, j, sum
Iterator variables: i, j
Javascript program:
function printMaxSum ( arr , n ) { let dp = new Array ( n ) ; dp . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { dp [ i ] = arr [ i ] ; let maxi = 0 ; for ( let j = 1 ; j <= Math . sqrt ( i + 1 ) ; j ++ ) { if ( ( ( i + 1 ) % j == 0 ) && ( i + 1 ) != j ) { if ( dp [ j - 1 ] > maxi ) maxi = dp [ j - 1 ] ; if ( dp [ parseInt ( ( i + 1 ) / j , 10 ) - 1 ] > maxi && j != 1 ) maxi = dp [ parseInt ( ( i + 1 ) / j , 10 ) - 1 ] ; } } dp [ i ] += maxi ; } for ( let i = 0 ; i < n ; i ++ ) document . write ( dp [ i ] + " " ) ; } let arr = [ 2 , 3 , 1 , 4 , 6 , 5 ] ; let n = arr . length ; printMaxSum ( arr , n ) ;

Index/key variables: i
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function minSum ( ar , n ) { if ( n <= 4 ) return Math . min . apply ( Math , ar ) ; var i ; var sum = Array ( n ) . fill ( n ) ; sum [ 0 ] = ar [ 0 ] ; sum [ 1 ] = ar [ 1 ] ; sum [ 2 ] = ar [ 2 ] ; sum [ 3 ] = ar [ 3 ] ; for ( i = 4 ; i < n ; i ++ ) { var temp = [ ] ; var it ; for ( it = i - 4 ; it < i ; it ++ ) temp . push ( sum [ it ] ) ; sum [ i ] = ar [ i ] + Math . min . apply ( Math , temp ) ; } var temp1 = [ ] ; for ( i = n - 4 ; i < n ; i ++ ) temp1 . push ( sum [ i ] ) ; return Math . min . apply ( Math , temp1 ) ; } var ar = [ 2 , 4 , 1 , 5 , 2 , 3 , 6 , 1 , 2 , 4 ] ; var n = ar . length ; document . write ( " " + minSum ( ar , n ) ) ;

Index/key variables: i, it
Accumulator variables: i, it
Iterator variables: i, it
Javascript program:
function maxAlternateSum ( arr , n ) { if ( n == 1 ) return arr [ 0 ] ; int min = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( min > arr [ i ] ) min = arr [ i ] ; } if ( min == arr [ 0 ] ) { return arr [ 0 ] ; } let dec = new Array ( n ) ; dec . fill ( 0 ) ; let inc = new Array ( n ) ; inc . fill ( 0 ) ; dec [ 0 ] = inc [ 0 ] = arr [ 0 ] ; let flag = 0 ; for ( let i = 1 ; i < n ; i ++ ) { for ( let j = 0 ; j < i ; j ++ ) { if ( arr [ j ] > arr [ i ] ) { dec [ i ] = Math . max ( dec [ i ] , inc [ j ] + arr [ i ] ) ; flag = 1 ; } else if ( arr [ j ] < arr [ i ] && flag == 1 ) inc [ i ] = Math . max ( inc [ i ] , dec [ j ] + arr [ i ] ) ; } } let result = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { if ( result < inc [ i ] ) result = inc [ i ] ; if ( result < dec [ i ] ) result = dec [ i ] ; } return result ; } let arr = [ 8 , 2 , 3 , 5 , 7 , 9 , 10 ] ; document . write ( " " + maxAlternateSum ( arr , arr . length ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function pad ( n ) { let pPrevPrev = 1 ; let pPrev = 1 ; let pCurr = 1 ; let pNext = 1 ; for ( let i = 3 ; i <= n ; i ++ ) { pNext = pPrevPrev + pPrev ; pPrevPrev = pPrev ; pPrev = pCurr ; pCurr = pNext ; } return pNext ; } let n = 12 ; document . write ( pad ( n ) ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function lucas ( n ) { let a = 2 , b = 1 , c , i ; if ( n == 0 ) return a ; for ( i = 2 ; i <= n ; i ++ ) { c = a + b ; a = b ; b = c ; } return b ; } let n = 9 ; document . write ( lucas ( n ) ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
let MAX = 1000000 ; function breakSum ( n ) { let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i < dp ; i ++ ) { dp [ i ] = 0 ; } dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( let i = 2 ; i <= n ; i ++ ) dp [ i ] = Math . max ( dp [ Math . floor ( i / 2 ) ] + dp [ Math . floor ( i / 3 ) ] + dp [ Math . floor ( i / 4 ) ] , i ) ; return dp [ n ] ; } let n = 24 ; document . write ( breakSum ( n ) ) ;

Index/key variables: i, n
Accumulator variables: i
Iterator variables: i
Javascript program:
function longestRepeatedSubstring ( str ) { let n = str . length ; let LCSRe = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) { LCSRe [ i ] = new Array ( n + 1 ) ; } for ( let i = 0 ; i < n + 1 ; i ++ ) { for ( let j = 0 ; j < n + 1 ; j ++ ) { LCSRe [ i ] [ j ] = 0 ; } } let i , index = 0 ; for ( i = 1 ; i <= n ; i ++ ) { for ( let j = i + 1 ; j <= n ; j ++ ) { if ( str [ i - 1 ] == str [ j - 1 ] && LCSRe [ i - 1 ] [ j - 1 ] < ( j - i ) ) { LCSRe [ i ] [ j ] = LCSRe [ i - 1 ] [ j - 1 ] + 1 ; if ( LCSRe [ i ] [ j ] > res_length ) { res_length = LCSRe [ i ] [ j ] ; index = Math . max ( i , index ) ; } } else { LCSRe [ i ] [ j ] = 0 ; } } } if ( res_length > 0 ) { for ( i = index - res_length + 1 ; i <= index ; i ++ ) { res += str . charAt ( i - 1 ) ; } } return res ; } let str = " " ; document . write ( longestRepeatedSubstring ( str ) ) ;

Index/key variables: i, j
Accumulator variables: i, j, res
Iterator variables: i, j
Javascript program:
function minCost ( cost , n ) { let dp = new Array ( n + 1 ) ; dp [ 0 ] = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { let min_cost = Number . MAX_VALUE ; for ( let j = 0 ; j < i ; j ++ ) if ( j < n ) min_cost = Math . min ( min_cost , cost [ j ] + dp [ i - j - 1 ] ) ; dp [ i ] = min_cost ; } return dp [ n ] ; } let cost = [ 20 , 10 , 4 , 50 , 100 ] ; let W = cost . length ; document . write ( minCost ( cost , W ) ) ;

Index/key variables: i, j, n
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function findSum ( arr ) { let sum = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) sum += arr [ i ] ; return sum ; } function printMaxSumIs ( arr , n ) { let L = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) L [ i ] = [ ] ; L [ 0 ] . push ( arr [ 0 ] ) ; for ( let i = 1 ; i < n ; i ++ ) { for ( let j = 0 ; j < i ; j ++ ) { if ( ( arr [ i ] > arr [ j ] ) && ( findSum ( L [ i ] ) < findSum ( L [ j ] ) ) ) { for ( let k = 0 ; k < L [ j ] . length ; k ++ ) if ( ! L [ i ] . includes ( L [ j ] [ k ] ) ) L [ i ] . push ( L [ j ] [ k ] ) ; } } L [ i ] . push ( arr [ i ] ) ; } let res = L [ 0 ] ; for ( let x = 0 ; x < L . length ; x ++ ) if ( findSum ( L [ x ] ) > findSum ( res ) ) res = L [ x ] ; for ( let i = 0 ; i < res . length ; i ++ ) document . write ( res [ i ] + " " ) ; document . write ( " " ) ; } let arr = [ 3 , 2 , 6 , 4 , 5 , 1 ] ; let n = arr . length ; printMaxSumIs ( arr , n ) ;

Index/key variables: i, j, k, x
Accumulator variables: i, j, k, sum, x
Iterator variables: i, j, k, x
Javascript program:
function minTime ( arr , n ) { if ( n <= 0 ) return 0 ; let incl = arr [ 0 ] ; let excl = 0 ; for ( let i = 1 ; i < n ; i ++ ) { let incl_new = arr [ i ] + Math . min ( excl , incl ) ; let excl_new = incl ; incl = incl_new ; excl = excl_new ; } return Math . min ( incl , excl ) ; } let arr1 = [ 10 , 5 , 2 , 7 , 10 ] ; let n1 = arr1 . length ; document . write ( minTime ( arr1 , n1 ) + " " ) ; let arr2 = [ 10 , 5 , 7 , 10 ] ; let n2 = arr2 . length ; document . write ( minTime ( arr2 , n2 ) + " " ) ; let arr3 = [ 10 , 5 , 2 , 4 , 8 , 6 , 7 , 10 ] ; let n3 = arr3 . length ; document . write ( minTime ( arr3 , n3 ) + " " ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function multiply ( a , b ) { var mul = Array ( 3 ) . fill ( 0 ) . map ( x => Array ( 3 ) . fill ( 0 ) ) ; for ( i = 0 ; i < 3 ; i ++ ) { for ( j = 0 ; j < 3 ; j ++ ) { mul [ i ] [ j ] = 0 ; for ( k = 0 ; k < 3 ; k ++ ) mul [ i ] [ j ] += a [ i ] [ k ] * b [ k ] [ j ] ; } } for ( i = 0 ; i < 3 ; i ++ ) for ( j = 0 ; j < 3 ; j ++ ) a [ i ] [ j ] = mul [ i ] [ j ] ; } function power ( F , n ) { var M = [ [ 1 , 1 , 1 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] ] ; if ( n == 1 ) return F [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] ; power ( F , parseInt ( n / 2 ) ) ; multiply ( F , F ) ; if ( n % 2 != 0 ) multiply ( F , M ) ; return F [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] ; } function findNthTerm ( n ) { var F = [ [ 1 , 1 , 1 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] ] ; return power ( F , n - 2 ) ; } var n = 5 ; document . write ( " " + findNthTerm ( n ) ) ;

Index/key variables: i, j, k
Accumulator variables: i, j, k
Iterator variables: i, j, k
Javascript program:
function count ( n ) { let dp = [ ] ; dp [ 0 ] = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { if ( i >= 1 && i <= 3 ) dp [ i ] = 1 ; else if ( i == 4 ) dp [ i ] = 2 ; else { dp [ i ] = dp [ i - 1 ] + dp [ i - 4 ] ; } } return dp [ n ] ; } let n = 5 ; document . write ( " " + count ( n ) ) ;

Index/key variables: i, n
Accumulator variables: i
Iterator variables: i
Javascript program:
function nCrModp ( n , r , p ) { if ( r > n - r ) r = n - r ; let C = new Array ( r + 1 ) ; for ( let i = 0 ; i < r + 1 ; i ++ ) C [ i ] = 0 ; C [ 0 ] = 1 ; for ( let i = 1 ; i <= n ; i ++ ) { for ( let j = Math . min ( i , r ) ; j > 0 ; j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p ; } return C [ r ] ; } let n = 10 , r = 2 , p = 13 ; document . write ( " " + nCrModp ( n , r , p ) ) ;

Index/key variables: i, j, r
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function countDer ( n ) { if ( n == 1 n == 2 ) { return n - 1 ; } let a = 0 ; let b = 1 ; for ( let i = 3 ; i <= n ; ++ i ) { let cur = ( i - 1 ) * ( a + b ) ; a = b ; b = cur ; } return b ; } document . write ( " " + countDer ( 4 ) ) ;

Iterator variables: i