dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
class Node { constructor ( data ) { this . data = data ; this . left = this . right = null ; this . children = 0 ; } } function getElements ( root ) { if ( root == null ) return 0 ; return getElements ( root . left ) + getElements ( root . right ) + 1 ; } function insertChildrenCount ( root ) { if ( root == null ) return null ; root . children = getElements ( root ) - 1 ; root . left = insertChildrenCount ( root . left ) ; root . right = insertChildrenCount ( root . right ) ; return root ; } function children ( root ) { if ( root == null ) return 0 ; return root . children + 1 ; } function randomNodeUtil ( root , count ) { if ( root == null ) return 0 ; if ( count == children ( root . left ) ) return root . data ; if ( count < children ( root . left ) ) return randomNodeUtil ( root . left , count ) ; return randomNodeUtil ( root . right , count - children ( root . left ) - 1 ) ; } function randomNode ( root ) { let count = Math . floor ( Math . random ( ) * ( root . children + 1 ) ) ; return randomNodeUtil ( root , count ) ; } let root = new Node ( 10 ) ; root . left = new Node ( 20 ) ; root . right = new Node ( 30 ) ; root . left . right = new Node ( 40 ) ; root . left . right = new Node ( 50 ) ; root . right . left = new Node ( 60 ) ; root . right . right = new Node ( 70 ) ; insertChildrenCount ( root ) ; document . write ( " " + randomNode ( root ) + " " ) ;

Class/struct names: Node
Javascript program:
class INT { constructor ( d ) { this . data = d ; } } function findPostOrderUtil ( pre , n , minval , maxval , preIndex ) { if ( preIndex . data == n ) return ; if ( pre [ preIndex . data ] < minval pre [ preIndex . data ] > maxval ) { return ; } let val = pre [ preIndex . data ] ; preIndex . data ++ ; findPostOrderUtil ( pre , n , minval , val , preIndex ) ; findPostOrderUtil ( pre , n , val , maxval , preIndex ) ; document . write ( val + " " ) ; } function findPostOrder ( pre , n ) { let preIndex = new INT ( 0 ) ; findPostOrderUtil ( pre , n , Number . MIN_VALUE , Number . MAX_VALUE , preIndex ) ; } let pre = [ 40 , 30 , 35 , 80 , 100 ] ; let n = pre . length ; findPostOrder ( pre , n ) ;

Accumulator variables: data
Class/struct names: INT
Javascript program:
function fillDepth ( parent , i , depth ) { if ( depth [ i ] != 0 ) { return ; } if ( parent [ i ] == - 1 ) { depth [ i ] = 1 ; return ; } if ( depth [ parent [ i ] ] == 0 ) { fillDepth ( parent , parent [ i ] , depth ) ; } depth [ i ] = depth [ parent [ i ] ] + 1 ; } function findHeight ( parent , n ) { var depth = Array ( n ) . fill ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { depth [ i ] = 0 ; } for ( i = 0 ; i < n ; i ++ ) { fillDepth ( parent , i , depth ) ; } var ht = depth [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) { if ( ht < depth [ i ] ) { ht = depth [ i ] ; } } return ht ; } var parent = [ - 1 , 0 , 0 , 1 , 1 , 3 , 5 ] ; var n = parent . length ; document . write ( " " + findHeight ( parent , n ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function findpath ( N , a ) { if ( a [ 0 ] ) { document . write ( N + 1 ) ; for ( let i = 1 ; i <= N ; i ++ ) document . write ( i ) ; return ; } for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( ! a [ i ] && a [ i + 1 ] ) { for ( let j = 1 ; j <= i ; j ++ ) document . write ( j + " " ) ; document . write ( N + 1 + " " ) ; for ( let j = i + 1 ; j <= N ; j ++ ) document . write ( j + " " ) ; return ; } } for ( let i = 1 ; i <= N ; i ++ ) document . write ( i + " " ) ; document . write ( N + 1 + " " ) ; } let N = 3 , arr = [ 0 , 1 , 0 ] ; findpath ( N , arr ) ;

Index/key variables: i
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function balBracketSequence ( str ) { let n = str . length ; if ( str [ 0 ] == str [ n - 1 ] ) { document . write ( " " ) ; } else { let cntForOpen = 0 , cntForClose = 0 ; let check = 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) cntForOpen ++ ; else if ( str [ i ] == str [ n - 1 ] ) cntForOpen -- ; else cntForOpen ++ ; if ( cntForOpen < 0 ) { check = 0 ; break ; } } if ( check && cntForOpen == 0 ) { document . write ( " " ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ n - 1 ] ) document . write ( ' ' ) ; else document . write ( ' ' ) ; } return ; } else { for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) cntForClose ++ ; else cntForClose -- ; if ( cntForClose < 0 ) { check = 0 ; break ; } } if ( check && cntForClose == 0 ) { document . write ( " " ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) document . write ( ' ' ) ; else document . write ( ' ' ) ; } return ; } } document . write ( " " ) ; } } let str = " " ; balBracketSequence ( str ) ;

