dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
function check_distribution ( n , k , age , candy ) { let mxage = age [ 0 ] ; for ( let i = 0 ; i < age . length ; i ++ ) { if ( mxage < age [ i ] ) { mxage = age [ i ] ; } } let mxcandy = candy [ 0 ] ; for ( let i = 0 ; i < candy . length ; i ++ ) { if ( mxcandy < candy [ i ] ) { mxcandy = candy [ i ] ; } } let fr1 = new Array ( mxage + 1 ) ; fr1 . fill ( 0 ) ; let fr2 = new Array ( mxcandy + 1 ) ; fr2 . fill ( 0 ) ; for ( let j = 0 ; j < n ; j ++ ) { fr1 [ age [ j ] ] += 1 ; } for ( let j = 0 ; j < k ; j ++ ) { fr2 [ candy [ j ] ] += 1 ; } k = 0 ; let Tf = true ; for ( let j = 0 ; j < mxage ; j ++ ) { if ( fr1 [ j ] == 0 ) { continue ; } let flag = false ; while ( k < mxcandy ) { if ( fr1 [ j ] <= fr2 [ k ] ) { flag = true ; break ; } k += 1 ; } k = k + 1 ; if ( flag == false ) { Tf = false ; break ; } } if ( Tf ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let age = [ 5 , 15 , 10 ] ; let candy = [ 2 , 2 , 2 , 3 , 3 , 4 ] ; let n = age . length ; let k = candy . length ; check_distribution ( n , k , age , candy ) ;

Index/key variables: i, j, k
Accumulator variables: i, j, k
Iterator variables: i, j
Boolean variables: Tf, flag
Javascript program:
function checkPalindrome ( s ) { let x = reverse ( s ) ; return x == ( s ) ; } function findStringB ( A ) { let B = reverse ( A ) ; B = B + A ; if ( checkPalindrome ( A ) ) { return A ; } return B ; } 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 A = " " ; document . write ( findStringB ( A ) ) ;

Index/key variables: left, right
Accumulator variables: left, right
Iterator variables: left
Javascript program:
function minChanges ( A , n ) { var cnt = 0 ; for ( var i = 0 ; i < n - 2 ; ++ i ) { if ( ( i - 1 >= 0 ) && A [ i - 1 ] == 1 && A [ i + 1 ] == 1 && A [ i ] == 0 ) { A [ i + 1 ] = 0 ; cnt ++ ; } } return cnt ; } var A = [ 1 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 0 ] ; var n = A . length ; document . write ( minChanges ( A , n ) ) ;

Index/key variables: i
Accumulator variables: cnt
Iterator variables: i
Javascript program:
function completeSequence ( s ) { let n = s . length ; let open = 0 , close = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) open ++ ; else close ++ ; if ( close > open ) { document . write ( " " ) ; return ; } } document . write ( s ) ; for ( let i = 0 ; i < open - close ; i ++ ) document . write ( " " ) ; } let s = " " ; completeSequence ( s ) ;

