dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
function maxDiff ( arr , n ) { var result = 0 ; arr . sort ( ( a , b ) => a - b ) for ( var i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) result += Math . abs ( arr [ i ] ) ; else i ++ ; } if ( arr [ n - 2 ] != arr [ n - 1 ] ) result += Math . abs ( arr [ n - 1 ] ) ; return result ; } var arr = [ 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 ] ; var n = arr . length ; document . write ( " " + maxDiff ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: i, result
Iterator variables: i
Javascript program:
/ *	JavaScript program to implement the above approach function findNewString ( s ) { var ans = 0 ; var freq = new Array ( 26 ) . fill ( 0 ) ; var n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { ans += freq [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ; freq [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] += 1 ; } return ans ; } var s = " " ; document . write ( findNewString ( s ) ) ;

Index/key variables: i
Accumulator variables: ans, i
Iterator variables: i
Javascript program:
function maxDiff ( arr , n ) { let hashPositive = new Map ( ) ; let hashNegative = new Map ( ) ; let SubsetSum_1 = 0 , SubsetSum_2 = 0 ; for ( let i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] > 0 ) { if ( hashPositive . has ( arr [ i ] ) ) { hashPositive . set ( arr [ i ] , hashPositive . get ( arr [ i ] ) + 1 ) ; } else { hashPositive . set ( arr [ i ] , 1 ) ; } } } for ( let i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] > 0 && hashPositive . has ( arr [ i ] ) ) { if ( hashPositive . get ( arr [ i ] ) == 1 ) { SubsetSum_1 += arr [ i ] ; } } } for ( let i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] < 0 ) { if ( hashNegative . has ( Math . abs ( arr [ i ] ) ) ) { hashNegative . set ( Math . abs ( arr [ i ] ) , hashNegative . get ( Math . abs ( arr [ i ] ) ) + 1 ) ; } else { hashNegative . set ( Math . abs ( arr [ i ] ) , 1 ) ; } } } for ( let i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] < 0 && hashNegative . has ( Math . abs ( arr [ i ] ) ) ) { if ( hashNegative . get ( Math . abs ( arr [ i ] ) ) == 1 ) { SubsetSum_2 += arr [ i ] ; } } } return Math . abs ( SubsetSum_1 - SubsetSum_2 ) ; } let arr = [ 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 ] ; let n = arr . length ; document . write ( " " + maxDiff ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: SubsetSum_1, SubsetSum_2, i
Iterator variables: i
Javascript program:
function proofreadSpell ( str ) { var result = [ ] ; str . split ( ' ' ) . forEach ( c => { result . push ( c ) ; var n = result . length ; if ( n >= 3 ) { if ( result [ n - 1 ] == result [ n - 2 ] && result [ n - 1 ] == result [ n - 3 ] ) { result . pop ( ) ; } } n = result . length ; if ( n >= 4 ) { if ( result [ n - 1 ] == result [ n - 2 ] && result [ n - 3 ] == result [ n - 4 ] ) { result . pop ( ) ; } } } ) ; var resultStr = " " ; result . forEach ( c => { resultStr += c ; } ) ; return resultStr ; } var str = " " ; document . write ( proofreadSpell ( str ) ) ;

Accumulator variables: resultStr
Javascript program:
function minRange ( arr , n , k ) { let l = 0 , r = n ; for ( let i = 0 ; i < n ; i ++ ) { let s = new Set ( ) ; let j ; for ( j = i ; j < n ; j ++ ) { s . add ( arr [ j ] ) ; if ( s . size == k ) { if ( ( j - i ) < ( r - l ) ) { r = j ; l = i ; } break ; } } if ( j == n ) break ; } if ( l == 0 && r == n ) document . write ( " " ) ; else document . write ( l + " " + r ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let n = arr . length ; let k = 3 ; minRange ( arr , n , k ) ;

Index/key variables: j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function minRange ( arr , n , k ) { let l = 0 , r = n ; let j = - 1 ; let hm = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { while ( j < n ) { j ++ ; if ( j < n && hm . size < k ) { if ( hm . has ( arr [ j ] ) ) hm . set ( arr [ j ] , hm . get ( arr [ j ] ) + 1 ) ; else hm . set ( arr [ j ] , 1 ) ; } if ( hm . size == k && ( ( r - l ) >= ( j - i ) ) ) { l = i ; r = j ; break ; } } if ( hm . size < k ) break ; while ( hm . size == k ) { if ( hm . has ( arr [ i ] ) && hm . get ( arr [ i ] ) == 1 ) hm . delete ( arr [ i ] ) ; else if ( hm . has ( arr [ i ] ) ) hm . set ( arr [ i ] , hm . get ( arr [ i ] ) - 1 ) ; i ++ ; if ( hm . size == k && ( r - l ) >= ( j - i ) ) { l = i ; r = j ; } } if ( hm . has ( arr [ i ] ) && hm . get ( arr [ i ] ) == 1 ) hm . delete ( arr [ i ] ) ; else if ( hm . has ( arr [ i ] ) ) hm . set ( arr [ i ] , hm . get ( arr [ i ] ) - 1 ) ; } if ( l == 0 && r == n ) document . write ( " " ) ; else document . write ( l + " " + r ) ; } let arr = [ 1 , 1 , 2 , 2 , 3 , 3 , 4 , 5 ] ; let n = arr . length ; let k = 3 ; minRange ( arr , n , k ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i
Javascript program:
function sum ( a , n ) { var cnt = new Map ( ) ; var ans = 0 , pre_sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) { ans += ( i * a [ i ] ) - pre_sum ; pre_sum += a [ i ] ; if ( cnt . has ( a [ i ] - 1 ) ) ans -= cnt . get ( a [ i ] - 1 ) ; if ( cnt . has ( a [ i ] + 1 ) ) ans += cnt . get ( a [ i ] + 1 ) ; if ( cnt . has ( a [ i ] ) ) cnt . set ( a [ i ] , cnt . get ( a [ i ] ) + 1 ) else cnt . set ( a [ i ] , 1 ) } return ans ; } var a = [ 1 , 2 , 3 , 1 , 3 ] ; var n = a . length ; document . write ( sum ( a , n ) ) ;

Index/key variables: i
Accumulator variables: ans, i, pre_sum
Iterator variables: i
Javascript program:
function minDistance ( a , n ) { let max = - 1 , min = Number . MAX_VALUE ; let min_index = - 1 , max_index = - 1 ; let min_dist = n + 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] > max ) max = a [ i ] ; if ( a [ i ] < min ) min = a [ i ] ; } for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] == min ) min_index = i ; if ( a [ i ] == max ) max_index = i ; if ( min_index != - 1 && max_index != - 1 ) min_dist = Math . min ( min_dist , Math . abs ( min_index - max_index ) ) ; } return min_dist ; } let n = 12 ; let a = [ 3 , 2 , 1 , 2 , 1 , 4 , 5 , 8 , 6 , 7 , 8 , 2 ] ; document . write ( minDistance ( a , n ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function printDiagonal ( K , N , M ) { let startrow , startcol ; if ( K - 1 < N ) { startrow = K - 1 ; startcol = 0 ; } else { startrow = N - 1 ; startcol = K - N ; } for ( ; startrow >= 0 && startcol < N ; startrow -- , startcol ++ ) { document . write ( M [ startrow ] [ startcol ] + " " ) ; } } let N = 3 , K = 4 ; let M = [ [ 4 , 7 , 8 ] , [ 9 , 2 , 3 ] , [ 0 , 4 , 1 ] ] ; printDiagonal ( K , N , M ) ;

Index/key variables: startcol, startrow
Accumulator variables: startcol, startrow
Javascript program:
function ReplaceElements ( arr , n ) { if ( n < 2 ) { document . write ( " " ) ; return ; } let firstSmallest = Number . MAX_VALUE ; let secondSmallest = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < firstSmallest ) { secondSmallest = firstSmallest ; firstSmallest = arr [ i ] ; } else if ( arr [ i ] < secondSmallest && arr [ i ] != firstSmallest ) secondSmallest = arr [ i ] ; } for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != firstSmallest ) arr [ i ] = firstSmallest ; else arr [ i ] = secondSmallest ; } for ( let i = 0 ; i < n ; ++ i ) { document . write ( arr [ i ] + " " ) ; } } let arr = [ 4 , 2 , 1 , 3 ] ; let n = arr . length ; ReplaceElements ( arr , n ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function countSubarrWithEqualZeroAndOne ( arr , n ) { var um = new Map ( ) ; var curr_sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) { curr_sum += ( arr [ i ] == 0 ) ? - 1 : arr [ i ] ; if ( um . has ( curr_sum ) ) um . set ( curr_sum , um . get ( curr_sum ) + 1 ) ; else um . set ( curr_sum , 1 ) } var count = 0 ; um . forEach ( ( value , key ) => { if ( value > 1 ) count += ( ( value * ( value - 1 ) ) / 2 ) ; } ) ; if ( um . has ( 0 ) ) count += um . get ( 0 ) ; return count ; } var arr = [ 1 , 0 , 0 , 1 , 0 , 1 , 1 ] ; var n = arr . length ; document . write ( " " + countSubarrWithEqualZeroAndOne ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: count, curr_sum, i
Iterator variables: i
Javascript program:
function lenOfLongSubarr ( arr , n ) { var um = new Map ( ) ; var sum = 0 , maxLen = 0 ; for ( var i = 0 ; i < n ; i ++ ) { sum += arr [ i ] == 0 ? - 1 : 1 ; if ( sum == 1 ) maxLen = i + 1 ; else if ( ! um . has ( sum ) ) um . set ( sum , i ) ; if ( um . has ( sum - 1 ) ) { if ( maxLen < ( i - um . get ( sum - 1 ) ) ) maxLen = i - um . get ( sum - 1 ) ; } } return maxLen ; } var arr = [ 0 , 1 , 1 , 0 , 0 , 1 ] ; var n = arr . length ; document . write ( " " + lenOfLongSubarr ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function maxSubarraySum ( arr , N , K ) { let cum_set = new Set ( ) ; cum_set . add ( 0 ) ; let max_sum = Number . MIN_SAFE_INTEGER ; let cSum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { cSum += arr [ i ] ; let al = [ ] ; let end = 0 ; for ( let it of cum_set ) { end = it ; al . push ( it ) ; } al . sort ( ( a , b ) => a - b ) ; let sit = lower_bound ( al , cSum - K ) ; if ( sit != end ) max_sum = Math . max ( max_sum , cSum - sit ) ; cum_set . add ( cSum ) ; } return max_sum ; } let lower_bound = ( al , x ) => al . filter ( ( item ) => item > x ) [ 0 ] let arr = [ 5 , - 2 , 6 , 3 , - 5 ] ; let K = 15 ; let N = arr . length ; document . write ( maxSubarraySum ( arr , N , K ) ) ;

Index/key variables: i
Accumulator variables: cSum, i
Iterator variables: i, it
Javascript program:
function printAllAPTriplets ( arr , n ) { const s = new Set ( ) for ( let i = 0 ; i < n - 1 ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { let diff = arr [ j ] - arr [ i ] ; if ( s . has ( arr [ i ] - diff ) ) document . write ( arr [ i ] - diff + " " + arr [ i ] + " " + arr [ j ] + " " ) ; } s . add ( arr [ i ] ) ; } } let arr = [ 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 ] ; let n = arr . length ; printAllAPTriplets ( arr , n ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function printAllAPTriplets ( arr , n ) { for ( let i = 1 ; i < n - 1 ; i ++ ) { for ( let j = i - 1 , k = i + 1 ; j >= 0 && k < n ; ) { if ( arr [ j ] + arr [ k ] == 2 * arr [ i ] ) { document . write ( arr [ j ] + " " + arr [ i ] + " " + arr [ k ] + " " ) ; k ++ ; j -- ; } else if ( arr [ j ] + arr [ k ] < 2 * arr [ i ] ) k ++ ; else j -- ; } } } let arr = [ 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 ] ; let n = arr . length ; printAllAPTriplets ( arr , n ) ;

Index/key variables: i, j, k
Accumulator variables: i, j, k
Iterator variables: i, j
Javascript program:
function findTriplets ( a , n , sum ) { let i ; a . sort ( function ( a , b ) { return a - b } ) ; let flag = false ; for ( i = 0 ; i < n - 2 ; i ++ ) { if ( i == 0 a [ i ] > a [ i - 1 ] ) { let start = i + 1 ; let end = n - 1 ; let target = sum - a [ i ] ; while ( start < end ) { if ( start > i + 1 && a [ start ] == a [ start - 1 ] ) { start ++ ; continue ; } if ( end < n - 1 && a [ end ] == a [ end + 1 ] ) { end -- ; continue ; } if ( target == a [ start ] + a [ end ] ) { document . write ( " " + a [ i ] + " " + a [ start ] + " " + a [ end ] + " " ) ; flag = true ; start ++ ; end -- ; } else if ( target > ( a [ start ] + a [ end ] ) ) { start ++ ; } else { end -- ; } } } } if ( flag == false ) { document . write ( " " ) ; } } let a = [ 12 , 3 , 6 , 1 , 6 , 9 ] ; let n = a . length ; let sum = 24 ; a . sort ( ) ; findTriplets ( a , n , sum ) ;

Index/key variables: end, i, start
Accumulator variables: end, i, start
Iterator variables: i
Boolean variables: flag
Javascript program:
let m = 6 , n = 4 ; function linearCheck ( ar , arr ) { for ( let i = 0 ; i < m ; i ++ ) { let matched = true ; for ( let j = 0 ; j < n ; j ++ ) { if ( ar [ i ] [ j ] != arr [ j ] ) { matched = false ; break ; } } if ( matched ) return i + 1 ; } return - 1 ; } let mat = [ [ 0 , 0 , 1 , 0 ] , [ 10 , 9 , 22 , 23 ] , [ 40 , 40 , 40 , 40 ] , [ 43 , 44 , 55 , 68 ] , [ 81 , 73 , 100 , 132 ] , [ 100 , 75 , 125 , 133 ] ] ; let row = [ 10 , 9 , 22 , 23 ] ; document . write ( linearCheck ( mat , row ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Boolean variables: matched
Javascript program:
/ *function countTriplets(arr,n,m) { let count = 0 ; for ( let i = 0 ; i < n - 2 ; i ++ ) for ( let j = i + 1 ; j < n - 1 ; j ++ ) for ( let k = j + 1 ; k < n ; k ++ ) if ( arr [ i ] * arr [ j ] * arr [ k ] == m ) count ++ ; return count ; } let arr = [ 1 , 4 , 6 , 2 , 3 , 8 ] ; let m = 24 ; document . write ( countTriplets ( arr , arr . length , m ) ) ;

Index/key variables: i, j, k
Accumulator variables: count, i, j, k
Iterator variables: i, j, k
Javascript program:
function countPairs ( arr , n ) { let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] == arr [ j ] ) ans ++ ; return ans ; } let arr = [ 1 , 1 , 2 ] ; let n = arr . length ; document . write ( countPairs ( arr , n ) ) ;

Index/key variables: i, j
Accumulator variables: ans, i, j
Iterator variables: i, j
Javascript program:
function preCalculate ( binary , n , left ) { let count1 = 0 , count0 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { left [ i ] [ 0 ] = count1 ; left [ i ] [ 1 ] = count0 ; if ( binary [ i ] != 0 ) count1 ++ ; else count0 ++ ; } } let binary = [ 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 ] ; let n = binary . length ; let left = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) left [ i ] = [ 0 , 0 ] ; preCalculate ( binary , n , left ) ; let queries = [ 0 , 1 , 2 , 4 ] ; let q = queries . length ; for ( let i = 0 ; i < q ; i ++ ) document . write ( left [ queries [ i ] ] [ 0 ] + " " + left [ queries [ i ] ] [ 1 ] + " " ) ;

Index/key variables: i
Accumulator variables: count0, count1, i
Iterator variables: i
Javascript program:
function countPairs ( arr , n ) { let hm = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( hm . has ( arr [ i ] ) ) hm . set ( arr [ i ] , hm . get ( arr [ i ] ) + 1 ) ; else hm . set ( arr [ i ] , 1 ) ; } let ans = 0 ; for ( let [ key , value ] of hm . entries ( ) ) { let count = value ; ans += ( count * ( count - 1 ) ) / 2 ; } return ans ; } let arr = [ 1 , 2 , 3 , 1 ] ; document . write ( countPairs ( arr , arr . length ) ) ;

Index/key variables: i
Accumulator variables: ans, i
Iterator variables: i
Javascript program:
let pos1 = - 1 ; let pos2 = - 1 ; function equiSumUtil ( arr ) { let n = arr . length ; let pre = new Array ( n ) ; let sum = 0 , i ; for ( i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; pre [ i ] = sum ; } let suf = new Array ( n ) ; sum = 0 ; for ( i = n - 1 ; i >= 0 ; i -- ) { sum += arr [ i ] ; suf [ i ] = sum ; } let total_sum = sum ; let j = n - 1 ; i = 0 ; while ( i < j - 1 ) { if ( pre [ i ] == total_sum / 3 ) { pos1 = i ; } if ( suf [ j ] == total_sum / 3 ) { pos2 = j ; } if ( pos1 != - 1 && pos2 != - 1 ) { if ( suf [ pos1 + 1 ] - suf [ pos2 ] == total_sum / 3 ) { return true ; } else { return false ; } } if ( pre [ i ] < suf [ j ] ) { i ++ ; } else { j -- ; } } return false ; } function equiSum ( arr ) { let ans = equiSumUtil ( arr ) ; if ( ans ) { document . write ( " " ) ; for ( let i = 0 ; i <= pos1 ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; document . write ( " " ) ; for ( let i = pos1 + 1 ; i < pos2 ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; document . write ( " " ) ; for ( let i = pos2 ; i < arr . length ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; } else { document . writeLine ( " " + " " ) ; } } let arr = [ 1 , 3 , 6 , 2 , 7 , 1 , 2 , 8 ] ; equiSum ( arr ) ;

Index/key variables: i, j, pos2
Accumulator variables: i, j, sum
Iterator variables: i
Javascript program:
function getRightMin ( arr , n ) { let min = arr [ 0 ] ; let rightMin = 0 ; let i = 1 ; while ( i < n ) { if ( arr [ i ] == min ) rightMin = i ; i *= 2 ; } i = rightMin + 1 ; while ( i < n && arr [ i ] == min ) { rightMin = i ; i ++ ; } return rightMin ; } function getLeftMax ( arr , n ) { let max = arr [ n - 1 ] ; let leftMax = n - 1 ; let i = n - 2 ; while ( i > 0 ) { if ( arr [ i ] == max ) leftMax = i ; i = parseInt ( i / 2 , 10 ) ; } i = leftMax - 1 ; while ( i >= 0 && arr [ i ] == max ) { leftMax = i ; i -- ; } return leftMax ; } let arr = [ 0 , 0 , 1 , 2 , 5 , 5 , 6 , 8 , 8 ] ; let n = arr . length ; document . write ( " " + 0 + " " ) ; document . write ( " " + getRightMin ( arr , n ) + " " ) ; document . write ( " " + getLeftMax ( arr , n ) + " " ) ; document . write ( " " + ( n - 1 ) ) ;

Index/key variables: i
Accumulator variables: i
Javascript program:
function countNum ( arr , n ) { let count = 0 ; arr . sort ( ) ; for ( let i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] != arr [ i + 1 ] && arr [ i ] != arr [ i + 1 ] - 1 ) count += arr [ i + 1 ] - arr [ i ] - 1 ; return count ; } let arr = [ 3 , 5 , 8 , 6 ] ; let n = arr . length ; document . write ( countNum ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: count, i
Iterator variables: i
Javascript program:
function findElementsAfterDel ( arr , m , del , n , k ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; ++ i ) { if ( mp . has ( del [ i ] ) ) { mp . set ( del [ i ] , mp . get ( del [ i ] ) + 1 ) ; } else { mp . set ( del [ i ] , 1 ) ; } } let heap = [ ] ; for ( let i = 0 ; i < m ; ++ i ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) - 1 ) ; if ( mp . get ( arr [ i ] ) == 0 ) mp . delete ( arr [ i ] ) ; } else heap . push ( arr [ i ] ) ; } heap . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < k ; ++ i ) { document . write ( heap [ 0 ] + " " ) ; heap . splice ( 0 , 1 ) ; } } let array = [ 5 , 12 , 33 , 4 , 56 , 12 , 20 ] ; let m = array . length ; let del = [ 12 , 56 , 5 ] ; let n = del . length ; let k = 3 ; findElementsAfterDel ( array , m , del , n , k ) ;