Index/key variables: i
Accumulator variables: cntForClose, cntForOpen, i
Iterator variables: i
Javascript program:
function getPostOrderBST ( pre , N ) { var pivotPoint = 0 ; for ( var i = 1 ; i < N ; i ++ ) { if ( pre [ 0 ] <= pre [ i ] ) { pivotPoint = i ; break ; } } for ( var i = pivotPoint - 1 ; i > 0 ; i -- ) { document . write ( pre [ i ] + " " ) ; } for ( var i = N - 1 ; i >= pivotPoint ; i -- ) { document . write ( pre [ i ] + " " ) ; } document . write ( pre [ 0 ] ) ; }

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function countSubstrings ( s ) { let ans = 0 ; let subs = 1 ; let pre = ' ' ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( pre == s [ i ] ) { subs += 1 ; } else { subs = 1 ; } ans += subs ; pre = s [ i ] ; } document . write ( ans ) ; } / *	Driver Code / let s = " " ; countSubstrings ( s ) ;

Index/key variables: i
Accumulator variables: ans, i, subs
Iterator variables: i
Javascript program:
function constructTree ( n , d , h ) { if ( d == 1 ) { if ( n == 2 && h == 1 ) { document . write ( " " , " " ) ; return ; } document . write ( " " , " " ) ; return ; } if ( d > 2 * h ) { document . write ( " " , " " ) ; return ; } for ( var i = 1 ; i <= h ; i ++ ) document . write ( i + " " + ( i + 1 ) , " " ) ; if ( d > h ) { document . write ( " " + " " + ( h + 2 ) , " " ) ; for ( var i = h + 2 ; i <= d ; i ++ ) { document . write ( i + " " + ( i + 1 ) , " " ) ; } } for ( var i = d + 1 ; i < n ; i ++ ) { var k = 1 ; if ( d == h ) k = 2 ; document . write ( k + " " + ( i + 1 ) , " " ) ; } } var n = 5 , d = 3 , h = 2 ; constructTree ( n , d , h ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function longestsubarray ( a , N ) { var maxlength = 0 , sum = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( a [ i ] == 1 ) { sum ++ ; } else { sum = 0 ; } maxlength = Math . max ( maxlength , sum ) ; } return maxlength ; } function solveQueries ( arr , n , Q , k ) { var cntQuery = Q . length ; for ( var i = 0 ; i < cntQuery ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { document . write ( longestsubarray ( arr , n ) + " " ) ; } else { arr [ Q [ i ] [ 1 ] - 1 ] ^= 1 ; } } } var N = 10 ; var arr = [ 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 ] ; var Q = [ [ 1 ] , [ 2 , 3 ] , [ 1 ] ] ; var K = 3 ; solveQueries ( arr , N , Q , K ) ;

