dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
const INT_MAX = 2147483647 const findTotalDistance = ( S , N ) => { let prefixDistance = new Array ( N ) . fill ( 0 ) ; let suffixDistance = new Array ( N ) . fill ( 0 ) ; let cnt = 0 ; let haveOne = false ; for ( let i = 0 ; i < N ; ++ i ) { if ( S [ i ] == ' ' ) { haveOne = true ; cnt = 0 ; prefixDistance [ i ] = 0 ; } else if ( haveOne ) { cnt ++ ; prefixDistance [ i ] = cnt ; } else prefixDistance [ i ] = INT_MAX ; } haveOne = false ; for ( let i = N - 1 ; i >= 0 ; -- i ) { if ( S [ i ] == ' ' ) { haveOne = true ; cnt = 0 ; suffixDistance [ i ] = 0 ; } else if ( haveOne ) { cnt ++ ; suffixDistance [ i ] = cnt ; } else suffixDistance [ i ] = INT_MAX ; } let sum = 0 ; for ( let i = 0 ; i < N ; ++ i ) { if ( S [ i ] == ' ' ) { sum += Math . min ( prefixDistance [ i ] , suffixDistance [ i ] ) ; } } document . write ( ` ${ sum } ` ) ; } let S = " " ; let N = S . length ; findTotalDistance ( S , N ) ;

Index/key variables: i
Accumulator variables: cnt, sum
Iterator variables: i
Boolean variables: haveOne
Javascript program:
function minimizeCost ( A , B , C ) { let ans = A + B + C ; for ( let i = 1 ; i <= 2 * C ; i ++ ) { let j = 0 ; while ( i * j <= 2 * C ) { ans = Math . min ( ans , Math . abs ( A - i ) + Math . abs ( B - j ) + Math . abs ( i * j - C ) ) ; j ++ ; } } return ans ; } let A = 19 , B = 28 , C = 522 ; document . write ( minimizeCost ( A , B , C ) ) ;

