dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
var MAXN = 1000001 ; var freq = Array ( MAXN ) . fill ( 0 ) ; var mul = Array ( MAXN ) . fill ( 0 ) ; var cnt = Array ( MAXN ) . fill ( 0 ) ; function nC3 ( n ) { if ( n < 3 ) return 0 ; return ( n * ( n - 1 ) * ( n - 2 ) ) / 6 ; } function count_triplet ( arr , N , K ) { for ( var i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( var i = 1 ; i <= 1000000 ; i ++ ) { for ( var j = i ; j <= 1000000 ; j += i ) { mul [ i ] += freq [ j ] ; } cnt [ i ] = nC3 ( mul [ i ] ) ; } for ( var i = 1000000 ; i >= 1 ; i -- ) { for ( var j = 2 * i ; j <= 1000000 ; j += i ) { cnt [ i ] -= cnt [ j ] ; } } document . write ( " " + " " + K ) ; document . write ( " " + cnt [ K ] ) ; } var arr = [ 1 , 7 , 12 , 6 , 15 , 9 ] ; var N = 6 , K = 3 ; count_triplet ( arr , N , K ) ;

Index/key variables: K, i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function isSubseqPossible ( arr , N , K ) { let i ; let odd = 0 , even = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd ++ ; else even ++ ; } if ( odd == 0 || ( even == 0 && K % 2 == 0 ) ) return false ; return true ; } let arr = [ 2 , 3 , 5 , 7 , 4 ] ; let N = arr . length ; let K = 3 ; document . write ( isSubseqPossible ( arr , N , K ) ? " " : " " ) ;

Index/key variables: i
Accumulator variables: even, i, odd
Iterator variables: i
Javascript program:
function print_path ( N , jump , coin ) { if ( jump > coin || jump * ( N - 1 ) < coin ) { document . write ( " " ) ; } else { let pos = 1 ; while ( jump > 0 ) { let tmp = Math . min ( N - 1 , coin - ( jump - 1 ) ) ; if ( pos + tmp <= N ) { pos += tmp ; } else { pos -= tmp ; } document . write ( pos + " " ) ; ; coin -= tmp ; jump -= 1 ; } } } let N = 5 , K = 4 , M = 12 ; print_path ( N , K , M ) ;

Accumulator variables: coin, jump, pos
Javascript program:
let INF = 10000009 ; function findMinHike ( arr , n ) { arr . unshift ( INF ) ; arr . push ( INF ) ; let hike = new Array ( n + 2 ) ; for ( let i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] >= arr [ i ] && arr [ i ] <= arr [ i + 1 ] ) { hike [ i ] = 1 ; } } for ( let i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] < arr [ i ] && arr [ i ] <= arr [ i + 1 ] ) { hike [ i ] = hike [ i - 1 ] + 1 ; } } for ( let i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] >= arr [ i ] && arr [ i ] > arr [ i + 1 ] ) { hike [ i ] = hike [ i + 1 ] + 1 ; } } for ( let i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] < arr [ i ] && arr [ i ] > arr [ i + 1 ] ) { hike [ i ] = Math . max ( hike [ i - 1 ] , hike [ i + 1 ] ) + 1 ; } } for ( let i = 1 ; i <= n ; i ++ ) { document . write ( hike [ i ] + " " ) ; } } let arr = [ 5 , 3 , 4 , 2 , 1 , 6 ] ; findMinHike ( arr , arr . length ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function minimumCost ( arr , cost , N ) { var dp = Array ( N ) . fill ( ) . map ( ( ) => Array ( 3 ) . fill ( 0 ) ) ; dp [ 0 ] [ 0 ] = 0 ; dp [ 0 ] [ 1 ] = cost [ 0 ] ; dp [ 0 ] [ 2 ] = cost [ 0 ] * 2 ; for ( i = 1 ; i < N ; i ++ ) { for ( j = 0 ; j < 3 ; j ++ ) { var minimum = parseInt ( 1e6 ) ; if ( j + arr [ i ] != arr [ i - 1 ] ) minimum = Math . min ( minimum , dp [ i - 1 ] [ 0 ] ) ; if ( j + arr [ i ] != arr [ i - 1 ] + 1 ) minimum = Math . min ( minimum , dp [ i - 1 ] [ 1 ] ) ; if ( j + arr [ i ] != arr [ i - 1 ] + 2 ) minimum = Math . min ( minimum , dp [ i - 1 ] [ 2 ] ) ; dp [ i ] [ j ] = j * cost [ i ] + minimum ; } } var ans = parseInt ( 1e6 ) ; for ( i = 0 ; i < 3 ; i ++ ) ans = Math . min ( ans , dp [ N - 1 ] [ i ] ) ; document . write ( ans + " " ) ; } var arr = [ 1 , 1 , 2 , 2 , 3 , 4 ] ; var cost = [ 3 , 2 , 5 , 4 , 2 , 1 ] ; var N = arr . length ; minimumCost ( arr , cost , N ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function maximumSumSubarray ( arr , n ) { var mp = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= 0 && ! mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , 1 ) ; } var first = 0 ; var last = 0 ; var ans = 0 ; var INF = 1000000 ; mp . forEach ( ( value , key ) => { var mx = key ; var curr = 0 ; var curr_start ; for ( var j = 0 ; j < n ; j ++ ) { if ( curr == 0 ) curr_start = j ; var val = arr [ j ] > mx ? - INF : arr [ j ] ; curr += val ; if ( curr < 0 ) curr = 0 ; if ( curr > ans ) { ans = curr ; first = curr_start ; last = j ; } } } ) ; document . write ( first + 1 + " " + ( last + 1 ) ) ; } var arr = [ 5 , - 2 , 10 , - 1 , 4 ] ; var size = arr . length ; maximumSumSubarray ( arr , size ) ;