Index/key variables: i
Accumulator variables: close, i, open
Iterator variables: i
Javascript program:
function smallestPermute ( n ) { var res = Array ( n + 1 ) . fill ( 0 ) ; if ( n % 2 == 0 ) { for ( var i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) res [ i ] = 48 + i + 2 ; else res [ i ] = 48 + i ; } } else { for ( var i = 0 ; i < n - 2 ; i ++ ) { if ( i % 2 == 0 ) res [ i ] = 48 + i + 2 ; else res [ i ] = 48 + i ; } res [ n - 1 ] = 48 + n - 2 ; res [ n - 2 ] = 48 + n ; res [ n - 3 ] = 48 + n - 1 ; } for ( var i = 0 ; i < res . length ; i ++ ) { res [ i ] = String . fromCharCode ( res [ i ] ) ; } return res . join ( " " ) ; } var n = 7 ; document . write ( smallestPermute ( n ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function minInsertions ( H , n , K ) { var inser = 0 ; for ( var i = 1 ; i < n ; ++ i ) { var diff = Math . abs ( H [ i ] - H [ i - 1 ] ) ; if ( diff <= K ) continue ; else inser += Math . ceil ( diff / K ) - 1 ; } return inser ; } var H = [ 2 , 4 , 8 , 16 ] ; var K = 3 ; var n = H . length ; document . write ( minInsertions ( H , n , K ) ) ;

Index/key variables: i
Accumulator variables: inser
Iterator variables: i
Javascript program:
function count_minimum_operations ( n ) { let count = 0 ; while ( n > 1 ) { if ( n % 3 == 0 ) n /= 3 ; else if ( n % 3 == 1 ) n -- ; else { if ( n == 2 ) n -- ; else n ++ ; } count ++ ; } return count ; } let n = 4 ; let ans = count_minimum_operations ( n ) ; document . write ( ans ) ;

Accumulator variables: count, n
Javascript program:
function maxSum ( a , n ) { let l = [ ] ; let s = 0 ; for ( let i = 0 ; i < n ; i ++ ) { s += Math . abs ( a [ i ] ) ; if ( a [ i ] >= 0 ) continue ; if ( i == 0 ) l . push ( i + 1 ) ; else { l . push ( i + 1 ) ; l . push ( i ) ; } } document . write ( s + " " ) ; for ( let i = 0 ; i < l . length ; i ++ ) document . write ( l [ i ] + " " ) ; } let n = 4 ; let a = [ 1 , - 2 , - 3 , 4 ] ; maxSum ( a , n ) ;

Index/key variables: i
Accumulator variables: i, s
Iterator variables: i
Javascript program:
function LengthLCP ( x , y ) { let fr = Array ( 26 ) . fill ( 0 ) ; for ( let i = 0 ; i < b ; i ++ ) { fr [ y [ i ] . charCodeAt ( ) - 97 ] += 1 ; } let c = 0 ; for ( let i = 0 ; i < a ; i ++ ) { if ( fr [ x [ i ] . charCodeAt ( ) - 97 ] > 0 ) { c += 1 ; fr [ x [ i ] . charCodeAt ( ) - 97 ] -= 1 ; } else break ; } document . write ( ( c ) ) ; } let x = " " , y = " " ; LengthLCP ( x , y ) ;

Index/key variables: i
Accumulator variables: c, i
Iterator variables: i
Javascript program:
var V = 5 ; var parent = Array ( V ) . fill ( 0 ) ; var INF = 1000000000 ; function find ( i ) { while ( parent [ i ] != i ) i = parent [ i ] ; return i ; } function union1 ( i , j ) { var a = find ( i ) ; var b = find ( j ) ; parent [ a ] = b ; } function kruskalMST ( cost ) { for ( var i = 0 ; i < V ; i ++ ) parent [ i ] = i ; var edge_count = 0 ; while ( edge_count < V - 1 ) { var min = INF , a = - 1 , b = - 1 ; for ( var i = 0 ; i < V ; i ++ ) { for ( var j = 0 ; j < V ; j ++ ) { if ( find ( i ) != find ( j ) && cost [ i ] [ j ] < min ) { min = cost [ i ] [ j ] ; a = i ; b = j ; } } } union1 ( a , b ) ; document . write ( ` ${ edge_count ++ } ${ a } ${ b } ${ min } ` ) ; mincost += min ; } document . write ( ` ${ mincost } ` ) ; } var cost = [ [ INF , 2 , INF , 6 , INF ] , [ 2 , INF , 3 , 8 , 5 ] , [ INF , 3 , INF , INF , 7 ] , [ 6 , 8 , INF , INF , 9 ] , [ INF , 5 , 7 , 9 , INF ] ] ; kruskalMST ( cost ) ;

Index/key variables: a, i, j
Accumulator variables: edge_count, i, j, mincost
Iterator variables: i, j
Javascript program:
function getSubArray ( arr , n , K ) { let i = - 1 ; let j = - 1 ; let currSum = 0 ; let result = [ i , j , Math . abs ( K - Math . abs ( currSum ) ) ] ; for ( i = 0 ; i < n ; i ++ ) { currSum = 0 ; for ( j = i ; j < n ; j ++ ) { currSum += arr [ j ] ; let currDev = Math . abs ( K - Math . abs ( currSum ) ) ; if ( currDev < result [ 2 ] ) { result [ 0 ] = i ; result [ 1 ] = j ; result [ 2 ] = currDev ; } if ( currDev == 0 ) return result ; } } return result ; } let arr = [ 15 , - 3 , 5 , 2 , 7 , 6 , 34 , - 6 ] ; let n = arr . length ; let K = 50 ; let ans = getSubArray ( arr , n , K ) ; if ( ans [ 0 ] == - 1 ) { document . write ( " " + " " ) ; } else { for ( let i = ans [ 0 ] ; i <= ans [ 1 ] ; i ++ ) document . write ( arr [ i ] + " " ) ; }

Index/key variables: i, j
Accumulator variables: currSum, i, j
Iterator variables: i, j
Javascript program:
function longestSubsequence ( a , n , q , m ) { a . sort ( function ( a , b ) { return a - b } ) ; let sum = 0 ; let b = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; let av = parseInt ( ( sum ) / ( i + 1 ) , 10 ) ; b [ i ] = ( av + 1 ) ; } b . sort ( function ( a , b ) { return a - b } ) ; for ( let i = 0 ; i < m ; i ++ ) { let k = q [ i ] ; let longest = upperBound ( b , 0 , n , k ) ; document . write ( " " + ( i + 1 ) + " " + longest + " " ) ; } } function upperBound ( a , low , high , element ) { while ( low < high ) { let middle = low + parseInt ( ( high - low ) / 2 , 10 ) ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } let a = [ 1 , 3 , 2 , 5 , 4 ] ; let n = a . length ; let q = [ 4 , 2 , 1 , 5 ] ; let m = q . length ; longestSubsequence ( a , n , q , m ) ;

Index/key variables: i, middle
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function DecreasingArray ( a , n ) { var sum = 0 , dif = 0 ; var pq = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( pq . length != 0 && pq [ pq . length - 1 ] < a [ i ] ) { dif = a [ i ] - pq [ pq . length - 1 ] ; sum += dif ; pq . pop ( ) ; } pq . push ( a [ i ] ) ; pq . sort ( ( a , b ) => b - a ) ; } return sum ; } var a = [ 3 , 1 , 2 , 1 ] ; var n = a . length ; document . write ( DecreasingArray ( a , n ) ) ;

Index/key variables: i
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function solve ( a , b , n ) { let i ; let s = 0 ; for ( i = 0 ; i < n ; i ++ ) s += ( a [ i ] + b [ i ] ) ; if ( n == 1 ) return a [ 0 ] + b [ 0 ] ; if ( s % n != 0 ) return - 1 ; let x = s / n ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] > x ) return - 1 ; if ( i > 0 ) { a [ i ] += b [ i - 1 ] ; b [ i - 1 ] = 0 ; } if ( a [ i ] == x ) continue ; let y = a [ i ] + b [ i ] ; if ( i + 1 < n ) y += b [ i + 1 ] ; if ( y == x ) { a [ i ] = y ; b [ i ] = 0 ; continue ; } if ( a [ i ] + b [ i ] == x ) { a [ i ] += b [ i ] ; b [ i ] = 0 ; continue ; } if ( i + 1 < n && a [ i ] + b [ i + 1 ] == x ) { a [ i ] += b [ i + 1 ] ; b [ i + 1 ] = 0 ; continue ; } return - 1 ; } for ( i = 0 ; i < n ; i ++ ) if ( b [ i ] != 0 ) return - 1 ; return x ; } let a = [ 6 , 14 , 21 , 1 ] ; let b = [ 15 , 7 , 10 , 10 ] ; let n = a . length ; document . write ( solve ( a , b , n ) ) ;