Accumulator variables: i, j
Iterator variables: i
Javascript program:
function averageSetBits ( N , K , arr ) { let p = N ; let q = 0 ; for ( let i = 0 ; i < K ; i ++ ) { let _p = p , _q = q ; p = _p - _p * arr [ i ] / N + _q * arr [ i ] / N ; q = _q - _q * arr [ i ] / N + _p * arr [ i ] / N ; } return p ; } let N = 5 ; let arr = [ 1 , 2 , 3 ] ; let K = arr . length ; document . write ( averageSetBits ( N , K , arr ) . toPrecision ( 3 ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
const maxSubseq = ( arr , N , K ) => { let sum = 0 ; arr . sort ( ( a , b ) => a - b ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( K == 0 ) break ; if ( arr [ i ] < 0 ) { arr [ i ] = - arr [ i ] ; K -- ; } } for ( let i = 0 ; i < N ; i ++ ) if ( arr [ i ] > 0 ) sum += arr [ i ] ; return sum ; } let arr = [ 6 , - 10 , - 1 , 0 , - 4 , 2 ] ; let K = 2 ; let N = arr . length document . write ( maxSubseq ( arr , N , K ) ) ;

Index/key variables: i
Accumulator variables: K, i, sum
Iterator variables: i
Javascript program:
function minimizeSegment ( A , X ) { A . sort ( function ( a , b ) { return a . first - b . first ; } ) A . push ( { first : Number . MAX_VALUE , second : Number . MAX_VALUE } ) ; let start = X . first ; let end = X . first - 1 ; let cnt = 0 ; for ( let i = 0 ; i < A . length ; ) { if ( A [ i ] . first <= start ) { end = Math . max ( A [ i ++ ] . second , end ) ; } else { start = end ; ++ cnt ; if ( A [ i ] . first > end end >= X . second ) { break ; } } } if ( end < X . second ) { return - 1 ; } return cnt ; } let A = [ { first : 1 , second : 3 } , { first : 2 , second : 4 } , { first : 2 , second : 10 } , { first : 2 , second : 3 } , { first : 1 , second : 1 } ] ; let X = { first : 1 , second : 10 } ; document . write ( minimizeSegment ( A , X ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function KthSolution ( X , K ) { let ans = 0 ; for ( let i = 0 ; i < 64 ; i ++ ) { if ( ! ( X & ( 1 << i ) ) ) { if ( K & 1 ) { ans |= ( 1 << i ) ; } K >>= 1 ; if ( ! K ) { break ; } } } return ans ; } let X = 5 , K = 5 ; document . write ( KthSolution ( X , K ) ) ;

Accumulator variables: ans, i
Iterator variables: i
Javascript program:
function printTheArray ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) document . write ( Math . floor ( arr [ i ] ) + " " ) ; } function rearrange ( arr , N ) { if ( N & 1 ) N -- ; let odd_idx = 1 , even_idx = 0 ; let i , max_elem = arr [ N - 1 ] + 1 ; for ( i = 0 ; i < N / 2 ; i ++ ) { arr [ i ] += ( arr [ odd_idx ] % max_elem ) * max_elem ; odd_idx += 2 ; } for ( ; i < N ; i ++ ) { arr [ i ] += ( arr [ even_idx ] % max_elem ) * max_elem ; even_idx += 2 ; } for ( let i = 0 ; i < N ; i ++ ) { arr [ i ] = arr [ i ] / max_elem ; } } let arr = [ 1 , 2 , 3 , 4 , 5 , 16 , 18 , 19 ] ; let N = arr . length ; rearrange ( arr , N ) ; printTheArray ( arr , N ) ;

Index/key variables: even_idx, i, odd_idx
Accumulator variables: N, even_idx, i, odd_idx
Iterator variables: i
Javascript program:
function findRange ( arr , N , K ) { let L = Number . MIN_VALUE ; let R = Number . MAX_VALUE ; for ( let i = 0 ; i < N ; i ++ ) { let l = Math . ceil ( 1.0 * arr [ i ] * K / ( i + 1 ) ) ; let r = Math . ceil ( ( 1.0 + arr [ i ] ) * K / ( i + 1 ) ) - 1 ; L = Math . max ( L , l ) ; R = Math . min ( R , r ) ; } document . write ( L + " " + R ) ; } let arr = [ 2 , 4 , 6 , 9 , 11 ] ; let K = 10 ; let N = arr . length ; findRange ( arr , N , K ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function swap ( str , i , j ) { if ( i == j ) { return str ; } if ( j < i ) { var temp = j ; j = i ; i = temp ; } if ( i >= str . length ) { return str ; } return str . substring ( 0 , i ) + str [ j ] + str . substring ( i + 1 , j ) + str [ i ] + str . substring ( j + 1 ) ; } function findAnagram ( s ) { let check = s . slice ( ) ; let i = 0 , j = s . length - 1 ; while ( i < s . length && j >= 0 ) { if ( s [ i ] != s [ j ] && check [ i ] != s [ j ] && check [ j ] != s [ i ] ) { s = swap ( s , i , j ) i ++ ; j = s . length - 1 ; } else { j -- ; } } if ( s . length % 2 != 0 ) { let mid = s . length / 2 ; if ( check [ mid ] == s [ mid ] ) { for ( let i = 0 ; i < s . length ; i ++ ) { if ( check [ i ] != s [ mid ] && s [ i ] != s [ mid ] ) { s = swap ( s , i , mid ) break ; } } } } let ok = true ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( check [ i ] == s [ i ] ) { ok = false ; break ; } } if ( ok ) document . write ( s ) ; else document . write ( - 1 ) ; } let S = " " ; findAnagram ( S ) ;

Index/key variables: i, j, mid
Accumulator variables: i, j
Iterator variables: i
Boolean variables: ok
Javascript program:
function constructPermutation ( S , N ) { let ans = new Array ( N ) ; ans [ 0 ] = 1 ; for ( let i = 1 ; i < N ; ++ i ) { if ( S [ i - 1 ] == ' ' ) { ans [ i ] = i + 1 ; } else { ans [ i ] = ans [ i - 1 ] ; } for ( let j = 0 ; j < i ; ++ j ) { if ( ans [ j ] >= ans [ i ] ) { ans [ j ] ++ ; } } } for ( let i = 0 ; i < N ; i ++ ) { document . write ( ans [ i ] ) ; if ( i != N - 1 ) { document . write ( " " ) ; } } } let S = " " ; constructPermutation ( S , S . length + 1 ) ;

Index/key variables: i, j
Accumulator variables: i
Iterator variables: i, j
Javascript program:
function KthSolution ( X , K ) { let ans = 0 ; for ( let i = 0 ; i < 64 ; i ++ ) { if ( ! ( X & ( 1 << i ) ) ) { if ( K & 1 ) { ans |= ( 1 << i ) ; } K >>= 1 ; if ( ! K ) { break ; } } } return ans ; } let X = 10 , K = 5 ; document . write ( KthSolution ( X , K ) ) ;

Accumulator variables: ans, i
Iterator variables: i
Javascript program:
function countArrays ( n , k ) { let divisors = new Array ( k + 1 ) . fill ( new Array ( ) ) ; for ( let i = 1 ; i <= k ; i ++ ) { for ( let j = 2 * i ; j <= k ; j += i ) { divisors [ j ] . push ( i ) ; } } let dp = new Array ( n + 1 ) . fill ( new Array ( k + 1 ) ) ; for ( let j = 1 ; j <= k ; j ++ ) { dp [ 1 ] [ j ] = 1 ; } for ( let x = 2 ; x <= n ; x ++ ) { let sum = 0 ; for ( let j = 1 ; j <= k ; j ++ ) { sum += dp [ x - 1 ] [ j ] ; } for ( let y = 1 ; y <= k ; y ++ ) { dp [ x ] [ y ] = sum ; for ( let d of divisors [ y ] ) { dp [ x ] [ y ] = ( dp [ x ] [ y ] - dp [ x - 1 ] [ d ] ) ; } } } let sum = 0 ; for ( let j = 1 ; j <= k ; j ++ ) { sum += dp [ n ] [ j ] ; } sum ++ ; return sum ; } let N = 2 , K = 3 ; document . write ( countArrays ( N , K ) ) ;

Index/key variables: d, j, n, x, y
Accumulator variables: i, j, sum, x, y
Iterator variables: d, i, j, x, y
Javascript program:
function minimumMoves ( A , B , N ) { var ans = 0 ; var i ; var sum_A = 0 , sum_B = 0 ; for ( i = 0 ; i < N ; i ++ ) { sum_A += A [ i ] ; } for ( i = 0 ; i < N ; i ++ ) { sum_B += B [ i ] ; } if ( sum_A != sum_B ) { return - 1 ; } var i = 0 ; while ( i < N ) { if ( A [ i ] > B [ i ] ) { var temp = A [ i ] - B [ i ] ; var j = i - 1 ; while ( j >= 0 && temp > 0 ) { if ( B [ j ] > A [ j ] ) { var cnt = Math . min ( temp , ( B [ j ] - A [ j ] ) ) ; A [ j ] += cnt ; temp -= cnt ; ans += ( cnt * Math . abs ( j - i ) ) ; } j -- ; } if ( temp > 0 ) { var j = i + 1 ; while ( j < N && temp > 0 ) { if ( B [ j ] > A [ j ] ) { var cnt = Math . min ( temp , ( B [ j ] - A [ j ] ) ) ; A [ j ] += cnt ; temp -= cnt ; ans += ( cnt * Math . abs ( j - i ) ) ; } j ++ ; } } } i ++ ; } return ans ; } var A = [ 1 , 5 , 7 ] ; var B = [ 13 , 0 , 0 ] ; var N = A . length ; document . write ( minimumMoves ( A , B , N ) ) ;

Index/key variables: i, j
Accumulator variables: ans, i, j, sum_A, sum_B, temp
Iterator variables: i
Javascript program:
function arrayDivisionByTwo ( arr , n ) { let cnt = 0 ; let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { cnt = 0 ; while ( arr [ i ] % 2 == 0 ) { arr [ i ] = Math . floor ( arr [ i ] / 2 ) ; cnt ++ ; } ans = Math . min ( ans , cnt ) ; } return ans ; } let arr = [ 8 , 12 , 40 ] ; let n = arr . length ; document . write ( arrayDivisionByTwo ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: cnt, i
Iterator variables: i
Javascript program:
function printMinInteger ( P , Q , N ) { if ( Q > P ) { swap ( P , Q ) ; } if ( Q == 0 && ( P == 0 N % P != 0 ) ) { document . write ( " " ) ; return ; } let count_P = 0 , count_Q = 0 ; while ( N > 0 ) { if ( N % P == 0 ) { count_P += Math . floor ( N / P ) ; N = 0 ; } else { N = N - Q ; count_Q ++ ; } } if ( N == 0 ) { for ( let i = 0 ; i < count_Q ; i ++ ) document . write ( Q ) ; for ( let i = 0 ; i < count_P ; i ++ ) document . write ( P ) ; } else { document . write ( " " ) ; } } let N = 32 ; let P = 7 ; let Q = 4 ; printMinInteger ( P , Q , N ) ;

Accumulator variables: count_P, count_Q, i
Iterator variables: i
Javascript program:
function minPushes ( N , K , arr ) { let dp = new Array ( 100000 ) . fill ( - 1 ) ; dp [ N ] = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { for ( let xx = 0 ; xx < 100000 ; xx ++ ) { let x = xx ; if ( dp [ x ] == - 1 ) continue ; let next = ( x + arr [ i ] ) % 100000 ; while ( dp [ next ] == - 1 dp [ next ] > dp [ x ] + 1 ) { dp [ next ] = dp [ x ] + 1 ; x = next ; next = ( next + arr [ i ] ) % 100000 ; } } } return dp [ K ] ; } let N = 99880 , K = 89 ; let arr = [ 100 , 3 ] ; document . write ( minPushes ( N , K , arr ) ) ;

Index/key variables: K, N, i, next, x
Accumulator variables: i, xx
Iterator variables: i, xx
Javascript program:
function minimumOperations ( A , N ) { let cur_prefix_1 = 0 ; let parity = - 1 ; let minOperationsCase1 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { cur_prefix_1 += A [ i ] ; if ( cur_prefix_1 == 0 parity * cur_prefix_1 < 0 ) { minOperationsCase1 += Math . abs ( parity - cur_prefix_1 ) ; cur_prefix_1 = parity ; } parity *= - 1 ; } let cur_prefix_2 = 0 ; parity = 1 ; let minOperationsCase2 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { cur_prefix_2 += A [ i ] ; if ( cur_prefix_2 == 0 parity * cur_prefix_2 < 0 ) { minOperationsCase2 += Math . abs ( parity - cur_prefix_2 ) ; cur_prefix_2 = parity ; } parity *= - 1 ; } return Math . min ( minOperationsCase1 , minOperationsCase2 ) ; } let A = [ 1 , - 3 , 1 , 0 ] ; let N = A . length ; document . write ( minimumOperations ( A , N ) ) ;

Index/key variables: i
Accumulator variables: cur_prefix_1, cur_prefix_2, i, minOperationsCase1, minOperationsCase2, parity
Iterator variables: i
Javascript program:
function maximumSumArray ( arr , N ) { let arrA = new Array ( N ) ; let ans = new Array ( N ) ; let maxSum = 0 ; for ( let i = 0 ; i < N ; i ++ ) arrA [ i ] = arr [ i ] ; for ( let i = 0 ; i < N ; i ++ ) { let arrB = new Array ( N ) ; let maximum = arrA [ i ] ; arrB [ i ] = maximum ; for ( let j = i - 1 ; j >= 0 ; j -- ) { arrB [ j ] = Math . min ( maximum , arrA [ j ] ) ; maximum = arrB [ j ] ; } maximum = arrA [ i ] ; for ( let j = i + 1 ; j < N ; j ++ ) { arrB [ j ] = Math . min ( maximum , arrA [ j ] ) ; maximum = arrB [ j ] ; } let sum = 0 ; for ( let j = 0 ; j < N ; j ++ ) sum += arrB [ j ] ; if ( sum > maxSum ) { maxSum = sum ; ans = arrB ; } } for ( let val of ans ) { document . write ( val + " " ) ; } } let A = [ 10 , 6 , 8 ] ; let N = A . length ; maximumSumArray ( A , N ) ;

Index/key variables: i, j
Accumulator variables: i, j, sum
Iterator variables: i, j, val
Javascript program:
function minProductUtil ( R1 , B1 , R2 , B2 , M ) { let x = Math . min ( R1 - B1 , M ) ; M -= x ; R1 -= x ; if ( M > 0 ) { let y = Math . min ( R2 - B2 , M ) ; M -= y ; R2 -= y ; } return R1 * R2 ; } function minProduct ( R1 , B1 , R2 , B2 , M ) { let res1 = minProductUtil ( R1 , B1 , R2 , B2 , M ) ; let res2 = minProductUtil ( R2 , B2 , R1 , B1 , M ) ; return Math . min ( res1 , res2 ) ; } let R1 = 21 , B1 = 10 , R2 = 13 , B2 = 11 , M = 3 ; document . write ( ( minProduct ( R1 , B1 , R2 , B2 , M ) ) ) ;

Accumulator variables: M, R1, R2
Javascript program:
function maxAmount ( n , k , arr ) { let A = new Array ( 1000001 ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { A [ arr [ i ] ] ++ ; } let j = 0 ; for ( let i = 0 ; i < 1000001 ; i ++ ) { while ( A [ i ] != 0 ) { arr [ j ++ ] = i ; A [ i ] -- ; } } let ans = 0 ; let mod = 1e9 + 7 ; let i = n - 1 ; j = n - 2 ; while ( k > 0 && j >= 0 ) { if ( arr [ i ] > arr [ j ] ) { ans = ans + Math . min ( k , ( i - j ) ) * arr [ i ] ; k = k - ( i - j ) ; arr [ i ] -- ; } else { while ( j >= 0 && arr [ j ] == arr [ i ] ) j -- ; if ( j < 0 ) break ; ans = ans + Math . min ( k , ( i - j ) ) * arr [ i ] ; k = k - ( i - j ) ; arr [ i ] -- ; } } while ( k > 0 && arr [ i ] != 0 ) { ans = ans + Math . min ( n , k ) * arr [ i ] ; k -= n ; arr [ i ] -- ; } ans = ans % mod ; let x = ans ; return x ; } let n = 5 ; let k = 3 ; let arr = [ 4 , 3 , 6 , 2 , 4 ] ; let ans = maxAmount ( n , k , arr ) ; document . write ( ans ) ;

Index/key variables: i, j
Accumulator variables: i, j, k
Iterator variables: i
Javascript program:
function maxAverage ( A , N , X , Y ) { A . sort ( function ( a , b ) { return a - b ; } ) let sum = 0 ; let res = 0 ; let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; count ++ ; if ( count == X ) { if ( N - i - 1 < X ) { i ++ ; let cnt = 0 ; while ( i < N ) { cnt ++ ; sum += A [ i ] ; i ++ ; } X = X + cnt ; res += sum / X ; break ; } res += sum / X ; sum = 0 ; count = 0 ; } } document . write ( res . toPrecision ( 3 ) ) } let A = [ 4 , 10 , 6 , 5 ] ; let N = A . length ; let X = 2 , Y = 3 ; maxAverage ( A , N , X , Y ) ;

Index/key variables: i
Accumulator variables: cnt, count, i, res, sum
Iterator variables: i
Javascript program:
function findMaximumPoints ( N , X , H ) { let ans = 0 ; let prev = Number . MIN_VALUE ; for ( let i = 0 ; i < N ; ++ i ) { if ( prev < ( X [ i ] - H [ i ] ) ) { ++ ans ; prev = X [ i ] ; } else if ( i == N - 1 || ( X [ i ] + H [ i ] ) < X [ i + 1 ] ) { ++ ans ; prev = X [ i ] + H [ i ] ; } else { prev = X [ i ] ; } } ans ++ ; return ans ; } let X = [ 1 , 2 , 3 ] ; let H = [ 2 , 5 , 5 ] ; let N = X . length ; document . write ( findMaximumPoints ( N , X , H ) ) ;

Index/key variables: i
Accumulator variables: ans
Iterator variables: i
Javascript program:
function sumOfCombinationOf4OR5 ( arr , N ) { let ans = new Array ( N ) . fill ( - 1 ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 4 ) { continue ; } let sum = Number . MAX_SAFE_INTEGER , cnt = 0 ; for ( let j = 0 ; j <= arr [ i ] ; j += 4 ) { if ( ( arr [ i ] - j ) % 5 == 0 ) { sum = Math . min ( sum , cnt + Math . floor ( ( arr [ i ] - j ) / 5 ) ) ; } cnt ++ ; } if ( sum != Number . MAX_SAFE_INTEGER ) ans [ i ] = sum ; } for ( let num of ans ) document . write ( num + " " ) ; } let arr = [ 7 , 15 , 17 , 22 ] ; let N = arr . length ; sumOfCombinationOf4OR5 ( arr , N ) ;

Index/key variables: i
Accumulator variables: cnt, i, j
Iterator variables: i, j, num
Javascript program:
function printBinaryString ( arr , N ) { for ( let i = 1 ; i <= N ; i ++ ) { if ( i % 2 ) { document . write ( 0 ) ; } else { document . write ( 1 ) ; } } } let N = 5 , M = 3 ; let arr = [ [ 1 , 3 ] , [ 2 , 4 ] , [ 2 , 5 ] ] ; printBinaryString ( arr , N ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function maxZeroes ( A , B ) { let mp = new Map ( ) ; let N = A . length ; let ans = 0 ; let cnt = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let num = - B [ i ] ; let den = A [ i ] ; let gc = __gcd ( num , den ) ; if ( den != 0 ) { num /= gc ; den /= gc ; } if ( num <= 0 ) { num *= - 1 ; den *= - 1 ; } if ( den == 0 && num == 0 ) cnt ++ ; if ( den != 0 ) { if ( mp . has ( ` ${ num } ${ den } ` ) ) { mp . set ( ` ${ num } ${ den } ` , mp . get ( ` ${ num } ${ den } ` ) + 1 ) } else { mp . set ( ` ${ num } ${ den } ` , 1 ) } ans = Math . max ( mp . get ( ` ${ num } ${ den } ` ) , ans ) ; } } document . write ( ans + cnt + " " ) ; } let A = [ 1 , 2 , - 1 ] ; let B = [ - 6 , - 12 , 6 ] ; maxZeroes ( A , B ) ;

Index/key variables: i
Accumulator variables: cnt, den, i, num
Iterator variables: i
Javascript program:
function MinCost ( days , cost , N ) { let size = days [ N - 1 ] + 1 ; let dp = new Array ( size ) ; dp [ size - 1 ] = Math . min ( cost [ 0 ] , Math . min ( cost [ 1 ] , cost [ 2 ] ) ) ; let ptr = N - 2 ; for ( let i = size - 2 ; i > 0 ; i -- ) { if ( ptr >= 0 && days [ ptr ] == i ) { let val1 = dp [ i + 1 ] + cost [ 0 ] ; let val2 = cost [ 1 ] + ( ( i + 7 >= size ) ? 0 : dp [ i + 7 ] ) ; let val3 = cost [ 2 ] + ( ( i + 30 >= size ) ? 0 : dp [ i + 30 ] ) ; dp [ i ] = Math . min ( val1 , Math . min ( val2 , val3 ) ) ; ptr -- ; } else { dp [ i ] = dp [ i + 1 ] ; } } return dp [ 1 ] ; } let arr = [ 2 , 4 , 6 , 7 , 8 , 10 , 17 ] ; let cost = [ 3 , 8 , 20 ] ; let N = arr . length ; document . write ( MinCost ( arr , cost , N ) ) ;

Index/key variables: i, ptr
Accumulator variables: i, ptr
Iterator variables: i
Javascript program:
function minElements ( arr , N , K ) { let count = 0 ; let requiredNum = 1 ; let i = 0 ; while ( requiredNum <= K ) { if ( i < N && requiredNum >= arr [ i ] ) { requiredNum += arr [ i ] ; i ++ ; } else { count ++ ; requiredNum += requiredNum ; } } return count ; } let arr = [ 1 , 3 ] ; let K = 6 ; let N = arr . length ; document . write ( minElements ( arr , N , K ) + " " ) ;

Index/key variables: i
Accumulator variables: count, i, requiredNum
Javascript program:
function comp ( p1 , p2 ) { let a = p1 [ 0 ] , b = p1 [ 1 ] ; let c = p2 [ 0 ] , d = p2 [ 1 ] ; let val1 = 0 , val2 = 0 ; val1 = Math . floor ( a / b ) ; val2 = Math . floor ( c / d ) ; return val1 > val2 ; } function maximumProfit ( A , B , N , W ) { let V = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { let temp = Math . sqrt ( A [ i ] ) ; if ( temp * temp == A [ i ] ) continue ; V . push ( [ B [ i ] , A [ i ] ] ) ; } V . sort ( comp ) ; let profit = 0.00 ; for ( let i = 0 ; i < V . length ; i ++ ) { if ( V [ i ] [ 1 ] <= W ) { profit += V [ i ] [ 0 ] ; W -= V [ i ] [ 1 ] ; } else { profit += V [ i ] [ 0 ] * ( W / V [ i ] [ 1 ] ) ; break ; } } return profit . toFixed ( 5 ) ; } let N = 3 ; let W = 10 ; let A = [ 4 , 5 , 7 ] ; let B = [ 8 , 5 , 4 ] ; document . write ( maximumProfit ( A , B , N , W ) + " " ) ;

Index/key variables: i
Accumulator variables: W, i, profit
Iterator variables: i
Javascript program:
function maximumCount ( arr , N ) { let odd = 0 ; let even = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 ) odd ++ ; else even ++ ; } if ( odd && even ) return N - 2 ; else return N - 1 ; } let arr = [ 9 , 13 , 15 , 3 , 16 , 9 , 13 , 18 ] ; let N = arr . length ; document . write ( maximumCount ( arr , N ) ) ;