Index/key variables: i, j
Accumulator variables: curr, i, j
Iterator variables: i, j
Javascript program:
var dp = Array . from ( Array ( 100 ) , ( ) => Array ( 100 ) ) ; for ( var i = 0 ; i < 100 ; i ++ ) for ( var j = 0 ; j < 100 ; j ++ ) dp [ i ] [ j ] = new Array ( 100 ) . fill ( 0 ) ; function maxProfit ( profit , weight , n , max_W , max_E ) { for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = 1 ; j <= max_W ; j ++ ) { for ( var k = 1 ; k <= max_E ; k ++ ) { if ( j >= weight [ i - 1 ] ) { dp [ i ] [ j ] [ k ] = Math . max ( dp [ i - 1 ] [ j ] [ k ] , dp [ i - 1 ] [ j - weight [ i - 1 ] ] [ k - 1 ] + profit [ i - 1 ] ) ; } else { dp [ i ] [ j ] [ k ] = dp [ i - 1 ] [ j ] [ k ] ; } } } } return dp [ n ] [ max_W ] [ max_E ] ; } var n = 5 ; var profit = [ 2 , 7 , 1 , 5 , 3 ] ; var weight = [ 2 , 5 , 2 , 3 , 4 ] ; var max_weight = 8 ; var max_element = 2 ; document . write ( maxProfit ( profit , weight , n , max_weight , max_element ) + " " ) ;

Index/key variables: i, j, k, max_E, max_W, n
Accumulator variables: i, j, k
Iterator variables: i, j, k
Javascript program:
function make_xor_half ( arr ) { let sum = 0 , xr = 0 ; for ( let a = 0 ; a < arr . length ; a ++ ) { sum += arr [ a ] ; xr ^= arr [ a ] ; } if ( 2 * xr == sum ) return - 1 ; if ( xr == 0 ) { arr . push ( sum ) ; return 1 ; } arr . push ( xr ) ; arr . push ( sum + xr ) ; return 2 ; } let N = 7 ; let nums = [ 3 , 4 , 7 , 1 , 2 , 5 , 6 ] ; let count = make_xor_half ( nums ) ; if ( count == - 1 ) document . write ( " " ) ; else if ( count == 1 ) document . write ( nums [ N ] + " " ) ; else document . write ( nums [ N ] + " " + nums [ N + 1 ] ) ;