Index/key variables: i
Accumulator variables: i, s, y
Iterator variables: i
Javascript program:
function survival ( S , N , M ) { if ( ( ( N * 6 ) < ( M * 7 ) && S > 6 ) M > N ) document . write ( " " ) ; else { let days = ( M * S ) / N ; if ( ( ( M * S ) % N ) != 0 ) days ++ ; document . write ( " " + Math . round ( days ) ) ; } } let S = 10 , N = 16 , M = 2 ; survival ( S , N , M ) ;

Accumulator variables: days
Javascript program:
function subsequence ( s , t , n , k ) { var last = 0 , cnt = 0 , new_last = 0 , size = 0 ; for ( var ch = ' ' . charCodeAt ( 0 ) ; ch >= ' ' . charCodeAt ( 0 ) ; ch -- ) { cnt = 0 ; for ( var i = last ; i < n ; i ++ ) { if ( s [ i ] . charCodeAt ( 0 ) == ch ) cnt ++ ; } if ( cnt >= k ) { for ( var i = last ; i < n ; i ++ ) { if ( s [ i ] . charCodeAt ( 0 ) == ch ) { t [ size ++ ] = String . fromCharCode ( ch ) ; new_last = i ; } } last = new_last ; } } } var s = " " ; var n = s . length ; var k = 2 ; var t = Array ( n ) ; subsequence ( s , t , n - 1 , k ) ; document . write ( t . join ( ' ' ) ) ;