Index/key variables: i
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function uniqueElements ( arr , start , K , mp ) { let st = new Set ( ) ; for ( let i = 0 ; i < K ; i ++ ) st . add ( arr [ start + i ] ) ; for ( let itr of st ) { if ( mp . has ( itr ) ) { mp . set ( itr , mp . get ( itr ) + 1 ) ; } else { mp . set ( itr , 1 ) ; } } } function checkAnswer ( mp , N , K ) { for ( let i = 1 ; i <= N ; i ++ ) { if ( mp . has ( i ) ) if ( mp . get ( i ) == ( N - K + 1 ) ) { document . write ( i + " " ) ; return ; } } document . write ( - 1 + " " ) ; } function smallestPresentNumber ( arr , N , K ) { let mp = new Map ( ) ; for ( let i = 0 ; i <= N - K ; i ++ ) { uniqueElements ( arr , i , K , mp ) ; } checkAnswer ( mp , N , K ) ; } function generateK ( arr , N ) { for ( let k = 1 ; k <= N ; k ++ ) smallestPresentNumber ( arr , N , k ) ; } let arr = [ 2 , 3 , 5 , 3 , 2 , 3 , 1 , 3 , 2 , 7 ] ; let N = arr . length ; generateK ( arr , N ) ;

Accumulator variables: i, k
Iterator variables: i, itr, k
Javascript program:
function checkString ( X , Y ) { var L = - 1 ; var R = - 1 ; for ( var i = 0 ; i < X . length ; ++ i ) { if ( X [ i ] !== Y [ i ] ) { L = i ; break ; } } for ( var i = X . length - 1 ; i > 0 ; -- i ) { if ( X [ i ] !== Y [ i ] ) { R = i ; break ; } } X = X . substring ( 0 , L ) + reverse ( X . substring ( L , R + 1 ) ) + X . substring ( R + 1 ) ; if ( X === Y ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } function reverse ( input ) { var a = input . split ( " " ) ; var l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { var temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return a . join ( " " ) ; } var X = " " , Y = " " ; checkString ( X , Y ) ;

Index/key variables: i, l, r
Accumulator variables: l, r
Iterator variables: i, l
Javascript program:
var seg ; function maxLength ( a , b , n , c ) { if ( n == 0 ) return 0 ; var max_length = 0 ; var low = 0 , high = n ; while ( low <= high ) { var mid = low + parseInt ( ( high - low ) / 2 ) ; if ( possible ( a , b , n , c , mid ) ) { max_length = mid ; low = mid + 1 ; } else high = mid - 1 ; } return max_length ; } function possible ( a , b , n , c , k ) { var sum = 0 ; for ( i = 0 ; i < k ; i ++ ) { sum += a [ i ] ; } var total_cost = sum * k + getMax ( b , 0 , n - 1 , 0 , k - 1 , 0 ) ; if ( total_cost <= c ) return true ; for ( i = k ; i < n ; i ++ ) { sum += a [ i ] ; sum -= a [ i - k ] ; total_cost = sum * k + getMax ( b , 0 , n - 1 , i - k + 1 , i , 0 ) ; if ( total_cost <= c ) return true ; } return false ; } function build ( b , index , s , e ) { if ( s == e ) { seg [ index ] = b [ s ] ; return ; } var mid = s + parseInt ( ( e - s ) / 2 ) ; build ( b , 2 * index + 1 , s , mid ) ; build ( b , 2 * index + 2 , mid + 1 , e ) ; seg [ index ] = Math . max ( seg [ 2 * index + 1 ] , seg [ 2 * index + 2 ] ) ; } function getMax ( b , ss , se , qs , qe , index ) { if ( se < qs ss > qe ) return parseInt ( Number . MIN_VALUE / 2 ) ; if ( ss >= qs && se <= qe ) return seg [ index ] ; var mid = ss + ( se - ss ) / 2 ; return Math . max ( getMax ( b , ss , mid , qs , qe , 2 * index + 1 ) , getMax ( b , mid + 1 , se , qs , qe , 2 * index + 2 ) ) ; } function initialiseSegmentTree ( N ) { seg = Array ( 4 * N ) . fill ( 0 ) ; } var A = [ 1 , 2 , 1 , 6 , 5 , 5 , 6 , 1 ] ; var B = [ 14 , 8 , 15 , 15 , 9 , 10 , 7 , 12 ] ; var C = 40 ; var N = A . length ; initialiseSegmentTree ( N ) ; build ( B , 0 , 0 , N - 1 ) ; document . write ( maxLength ( A , B , N , C ) ) ;

Index/key variables: i, index, s
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function maxLength ( a , b , n , c ) { if ( n == 0 ) return 0 ; let max_length = 0 ; let low = 0 , high = n ; while ( low <= high ) { let mid = low + parseInt ( ( high - low ) / 2 , 10 ) ; if ( possible ( a , b , n , c , mid ) ) { max_length = mid ; low = mid + 1 ; } else high = mid - 1 ; } return max_length ; } function possible ( a , b , n , c , k ) { let dq = [ ] ; let sum = 0 ; for ( let i = 0 ; i < k ; i ++ ) { sum += a [ i ] ; while ( dq . length > 0 && b [ i ] > b [ dq [ dq . length - 1 ] ] ) dq . pop ( ) ; dq . push ( i ) ; } let total_cost = sum * k + b [ dq [ 0 ] ] ; if ( total_cost <= c ) return true ; for ( let i = k ; i < n ; i ++ ) { sum += a [ i ] ; sum -= a [ i - k ] ; while ( dq . length > 0 && dq [ 0 ] <= i - k ) dq . pop ( ) ; while ( dq . length > 0 && b [ i ] > b [ dq [ dq . length - 1 ] ] ) dq . pop ( ) ; dq . push ( i ) ; total_cost = sum * k + b [ dq [ 0 ] ] ; if ( total_cost <= c ) return true ; } return false ; } let A = [ 1 , 2 , 1 , 6 , 5 , 5 , 6 , 1 ] ; let B = [ 14 , 8 , 15 , 15 , 9 , 10 , 7 , 12 ] ; let N = A . length ; let C = 40 ; document . write ( maxLength ( A , B , N , C ) ) ;

Index/key variables: i
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function longestSubWithMaxSum ( arr , N ) { let Max = Math . max ( ... arr ) ; if ( Max < 0 ) { document . write ( Max ) ; return ; } for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= 0 ) { document . write ( arr [ i ] + " " ) ; } } } let arr = [ 1 , 2 , - 4 , - 2 , 3 , 0 ] ; let N = arr . length ; longestSubWithMaxSum ( arr , N ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function digiSum ( a ) { let sum = 0 ; while ( a ) { sum += a % 10 ; a = Math . floor ( a / 10 ) ; } return sum ; } function isPrime ( r ) { let s = true ; for ( let i = 2 ; i * i <= r ; i ++ ) { if ( r % i == 0 ) { s = false ; break ; } } return s ; } function isMorannumber ( n ) { let dup = n ; let sum = digiSum ( dup ) ; if ( n % sum == 0 ) { let c = n / sum ; if ( isPrime ( c ) ) { return true ; } } return false ; } function FirstKMorannumber ( a , n , k ) { let X = k ; a = a . sort ( ( x , y ) => x - y ) let s = new Set ( ) ; for ( let i = n - 1 ; i >= 0 && k > 0 ; i -- ) { if ( isMorannumber ( a [ i ] ) ) { s . add ( a [ i ] ) ; k -- ; } } if ( k > 0 ) { document . write ( X + " " + " " + " " ) ; return ; } s = [ ... s ] . sort ( ( a , b ) => a - b ) for ( let it of s ) { document . write ( it + " " ) ; } document . write ( " " ) ; } let A = [ 34 , 198 , 21 , 42 , 63 , 45 , 22 , 44 , 43 ] ; let K = 4 ; let N = A . length ; FirstKMorannumber ( A , N , K ) ;

Index/key variables: i
Accumulator variables: i, k, sum
Iterator variables: i, it
Boolean variables: s
Javascript program:
function countPair ( arr , n ) { var m = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( m . has ( arr [ i ] ) ) m . set ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) else m . set ( arr [ i ] , 1 ) } var ans = 0 ; for ( var i = 0 ; i < 31 ; i ++ ) { var key = Math . pow ( 2 , i ) ; for ( var j = 0 ; j < n ; j ++ ) { var k = key - arr [ j ] ; if ( ! m . has ( k ) ) continue ; else ans += m . get ( k ) ; if ( k == arr [ j ] ) ans ++ ; } } return ans / 2 ; } var arr = [ 1 , 8 , 2 , 10 , 6 ] var n = arr . length document . write ( countPair ( arr , n ) ) ;