Index/key variables: N, a
Accumulator variables: a, sum, xr
Iterator variables: a
Javascript program:
function count_triangles ( a , b , c , d ) { var ans = 0 ; for ( x = a ; x <= b ; ++ x ) { var num_greater_than_d = Math . max ( d , c + x ) - Math . max ( d , b + x - 1 ) ; ans += num_greater_than_d * ( d - c + 1 ) ; var r = Math . min ( Math . max ( c , c + x ) , d ) - c ; var l = Math . min ( Math . max ( c , b + x - 1 ) , d ) - c ; var x1 = ( r * ( r + 1 ) ) / 2 ; var x2 = ( l * ( l + 1 ) ) / 2 ; ans += x1 - x2 ; } return ans ; } var a = 2 , b = 3 , c = 4 , d = 5 ; document . write ( count_triangles ( a , b , c , d ) ) ;

Accumulator variables: ans
Iterator variables: x
Javascript program:
function minFlip ( s , n , k , a , p ) { let allowed = Array . from ( { length : 26 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < p ; i ++ ) { allowed [ a [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] = true ; } let freq = new Array ( k ) ; for ( var i = 0 ; i < freq . length ; i ++ ) { freq [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < k ; i ++ ) for ( let j = 0 ; j < 26 ; j ++ ) freq [ i ] [ j ] = 0 ; for ( let i = 0 ; i < n ; i ++ ) { freq [ i % k ] [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] += 1 ; } let ans = 0 ; let totalpositions = Math . floor ( n / k ) ; for ( let i = 0 ; i < k ; i ++ ) { let maxfrequency = 0 ; for ( let j = 0 ; j < 26 ; j ++ ) { if ( freq [ i ] [ j ] > maxfrequency && allowed [ j ] == true ) maxfrequency = freq [ i ] [ j ] ; } ans += ( totalpositions - maxfrequency + ( ( i % k < n % k ) ? 1 : 0 ) ) ; } document . write ( ans + " " ) ; } let S = " " ; let n = S . length ; let K = 3 ; let A = [ ' ' , ' ' , ' ' , ' ' , ' ' ] ; let p = A . length ; minFlip ( S , n , K , A , p ) ;

Index/key variables: i, j
Accumulator variables: ans, i, j
Iterator variables: i, j
Javascript program:
function colorRequired ( arr , n ) { var all_same = true ; var one_adjacent_same = false ; for ( var i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) { all_same = false ; } if ( arr [ i ] == arr [ i + 1 ] ) { one_adjacent_same = true ; } } if ( all_same == true ) { document . write ( 1 + " " ) ; return ; } if ( n % 2 == 0 one_adjacent_same == true ) { document . write ( 2 + " " ) ; return ; } document . write ( 3 + " " ) ; } var arr = [ 1 , 2 , 1 , 1 , 2 ] ; var n = arr . length ; colorRequired ( arr , n ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Boolean variables: all_same, one_adjacent_same
Javascript program:
function maxRemainingSum ( arr , n ) { var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; var i = 0 ; var j = n - 1 ; var result = 0 ; while ( i < j ) { if ( arr [ i ] < arr [ j ] ) { sum -= arr [ i ] ; i ++ ; } else { sum -= arr [ j ] ; j -- ; } result += sum ; } return result ; } var arr = [ 3 , 1 , 7 , 2 , 1 ] ; var N = arr . length ; document . write ( maxRemainingSum ( arr , N ) ) ;

Index/key variables: i, j
Accumulator variables: i, j, result, sum
Iterator variables: i
Javascript program:
function printKParts ( N , K ) { if ( N % K == 0 ) { for ( var i = 1 ; i < K ; i ++ ) document . write ( " " ) ; document . write ( ( N - ( K - 1 ) ) + " " ) ; } else { if ( K == 2 ) { document . write ( " " + " " ) ; return ; } for ( var i = 1 ; i < K - 1 ; i ++ ) document . write ( 1 + " " ) ; document . write ( 2 + " " + ( N - K ) + " " ) ; } } var N = 18 , K = 5 ; printKParts ( N , K ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function minOperations ( s , len ) { var operationCnt = 0 ; var st = [ ] ; var cntClosing = 0 ; for ( var i = 0 ; i < len ; i ++ ) { if ( s [ i ] == ' ' ) { if ( cntClosing > 0 ) { operationCnt ++ ; st . pop ( ) ; } st . push ( s [ i ] ) ; cntClosing = 0 ; } else if ( st . length == 0 ) { st . push ( ' ' ) ; operationCnt ++ ; cntClosing = 1 ; } else { cntClosing = ( cntClosing + 1 ) % 2 ; if ( cntClosing == 0 ) { st . pop ( ) ; } } } operationCnt += st . length * 2 - cntClosing ; return operationCnt ; } var str = " " ; var len = str . length ; document . write ( minOperations ( str , len ) ) ;

Index/key variables: i
Accumulator variables: i, operationCnt
Iterator variables: i
Javascript program:
function longestSubsequence ( s ) { var n = s . length ; var answer = 0 ; var prev = ' ' ; for ( var i = 0 ; i < n ; i ++ ) { if ( prev != s [ i ] ) { prev = s [ i ] ; answer ++ ; } } return answer ; } var str = " " ; document . write ( longestSubsequence ( str ) ) ;

Index/key variables: i
Accumulator variables: answer, i
Iterator variables: i
Javascript program:
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function maximumGcdSubarray ( arr , n ) { if ( n == 1 ) return 0 ; let k = 1 ; for ( let i = 1 ; i < n ; ++ i ) { k = Math . max ( k , gcd ( arr [ i ] , arr [ i - 1 ] ) ) ; } let cnt = 0 ; let maxLength = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { cnt ++ ; } else { maxLength = Math . max ( maxLength , cnt ) ; cnt = 0 ; } } maxLength = Math . max ( maxLength , cnt ) ; return maxLength ; } let arr = [ 18 , 3 , 6 , 9 ] ; let n = arr . length ; document . write ( maximumGcdSubarray ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: cnt, i
Iterator variables: i
Javascript program:
function MinDiff ( n ) { var val = parseInt ( Math . pow ( 2 , n ) ) ; var sep = n / 2 ; var grp1 = 0 ; var grp2 = 0 ; grp1 = grp1 + val ; for ( i = 1 ; i < sep ; i ++ ) grp1 = grp1 + parseInt ( Math . pow ( 2 , i ) ) ; for ( i = sep ; i < n ; i ++ ) grp2 = grp2 + parseInt ( Math . pow ( 2 , i ) ) ; document . write ( Math . abs ( grp1 - grp2 ) ) ; } var n = 4 ; MinDiff ( n ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function countDist ( arr1 , n , arr2 , m , k ) { var count = 0 ; var smallest = arr2 [ 0 ] ; var largest = arr2 [ 0 ] ; for ( i = 0 ; i < m ; i ++ ) { smallest = Math . max ( smallest , arr2 [ i ] ) ; largest = Math . min ( largest , arr1 [ i ] ) ; } for ( i = 0 ; i < n ; i ++ ) { if ( Math . abs ( arr1 [ i ] - smallest ) > k || Math . abs ( arr1 [ i ] - largest ) > k ) count ++ ; } document . write ( count ) ; } var arr1 = [ 3 , 1 , 4 ] ; var n = arr1 . length ; var arr2 = [ 5 , 1 , 2 ] ; var m = arr2 . length ; var k = 2 ; countDist ( arr1 , n , arr2 , m , k ) ;

Index/key variables: i
Accumulator variables: count, i
Iterator variables: i
Javascript program:
function MakeArray ( a , n , k ) { var mp = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( ! mp . has ( a [ i ] ) ) mp . set ( a [ i ] , 1 ) ; } if ( mp . count > k ) { document . write ( " " ) ; return ; } var ans = [ ] ; mp . forEach ( ( value , key ) => { ans . push ( key ) ; } ) ; while ( ans . length < k ) { ans . push ( 1 ) ; } for ( var i = 0 ; i < 2 ; i ++ ) { for ( var j = 0 ; j < k ; j ++ ) document . write ( ans [ j ] + " " ) ; } } var arr = [ 1 , 2 , 2 , 1 ] ; var K = 2 ; var size = arr . length ; MakeArray ( arr , size , K ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function MinimumValue ( a , n ) { let answer = Number . MAX_SAFE_INTEGER ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { answer = Math . min ( answer , ( ( a [ i ] & a [ j ] ) ^ ( a [ i ] a [ j ] ) ) ) ; } } return answer ; } let N = 6 ; let A = [ 12 , 3 , 14 , 5 , 9 , 8 ] ; document . write ( MinimumValue ( A , N ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function countOfSubStrings ( s ) { let n = s . length ; let prefix_sum = Array . from ( { length : n } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) prefix_sum [ i ] = 1 ; } for ( let i = 1 ; i < n ; i ++ ) prefix_sum [ i ] += prefix_sum [ i - 1 ] ; let answer = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i ; j < n ; j ++ ) { let countOfOnes = prefix_sum [ j ] - ( i - 1 >= 0 ? prefix_sum [ i - 1 ] : 0 ) ; let length = j - i + 1 ; if ( countOfOnes > 0 && length % countOfOnes == 0 ) answer ++ ; } } return answer ; } let S = " " ; document . write ( countOfSubStrings ( S ) ) ;

Index/key variables: i, j
Accumulator variables: answer, i, j
Iterator variables: i, j
Javascript program:
function count_element ( N , K , arr ) { let mp = new Map ( ) ; for ( let i = 0 ; i < N ; ++ i ) if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) } else { mp . set ( arr [ i ] , 1 ) } let answer = 0 ; for ( let i of mp ) { if ( mp . has ( i [ 0 ] + K ) ) answer += i [ 1 ] ; } return answer ; } let arr = [ 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 ] ; let N = arr . length ; let K = 2 ; document . write ( count_element ( N , K , arr ) ) ;

Index/key variables: i
Accumulator variables: answer
Iterator variables: i
Javascript program:
function digit_xor ( x ) { let xorr = 0 ; while ( x ) { xorr ^= x % 10 ; x = x / 10 ; } return xorr ; } function find_count ( n ) { let mpp = new Map ( ) ; for ( let i = 1 ; i <= n ; i ++ ) { let t = digit_xor ( i ) ; if ( mpp . has ( t ) ) { mpp . set ( t , mpp . get ( t ) + 1 ) } else { mpp . set ( t , 1 ) } } let maxm = 0 ; for ( let x of mpp ) { if ( x [ 1 ] > maxm ) maxm = x [ 1 ] ; } return maxm ; } let N = 13 ; document . write ( find_count ( N ) ) ;

Accumulator variables: i, xorr
Iterator variables: i, x
Javascript program:
function digit_prod ( x ) { let prod = 1 ; while ( x != 0 ) { prod = prod * ( x % 10 ) ; x = Math . floor ( x / 10 ) ; } return prod ; } function find_count ( n ) { let mpp = new Map ( ) ; for ( let i = 1 ; i <= n ; i ++ ) { let t = digit_prod ( i ) if ( mpp . has ( t ) ) { mpp . set ( t , mpp . get ( t ) + 1 ) } else { mpp . set ( t , 1 ) } } let ans = 1 ; let maxm = 0 ; for ( let x of mpp ) { if ( x [ 1 ] > maxm ) { maxm = x [ 1 ] ; ans = 1 ; } else if ( x [ 1 ] == maxm ) { ans ++ ; } } return ans ; } let N = 13 ; document . write ( find_count ( N ) ) ;

Accumulator variables: ans, i
Iterator variables: i, x
Javascript program:
function isPrime ( n ) { let i ; if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) { return false ; } for ( i = 5 ; i * i <= n ; i += 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } function findSubarraySum ( arr , n , K ) { let prevSum = new Map ( ) ; let res = 0 ; let currsum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { currsum += arr [ i ] ; if ( currsum == K ) { res ++ ; } if ( prevSum . has ( currsum - K ) ) res += ( prevSum . get ( currsum - K ) ) ; if ( prevSum . has ( currsum ) ) { prevSum . set ( currsum , prevSum . get ( currsum ) + 1 ) } else { prevSum . set ( currsum , 1 ) } } return res ; } function countSubarray ( arr , n , K ) { for ( let i = 0 ; i < n ; i ++ ) { if ( isPrime ( arr [ i ] ) ) { arr [ i ] = 1 ; } else { arr [ i ] = 0 ; } } document . write ( findSubarraySum ( arr , n , K ) ) ; } let arr = [ 1 , 2 , 3 , 4 ] ; let K = 2 ; let N = arr . length ; countSubarray ( arr , N , K ) ;

Index/key variables: i
Accumulator variables: currsum, i, res
Iterator variables: i
Javascript program:
function find_maxm ( arr , n ) { let mpp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mpp . has ( arr [ i ] ) ) { mpp . set ( arr [ i ] , mpp . get ( arr [ i ] ) + 1 ) } else { mpp . set ( arr [ i ] , 1 ) } } let ans = 0 ; for ( let x of mpp ) { let value = x [ 0 ] ; let freq = x [ 1 ] ; if ( value == freq ) { ans ++ ; } } return ans ; } let arr = [ 3 , 2 , 2 , 3 , 4 , 3 ] ; let n = arr . length ; document . write ( find_maxm ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: ans, i
Iterator variables: i, x
Javascript program:
function pow ( x , y ) { if ( y == 1 ) return x ; if ( y == 0 ) return 1 ; let temp = pow ( x , y / 2 ) ; temp *= temp ; if ( y % 2 == 1 ) temp *= x ; return temp ; } function noOfPermutations ( a , n ) { if ( n == 1 ) { return 1 ; } a . sort ( ) ; if ( a [ n - 1 ] == a [ n - 2 ] ) { return 0 ; } let x = 0 ; for ( let i = 0 ; i < n - 2 ; ++ i ) { if ( a [ i ] == a [ i + 1 ] ) { if ( a [ i ] == a [ i + 2 ] ) { return 0 ; } x ++ ; i ++ ; } } return pow ( 2 , n - 2 * x - 1 ) ; } let a = [ 1 , 2 , 2 , 3 , 4 ] ; let n = a . length ; let num = noOfPermutations ( a , n ) ; document . write ( num ) ;

Index/key variables: i
Accumulator variables: i, temp, x
Iterator variables: i
Javascript program:
function GetMinSubarrayLength ( a , n ) { var ans = Math . max ( a [ 0 ] , a [ n - 1 ] ) ; var lo = 0 , hi = n - 1 ; while ( lo < n && a [ lo ] % ans == 0 ) lo ++ ; while ( hi > lo && a [ hi ] % ans == 0 ) hi -- ; return ( hi - lo + 1 ) ; } var arr = [ 4 , 8 , 2 , 1 , 4 ] ; var N = arr . length ; var length = GetMinSubarrayLength ( arr , N ) ; document . write ( length ) ;

Index/key variables: hi, lo
Accumulator variables: hi, lo
Javascript program:
function countPairs ( arr , N ) { let M = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( M [ arr [ i ] ] ) M [ arr [ i ] ] ++ ; else M [ arr [ i ] ] = 1 } let maxFreq = 0 ; for ( let it in M ) { maxFreq = Math . max ( maxFreq , M [ it ] ) ; } document . write ( N - maxFreq , ' ' ) ; } let a = [ 1 , 8 , 5 , 9 , 8 , 8 , 7 , 7 , 5 , 7 , 7 ] ; let N = a . length ; countPairs ( a , N ) ;