Index/key variables: i
Accumulator variables: ch, cnt, i, size
Iterator variables: ch, i
Javascript program:
function pageFaults ( pages , n , capacity ) { let s = new Set ( ) ; let indexes = new Map ( ) ; let page_faults = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s . size < capacity ) { if ( ! s . has ( pages [ i ] ) ) { s . add ( pages [ i ] ) ; page_faults ++ ; } indexes . set ( pages [ i ] , i ) ; } else { if ( ! s . has ( pages [ i ] ) ) { let lru = Number . MAX_VALUE , val = Number . MIN_VALUE ; for ( let itr of s . values ( ) ) { let temp = itr ; if ( indexes [ temp ] < lru ) { lru = indexes [ temp ] ; val = temp ; } } s . delete ( val ) ; indexes . delete ( val ) ; s . add ( pages [ i ] ) ; page_faults ++ ; } indexes . set ( pages [ i ] , i ) ; } } return page_faults ; } let pages = [ 7 , 0 , 1 , 2 , 0 , 3 , 0 , 4 , 2 , 3 , 0 , 3 , 2 ] ; let capacity = 4 ; document . write ( pageFaults ( pages , pages . length , capacity ) ) ;

Index/key variables: i, temp
Accumulator variables: i, page_faults
Iterator variables: i, itr
Javascript program:
function bestpermutation ( arr , k , n ) { let h = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { h . set ( arr [ i ] , i ) ; } if ( n <= k ) { arr . sort ( function ( a , b ) { return b - a ; } ) ; } else { for ( let j = n ; j >= 1 ; j -- ) { if ( k > 0 ) { let initial_index = h [ j ] ; let best_index = n - j ; if ( initial_index != best_index ) { h . set ( j , best_index ) ; let element = arr . get ( best_index ) ; h . set ( element , initial_index ) ; let temp = arr [ best_index ] ; arr . set ( best_index , arr [ initial_index ] ) ; arr . set ( initial_index , temp ) ; k -- ; } } } } } let arr = [ 3 , 1 , 4 , 2 , 5 ] ; let k = 10 ; let n = arr . length ; bestpermutation ( arr , k , n ) ; document . write ( " " + k + " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;

Index/key variables: best_index, i, initial_index, j
Accumulator variables: i, j, k
Iterator variables: i, j
Javascript program:
function bestFit ( weight , n , c ) { var res = 0 ; var bin_rem = Array ( n ) . fill ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { var j ; var min = c + 1 , bi = 0 ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] && bin_rem [ j ] - weight [ i ] < min ) { bi = j ; min = bin_rem [ j ] - weight [ i ] ; } } if ( min == c + 1 ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; bin_rem [ bi ] -= weight [ i ] ; } return res ; } var weight = [ 2 , 5 , 4 , 7 , 1 , 3 , 8 ] ; var c = 10 ; var n = weight . length ; document . write ( " " + bestFit ( weight , n , c ) ) ;

Index/key variables: bi, i, j, res
Accumulator variables: i, j, res
Iterator variables: i, j
Javascript program:
function worstFit ( weight , n , c ) { var res = 0 ; var bin_rem = Array ( n ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { var j ; var mx = - 1 , wi = 0 ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] && bin_rem [ j ] - weight [ i ] > mx ) { wi = j ; mx = bin_rem [ j ] - weight [ i ] ; } } if ( mx == - 1 ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } bin_rem [ wi ] -= weight [ i ] ; } return res ; } var weight = [ 2 , 5 , 4 , 7 , 1 , 3 , 8 ] ; var c = 10 ; var n = weight . length ; document . write ( " " + worstFit ( weight , n , c ) ) ;

