dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
function maxLength ( s , 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 ; } } for ( let i = 0 ; i < n - 1 ; i ++ ) if ( s [ i ] == ' ' && s [ i + 1 ] == ' ' ) dp [ i ] [ i + 1 ] = 2 ; for ( let l = 2 ; l < n ; l ++ ) { for ( let i = 0 , j = l ; j < n ; i ++ , j ++ ) { if ( s [ i ] == ' ' && s [ j ] == ' ' ) dp [ i ] [ j ] = 2 + dp [ i + 1 ] [ j - 1 ] ; for ( let k = i ; k < j ; k ++ ) dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i ] [ k ] + dp [ k + 1 ] [ j ] ) ; } } return dp [ 0 ] [ n - 1 ] ; } let s = " " ; let n = s . length ; document . write ( maxLength ( s , n ) ) ;

Index/key variables: i, j, k
Accumulator variables: i, j, k, l
Iterator variables: i, j, k, l
Javascript program:
function find_partial_sum ( arr , start , end ) { let sum = 0 ; for ( let i = start ; i < end ; i ++ ) sum += arr [ i ] ; return sum ; } function maxSumBitonicSubArr ( arr , n ) { let max_sum = - 1000000 ; let i = 0 ; while ( i < n ) { let j = i ; while ( j + 1 < n && arr [ j ] < arr [ j + 1 ] ) j ++ ; while ( i < j && arr [ i ] <= 0 ) i ++ ; let k = j ; while ( k + 1 < n && arr [ k ] > arr [ k + 1 ] ) k ++ ; let last = k ; while ( k > j && arr [ k ] <= 0 ) k -- ; let sum_inc = find_partial_sum ( arr , i , j + 1 ) ; let sum_dec = find_partial_sum ( arr , j , k + 1 ) ; let sum_all = sum_inc + sum_dec - arr [ j ] ; max_sum = Math . max ( Math . max ( max_sum , sum_inc ) , Math . max ( sum_dec , sum_all ) ) ; i = Math . max ( last , i + 1 ) ; } return max_sum ; } let arr = [ 5 , 3 , 9 , 2 , 7 , 6 , 4 ] ; let n = arr . length ; document . write ( " " + maxSumBitonicSubArr ( arr , n ) + " " ) ; let arr2 = [ 1 , 2 , 3 , 4 , 5 ] ; let n2 = arr2 . length ; document . write ( " " + maxSumBitonicSubArr ( arr2 , n2 ) + " " ) ; let arr3 = [ 5 , 4 , 3 , 2 , 1 ] ; let n3 = arr3 . length ; document . write ( " " + maxSumBitonicSubArr ( arr3 , n3 ) + " " ) ; let arr4 = [ 5 , 5 , 5 , 5 ] ; let n4 = arr4 . length ; document . write ( " " + maxSumBitonicSubArr ( arr4 , n4 ) + " " ) ; let arr5 = [ - 1 , 0 , 1 , 2 , 3 , 1 , 0 , - 1 , - 10 ] ; let n5 = arr5 . length ; document . write ( " " + maxSumBitonicSubArr ( arr5 , n5 ) + " " ) ; let arr6 = [ - 1 , 0 , 1 , 2 , 0 , - 1 , - 2 , 0 , 1 , 3 ] ; let n6 = arr6 . length ; document . write ( " " + maxSumBitonicSubArr ( arr6 , n6 ) + " " ) ;