Index/key variables: i
Accumulator variables: even, i, odd
Iterator variables: i
Javascript program:
function minPossibleValue ( N , K , X ) { for ( let i = 1 ; i <= N ; i ++ ) { if ( X <= K ) { X = X * 2 ; } else { X = X + K ; } } return X ; } let N = 7 , K = 4 , X = 1 ; document . write ( minPossibleValue ( N , K , X ) ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function findMissingValue ( arr , N ) { let minE = Number . MAX_VALUE , maxE = Number . MIN_VALUE ; for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] == - 1 && arr [ i + 1 ] != - 1 ) { minE = Math . min ( minE , arr [ i + 1 ] ) ; maxE = Math . max ( maxE , arr [ i + 1 ] ) ; } if ( arr [ i ] != - 1 && arr [ i + 1 ] == - 1 ) { minE = Math . min ( minE , arr [ i ] ) ; maxE = Math . max ( maxE , arr [ i ] ) ; } } if ( minE == Number . MAX_VALUE && maxE == Number . MIN_VALUE ) { document . write ( " " ) ; } else { document . write ( ( minE + maxE ) / 2 ) ; } } let arr = [ 1 , - 1 , - 1 , - 1 , 5 ] ; let N = arr . length ; findMissingValue ( arr , N ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function findLastElement ( N , A ) { let l = A . length ; let j = 0 ; while ( N > l * ( Math . pow ( 2 , j ) ) ) { N = N - l * Math . pow ( 2 , j ) ; j += 1 ; } let k = 1 ; let r = Math . pow ( 2 , j ) ; for ( let i = 1 ; i < l ; i ++ ) { if ( N > r * i ) k += 1 ; } for ( let i = 0 ; i < l ; i ++ ) { if ( i + 1 == k ) { document . write ( A [ i ] ) ; return ; } } } let K = 7 ; let A = [ 1 , 2 , 3 ] ; findLastElement ( K , A ) ;

Index/key variables: i
Accumulator variables: i, j, k
Iterator variables: i
Javascript program:
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function findMaxD ( arr , N , K ) { for ( let i = 0 ; i < N ; i ++ ) { arr [ i ] = Math . abs ( arr [ i ] - K ) ; } let D = arr [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { D = gcd ( D , arr [ i ] ) ; } return D ; } let arr = [ 1 , 7 , 11 ] ; let N = arr . length ; let K = 3 ; document . write ( findMaxD ( arr , N , K ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function maxOperations ( S , N ) { let X = 0 ; let Y = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' ) break ; Y ++ ; } for ( let i = N - 1 ; i >= 0 ; i -- ) { if ( S [ i ] == ' ' ) break ; X ++ ; } if ( N == X + Y ) return 0 ; return N - ( X + Y ) - 1 ; } let S = " " ; let N = S . length ; document . write ( maxOperations ( S , N ) ) ;

Index/key variables: i
Accumulator variables: X, Y, i
Iterator variables: i
Javascript program:
function maximumSum ( S , N , K ) { let sum = 0 ; let freq = Array . from ( { length : 256 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { freq [ S [ i ] . charCodeAt ( ) ] ++ ; } freq . sort ( ( a , b ) => b - a ) ; for ( let i = 0 ; i < 256 ; i ++ ) { if ( K > freq [ i ] ) { sum += freq [ i ] * freq [ i ] ; K -= freq [ i ] ; } else { sum += freq [ i ] * K ; break ; } } return sum ; } let S = " " ; let K = 10 ; let N = S . length ; document . write ( maximumSum ( S . split ( ' ' ) , N , K ) )

Index/key variables: i
Accumulator variables: K, i, sum
Iterator variables: i
Javascript program:
function possibleNumbers ( numbers , N , M , A , B ) { if ( M == 0 ) { numbers . add ( N ) ; return ; } possibleNumbers ( numbers , N + A , M - 1 , A , B ) ; possibleNumbers ( numbers , N + B , M - 1 , A , B ) ; } var N = 5 , M = 3 , A = 4 , B = 6 ; var numbers = new Set ( ) ; possibleNumbers ( numbers , N , M , A , B ) ; for ( let x of numbers ) { document . write ( x + ' ' ) ; }

Iterator variables: x
Javascript program:
function maxSum ( arr , n ) { let sum = 0 ; let max_neg = Number . MAX_SAFE_INTEGER ; let tempsum = 0 ; let small = Number . MAX_SAFE_INTEGER ; for ( let i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; small = Math . min ( small , arr [ i ] ) ; if ( arr [ i ] > 0 ) { tempsum = 0 ; } else { tempsum += arr [ i ] ; } max_neg = Math . min ( max_neg , tempsum ) ; } if ( max_neg == 0 ) { max_neg = small ; } document . write ( sum - max_neg ) ; } let arr = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ] ; let n = arr . length ; maxSum ( arr , n ) ;

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