Index/key variables: i
Iterator variables: i
Javascript program:
function countNum ( arr , n ) { let s = new Set ( ) ; let count = 0 , maxm = Number . MIN_VALUE , minm = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { s . add ( arr [ i ] ) ; if ( arr [ i ] < minm ) minm = arr [ i ] ; if ( arr [ i ] > maxm ) maxm = arr [ i ] ; } for ( let i = minm ; i <= maxm ; i ++ ) if ( ! s . has ( i ) ) count ++ ; return count ; } let arr = [ 3 , 5 , 8 , 6 ] ; let n = arr . length ; document . write ( countNum ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: count, i
Iterator variables: i
Javascript program:
function isPossible ( A , n , H , K ) { var time = 0 ; for ( var i = 0 ; i < n ; ++ i ) time += parseInt ( ( A [ i ] - 1 ) / K ) + 1 ; return time <= H ; } function minJobSpeed ( A , n , H ) { if ( H < n ) return - 1 ; var max = A . reduce ( ( a , b ) => Math . max ( a , b ) ) ; var lo = 1 , hi = max ; while ( lo < hi ) { var mi = lo + parseInt ( ( hi - lo ) / 2 ) ; if ( ! isPossible ( A , n , H , mi ) ) lo = mi + 1 ; else hi = mi ; } return lo ; } var A = [ 3 , 6 , 7 , 11 ] , H = 8 ; var n = A . length ; document . write ( minJobSpeed ( A , n , H ) ) ;

Index/key variables: i
Accumulator variables: time
Iterator variables: i
Javascript program:
let MAXN = 11 ; let ver = 2 ; let hashtable = new Array ( ver ) ; for ( var i = 0 ; i < hashtable . length ; i ++ ) { hashtable [ i ] = new Array ( 2 ) ; } let pos = Array ( ver ) . fill ( 0 ) ; function initTable ( ) { for ( let j = 0 ; j < MAXN ; j ++ ) for ( let i = 0 ; i < ver ; i ++ ) hashtable [ i ] [ j ] = Number . MIN_VALUE ; } function hash ( functionn , key ) { switch ( functionn ) { case 1 : return key % MAXN ; case 2 : return ( Math . floor ( key / MAXN ) ) % MAXN ; } return Number . MIN_VALUE ; } function place ( key , tableID , cnt , n ) { if ( cnt == n ) { document . write ( key + " " + " " ) ; document . write ( " " + " " ) ; return ; } for ( let i = 0 ; i < ver ; i ++ ) { pos [ i ] = hash ( i + 1 , key ) ; if ( hashtable [ i ] [ pos [ i ] ] == key ) return ; } if ( hashtable [ tableID ] [ pos [ tableID ] ] != Number . MIN_VALUE ) { let dis = hashtable [ tableID ] [ pos [ tableID ] ] ; hashtable [ tableID ] [ pos [ tableID ] ] = key ; place ( dis , ( tableID + 1 ) % ver , cnt + 1 , n ) ; } else hashtable [ tableID ] [ pos [ tableID ] ] = key ; } function printTable ( ) { document . write ( " " + " " ) ; for ( let i = 0 ; i < ver ; i ++ , document . write ( " " ) ) for ( let j = 0 ; j < MAXN ; j ++ ) if ( hashtable [ i ] [ j ] == Number . MIN_VALUE ) document . write ( " " ) ; else document . write ( hashtable [ i ] [ j ] + " " ) ; document . write ( " " ) ; } function cuckoo ( keys , n ) { initTable ( ) ; for ( let i = 0 , cnt = 0 ; i < n ; i ++ , cnt = 0 ) place ( keys [ i ] , 0 , cnt , n ) ; printTable ( ) ; } let keys_1 = [ 20 , 50 , 53 , 75 , 100 , 67 , 105 , 3 , 36 , 39 ] ; let n = keys_1 . length ; cuckoo ( keys_1 , n ) ; let keys_2 = [ 20 , 50 , 53 , 75 , 100 , 67 , 105 , 3 , 36 , 39 , 6 ] ; let m = keys_2 . length ; cuckoo ( keys_2 , m ) ;

Index/key variables: i, j, tableID
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function countAnomalies ( a , n , k ) { a . sort ( ) ; let res = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let u = upper_bound ( a , 0 , n , a [ i ] ) ; if ( u < n && a [ u ] - a [ i ] <= k ) continue ; let s = lower_bound ( a , 0 , n , a [ i ] ) ; if ( u - s > 1 ) continue ; if ( s > 0 && a [ s - 1 ] - a [ i ] <= k ) continue ; res ++ ; } return res ; } function lower_bound ( a , low , high , element ) { while ( low < high ) { let middle = low + Math . floor ( ( high - low ) / 2 ) ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } function upper_bound ( a , low , high , element ) { while ( low < high ) { let middle = low + Math . floor ( ( high - low ) / 2 ) ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } let arr = [ 7 , 1 , 8 ] , k = 5 ; let n = arr . length ; document . write ( countAnomalies ( arr , n , k ) ) ;

Index/key variables: i, middle, u
Accumulator variables: i, res
Iterator variables: i
Javascript program:
function sumoflength ( arr , n ) { let s = new Set ( ) ; let j = 0 , ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { while ( j < n && ! s . has ( arr [ j ] ) ) { s . add ( arr [ i ] ) ; j ++ ; } ans += Math . floor ( ( ( j - i ) * ( j - i + 1 ) ) / 2 ) ; s . delete ( arr [ i ] ) ; } return ans ; } let arr = [ 1 , 2 , 3 , 4 ] ; let n = arr . length ; document . write ( sumoflength ( arr , n ) ) ;

Index/key variables: i, j
Accumulator variables: ans, i, j
Iterator variables: i
Javascript program:
/ *function countDistictSubarray(arr,n) { let vis = new Map ( ) ; for ( let i = 0 ; i < n ; ++ i ) vis . set ( arr [ i ] , 1 ) ; let k = vis . size ; let vid = new Map ( ) ; let ans = 0 , right = 0 , window = 0 ; for ( let left = 0 ; left < n ; left ++ ) { while ( right < n && window < k ) { if ( vid . has ( arr [ right ] ) ) vid . set ( arr [ right ] , vid . get ( arr [ right ] ) + 1 ) ; else vid . set ( arr [ right ] , 1 ) ; if ( vid . get ( arr [ right ] ) == 1 ) window ++ ; right ++ ; } if ( window == k ) ans += ( n - right + 1 ) ; if ( vid . has ( arr [ left ] ) ) vid . set ( arr [ left ] , vid . get ( arr [ left ] ) - 1 ) ; if ( vid . get ( arr [ left ] ) == 0 ) -- window ; } return ans ; } let arr = [ 2 , 1 , 3 , 2 , 3 ] ; document . write ( countDistictSubarray ( arr , arr . length ) ) ;

Index/key variables: i, left, right
Accumulator variables: ans, left, right, window
Iterator variables: i, left
Javascript program:
function countSubarrays ( arr , n ) { let difference = 0 ; let ans = 0 ; let hash_positive = new Array ( n + 1 ) ; let hash_negative = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) { hash_positive [ i ] = 0 ; hash_negative [ i ] = 0 ; } hash_positive [ 0 ] = 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) == 1 ) { difference ++ ; } else { difference -- ; } if ( difference < 0 ) { ans += hash_negative [ - difference ] ; hash_negative [ - difference ] ++ ; } else { ans += hash_positive [ difference ] ; hash_positive [ difference ] ++ ; } } return ans ; } let arr = [ 3 , 4 , 6 , 8 , 1 , 10 , 5 , 7 ] ; let n = arr . length ; document . write ( " " + " " + countSubarrays ( arr , n ) ) ;

Index/key variables: difference, i
Accumulator variables: ans, difference, i
Iterator variables: i