dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
function maxCost ( mat , N ) { let dp = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { dp [ i ] = new Array ( N ) ; for ( let j = 0 ; j < N ; j ++ ) { dp [ i ] [ j ] = 0 ; } } 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 i = 1 ; i < N ; i ++ ) for ( let j = 1 ; j < i + 1 && j < N ; j ++ ) dp [ i ] [ j ] = mat [ i ] [ j ] + Math . max ( dp [ i - 1 ] [ j - 1 ] , dp [ i - 1 ] [ j ] ) ; let result = 0 ; for ( let i = 0 ; i < N ; i ++ ) if ( result < dp [ N - 1 ] [ i ] ) result = dp [ N - 1 ] [ i ] ; return result ; } let mat = [ [ 4 , 1 , 5 , 6 , 1 ] , [ 2 , 9 , 2 , 11 , 10 ] , [ 15 , 1 , 3 , 15 , 2 ] , [ 16 , 92 , 41 , 4 , 3 ] , [ 8 , 142 , 6 , 4 , 8 ] ] ; let N = 5 ; document . write ( " " + maxCost ( mat , N ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
let M = 100 ; let memo = new Array ( M ) ; for ( let i = 0 ; i < M ; i ++ ) { memo [ i ] = new Array ( M ) ; for ( let j = 0 ; j < M ; j ++ ) { memo [ i ] [ j ] = 0 ; } } function numberOfPermWithKInversion ( N , K ) { if ( N == 0 ) return 0 ; if ( K == 0 ) return 1 ; if ( memo [ N ] [ K ] != 0 ) return memo [ N ] [ K ] ; let sum = 0 ; for ( let i = 0 ; i <= K ; i ++ ) { if ( i <= N - 1 ) sum += numberOfPermWithKInversion ( N - 1 , K - i ) ; } memo [ N ] [ K ] = sum ; return sum ; } let N = 4 ; let K = 2 ; document . write ( numberOfPermWithKInversion ( N , K ) ) ;

Index/key variables: K, N, i, j
Accumulator variables: i, j, sum
Iterator variables: i, j
Javascript program:
function KnapSack ( val , wt , n , W ) { let mat = new Array ( 2 ) ; for ( let i = 0 ; i < 2 ; i ++ ) { mat [ i ] = new Array ( W + 1 ) ; } for ( let i = 0 ; i < 2 ; i ++ ) { for ( let j = 0 ; j < W + 1 ; j ++ ) { mat [ i ] [ j ] = 0 ; } } let i = 0 ; while ( i < n ) { let j = 0 ; if ( i % 2 != 0 ) { while ( ++ j <= W ) { if ( wt [ i ] <= j ) { mat [ 1 ] [ j ] = Math . max ( val [ i ] + mat [ 0 ] [ j - wt [ i ] ] , mat [ 0 ] [ j ] ) ; } else { mat [ 1 ] [ j ] = mat [ 0 ] [ j ] ; } } } else { while ( ++ j <= W ) { if ( wt [ i ] <= j ) { mat [ 0 ] [ j ] = Math . max ( val [ i ] + mat [ 1 ] [ j - wt [ i ] ] , mat [ 1 ] [ j ] ) ; } else { mat [ 0 ] [ j ] = mat [ 1 ] [ j ] ; } } } i ++ ; } return ( n % 2 != 0 ) ? mat [ 0 ] [ W ] : mat [ 1 ] [ W ] ; } let val = [ 7 , 8 , 4 ] ; let wt = [ 3 , 8 , 6 ] ; let W = 10 , n = 3 ; document . write ( KnapSack ( val , wt , n , W ) ) ;

Index/key variables: W, i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function maxProfit ( price , n , k ) { var profit = Array ( k + 1 ) . fill ( 0 ) . map ( x => Array ( n + 1 ) . fill ( 0 ) ) ; for ( var i = 0 ; i <= k ; i ++ ) profit [ i ] [ 0 ] = 0 ; for ( j = 0 ; j <= n ; j ++ ) profit [ 0 ] [ j ] = 0 ; for ( var i = 1 ; i <= k ; i ++ ) { var prevDiff = - Number . MAX_VALUE ; for ( var j = 1 ; j < n ; j ++ ) { prevDiff = Math . max ( prevDiff , profit [ i - 1 ] [ j - 1 ] - price [ j - 1 ] ) ; profit [ i ] [ j ] = Math . max ( profit [ i ] [ j - 1 ] , price [ j ] + prevDiff ) ; } } return profit [ k ] [ n - 1 ] ; } var k = 3 ; var price = [ 12 , 14 , 17 , 10 , 14 , 13 , 12 , 15 ] ; var n = price . length ; document . write ( " " + maxProfit ( price , n , k ) ) ;

Index/key variables: i, j, k
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
let lookup = new Array ( 1000 ) ; for ( let i = 0 ; i < 1000 ; i ++ ) { lookup [ i ] = new Array ( 1000 ) ; } function countSeqUtil ( n , dif ) { if ( Math . abs ( dif ) > n ) return 0 ; if ( n == 1 && dif == 0 ) return 2 ; if ( n == 1 && Math . abs ( dif ) == 1 ) return 1 ; if ( lookup [ n ] [ n + dif ] != - 1 ) return lookup [ n ] [ n + dif ] ; countSeqUtil ( n - 1 , dif + 1 ) + 2 * countSeqUtil ( n - 1 , dif ) + countSeqUtil ( n - 1 , dif - 1 ) ; return lookup [ n ] [ n + dif ] = res ; } function countSeq ( n ) { for ( let k = 0 ; k < lookup . length ; k ++ ) { for ( let j = 0 ; j < lookup . length ; j ++ ) { lookup [ k ] [ j ] = - 1 ; } } return countSeqUtil ( n , 0 ) ; } let n = 2 ; document . write ( " " + countSeq ( 2 ) ) ;

Index/key variables: i, j, k, n
Accumulator variables: i, j, k
Iterator variables: i, j, k
Javascript program:
function countWays ( N , S ) { 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 + 1 ; j ++ ) { dp [ i ] [ j ] = new Array ( 2 ) ; for ( let k = 0 ; k < 2 ; k ++ ) dp [ i ] [ j ] [ k ] = - 1 ; } } return parenthesis_count ( S , 0 , N - 1 , 1 , dp ) ; } function parenthesis_count ( str , i , j , isTrue , dp ) { if ( i > j ) return 0 ; if ( i == j ) { if ( isTrue == 1 ) { return ( str [ i ] == ' ' ) ? 1 : 0 ; } else { return ( str [ i ] == ' ' ) ? 1 : 0 ; } } if ( dp [ i ] [ j ] [ isTrue ] != - 1 ) return dp [ i ] [ j ] [ isTrue ] ; let temp_ans = 0 ; let leftTrue , rightTrue , leftFalse , rightFalse ; for ( let k = i + 1 ; k <= j - 1 ; k = k + 2 ) { if ( dp [ i ] [ k - 1 ] [ 1 ] != - 1 ) leftTrue = dp [ i ] [ k - 1 ] [ 1 ] ; else { leftTrue = parenthesis_count ( str , i , k - 1 , 1 , dp ) ; } if ( dp [ i ] [ k - 1 ] [ 0 ] != - 1 ) leftFalse = dp [ i ] [ k - 1 ] [ 0 ] ; else { leftFalse = parenthesis_count ( str , i , k - 1 , 0 , dp ) ; } if ( dp [ k + 1 ] [ j ] [ 1 ] != - 1 ) rightTrue = dp [ k + 1 ] [ j ] [ 1 ] ; else { rightTrue = parenthesis_count ( str , k + 1 , j , 1 , dp ) ; } if ( dp [ k + 1 ] [ j ] [ 0 ] != - 1 ) rightFalse = dp [ k + 1 ] [ j ] [ 0 ] ; else { rightFalse = parenthesis_count ( str , k + 1 , j , 0 , dp ) ; } if ( str [ k ] == ' ' ) { if ( isTrue == 1 ) { temp_ans = temp_ans + leftTrue * rightTrue ; } else { temp_ans = temp_ans + leftTrue * rightFalse + leftFalse * rightTrue + leftFalse * rightFalse ; } } else if ( str [ k ] == ' ' ) { if ( isTrue == 1 ) { temp_ans = temp_ans + leftTrue * rightTrue + leftTrue * rightFalse + leftFalse * rightTrue ; } else { temp_ans = temp_ans + leftFalse * rightFalse ; } } else if ( str [ k ] == ' ' ) { if ( isTrue == 1 ) { temp_ans = temp_ans + leftTrue * rightFalse + leftFalse * rightTrue ; } else { temp_ans = temp_ans + leftTrue * rightTrue + leftFalse * rightFalse ; } } dp [ i ] [ j ] [ isTrue ] = temp_ans ; } return temp_ans ; } let symbols = " " ; let operators = " " ; let S = [ ] ; let j = 0 ; for ( let i = 0 ; i < symbols . length ; i ++ ) { S . push ( symbols [ i ] ) ; if ( j < operators . length ) S . push ( operators [ j ++ ] ) ; } let N = S . length ; document . write ( countWays ( N , S . join ( " " ) ) ) ;