Index/key variables: i, it
Accumulator variables: i
Iterator variables: i, it
Javascript program:
function minimumValue ( arr , n , k ) { arr . sort ( ( a , b ) => a - b ) ; let answer = 0 ; for ( let i = 0 ; i < k ; i ++ ) answer += arr [ i ] * arr [ i ] ; return answer * ( 2 * k - 2 ) ; } let arr = [ 4 , 21 , 5 , 3 , 8 ] , k = 3 ; let n = arr . length ; document . write ( minimumValue ( arr , n , k ) ) ;

Index/key variables: i
Accumulator variables: answer, i
Iterator variables: i
Javascript program:
let min_val = Number . MAX_VALUE ; let min_steps = 0 ; function sumOfDigits ( n ) { let s = n . toString ( ) ; let sum = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { sum += ( s [ i ] - ' ' ) ; } return sum ; } function Transform ( n , d , steps ) { if ( n < min_val ) { min_val = n ; min_steps = steps ; } else if ( n == min_val ) { min_steps = Math . min ( min_steps , steps ) ; } if ( steps < 15 ) { Transform ( sumOfDigits ( n ) , d , steps + 1 ) ; Transform ( n + d , d , steps + 1 ) ; } } let N = 9 , D = 3 ; Transform ( N , D , 0 ) ; document . write ( min_val + " " + min_steps ) ;