Index/key variables: i, j, res, wi
Accumulator variables: i, j, res
Iterator variables: i, j
Javascript program:
function getMax ( arr , n ) { let result = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) if ( arr [ i ] > result ) result = arr [ i ] ; return result ; } function isPossible ( time , K , job , n ) { let cnt = 1 ; let curr_time = 0 ; for ( let i = 0 ; i < n ; ) { if ( curr_time + job [ i ] > time ) { curr_time = 0 ; cnt ++ ; } else { curr_time += job [ i ] ; i ++ ; } } return ( cnt <= K ) ; } function findMinTime ( K , T , job , n ) { let end = 0 , start = 0 ; for ( let i = 0 ; i < n ; ++ i ) end += job [ i ] ; let ans = end ; let job_max = getMax ( job , n ) ; while ( start <= end ) { let mid = parseInt ( ( start + end ) / 2 , 10 ) ; if ( mid >= job_max && isPossible ( mid , K , job , n ) ) { ans = Math . min ( ans , mid ) ; end = mid - 1 ; } else start = mid + 1 ; } return ( ans * T ) ; } let job = [ 10 , 7 , 8 , 12 , 6 , 8 ] ; let n = job . length ; let k = 4 , T = 5 ; document . write ( findMinTime ( k , T , job , n ) ) ;

Index/key variables: i
Accumulator variables: cnt, curr_time, end, i
Iterator variables: i
Javascript program:
function LongestOddEvenSubarray ( A , N ) { let dp = new Array ( N ) ; dp [ 0 ] = 1 ; let ans = 1 ; for ( let i = 1 ; i < N ; i ++ ) { if ( ( A [ i ] % 2 == 0 && A [ i - 1 ] % 2 == 0 ) || ( A [ i ] % 2 != 0 && A [ i - 1 ] % 2 != 0 ) ) { dp [ i ] = dp [ i - 1 ] + 1 ; } else dp [ i ] = 1 ; } for ( let i = 0 ; i < N ; i ++ ) ans = Math . max ( ans , dp [ i ] ) ; return ans ; } let A = [ 2 , 5 , 7 , 2 , 4 , 6 , 8 , 3 ] ; let N = A . length ; document . write ( LongestOddEvenSubarray ( A , N ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function totalSubarrays ( arr , n , k ) { let ans = 0 , i = 0 ; while ( i < n ) { if ( arr [ i ] > k ) { i ++ ; continue ; } let count = 0 ; while ( i < n && arr [ i ] <= k ) { i ++ ; count ++ ; } ans += ( Math . floor ( ( count * ( count + 1 ) ) / 2 ) ) ; } return ans ; } function countSubarrays ( arr , n , k ) { let count1 = totalSubarrays ( arr , n , k - 1 ) ; let count2 = totalSubarrays ( arr , n , k ) ; let ans = count2 - count1 ; return ans ; } let n = 4 , k = 3 ; let arr = [ 2 , 1 , 3 , 4 ] ; document . write ( countSubarrays ( arr , n , k ) ) ;

Index/key variables: i
Accumulator variables: ans, count, i
Javascript program:
let dp = new Array ( 100 ) ; for ( let i = 0 ; i < 100 ; i ++ ) { dp [ i ] = new Array ( 1 << 10 ) . fill ( - 1 ) ; } function countOfNumbers ( digit , mask , N ) { if ( digit == N + 1 ) { if ( mask == ( 1 << 10 ) - 1 ) return 1 ; return 0 ; } let val = dp [ digit ] [ mask ] ; if ( val != - 1 ) return val ; val = 0 ; if ( digit == 1 ) { for ( let i = ( N == 1 ? 0 : 1 ) ; i <= 9 ; ++ i ) { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , N ) ; } } else { for ( let i = 0 ; i <= 9 ; ++ i ) { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , N ) ; } } return val ; } let N = 10 ; document . write ( countOfNumbers ( 1 , 0 , N ) ) ;