Index/key variables: i, isTrue, j, k
Accumulator variables: i, j, k
Iterator variables: i, j, k
Javascript program:
function min ( arr , l , h ) { let mn = arr [ l ] ; for ( let i = l + 1 ; i <= h ; i ++ ) { if ( mn > arr [ i ] ) mn = arr [ i ] ; } return mn ; } function max ( arr , l , h ) { let mx = arr [ l ] ; for ( let i = l + 1 ; i <= h ; i ++ ) { if ( mx < arr [ i ] ) mx = arr [ i ] ; } return mx ; } function minRemovals ( arr , l , h ) { if ( l >= h ) return 0 ; let mn = min ( arr , l , h ) ; let mx = max ( arr , l , h ) ; if ( 2 * mn > mx ) return 0 ; return Math . min ( minRemovals ( arr , l + 1 , h ) , minRemovals ( arr , l , h - 1 ) ) + 1 ; } let arr = [ 4 , 5 , 100 , 9 , 10 , 11 , 12 , 15 , 200 ] ; let n = arr . length ; document . write ( minRemovals ( arr , 0 , n - 1 ) ) ;

Index/key variables: i, l
Accumulator variables: i
Iterator variables: i
Javascript program:
class Node { constructor ( data ) { this . data = data ; this . left = this . right = null ; } } let sum ; function getTotalHeightUtil ( root ) { if ( root == null ) { return 0 ; } let lh = getTotalHeightUtil ( root . left ) ; let rh = getTotalHeightUtil ( root . right ) ; let h = Math . max ( lh , rh ) + 1 ; sum = sum + h ; return h ; } function getTotalHeight ( root ) { sum = 0 ; getTotalHeightUtil ( root ) ; return sum ; } let root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; document . write ( " " , getTotalHeight ( root ) ) ;