Index/key variables: i
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function noOfMoves ( arr , n ) { let cnt = 0 ; let maxCnt = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { cnt ++ ; } else { if ( cnt != 0 ) { maxCnt = Math . max ( maxCnt , cnt ) ; cnt = 0 ; } } } document . write ( maxCnt + " " ) ; } let arr = [ 0 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 ] ; let N = arr . length ; noOfMoves ( arr , N ) ; let arr1 = [ 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 ] ; N = arr . length ; noOfMoves ( arr1 , N ) ;

Index/key variables: i
Accumulator variables: cnt, i
Iterator variables: i
Javascript program:
function knowPair ( a , b , n , s , m ) { var i = 0 , rem = 0 ; var count_b = 0 , flag = 0 ; while ( i <= a ) { rem = s - ( n * i ) ; count_b = parseInt ( rem / m ) ; if ( rem % m == 0 && count_b <= b ) { flag = 1 ; } i ++ ; } if ( flag == 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var A = 1 ; var B = 2 ; var n = 3 ; var S = 4 ; var m = 2 ; knowPair ( A , B , n , S , m ) ;

Accumulator variables: i
Javascript program:
function sumDivisibles ( A , B , M ) { var sum = 0 ; for ( var i = A ; i <= B ; i ++ ) if ( i % M == 0 ) sum += i ; return sum ; } var A = 6 , B = 15 , M = 3 ; document . write ( sumDivisibles ( A , B , M ) ) ;

Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function findSmallNum ( N , K ) { var rem = N % K ; if ( rem == 0 ) return N ; else return N - rem ; } function findLargeNum ( N , K ) { var rem = ( N + K ) % K ; if ( rem == 0 ) return N ; else return N + K - rem ; } function sumDivisibles ( A , B , M ) { var sum = 0 ; var first = findSmallNum ( A , M ) ; var last = findLargeNum ( B , M ) ; if ( first < A ) first += M ; if ( last > B ) first -= M ; var n = ( parseInt ( B / M ) - parseInt ( ( A - 1 ) / M ) ) ; return n * ( first + last ) / 2 ; } var A = 6 , B = 15 , M = 3 ; document . write ( sumDivisibles ( A , B , M ) ) ;

Accumulator variables: first
Javascript program:
function distinctSubstring ( P , N ) { var S = new Set ( ) ; for ( var i = 0 ; i < N ; ++ i ) { var freq = Array ( 26 ) . fill ( false ) ; var s = " " ; for ( var j = i ; j < N ; ++ j ) { var pos = P [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( freq [ pos ] == true ) break ; freq [ pos ] = true ; s += P [ j ] ; S . add ( s ) ; } } return S . size ; } var S = " " ; var N = S . length ; document . write ( distinctSubstring ( S , N ) ) ;

Index/key variables: j, pos
Accumulator variables: s
Iterator variables: i, j
Javascript program:
function calcSum ( arr , n , k ) { for ( var i = 0 ; i <= n - k ; i ++ ) { var sum = 0 ; for ( var j = i ; j < k + i ; j ++ ) sum += arr [ j ] ; document . write ( sum + " " ) ; } } var arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; var n = arr . length ; var k = 3 ; calcSum ( arr , n , k ) ;

Index/key variables: j
Accumulator variables: i, j, sum
Iterator variables: i, j
Javascript program:
function calcSum ( arr , n , k ) { var sum = 0 ; for ( var i = 0 ; i < k ; i ++ ) sum += arr [ i ] ; document . write ( sum + " " ) ; for ( var i = k ; i < n ; i ++ ) { sum = ( sum - arr [ i - k ] ) + arr [ i ] ; document . write ( sum + " " ) ; } } var arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; var n = arr . length ; var k = 3 ; calcSum ( arr , n , k ) ;

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