Index/key variables: i, j
Accumulator variables: ans, i, j
Iterator variables: i, j
Javascript program:
function isdivisible ( newstr , K ) { var num = 0 ; for ( var i = 0 ; i < newstr . length ; i ++ ) { num = num * 10 + newstr [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; } if ( num % K == 0 ) return true ; else return false ; } function findLargestNo ( str , newstr , ans , index , K ) { if ( index == str . length ) { if ( isdivisible ( newstr , K ) ) { if ( ( ans < newstr && ans . length == newstr . length ) newstr . length > ans . length ) { ans = newstr ; } } return ans ; } var x = newstr + str [ index ] ; ans = findLargestNo ( str , x , ans , index + 1 , K ) ; ans = findLargestNo ( str , newstr , ans , index + 1 , K ) ; return ans ; } var str = " " ; var ans = " " , newstr = " " ; var K = 8 ; ans = findLargestNo ( str , newstr , ans , 0 , K ) ; if ( ans != " " ) document . write ( ans ) else document . write ( - 1 )

Index/key variables: i, index
Accumulator variables: i
Iterator variables: i
Javascript program:
function findRockSample ( ranges , n , r , arr ) { let a = [ ] ; for ( let i = 0 ; i < r ; i ++ ) { let c = 0 ; let l = ranges [ i ] [ 0 ] ; let h = ranges [ i ] [ 1 ] ; for ( let j = 0 ; j < arr . length ; j ++ ) { if ( l <= arr [ j ] && arr [ j ] <= h ) c += 1 ; } a . push ( c ) ; } for ( let i = 0 ; i < a . length ; i ++ ) { document . write ( a [ i ] + " " ) ; } } let n = 5 ; let r = 2 ; let arr = [ 400 , 567 , 890 , 765 , 987 ] ; let ranges = [ [ 300 , 380 ] , [ 800 , 1000 ] ] ; findRockSample ( ranges , n , r , arr ) ;

Index/key variables: i, j
Accumulator variables: c, i, j
Iterator variables: i, j
Javascript program:
function findNumbers ( st , result , prev , n , K ) { if ( st . length == n ) { result . push ( parseInt ( st ) ) ; return ; } if ( st . length == n - 1 ) { if ( prev - K >= 0 ) { let pt = " " ; pt += String . fromCharCode ( prev - K + 48 ) ; findNumbers ( st + pt , result , prev - K , n , K ) ; } if ( K != 0 && prev + K < 10 ) { let pt = " " ; pt += String . fromCharCode ( prev + K + 48 ) ; findNumbers ( st + pt , result , prev + K , n , K ) ; } } else { for ( let j = 1 ; j <= 9 ; j ++ ) { let pt = " " ; pt += String . fromCharCode ( j + 48 ) ; findNumbers ( st + pt , result , prev , n , K ) ; } } } function numDifference ( N , K ) { let res = [ ] ; let st = " " ; if ( N == 1 && K == 0 ) { res . push ( 0 ) ; } else if ( N == 1 && K > 0 ) { return res ; } for ( let i = 1 ; i < 10 ; i ++ ) { let temp = " " ; temp += String . fromCharCode ( 48 + i ) ; findNumbers ( st + temp , res , i , N , K ) ; st = " " ; } return res ; } function numDifferenceUtil ( N , K ) { let res = [ ] ; res = numDifference ( N , K ) ; for ( let i = 0 ; i < res . length ; i ++ ) { document . write ( res [ i ] + " " ) ; } } let N = 2 , K = 9 ; numDifferenceUtil ( N , K ) ;

Index/key variables: i
Accumulator variables: i, j, pt, temp
Iterator variables: i, j
Javascript program:
function countMaximum ( a , n ) { a . sort ( ) ; a . reverse ( ) ; let count = 0 ; let mark = 0 ; for ( let i = 0 ; i < n ; ++ i ) { if ( ( a [ i ] + n >= mark ) ) { count += 1 ; } mark = Math . max ( mark , a [ i ] + i + 1 ) ; } document . write ( count ) ; } let arr = [ 8 , 9 , 6 ] ; let N = arr . length ; countMaximum ( arr , N ) ;

Index/key variables: i
Accumulator variables: count
Iterator variables: i
Javascript program:
function reverse ( input ) { let a = input . split ( ' ' ) ; let l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { let temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return a . join ( " " ) ; } function findKthChar ( n , k ) { let prev = " " ; let cur = " " ; if ( n == 1 ) { return ' ' ; } for ( let j = 2 ; j <= n ; j ++ ) { cur = prev + " " ; for ( let i = 0 ; i < prev . length ; i ++ ) { if ( prev [ i ] == ' ' ) { prev [ prev [ i ] ] = ' ' ; } else { prev [ prev [ i ] ] = ' ' ; } } prev = reverse ( prev ) ; cur += prev ; prev = cur ; } return cur [ k ] ; } let N = 4 ; let K = 3 ; document . write ( findKthChar ( N , K ) ) ;

Index/key variables: i, k, l, r
Accumulator variables: cur, i, j, l, r
Iterator variables: i, j, l
Javascript program:
function maxSubarray ( a , k , n ) { var answer = 0 ; var start = 0 ; var s = 0 ; var dq = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { var x = a [ i ] ; while ( dq . length != 0 && a [ dq [ 0 ] ] <= x ) dq . shift ( ) ; dq . push ( i ) ; s += x ; var cost = a [ dq [ 0 ] ] * ( answer + 1 ) - s ; if ( cost <= k ) answer ++ ; else { if ( dq [ 0 ] == start ) dq . shift ( ) ; s -= a [ start ++ ] ; } } return answer ; } var a = [ 2 , 2 , 4 ] ; var k = 10 ; var n = a . length ; document . write ( maxSubarray ( a , k , n ) ) ;

Index/key variables: i
Accumulator variables: answer, i, s, start
Iterator variables: i
Javascript program:
function canSplitArray ( arr , n , k ) { for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] <= k ) { return " " ; } } return " " ; } let arr = [ 2 , 4 , 6 , 1 , 8 , 16 ] ; let N = arr . length ; let K = 3 ; document . write ( canSplitArray ( arr , N , K ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function smallSumSubset ( data , target , maxVal ) { let sum = 0 ; for ( let i = 0 ; i < data . length ; i ++ ) sum += data [ i ] ; if ( target <= 0 ) return 0 ; else if ( sum < target ) return maxVal ; else if ( sum == target ) return data . length ; else if ( data [ 0 ] >= target ) return 1 ; else if ( data [ 0 ] < target ) { let temp = [ ] ; for ( let i = 1 ; i < data . length ; i ++ ) temp . push ( data [ i ] ) ; return Math . min ( smallSumSubset ( temp , target , maxVal ) , 1 + smallSumSubset ( temp , target - data [ 0 ] , maxVal ) ) ; } } let data = [ 3 , 1 , 7 , 1 , 2 ] ; let target = 11 ; let val = smallSumSubset ( data , target , data . length + 1 ) ; if ( val > data . length ) document . write ( - 1 ) ; else document . write ( val ) ;

Index/key variables: i
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function swapXandY ( str , X , Y ) { let N = str . length ; X = X + Y ; Y = X - Y ; X = X - Y ; for ( let i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ' ) { str [ i ] = ' ' ; } else if ( str [ i ] == ' ' ) { str [ i ] = ' ' ; } } return true ; } function maxCost ( str , X , Y ) { let N = str . length ; if ( Y > X ) { swapXandY ( str . split ( ' ' ) , X , Y ) ; } let res = 0 ; let countP = 0 ; let countR = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ' ) { countP ++ ; } else if ( str [ i ] == ' ' ) { if ( countP > 0 ) { countP -- ; res += X ; } else countR ++ ; } else { res += Math . min ( countP , countR ) * Y ; countP = 0 ; countR = 0 ; } } res += Math . min ( countP , countR ) * Y ; return res ; } let str = " " ; let X = 5 , Y = 4 ; document . write ( maxCost ( str , X , Y ) ) ;

Index/key variables: i
Accumulator variables: countP, countR, i, res
Iterator variables: i
Javascript program:
function uniqueElement ( arr ) { var x = arr [ 0 ] ; var flag = 1 ; for ( var i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] != x ) { flag = 0 ; break ; } } if ( flag == 1 ) document . write ( " " ) ; else document . write ( " " ) ; } var arr = [ 9 , 9 , 9 , 9 , 9 , 9 , 9 ] ; uniqueElement ( arr ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function maxCount ( freq , maxi , mini ) { let n = freq . length - 1 ; let ans = 0 ; for ( let sum = 2 * mini ; sum <= 2 * maxi ; ++ sum ) { let possiblePair = 0 ; for ( let firElement = 1 ; firElement < Math . floor ( ( sum + 1 ) / 2 ) ; firElement ++ ) { if ( sum - firElement <= maxi ) { possiblePair += Math . min ( freq [ firElement ] , freq [ sum - firElement ] ) ; } } if ( sum % 2 == 0 ) { possiblePair += freq [ sum / 2 ] / 2 ; } ans = Math . max ( ans , possiblePair ) ; } return ans ; } function countofPairs ( a ) { let n = a . length ; let freq = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; let maxi = - 1 ; let mini = n + 1 ; for ( let i = 0 ; i < n ; i ++ ) { maxi = Math . max ( maxi , a [ i ] ) ; mini = Math . min ( mini , a [ i ] ) ; } for ( let i = 0 ; i < n ; ++ i ) freq [ a [ i ] ] ++ ; return maxCount ( freq , maxi , mini ) ; } let a = [ 1 , 2 , 4 , 3 , 3 , 5 , 6 ] ; document . write ( countofPairs ( a ) ) ;

Index/key variables: firElement, i
Accumulator variables: firElement, i, possiblePair
Iterator variables: firElement, i, sum
Javascript program:
function getMaxSum ( a , n ) { let dp = new Array ( n ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } dp [ 0 ] [ 0 ] = a [ 0 ] ; dp [ 0 ] [ 1 ] = a [ 0 ] * a [ 0 ] ; let max_sum = Math . max ( dp [ 0 ] [ 0 ] , dp [ 0 ] [ 1 ] ) ; for ( let i = 1 ; i < n ; i ++ ) { dp [ i ] [ 0 ] = Math . max ( a [ i ] , dp [ i - 1 ] [ 0 ] + a [ i ] ) ; dp [ i ] [ 1 ] = Math . max ( dp [ i - 1 ] [ 1 ] + a [ i ] , a [ i ] * a [ i ] ) ; dp [ i ] [ 1 ] = Math . max ( dp [ i ] [ 1 ] , dp [ i - 1 ] [ 0 ] + a [ i ] * a [ i ] ) ; max_sum = Math . max ( max_sum , dp [ i ] [ 1 ] ) ; max_sum = Math . max ( max_sum , dp [ i ] [ 0 ] ) ; } return max_sum ; } let n = 5 ; let a = [ 1 , - 5 , 8 , 12 , - 8 ] ; document . write ( getMaxSum ( a , n ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function subArraylen ( arr , n , K ) { var mp = new Map ( ) ; mp . set ( arr [ 0 ] , 0 ) ; for ( var i = 1 ; i < n ; i ++ ) { arr [ i ] = arr [ i ] + arr [ i - 1 ] ; mp . set ( arr [ i ] , i ) ; } var len = 10000000000 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < K ) continue ; else { var x = arr [ i ] - K ; if ( x == 0 ) len = Math . min ( len , i ) ; if ( ! mp . has ( x ) ) continue ; else { len = Math . min ( len , i - mp . get ( x ) ) ; } } } return len ; } var arr = [ 1 , 2 , 4 , 3 , 2 , 4 , 1 ] ; var n = arr . length ; var K = 7 ; var len = subArraylen ( arr , n , K ) ; if ( len == 1000000000 ) { document . write ( " " ) ; } else { document . write ( len ) ; }

Index/key variables: i
Accumulator variables: i
Iterator variables: i