Index/key variables: digit, i, mask
Accumulator variables: i, val
Iterator variables: i
Javascript program:
function maxSumWO3Consec ( A , N ) { if ( N == 1 ) return A [ 0 ] ; if ( N == 2 ) return A [ 0 ] + A [ 1 ] ; let third = A [ 0 ] ; let second = third + A [ 1 ] ; let first = Math . max ( second , A [ 1 ] + A [ 2 ] ) ; let sum = Math . max ( Math . max ( third , second ) , first ) ; for ( let i = 3 ; i < N ; i ++ ) { sum = Math . max ( Math . max ( first , second + A [ i ] ) , third + A [ i ] + A [ i - 1 ] ) ; third = second ; second = first ; first = sum ; } return sum ; } let A = [ 3000 , 2000 , 1000 , 3 , 10 ] ; let N = A . length ; document . write ( maxSumWO3Consec ( A , N ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function findMinOperations ( n ) { let i = 0 ; let dp = new Array ( n + 1 ) ; for ( i = 0 ; i < n + 1 ; i ++ ) { dp [ i ] = 999999 ; } dp [ 2 ] = 0 ; for ( i = 2 ; i < n + 1 ; i ++ ) { if ( i * 5 <= n ) { dp [ i * 5 ] = Math . min ( dp [ i * 5 ] , dp [ i ] + 1 ) ; } if ( i + 3 <= n ) { dp [ i + 3 ] = Math . min ( dp [ i + 3 ] , dp [ i ] + 1 ) ; } } return dp [ n ] ; } let n = 28 ; let m = findMinOperations ( n ) ; if ( m != 9999 ) document . write ( m ) ; else document . write ( - 1 ) ;

Index/key variables: i, n
Accumulator variables: i
Iterator variables: i
Javascript program:
function countSortedArrays ( n , m ) { let dp = new Array ( m + 1 ) ; dp . fill ( 0 ) ; dp [ 0 ] = 1 ; for ( let i = 1 ; i <= n ; i ++ ) { for ( let j = 1 ; j <= m ; j ++ ) { dp [ j ] = dp [ j - 1 ] + dp [ j ] ; } } return dp [ m ] ; } let n = 2 , m = 3 ; document . write ( countSortedArrays ( n , m ) ) ;

Index/key variables: j, m
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function getValue ( arr , N ) { let dp = Array . from ( { length : N } , ( _ , i ) => 0 ) ; dp [ 0 ] = 0 ; for ( let i = 1 ; i < N ; i ++ ) { let min = arr [ i ] ; let max = arr [ i ] ; for ( let j = i ; j >= 0 ; j -- ) { min = Math . min ( arr [ j ] , min ) ; max = Math . max ( arr [ j ] , max ) ; dp [ i ] = Math . max ( dp [ i ] , max - min + ( ( j >= 1 ) ? dp [ j - 1 ] : 0 ) ) ; } } return dp [ N - 1 ] ; } let arr = [ 8 , 1 , 7 , 9 , 2 ] ; let N = arr . length ; document . write ( getValue ( arr , N ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function count0 ( s ) { let count = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) { count ++ ; } } return count ; } function solve ( vec , A , B , idx ) { if ( idx == vec . length A + B == 0 ) { return 0 ; } let zero = count0 ( vec [ idx ] ) ; let one = vec [ idx ] . length - zero ; let inc = 0 ; if ( zero <= A && one <= B ) { inc = 1 + solve ( vec , A - zero , B - one , idx + 1 ) ; } let exc = solve ( vec , A , B , idx + 1 ) ; return Math . max ( inc , exc ) ; } function MaxSubsetlength ( arr , A , B ) { return solve ( arr , A , B , 0 ) ; } let arr = [ " " , " " , " " ] ; let A = 1 , B = 1 ; document . write ( MaxSubsetlength ( arr , A , B ) ) ;

Index/key variables: i, idx
Accumulator variables: count, i
Iterator variables: i
Javascript program:
function isUnique ( s ) { var set = new Set ( ) ; for ( let i = 0 ; i < s . length ; i ++ ) { set . add ( s [ i ] ) ; } return set . size == 1 ; } function maxScore ( s , a ) { let n = s . length ; if ( n == 0 ) return 0 ; if ( n == 1 ) return a [ 0 ] ; let mx = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i ; j < n ; j ++ ) { let sub = s . substring ( i , j + 1 ) ; if ( isUnique ( sub ) ) mx = Math . max ( mx , a [ sub . length - 1 ] + maxScore ( s . substring ( 0 , i ) + s . substring ( j + 1 ) , a ) ) ; } } return mx ; } let s = " " ; let a = [ 1 , 3 , 1 ] ; document . write ( maxScore ( s , a ) ) ;