Index/key variables: i, j, k
Accumulator variables: i, j, k, sum
Iterator variables: i
Javascript program:
function smallestSumSubarr ( arr , n ) { let min_ending_here = 2147483647 ; let min_so_far = 2147483647 ; for ( let i = 0 ; i < n ; i ++ ) { if ( min_ending_here > 0 ) min_ending_here = arr [ i ] ; else min_ending_here += arr [ i ] ; min_so_far = Math . min ( min_so_far , min_ending_here ) ; } return min_so_far ; } let arr = [ 3 , - 4 , 2 , - 3 , - 1 , 7 , - 5 ] ; let n = arr . length ; document . write ( " " + smallestSumSubarr ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: i, min_ending_here
Iterator variables: i
Javascript program:
let dp = new Array ( 300 ) ; for ( let i = 0 ; i < 300 ; i ++ ) { dp [ i ] = new Array ( 300 ) ; for ( let j = 0 ; j < 300 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } function minimumSquare ( m , n ) { let vertical_min = Number . MAX_VALUE ; let horizontal_min = Number . MAX_VALUE ; if ( n == 13 && m == 11 ) return 6 ; if ( m == 13 && n == 11 ) return 6 ; if ( m == n ) return 1 ; if ( dp [ m ] [ n ] != 0 ) return dp [ m ] [ n ] ; for ( let i = 1 ; i <= parseInt ( m / 2 , 10 ) ; i ++ ) { horizontal_min = Math . min ( minimumSquare ( i , n ) + minimumSquare ( m - i , n ) , horizontal_min ) ; } for ( let j = 1 ; j <= parseInt ( n / 2 , 10 ) ; j ++ ) { vertical_min = Math . min ( minimumSquare ( m , j ) + minimumSquare ( m , n - j ) , vertical_min ) ; } dp [ m ] [ n ] = Math . min ( vertical_min , horizontal_min ) ; return dp [ m ] [ n ] ; } let m = 30 , n = 35 ; document . write ( minimumSquare ( m , n ) ) ;

Index/key variables: i, j, m, n
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function fact ( n ) { var res = 1 ; for ( var i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } function nCr ( n , r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } var n = 2 ; document . write ( " " + nCr ( n + 9 , 9 ) ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function countWays ( n , k ) { let total = k ; let mod = 1000000007 ; let same = 0 , diff = k ; for ( let i = 2 ; i <= n ; i ++ ) { same = diff ; diff = total * ( k - 1 ) ; diff = diff % mod ; total = ( same + diff ) % mod ; } return total ; } let n = 3 , k = 2 ; document . write ( countWays ( n , k ) ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function sumOfSubstrings ( num ) { let sum = 0 ; let mf = 1 ; for ( let i = num . length - 1 ; i >= 0 ; i -- ) { sum += ( num [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) * ( i + 1 ) * mf ; mf = mf * 10 + 1 ; } return sum ; } let num = " " ; document . write ( sumOfSubstrings ( num ) ) ;

Index/key variables: i
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function maxSumWithK ( a , n , k ) { let maxSum = new Array ( n ) ; maxSum [ 0 ] = a [ 0 ] ; let curr_max = a [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { curr_max = Math . max ( a [ i ] , curr_max + a [ i ] ) ; maxSum [ i ] = curr_max ; } let sum = 0 ; for ( let i = 0 ; i < k ; i ++ ) sum += a [ i ] ; let result = sum ; for ( let i = k ; i < n ; i ++ ) { sum = sum + a [ i ] - a [ i - k ] ; result = Math . max ( result , sum ) ; result = Math . max ( result , sum + maxSum [ i - k ] ) ; } return result ; } let arr = [ 1 , 2 , 3 , - 10 , - 3 ] ; let k = 4 ; document . write ( maxSumWithK ( arr , arr . length , k ) ) ;

Index/key variables: i
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
let arr = [ 1 , 5 , 6 ] ; function countWays ( N ) { let count = new Array ( N + 1 ) ; count . fill ( 0 ) ; count [ 0 ] = 1 ; for ( let i = 1 ; i <= N ; i ++ ) for ( let j = 0 ; j < arr . length ; j ++ ) if ( i >= arr [ j ] ) count [ i ] += count [ i - arr [ j ] ] ; return count [ N ] ; } let N = 7 ; document . write ( " " + countWays ( N ) ) ;

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

Index/key variables: i, j, m, n
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function lcs ( str1 , str2 , 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 ++ ) { L [ i ] [ j ] = 0 ; } } for ( i = 0 ; i <= m ; i ++ ) { for ( j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i ] [ j ] = 0 ; else if ( str1 [ i - 1 ] == str2 [ j - 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 ] ; } function printMinDelAndInsert ( str1 , str2 ) { let m = str1 . length ; let n = str2 . length ; let len = lcs ( str1 , str2 , m , n ) ; document . write ( " " + " " ) ; document . write ( ( m - len ) + " " ) ; document . write ( " " + " " ) ; document . write ( ( n - len ) + " " ) ; } let str1 = " " ; let str2 = " " ; printMinDelAndInsert ( str1 , str2 ) ;

Index/key variables: i, j, m, n
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function lis ( arr , n ) { let result = 0 ; let lis = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) lis [ i ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) for ( let j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; for ( let i = 0 ; i < n ; i ++ ) if ( result < lis [ i ] ) result = lis [ i ] ; return result ; } function minimumNumberOfDeletions ( arr , n ) { let len = lis ( arr , n ) ; return ( n - len ) ; } let arr = [ 30 , 40 , 2 , 5 , 1 , 7 , 45 , 50 , 8 ] ; let n = arr . length ; document . write ( " " + minimumNumberOfDeletions ( arr , n ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
let inf = 1000000000 ; function minCost ( a , n , k ) { let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i <= n ; i ++ ) { dp [ i ] = new Array ( k + 1 ) ; for ( let j = 0 ; j <= k ; j ++ ) dp [ i ] [ j ] = inf ; } dp [ 0 ] [ 0 ] = 0 ; for ( let i = 1 ; i <= n ; i ++ ) for ( let j = 1 ; j <= k ; j ++ ) for ( let m = i - 1 ; m >= 0 ; m -- ) dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , dp [ m ] [ j - 1 ] + ( a [ i - 1 ] - a [ m ] ) * ( a [ i - 1 ] - a [ m ] ) ) ; return dp [ n ] [ k ] ; } let k = 2 ; let a = [ 1 , 5 , 8 , 10 ] ; let n = a . length ; document . write ( minCost ( a , n , k ) ) ;

Index/key variables: i, j, k, m, n
Accumulator variables: i, j, m
Iterator variables: i, j, m
Javascript program:
function lps ( str ) { let n = str . length ; let L = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { L [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) { L [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < n ; i ++ ) L [ i ] [ i ] = 1 ; for ( let cl = 2 ; cl <= n ; cl ++ ) { for ( let i = 0 ; i < n - cl + 1 ; i ++ ) { let j = i + cl - 1 ; if ( str [ i ] == str [ j ] && cl == 2 ) L [ i ] [ j ] = 2 ; else if ( str [ i ] == str [ j ] ) L [ i ] [ j ] = L [ i + 1 ] [ j - 1 ] + 2 ; else L [ i ] [ j ] = Math . max ( L [ i ] [ j - 1 ] , L [ i + 1 ] [ j ] ) ; } } return L [ 0 ] [ n - 1 ] ; } function minimumNumberOfDeletions ( str ) { let n = str . length ; let len = lps ( str ) ; return ( n - len ) ; } let str = " " ; document . write ( " " + " " + minimumNumberOfDeletions ( str ) ) ;

Index/key variables: i, j
Accumulator variables: cl, i, j
Iterator variables: cl, i, j
Javascript program:
function offeringNumber ( n , templeHeight ) { for ( let i = 0 ; i < n ; ++ i ) { let left = 0 , right = 0 ; for ( let j = i - 1 ; j >= 0 ; -- j ) { if ( templeHeight [ j ] < templeHeight [ j + 1 ] ) ++ left ; else break ; } for ( let j = i + 1 ; j < n ; ++ j ) { if ( templeHeight [ j ] < templeHeight [ j - 1 ] ) ++ right ; else break ; } sum += Math . max ( right , left ) + 1 ; } return sum ; } let arr1 = [ 1 , 2 , 2 ] ; document . write ( offeringNumber ( 3 , arr1 ) + " " ) ; let arr2 = [ 1 , 4 , 3 , 6 , 2 , 1 ] ; document . write ( offeringNumber ( 6 , arr2 ) ) ;

Index/key variables: j
Accumulator variables: sum
Iterator variables: i, j
Javascript program:
function modularSum ( arr , n , m ) { if ( n > m ) return true ; let DP = new Array ( m ) ; for ( let l = 0 ; l < m ; l ++ ) DP [ l ] = false ; for ( let i = 0 ; i < n ; i ++ ) { if ( DP [ 0 ] ) return true ; let temp = new Array ( m ) ; for ( let l = 0 ; l < m ; l ++ ) temp [ l ] = false ; for ( let j = 0 ; j < m ; j ++ ) { if ( DP [ j ] == true ) { if ( DP [ ( j + arr [ i ] ) % m ] == false ) temp [ ( j + arr [ i ] ) % m ] = true ; } } for ( let j = 0 ; j < m ; j ++ ) if ( temp [ j ] ) DP [ j ] = true ; DP [ arr [ i ] % m ] = true ; } return DP [ 0 ] ; } let arr = [ 1 , 7 ] ; let n = arr . length ; let m = 5 ; if ( modularSum ( arr , n , m ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;

Index/key variables: i, j, l
Accumulator variables: i, j, l
Iterator variables: i, j, l
Javascript program:
function maxSum ( tri , n ) { if ( n > 1 ) tri [ 1 ] [ 1 ] = tri [ 1 ] [ 1 ] + tri [ 0 ] [ 0 ] ; tri [ 1 ] [ 0 ] = tri [ 1 ] [ 0 ] + tri [ 0 ] [ 0 ] ; for ( let i = 2 ; i < n ; i ++ ) { tri [ i ] [ 0 ] = tri [ i ] [ 0 ] + tri [ i - 1 ] [ 0 ] ; tri [ i ] [ i ] = tri [ i ] [ i ] + tri [ i - 1 ] [ i - 1 ] ; for ( let j = 1 ; j < i ; j ++ ) { if ( tri [ i ] [ j ] + tri [ i - 1 ] [ j - 1 ] >= tri [ i ] [ j ] + tri [ i - 1 ] [ j ] ) tri [ i ] [ j ] = tri [ i ] [ j ] + tri [ i - 1 ] [ j - 1 ] ; else tri [ i ] [ j ] = tri [ i ] [ j ] + tri [ i - 1 ] [ j ] ; } } let max = tri [ n - 1 ] [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( max < tri [ n - 1 ] [ i ] ) max = tri [ n - 1 ] [ i ] ; } return max ; } let tri = [ [ 1 ] , [ 2 , 1 ] , [ 3 , 3 , 2 ] ] ; document . write ( maxSum ( tri , 3 ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function maximumDifferenceSum ( arr , N ) { let dp = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { dp [ i ] = new Array ( 2 ) ; for ( let j = 0 ; j < 2 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < N ; i ++ ) dp [ i ] [ 0 ] = dp [ i ] [ 1 ] = 0 ; for ( let i = 0 ; i < ( N - 1 ) ; i ++ ) { dp [ i + 1 ] [ 0 ] = Math . max ( dp [ i ] [ 0 ] , dp [ i ] [ 1 ] + Math . abs ( 1 - arr [ i ] ) ) ; dp [ i + 1 ] [ 1 ] = Math . max ( dp [ i ] [ 0 ] + Math . abs ( arr [ i + 1 ] - 1 ) , dp [ i ] [ 1 ] + Math . abs ( arr [ i + 1 ] - arr [ i ] ) ) ; } return Math . max ( dp [ N - 1 ] [ 0 ] , dp [ N - 1 ] [ 1 ] ) ; } let arr = [ 3 , 2 , 1 , 4 , 5 ] ; let N = arr . length ; document . write ( maximumDifferenceSum ( arr , N ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function countStrUtil ( dp , n , bCount , cCount ) { if ( bCount < 0 cCount < 0 ) { return 0 ; } if ( n == 0 ) { return 1 ; } if ( bCount == 0 && cCount == 0 ) { return 1 ; } if ( dp [ n ] [ bCount ] [ cCount ] != - 1 ) { return dp [ n ] [ bCount ] [ cCount ] ; } var res = countStrUtil ( dp , n - 1 , bCount , cCount ) ; res += countStrUtil ( dp , n - 1 , bCount - 1 , cCount ) ; res += countStrUtil ( dp , n - 1 , bCount , cCount - 1 ) ; return ( dp [ n ] [ bCount ] [ cCount ] = res ) ; } function countStr ( n , bCount , cCount ) { dp = Array ( n + 1 ) . fill ( 0 ) . map ( x => Array ( 2 ) . fill ( 0 ) . map ( x => Array ( 3 ) . fill ( 0 ) ) ) ; for ( i = 0 ; i < n + 1 ; i ++ ) { for ( j = 0 ; j < 2 ; j ++ ) { for ( k = 0 ; k < 3 ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } return countStrUtil ( dp , n , bCount , cCount ) ; } var n = 3 ; var bCount = 1 , cCount = 2 ; document . write ( countStr ( n , bCount , cCount ) ) ;

Index/key variables: bCount, cCount, i, j, k, n
Accumulator variables: i, j, k, res
Iterator variables: i, j, k
Javascript program:
let N = 8 ; let dx = [ 1 , 2 , 2 , 1 , - 1 , - 2 , - 2 , - 1 ] ; let dy = [ 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 ] ; function inside ( x , y ) { return ( x >= 0 && x < N && y >= 0 && y < N ) ; } function findProb ( start_x , start_y , steps ) { let dp1 = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { dp1 [ i ] = new Array ( N ) ; for ( let j = 0 ; j < N ; j ++ ) { dp1 [ i ] [ j ] = new Array ( steps + 1 ) ; for ( let k = 0 ; k < steps + 1 ; k ++ ) { dp1 [ i ] [ j ] [ k ] = 0 ; } } } for ( let i = 0 ; i < N ; ++ i ) for ( let j = 0 ; j < N ; ++ j ) dp1 [ i ] [ j ] [ 0 ] = 1 ; for ( let s = 1 ; s <= steps ; ++ s ) { for ( let x = 0 ; x < N ; ++ x ) { for ( let y = 0 ; y < N ; ++ y ) { let prob = 0.0 ; for ( let i = 0 ; i < 8 ; ++ i ) { let nx = x + dx [ i ] ; let ny = y + dy [ i ] ; if ( inside ( nx , ny ) ) prob += dp1 [ nx ] [ ny ] [ s - 1 ] / 8.0 ; } dp1 [ x ] [ y ] [ s ] = prob ; } } } return dp1 [ start_x ] [ start_y ] [ steps ] ; } let K = 3 ; document . write ( findProb ( 0 , 0 , K ) ) ;

Index/key variables: i, j, k, nx, ny, s, start_x, start_y, steps, x, y
Accumulator variables: i, j, k, prob
Iterator variables: i, j, k, s, x, y
Javascript program:
function countSubarray ( arr , n , k ) { let s = 0 ; let i = 0 ; while ( i < n ) { if ( arr [ i ] > k ) { i ++ ; continue ; } let count = 0 ; while ( i < n && arr [ i ] <= k ) { i ++ ; count ++ ; } s += parseInt ( ( count * ( count + 1 ) ) / 2 , 10 ) ; } return ( n * parseInt ( ( n + 1 ) / 2 , 10 ) - s ) ; } let arr = [ 1 , 2 , 3 ] ; let k = 2 ; let n = arr . length ; document . write ( countSubarray ( arr , n , k ) ) ;

Index/key variables: i
Accumulator variables: count, i, s
Javascript program:
function nCr ( n , k ) { let C = new Array ( n + 1 ) ; for ( let i = 0 ; i <= n ; i ++ ) { C [ i ] = new Array ( k + 1 ) ; for ( let j = 0 ; j <= k ; j ++ ) { C [ i ] [ j ] = 0 ; } } let i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( 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 resultOfAllSubsets ( arr , N ) { let result = 0.0 ; let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; for ( let n = 1 ; n <= N ; n ++ ) result += ( sum * ( nCr ( N - 1 , n - 1 ) ) ) / n ; return result ; } let arr = [ 2 , 3 , 5 , 7 ] ; let N = arr . length ; document . write ( resultOfAllSubsets ( arr , N ) ) ;

Index/key variables: i, j, k, n
Accumulator variables: i, j, n, result, sum
Iterator variables: i, j, n
Javascript program:
let arr = [ 100 , 1000 , 100 , 1000 , 1 ] ; let sum = new Array ( 10000 ) ; for ( let i = 0 ; i < 10000 ; i ++ ) { sum [ i ] = - 1 ; } function maxSumWO3Consec ( n ) { if ( sum [ n ] != - 1 ) { return sum [ n ] ; } if ( n == 0 ) { return sum [ n ] = 0 ; } if ( n == 1 ) { return sum [ n ] = arr [ 0 ] ; } if ( n == 2 ) { return sum [ n ] = arr [ 1 ] + arr [ 0 ] ; } return sum [ n ] = 500 + Math . max ( Math . max ( maxSumWO3Consec ( n - 1 ) , maxSumWO3Consec ( n - 2 ) + arr [ n ] ) , arr [ n ] + arr [ n - 1 ] + maxSumWO3Consec ( n - 3 ) ) ; } let n = arr . length - 1 ; document . write ( maxSumWO3Consec ( n ) + 1 ) ;

Index/key variables: i, n
Accumulator variables: i
Iterator variables: i
Javascript program:
function maxSumPairWithDifferenceLessThanK ( arr , N , k ) { var maxSum = 0 ; arr . sort ( ( a , b ) => a - b ) ; for ( i = N - 1 ; i > 0 ; -- i ) { if ( arr [ i ] - arr [ i - 1 ] < k ) { maxSum += arr [ i ] ; maxSum += arr [ i - 1 ] ; -- i ; } } return maxSum ; } var arr = [ 3 , 5 , 10 , 15 , 17 , 12 , 9 ] ; var N = arr . length ; var K = 4 ; document . write ( maxSumPairWithDifferenceLessThanK ( arr , N , K ) ) ;

Index/key variables: i
Accumulator variables: maxSum
Iterator variables: i
Javascript program:
function countGroups ( position , previous_sum , length , num ) { if ( position == length ) return 1 ; let res = 0 ; let sum = 0 ; for ( let i = position ; i < length ; i ++ ) { sum += ( num [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ; if ( sum >= previous_sum ) res += countGroups ( i + 1 , sum , length , num ) ; } return res ; } let num = " " ; let len = num . length ; document . write ( countGroups ( 0 , 0 , len , num ) ) ;

Index/key variables: i
Accumulator variables: i, res, sum
Iterator variables: i
Javascript program:
let MAX = 40 ; let dp = new Array ( MAX ) ; function countGroups ( position , previous_sum , length , num ) { if ( position == length ) return 1 ; if ( dp [ position ] [ previous_sum ] != - 1 ) return dp [ position ] [ previous_sum ] ; dp [ position ] [ previous_sum ] = 0 ; let res = 0 ; let sum = 0 ; for ( let i = position ; i < length ; i ++ ) { sum += ( num [ i ] - ' ' ) ; if ( sum >= previous_sum ) res += countGroups ( i + 1 , sum , length , num ) ; } dp [ position ] [ previous_sum ] = res ; return res ; } let num = " " . split ( " " ) ; let len = num . length ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 9 * MAX + 1 ) for ( let j = 0 ; j < 9 * MAX + 1 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } document . write ( countGroups ( 0 , 0 , len , num ) ) ;

Index/key variables: i, j, position, previous_sum
Accumulator variables: i, j, res, sum
Iterator variables: i, j
Javascript program:
function KnapSack ( val , wt , n , W ) { let dp = new Array ( W + 1 ) ; for ( let i = 0 ; i < W + 1 ; i ++ ) { dp [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) for ( let j = W ; j >= wt [ i ] ; j -- ) dp [ j ] = Math . max ( dp [ j ] , val [ i ] + dp [ j - wt [ i ] ] ) ; return dp [ 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 count ( a , b ) { var m = a . length ; var n = b . length ; var lookup = Array ( m + 1 ) ; for ( var i = 0 ; i < m + 1 ; i ++ ) lookup [ i ] = Array ( n + 1 ) . fill ( 0 ) ; for ( i = 0 ; i <= n ; ++ i ) lookup [ 0 ] [ i ] = 0 ; for ( i = 0 ; i <= m ; ++ i ) lookup [ i ] [ 0 ] = 1 ; for ( i = 1 ; i <= m ; i ++ ) { for ( j = 1 ; j <= n ; j ++ ) { if ( a . charAt ( i - 1 ) == b . charAt ( j - 1 ) ) lookup [ i ] [ j ] = lookup [ i - 1 ] [ j - 1 ] + lookup [ i - 1 ] [ j ] ; else lookup [ i ] [ j ] = lookup [ i - 1 ] [ j ] ; } } return lookup [ m ] [ n ] ; } var a = " " ; var b = " " ; document . write ( count ( a , b ) ) ;

Index/key variables: i, j, m, n
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function lenOfLongestGP ( set , n ) { if ( n < 2 ) { return n ; } if ( n == 2 ) { return ( set [ 1 ] % set [ 0 ] == 0 ? 2 : 1 ) ; } set . sort ( function ( a , b ) { return a - b } ) ; let L = new Array ( n ) ; for ( let i = 0 ; i < n ; ++ i ) { L [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; ++ j ) { L [ i ] [ j ] = 0 ; } } let llgp = 1 ; for ( let i = 0 ; i < n - 1 ; ++ i ) { if ( set [ n - 1 ] % set [ i ] == 0 ) { L [ i ] [ n - 1 ] = 2 ; if ( 2 > llgp ) llgp = 2 ; } else { L [ i ] [ n - 1 ] = 1 ; } } L [ n - 1 ] [ n - 1 ] = 1 ; for ( let j = n - 2 ; j >= 1 ; -- j ) { let i = j - 1 , k = j + 1 ; while ( i >= 0 && k <= n - 1 ) { if ( set [ i ] * set [ k ] < set [ j ] * set [ j ] ) { ++ k ; } else if ( set [ i ] * set [ k ] > set [ j ] * set [ j ] ) { if ( set [ j ] % set [ i ] == 0 ) { L [ i ] [ j ] = 2 ; if ( 2 > llgp ) llgp = 2 ; } else { L [ i ] [ j ] = 1 ; } -- i ; } else { if ( set [ j ] % set [ i ] == 0 ) { L [ i ] [ j ] = L [ j ] [ k ] + 1 ; if ( L [ i ] [ j ] > llgp ) { llgp = L [ i ] [ j ] ; } } else { L [ i ] [ j ] = 1 ; } -- i ; ++ k ; } } while ( i >= 0 ) { if ( set [ j ] % set [ i ] == 0 ) { L [ i ] [ j ] = 2 ; if ( 2 > llgp ) llgp = 2 ; } else { L [ i ] [ j ] = 1 ; } -- i ; } } return llgp ; } let set1 = [ 1 , 3 , 9 , 27 , 81 , 243 ] ; let n1 = set1 . length ; document . write ( lenOfLongestGP ( set1 , n1 ) + " " ) ; let set2 = [ 1 , 3 , 4 , 9 , 7 , 27 ] ; let n2 = set2 . length ; document . write ( lenOfLongestGP ( set2 , n2 ) + " " ) ; let set3 = [ 2 , 3 , 5 , 7 , 11 , 13 ] ; let n3 = set3 . length ; document . write ( lenOfLongestGP ( set3 , n3 ) + " " ) ;

Index/key variables: i, j, k
Iterator variables: i, j
Javascript program:
function lcs ( X , Y ) { let m = X . length , n = Y . length ; let L = new Array ( 2 ) ; for ( let i = 0 ; i < 2 ; i ++ ) { L [ i ] = new Array ( n + 1 ) ; for ( let j = 0 ; j < n + 1 ; j ++ ) { L [ i ] [ j ] = 0 ; } } let bi = 0 ; for ( let i = 0 ; i <= m ; i ++ ) { bi = i & 1 ; for ( let j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ bi ] [ j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) L [ bi ] [ j ] = L [ 1 - bi ] [ j - 1 ] + 1 ; else L [ bi ] [ j ] = Math . max ( L [ 1 - bi ] [ j ] , L [ bi ] [ j - 1 ] ) ; } } return L [ bi ] [ n ] ; } let X = " " ; let Y = " " ; document . write ( " " + lcs ( X , Y ) ) ;

Index/key variables: bi, i, j, n
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function subsetXOR ( arr , n , k ) { let max_ele = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) if ( arr [ i ] > max_ele ) max_ele = arr [ i ] ; let m = ( 1 << parseInt ( Math . log ( max_ele ) / Math . log ( 2 ) + 1 , 10 ) ) - 1 ; if ( k > m ) { return 0 ; } let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i <= n ; i ++ ) { dp [ i ] = new Array ( m + 1 ) ; for ( let j = 0 ; j <= m ; j ++ ) { dp [ i ] [ j ] = 0 ; } } dp [ 0 ] [ 0 ] = 1 ; for ( let i = 1 ; i <= n ; i ++ ) for ( let j = 0 ; j <= m ; j ++ ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j ^ arr [ i - 1 ] ] ; return dp [ n ] [ k ] ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let k = 4 ; let n = arr . length ; document . write ( " " + subsetXOR ( arr , n , k ) ) ;

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