Class/struct names: Node
Javascript program:
function numberOfPaths ( m , n ) { var count = Array ( m ) . fill ( 0 ) . map ( x => Array ( n ) . fill ( 0 ) ) ; for ( i = 0 ; i < m ; i ++ ) count [ i ] [ 0 ] = 1 ; for ( j = 0 ; j < n ; j ++ ) count [ 0 ] [ j ] = 1 ; for ( i = 1 ; i < m ; i ++ ) { for ( j = 1 ; j < n ; j ++ ) count [ i ] [ j ] = count [ i - 1 ] [ j ] + count [ i ] [ j - 1 ] + count [ i - 1 ] [ j - 1 ] ; ; } return count [ m - 1 ] [ n - 1 ] ; } document . write ( numberOfPaths ( 3 , 3 ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function maxProd ( n ) { if ( n == 0 n == 1 ) return 0 ; let max_val = 0 ; for ( let i = 1 ; i < n ; i ++ ) { max_val = Math . max ( max_val , Math . max ( i * ( n - i ) , maxProd ( n - i ) * i ) ) ; } return max_val ; } document . write ( " " + maxProd ( 10 ) ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
const NUM_LINE = 2 ; const NUM_STATION = 4 ; function min ( a , b ) { return a < b ? a : b ; } function carAssembly ( a , t , e , x ) { let T1 = new Array ( NUM_STATION ) ; let T2 = new Array ( NUM_STATION ) ; let i ; T1 [ 0 ] = e [ 0 ] + a [ 0 ] [ 0 ] ; T2 [ 0 ] = e [ 1 ] + a [ 1 ] [ 0 ] ; for ( i = 1 ; i < NUM_STATION ; ++ i ) { T1 [ i ] = min ( T1 [ i - 1 ] + a [ 0 ] [ i ] , T2 [ i - 1 ] + t [ 1 ] [ i ] + a [ 0 ] [ i ] ) ; T2 [ i ] = min ( T2 [ i - 1 ] + a [ 1 ] [ i ] , T1 [ i - 1 ] + t [ 0 ] [ i ] + a [ 1 ] [ i ] ) ; } return min ( T1 [ NUM_STATION - 1 ] + x [ 0 ] , T2 [ NUM_STATION - 1 ] + x [ 1 ] ) ; } let a = [ [ 4 , 5 , 3 , 2 ] , [ 2 , 10 , 1 , 4 ] ] ; let t = [ [ 0 , 7 , 4 , 5 ] , [ 0 , 9 , 2 , 8 ] ] ; let e = [ 10 , 12 ] , x = [ 18 , 7 ] ; document . write ( carAssembly ( a , t , e , x ) ) ;

Index/key variables: i
Iterator variables: i
Javascript program:
function findMinInsertionsDP ( str , n ) { let table = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { table [ i ] = new Array ( n ) ; } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { table [ i ] [ j ] = 0 ; } } let l = 0 , h = 0 , gap = 0 ; for ( gap = 1 ; gap < n ; gap ++ ) { for ( l = 0 , h = gap ; h < n ; l ++ , h ++ ) { table [ l ] [ h ] = ( str [ l ] == str [ h ] ) ? table [ l + 1 ] [ h - 1 ] : ( Math . min ( table [ l ] [ h - 1 ] , table [ l + 1 ] [ h ] ) + 1 ) ; } } return table [ 0 ] [ n - 1 ] ; } let str = " " ; document . write ( findMinInsertionsDP ( str , str . length ) ) ;

Index/key variables: h, i, j, l
Accumulator variables: gap, h, i, j, l
Iterator variables: gap, i, j, l
Javascript program:
function max ( a , b ) { return ( a > b ) ? a : b ; } function max ( a , b , c ) { return Math . max ( Math . max ( a , b ) , c ) ; } function maxCrossingSum ( arr , l , m , h ) { let sum = 0 ; let left_sum = Number . MIN_VALUE ; for ( let i = m ; i >= l ; i -- ) { sum = sum + arr [ i ] ; if ( sum > left_sum ) left_sum = sum ; } sum = 0 ; let right_sum = Number . MIN_VALUE ; for ( let i = m + 1 ; i <= h ; i ++ ) { sum = sum + arr [ i ] ; if ( sum > right_sum ) right_sum = sum ; } return max ( left_sum + right_sum , left_sum , right_sum ) ; } function maxSubArraySum ( arr , l , h ) { if ( l == h ) return arr [ l ] ; let m = parseInt ( ( l + h ) / 2 , 10 ) ; return max ( maxSubArraySum ( arr , l , m ) , maxSubArraySum ( arr , m + 1 , h ) , maxCrossingSum ( arr , l , m , h ) ) ; } let arr = [ 2 , 3 , 4 , 5 , 7 ] ; let n = arr . length ; let max_sum = maxSubArraySum ( arr , 0 , n - 1 ) ; document . write ( " " + max_sum ) ;

Index/key variables: i, l
Accumulator variables: i
Iterator variables: i
Javascript program:
function findWater ( i , j , X ) { if ( j > i ) { document . write ( " " ) ; } var ll = Math . round ( ( i * ( i + 1 ) ) ) ; glass = Array . from ( { length : ll + 2 } , ( _ , i ) => 0.0 ) ; var index = 0 ; glass [ index ] = X ; for ( row = 1 ; row <= i ; ++ row ) { for ( col = 1 ; col <= row ; ++ col , ++ index ) { X = glass [ index ] ; glass [ index ] = ( X >= 1.0 ) ? 1.0 : X ; X = ( X >= 1.0 ) ? ( X - 1 ) : 0.0 ; glass [ index + row ] += X / 2 ; glass [ index + row + 1 ] += X / 2 ; } } return glass [ parseInt ( ( i * ( i - 1 ) / 2 + j - 1 ) ) ] ; } var i = 2 , j = 2 ; var X = 2.0 ; document . write ( " " + " " + findWater ( i , j , X ) ) ;

Index/key variables: index
Iterator variables: col, row
Javascript program:
class Triplet { constructor ( row , col , rem_water ) { this . row = row ; this . col = col ; this . rem_water = rem_water ; } } function findWater ( i , j , totalWater ) { let dp = new Array ( i + 1 ) ; for ( let k = 0 ; k < dp . length ; k ++ ) { dp [ k ] = new Array ( ( 2 * i ) + 1 ) ; for ( let l = 0 ; l < dp [ k ] . length ; l ++ ) { dp [ k ] [ l ] = 0 ; } } let queue = [ ] ; queue . push ( new Triplet ( 0 , i , totalWater ) ) ; while ( queue . length != 0 ) { let curr = queue . shift ( ) ; if ( curr . row == i ) break ; if ( dp [ curr . row ] [ curr . col ] != 1.0 ) { let rem_water = 1 - dp [ curr . row ] [ curr . col ] ; if ( rem_water > curr . rem_water ) { dp [ curr . row ] [ curr . col ] += curr . rem_water ; curr . rem_water = 0 ; } else { dp [ curr . row ] [ curr . col ] += rem_water ; curr . rem_water -= rem_water ; } } if ( curr . rem_water != 0 ) { queue . push ( new Triplet ( curr . row + 1 , curr . col - 1 , ( curr . rem_water / 2 ) ) ) ; queue . push ( new Triplet ( curr . row + 1 , curr . col + 1 , ( curr . rem_water / 2 ) ) ) ; } } return dp [ i - 1 ] [ 2 * j - 1 ] ; } let i = 2 , j = 2 ; let totalWater = 2 ; document . write ( " " ) ; document . write ( findWater ( i , j , totalWater ) . toFixed ( 6 ) ) ;

Index/key variables: k, l
Accumulator variables: k, l, rem_water
Iterator variables: k, l
Class/struct names: Triplet
Javascript program:
class Pair { constructor ( a , b ) { this . a = a ; this . b = b ; } } function maxChainLength ( arr , n ) { let i , j , max = 0 ; let mcl = new Array ( n ) ; for ( i = 0 ; i < n ; i ++ ) mcl [ i ] = 1 ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] . a > arr [ j ] . b && mcl [ i ] < mcl [ j ] + 1 ) mcl [ i ] = mcl [ j ] + 1 ; for ( i = 0 ; i < n ; i ++ ) if ( max < mcl [ i ] ) max = mcl [ i ] ; return max ; } let arr = [ new Pair ( 5 , 24 ) , new Pair ( 15 , 25 ) , new Pair ( 27 , 40 ) , new Pair ( 50 , 60 ) ] ; document . write ( " " + maxChainLength ( arr , arr . length ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Class/struct names: Pair
Javascript program:
function count_flips ( a , n , k ) { var set = false ; var ans = 0 , min_diff = Number . MAX_VALUE ; for ( i = 0 ; i < k ; i ++ ) { var curr_index = i , segment = 0 , count_zero = 0 , count_one = 0 ; while ( curr_index < n ) { if ( segment % 2 == 0 ) { if ( a [ curr_index ] == 1 ) count_zero ++ ; else count_one ++ ; } else { if ( a [ curr_index ] == 0 ) count_zero ++ ; else count_one ++ ; } curr_index = curr_index + k ; segment ++ ; } ans += Math . min ( count_one , count_zero ) ; if ( count_one < count_zero ) set = ! set ; min_diff = Math . min ( min_diff , Math . abs ( count_zero - count_one ) ) ; } if ( set ) return ans ; else return ans + min_diff ; } var n = 6 , k = 3 ; var a = [ 0 , 0 , 1 , 1 , 0 , 0 ] ; document . write ( count_flips ( a , n , k ) ) ;

Index/key variables: curr_index
Accumulator variables: ans, count_one, count_zero, i, segment
Iterator variables: i
Boolean variables: set
Javascript program:
class Node { constructor ( data ) { this . left = null ; this . right = null ; this . data = data ; } } function size ( node ) { if ( node == null ) return 0 ; else return ( size ( node . left ) + 1 + size ( node . right ) ) ; } function checkIsAP ( arr , n ) { if ( n == 1 ) return true ; arr . sort ( ) ; let d = arr [ 1 ] - arr [ 0 ] ; for ( let i = 2 ; i < n ; i ++ ) if ( arr [ i ] - arr [ i - 1 ] != d ) return false ; return true ; } function checkIsGP ( arr , n ) { if ( n == 1 ) return true ; arr . sort ( ) ; let r = arr [ 1 ] / arr [ 0 ] ; for ( let i = 2 ; i < n ; i ++ ) { if ( arr [ i ] / arr [ i - 1 ] != r ) return false ; } return true ; } function checkIsHP ( arr , n ) { if ( n == 1 ) { return true ; } let rec = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { rec [ i ] = ( ( 1 / arr [ i ] ) ) ; } rec . sort ( ) ; let d = ( rec [ 1 ] ) - ( rec [ 0 ] ) ; for ( let i = 2 ; i < n ; i ++ ) { if ( rec [ i ] - rec [ i - 1 ] != d ) { return false ; } } return true ; } function checktype ( root ) { let n = size ( root ) ; let arr = new Array ( n ) ; let i = 0 ; if ( root == null ) return ; let q = [ ] ; q . push ( root ) ; while ( q . length > 0 ) { let node = q [ 0 ] ; q . shift ( ) ; arr [ i ] = node . data ; i ++ ; if ( node . left != null ) q . push ( node . left ) ; if ( node . right != null ) q . push ( node . right ) ; } let flag = 0 ; if ( checkIsAP ( arr , n ) ) { document . write ( " " ) ; flag = 1 ; } else if ( checkIsGP ( arr , n ) ) { document . write ( " " ) ; flag = 1 ; } else if ( checkIsHP ( arr , n ) ) { document . write ( " " ) ; flag = 1 ; } else if ( flag == 0 ) { document . write ( " " ) ; } } let root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . right = new Node ( 8 ) ; root . right . right . left = new Node ( 6 ) ; root . right . right . right = new Node ( 7 ) ; checktype ( root ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Class/struct names: Node
Javascript program:
function q1 ( s , i ) { document . write ( " " ) ; if ( i === s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] === " " ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } function q2 ( s , i ) { document . write ( " " ) ; if ( i === s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] === " " ) q4 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } function q3 ( s , i ) { document . write ( " " ) ; if ( i === s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] === " " ) q4 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } function q4 ( s , i ) { document . write ( " " ) ; if ( i === s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] === " " ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } function q0 ( s , i ) { document . write ( " " ) ; if ( i === s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] === " " ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } var s = " " ; document . write ( " " ) ; q0 ( s , 0 ) ;

Index/key variables: i
Javascript program:
function doMatch ( A , B ) { for ( i = 0 ; i < A . length ; i ++ ) { if ( A . charAt ( i ) != ' ' && B . charAt ( i ) != ' ' ) { if ( A . charAt ( i ) != B . charAt ( i ) ) return 0 ; } } return 1 ; } var A = " " ; var B = " " ; document . write ( doMatch ( A , B ) ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function countSubstrs ( s1 , s2 ) { var ans = 0 ; for ( var i = 0 ; i < s1 . length ; i ++ ) { var s3 = " " ; var s4 = s1 ; for ( var j = i ; j < s1 . length ; j ++ ) { s3 += s4 [ j ] ; if ( s2 . indexOf ( s3 ) != - 1 ) ans ++ ; } } return ans ; } var s1 = " " , s2 = " " ; document . write ( countSubstrs ( s1 , s2 ) ) ;

Index/key variables: j
Accumulator variables: ans, i, j, s3
Iterator variables: i, j
Javascript program:
function factorial ( N ) { let fact = 1 ; for ( let i = 1 ; i <= N ; i ++ ) fact = fact * i ; return fact ; } function nthTerm ( N ) { return ( factorial ( N ) * ( N + 2 ) / 2 ) ; } let N = 6 ; document . write ( nthTerm ( N ) ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function regexMatch ( text , pat ) { let lenText = text . length ; let lenPat = pat . length ; let Text = text . split ( " " ) ; let Pat = pat . split ( " " ) ; let i = 0 , j = 0 ; while ( i < lenPat ) { if ( Pat [ i ] != ' ' ) { if ( Pat [ i ] != Text [ j ] ) return false ; i ++ ; j ++ ; } else { j ++ ; while ( Text [ j ] != Pat [ i + 1 ] ) j ++ ; i ++ ; } } return ( j == lenText ) ; } let str = " " ; let pat = " " ; if ( regexMatch ( str , pat ) ) document . write ( " " ) ; else document . write ( " " ) ;

Index/key variables: i, j
Accumulator variables: i, j
Javascript program:
function translate ( str ) { var len = str . length ; if ( len < 2 ) return ; var i = 0 ; var j = 0 ; while ( j < len - 1 ) { if ( str [ j ] == ' ' && str [ j + 1 ] == ' ' ) { j = j + 2 ; let firstPart = str . substr ( 0 , i ) ; let lastPart = str . substr ( i + 1 ) ; str = firstPart + ' ' + lastPart ; i += 1 ; continue ; } let firstPart = str . substr ( 0 , i ) ; let lastPart = str . substr ( i + 1 ) ; str = firstPart + str [ j ] + lastPart ; i += 1 ; j += 1 ; } if ( j == len - 1 ) { let firstPart = str . substr ( 0 , i ) ; let lastPart = str . substr ( i + 1 ) ; str = firstPart + str [ j ] + lastPart ; i += 1 ; } let firstPart = str . substr ( 0 , i ) ; let lastPart = str . substr ( i + 1 ) ; str = firstPart + ' ' + lastPart ; firstPart = str . substr ( 0 , len - 1 ) ; lastPart = str . substr ( len ) ; str = firstPart + ' ' + lastPart ; str [ len - 1 ] = ' ' ; return str ; } var str = " " ; document . write ( " " + " " + translate ( str ) ) ;

Index/key variables: j
Accumulator variables: i, j
Javascript program:
let R , C ; let x = [ - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 ] ; let y = [ - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 ] ; function search2D ( grid , row , col , word ) { if ( grid [ row ] [ col ] != word [ 0 ] ) return false ; let len = word . length ; for ( let dir = 0 ; dir < 8 ; dir ++ ) { let k , rd = row + x [ dir ] , cd = col + y [ dir ] ; for ( k = 1 ; k < len ; k ++ ) { if ( rd >= R rd < 0 cd >= C cd < 0 ) break ; if ( grid [ rd ] [ cd ] != word [ k ] ) break ; rd += x [ dir ] ; cd += y [ dir ] ; } if ( k == len ) return true ; } return false ; } function patternSearch ( grid , word ) { for ( let row = 0 ; row < R ; row ++ ) { for ( let col = 0 ; col < C ; col ++ ) { if ( search2D ( grid , row , col , word ) ) document . write ( " " + row + " " + col + " " ) ; } } } R = 3 ; C = 13 ; let grid = [ [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] ] ; patternSearch ( grid , " " ) ; document . write ( " " ) ; patternSearch ( grid , " " ) ;

Index/key variables: cd, col, dir, k, rd, row
Accumulator variables: cd, col, dir, k, rd, row
Iterator variables: col, dir, k, row
Javascript program:
function maxScoreWords ( words , letters , score ) { let letterCounts = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { letterCounts [ i ] = 0 ; } for ( let letter = 0 ; letter < letters . length ; letter ++ ) letterCounts [ letters [ letter ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; return helper ( words , 0 , letterCounts , score ) ; } function helper ( words , start , letterCounts , score ) { if ( start == words . length ) return 0 ; let currScore = 0 ; let wordScore = 0 ; let nextCounts = [ ... letterCounts ] ; for ( let i = 0 ; i < words [ start ] . length ; ++ i ) { let idx = words [ start ] [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( nextCounts [ idx ] == 0 ) { wordScore = - 1 ; break ; } wordScore += score [ idx ] ; nextCounts [ idx ] -- ; } if ( wordScore > 0 ) currScore = helper ( words , start + 1 , nextCounts , score ) + wordScore ; currScore = Math . max ( currScore , helper ( words , start + 1 , letterCounts , score ) ) ; return currScore ; } let words = [ " " , " " , " " , " " ] ; let letters = [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] ; let score = [ 1 , 0 , 9 , 5 , 0 , 0 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ; document . write ( maxScoreWords ( words , letters , score ) ) ;

Index/key variables: i, idx, letter, start
Accumulator variables: i, letter, wordScore
Iterator variables: i, letter