Index/key variables: i
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
let dp = new Map ( ) ; function maxScore ( s , a ) { if ( dp . has ( s ) ) return dp . get ( s ) ; let n = s . length ; if ( n == 0 ) return 0 ; if ( n == 1 ) return a [ 0 ] ; let head = 0 ; let mx = - 1 ; while ( head < n ) { let tail = head ; while ( tail < n ) { if ( s [ tail ] != s [ head ] ) { head = tail ; break ; } let sub = s . substring ( head , head + tail + 1 ) ; mx = Math . max ( mx , a [ sub . length - 1 ] + maxScore ( s . substring ( 0 , head ) + s . substring ( tail + 1 , tail + 1 + s . length ) , a ) ) ; tail += 1 ; } if ( tail == n ) break ; } dp . set ( s , mx ) ; return mx ; } let s = " " ; let a = [ 1 , 3 , 1 ] ; document . write ( ( maxScore ( s , a ) ) - 1 ) ;

Index/key variables: head, tail
Accumulator variables: tail
Javascript program:
let size = 100 ; let ans = new Array ( size ) ; function initialize ( ) { for ( let i = 0 ; i < size ; i ++ ) { ans [ i ] = new Array ( size ) ; for ( let j = 0 ; j < size ; j ++ ) { ans [ i ] [ j ] = 0 ; } } } function numberOfUniqueOutcomes ( n , s ) { if ( s < n ) ans [ n ] [ s ] = 0 ; else if ( n == 1 n == s ) ans [ n ] [ s ] = 1 ; else if ( ans [ n ] [ s ] == 0 ) { ans [ n ] [ s ] = numberOfUniqueOutcomes ( n - 1 , s - 1 ) + numberOfUniqueOutcomes ( n - 1 , s - 2 ) ; } return ans [ n ] [ s ] ; } initialize ( ) ; let N = 5 , S = 8 ; document . write ( numberOfUniqueOutcomes ( N , S ) ) ;

Index/key variables: i, j, n, s
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function printMinimumRemovals ( str , X ) { let N = str . length ; let M = X . length ; let dp = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { dp [ i ] = new Array ( M ) ; for ( let j = 0 ; j < M ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let j = 0 ; j < M ; j ++ ) { if ( str [ 0 ] == X [ j ] ) { dp [ 0 ] [ j ] = 1 ; } } for ( let i = 1 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( str [ i ] == X [ j ] ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + 1 ; if ( j != 0 ) dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - 1 ] ) ; } else { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } } } document . write ( dp [ N - 1 ] [ M - 1 ] ) ; } let str = " " ; let X = " " ; printMinimumRemovals ( str , X ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function maxSubarraySum ( sum , k , row ) { let curSum = 0 , curMax = Number . MIN_VALUE ; let sumSet = new Set ( ) ; sumSet . add ( 0 ) ; for ( let r = 0 ; r < row ; ++ r ) { curSum += sum [ r ] ; let list = [ ] ; list = Array . from ( sumSet ) ; let it = list . lastIndexOf ( curSum - k ) ; if ( it > - 1 ) { curMax = Math . max ( curMax , curSum - it ) ; } sumSet . add ( curSum ) ; } return curMax ; } function maxSumSubmatrix ( matrix , k ) { let row = matrix . length ; let col = matrix [ 0 ] . length ; let ret = Number . MIN_VALUE ; for ( let i = 0 ; i < col ; ++ i ) { let sum = new Array ( row ) ; for ( let j = 0 ; j < row ; j ++ ) sum [ j ] = 0 ; for ( let j = i ; j < col ; ++ j ) { for ( let r = 0 ; r < row ; ++ r ) { sum [ r ] += matrix [ r ] [ j ] ; } let curMax = maxSubarraySum ( sum , k , row ) ; ret = Math . max ( ret , curMax ) ; } } document . write ( ret ) ; } let matrix = [ [ 1 , 0 , 1 ] , [ 0 , - 2 , 3 ] ] ; let K = 2 ; maxSumSubmatrix ( matrix , K )

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