dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
const MAX = 10000 ; var primes = [ ] ; function sieveSundaram ( ) { let marked = new Array ( MAX / 2 + 100 ) . fill ( false ) ; for ( let i = 1 ; i <= ( Math . sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( let j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked [ j ] = true ; primes . push ( 2 ) ; for ( let i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . push ( 2 * i + 1 ) ; } function isSmith ( n ) { let original_no = n ; let pDigitSum = 0 ; for ( let i = 0 ; primes [ i ] <= Math . floor ( n / 2 ) ; i ++ ) { while ( n % primes [ i ] == 0 ) { let p = primes [ i ] ; n = Math . floor ( n / p ) ; while ( p > 0 ) { pDigitSum += ( p % 10 ) ; p = Math . floor ( p / 10 ) ; } } } if ( n != 1 && n != original_no ) { while ( n > 0 ) { pDigitSum = pDigitSum + n % 10 ; n = Math . floor ( n / 10 ) ; } } let sumDigits = 0 ; while ( original_no > 0 ) { sumDigits = sumDigits + original_no % 10 ; original_no = Math . floor ( original_no / 10 ) ; } return ( pDigitSum == sumDigits ) ; } function isSmithBrotherPair ( X , Y ) { return isSmith ( X ) && isSmith ( Y ) ; } function countSmithBrotherPairs ( A , N ) { let count = 0 ; A . sort ( function ( a , b ) { return a - b } ) ; for ( let i = 0 ; i < N - 2 ; i ++ ) if ( isSmithBrotherPair ( A [ i ] , A [ i + 1 ] ) ) count ++ ; return count ; } sieveSundaram ( ) ; let A = [ 728 , 729 , 28 , 2964 , 2965 ] ; let N = A . length ; document . write ( countSmithBrotherPairs ( A , N ) ) ;

Index/key variables: i, j
Accumulator variables: count, i, pDigitSum
Iterator variables: i, j
Javascript program:
function countPairs ( arr , N ) { let count = 0 , totalSum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { totalSum += arr [ i ] ; } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { if ( totalSum % ( arr [ i ] + arr [ j ] ) == 0 ) { count += 1 ; } } } document . write ( count ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let N = arr . length ; countPairs ( arr , N ) ;

Index/key variables: i, j
Accumulator variables: count, i, j, totalSum
Iterator variables: i, j
Javascript program:
function change ( pass , total ) { let currentPassRatio , newPassRatio ; let increase ; currentPassRatio = ( pass ) / total ; newPassRatio = ( ( pass + 1 ) ) / ( total + 1 ) ; increase = newPassRatio - currentPassRatio ; return increase ; } function maximumAverage ( v , M , N ) { let sum = 0 ; let increase , average ; let pq = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { increase = change ( v [ i ] [ 0 ] , v [ i ] [ 1 ] ) ; pq . push ( [ increase , i ] ) ; average = v [ i ] [ 0 ] * 1.0 / v [ i ] [ 1 ] ; sum += average ; } pq . sort ( function ( a , b ) { return a [ 0 ] - b [ 0 ] } ) ; while ( M > 0 ) { sum += pq [ pq . length - 1 ] [ 0 ] ; let i = pq [ pq . length - 1 ] [ 1 ] ; pq . pop ( ) ; v [ i ] [ 0 ] += 1 ; v [ i ] [ 1 ] += 1 ; pq . push ( [ change ( v [ i ] [ 0 ] , v [ i ] [ 1 ] ) , i ] ) ; M -- ; pq . sort ( function ( a , b ) { return a [ 0 ] - b [ 0 ] } ) ; } let ans = sum / N ; return ans ; } let V = [ [ 1 , 2 ] , [ 3 , 5 ] , [ 2 , 2 ] ] ; let M = 2 ; let N = V . length ; document . write ( maximumAverage ( V , M , N ) . toFixed ( 6 ) ) ;

Index/key variables: i
Accumulator variables: M, i, sum
Iterator variables: i
Javascript program:
function gcd ( a , b ) { return b == 0 ? a : gcd ( b , a % b ) ; } function printUnitaryDivisors ( n ) { for ( var i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i && gcd ( i , n / i ) == 1 ) { document . write ( i + " " ) ; } else { if ( gcd ( i , n / i ) == 1 ) { document . write ( i + " " + n / i + " " ) ; } } } } } var N = 12 ; printUnitaryDivisors ( N ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function gcd ( a , b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } function largestK ( A , N ) { let l = 0 , r = N - 1 , flag = 0 ; while ( l < r ) { if ( A [ l ] != A [ r ] ) { flag = 1 ; break ; } l ++ ; r -- ; } if ( flag == 0 ) return - 1 ; let K = Math . abs ( A [ 0 ] - A [ N - 1 ] ) ; for ( let i = 1 ; i < N / 2 ; i ++ ) K = gcd ( K , Math . abs ( A [ i ] - A [ N - i - 1 ] ) ) ; return K ; } let A = [ 1 , 2 , 3 , 2 , 1 ] ; let N = A . length ; document . write ( largestK ( A , N ) + " " ) ;

Index/key variables: i, l, r
Accumulator variables: i, l, r
Iterator variables: i
Javascript program:
function PermuteAndFind ( power , idx , SumSoFar , target ) { if ( idx == power . length ) { if ( SumSoFar == target ) return true ; return false ; } let select = PermuteAndFind ( power , idx + 1 , SumSoFar , target ) ; let notselect = PermuteAndFind ( power , idx + 1 , SumSoFar + power [ idx ] , target ) ; return select || notselect ; } function DistinctPowersOf3 ( N ) { let power = new Array ( 16 ) ; power [ 0 ] = 1 ; for ( let i = 1 ; i < 16 ; i ++ ) power [ i ] = 3 * power [ i - 1 ] ; let found = PermuteAndFind ( power , 0 , 0 , N ) ; if ( found == true ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let N = 91 ; DistinctPowersOf3 ( N ) ;

Index/key variables: i, idx
Accumulator variables: i
Iterator variables: i
Javascript program:
function game_with_number ( arr , n ) { for ( let i = 0 ; i < n - 1 ; i ++ ) { arr [ i ] = arr [ i ] ^ arr [ i + 1 ] ; } return arr ; } function print ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } let arr = [ 10 , 11 , 1 , 2 , 3 ] ; let n = arr . length ; let new_arr = game_with_number ( arr , n ) ; print ( new_arr , n ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function currSubArrayScore ( a , b , l , r ) { let straightScore = 0 ; let reverseScore = 0 ; for ( let i = l ; i <= r ; i ++ ) { straightScore += a [ i ] * b [ i ] ; reverseScore += a [ r - ( i - l ) ] * b [ i ] ; } return Math . max ( straightScore , reverseScore ) ; } function maxScoreSubArray ( a , b , n ) { let res = 0 , start = 0 , end = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i ; j < n ; j ++ ) { let currScore = currSubArrayScore ( a , b , i , j ) ; if ( currScore > res ) { res = currScore ; start = i ; end = j ; } } } document . write ( res ) ; } let A = [ 13 , 4 , 5 ] ; let B = [ 10 , 22 , 2 ] ; let N = A . length ; maxScoreSubArray ( A , B , N ) ;

Index/key variables: i
Accumulator variables: i, j, reverseScore, straightScore
Iterator variables: i, j
Javascript program:
function maxScoreSubArray ( a , b , n ) { let res = 0 ; for ( let mid = 0 ; mid < n ; mid ++ ) { let straightScore = a [ mid ] * b [ mid ] , reverseScore = a [ mid ] * a [ mid ] ; let prev = mid - 1 , next = mid + 1 ; res = Math . max ( res , Math . max ( straightScore , reverseScore ) ) ; while ( prev >= 0 && next < n ) { straightScore += ( a [ prev ] * b [ prev ] + a [ next ] * b [ next ] ) ; reverseScore += ( a [ prev ] * b [ next ] + a [ next ] * b [ prev ] ) ; res = Math . max ( res , Math . max ( straightScore , reverseScore ) ) ; prev -- ; next ++ ; } straightScore = 0 ; reverseScore = 0 ; prev = mid - 1 , next = mid ; while ( prev >= 0 && next < n ) { straightScore += ( a [ prev ] * b [ prev ] + a [ next ] * b [ next ] ) ; reverseScore += ( a [ prev ] * b [ next ] + a [ next ] * b [ prev ] ) ; res = Math . max ( res , Math . max ( straightScore , reverseScore ) ) ; prev -- ; next ++ ; } } document . write ( res ) ; } let A = [ 13 , 4 , 5 ] ; let B = [ 10 , 22 , 2 ] ; let N = A . length maxScoreSubArray ( A , B , N ) ;

Index/key variables: mid, next, prev
Accumulator variables: mid, next, prev, reverseScore, straightScore
Iterator variables: mid
Javascript program:
function minimumSizeSubarray ( arr , N ) { let i = 0 , j = N - 1 ; while ( i < N && arr [ i ] == 0 ) { i ++ ; } if ( i == N ) return 1 ; while ( j >= 0 && arr [ j ] == 0 ) { j -- ; } return ( j - i + 1 ) ; } let arr = [ 0 , 2 , 0 , 0 , 12 , 0 , 0 , 0 ] ; let N = arr . length ; document . write ( minimumSizeSubarray ( arr , N ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Javascript program:
function countValues ( A , B , C ) { if ( B >= A ) { document . write ( 0 ) ; return ; } if ( B == 0 ) { document . write ( parseInt ( C / A ) ) ; return ; } let ans = parseInt ( C / A ) ; if ( ans * A + B <= C ) { ans ++ ; } document . write ( ans ) ; } let A = 6 , B = 3 , N = 15 ; countValues ( A , B , N ) ;

Accumulator variables: ans
Javascript program:
function maximumSum ( arr , N ) { let ans = 0 ; for ( let bit = 0 ; bit < 32 ; bit ++ ) { let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] & ( 1 << bit ) ) { sum += arr [ i ] ; } } ans = Math . max ( ans , sum ) ; } return ans ; } let arr = [ 5 , 4 , 1 , 7 , 11 ] ; let N = arr . length ; document . write ( maximumSum ( arr , N ) ) ;

Index/key variables: i
Accumulator variables: bit, i, sum
Iterator variables: bit, i
Javascript program:
function maxCount ( arr , N , K ) { let st = new Set ( ) ; for ( let i = 0 ; i < N ; i ++ ) { st . add ( arr [ i ] ) ; } if ( N - st . size <= K ) { document . write ( " " ) ; } else document . write ( " " ) ; } let arr = [ 2 , 2 , 2 , 3 ] ; let K = 3 ; let N = arr . length maxCount ( arr , N , K ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function max_sum ( a , v , q , n ) { let x = 0 ; let m = Number . MIN_SAFE_INTEGER ; for ( let i = 0 ; i < q ; i ++ ) { let p , q , k ; p = v [ i ] [ 0 ] [ 0 ] ; q = v [ i ] [ 0 ] [ 1 ] ; k = v [ i ] [ 1 ] ; a [ p ] += k ; if ( q + 1 <= n ) a [ q + 1 ] -= k ; } for ( let i = 1 ; i <= n ; i ++ ) { x += a [ i ] ; m = Math . max ( m , x ) ; } return m ; } let n = 10 , q = 3 ; let a = new Array ( n + 5 ) . fill ( 0 ) ; let v = new Array ( q ) . fill ( 0 ) . map ( ( ) => new Array ( 2 ) . fill ( 0 ) . map ( ( ) => new Array ( 2 ) . fill ( 0 ) ) ) ; v [ 0 ] [ 0 ] [ 0 ] = 1 ; v [ 0 ] [ 0 ] [ 1 ] = 5 ; v [ 0 ] [ 1 ] = 3 ; v [ 1 ] [ 0 ] [ 0 ] = 4 ; v [ 1 ] [ 0 ] [ 1 ] = 8 ; v [ 1 ] [ 1 ] = 7 ; v [ 2 ] [ 0 ] [ 0 ] = 6 ; v [ 2 ] [ 0 ] [ 1 ] = 9 ; v [ 2 ] [ 1 ] = 1 ; document . write ( max_sum ( a , v , q , n ) ) ;

Index/key variables: i, p
Accumulator variables: i, x
Iterator variables: i
Javascript program:
function findWinner ( X , Y ) { let playerA = ( X ^ Y ) ; let flag = false ; for ( let i = 1 ; i <= X ; i ++ ) { for ( let j = 1 ; j <= Y ; j ++ ) { let val = ( i ^ j ) ; if ( val > playerA ) { flag = true ; break ; } } if ( flag ) { break ; } } if ( flag ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let A = 2 , B = 4 ; findWinner ( A , B ) ;

Accumulator variables: i, j
Iterator variables: i, j
Boolean variables: flag
Javascript program:
function prefixFactorialArray ( A , N ) { for ( let i = 1 ; i < N ; i ++ ) { A [ i ] += A [ i - 1 ] ; } let fact = new Array ( A [ N - 1 ] + 1 ) ; fact [ 0 ] = 1 ; for ( let i = 1 ; i <= A [ N - 1 ] ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; } for ( let i = 0 ; i < N ; i ++ ) { A [ i ] = fact [ A [ i ] ] ; } for ( let i = 0 ; i < N ; i ++ ) { document . write ( A [ i ] + " " ) ; } } let arr = [ 1 , 2 , 3 , 4 ] ; let N = arr . length prefixFactorialArray ( arr , N ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function maxFrequency ( arr , N , K ) { arr . sort ( ( a , b ) => a - b ) ; let start = 0 , end = 0 ; let sum = 0 , res = 0 ; for ( end = 0 ; end < N ; end ++ ) { sum += arr [ end ] ; while ( ( end - start + 1 ) * arr [ end ] - sum > K ) { sum -= arr [ start ] ; start ++ ; } res = Math . max ( res , end - start + 1 ) ; } document . write ( res + " " ) ; } let arr = [ 1 , 4 , 8 , 13 ] ; let N = 4 ; let K = 5 ; maxFrequency ( arr , N , K ) ;

Index/key variables: end, start
Accumulator variables: end, start, sum
Iterator variables: end
Javascript program:
function countPairs ( arr , n , x ) { let count = 0 ; let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( arr [ i ] - x * i ) ) { mp . set ( arr [ i ] - x * i , mp . get ( arr [ i ] - x * i ) + 1 ) } else { mp . set ( arr [ i ] - x * i , 1 ) } } for ( let x of mp ) { let n = x [ 1 ] ; count += Math . floor ( ( n * ( n - 1 ) ) / 2 ) ; } document . write ( count ) ; } let n = 6 , x = 3 ; let arr = [ 5 , 4 , 8 , 11 , 13 , 16 ] ; countPairs ( arr , n , x ) ;

Index/key variables: i
Accumulator variables: count, i
Iterator variables: i, x
Javascript program:
function recreationalSpot ( arr , N ) { if ( N < 3 ) { return " " ; } var preMin = new Array ( N ) ; preMin [ 0 ] = arr [ 0 ] ; var i ; for ( i = 1 ; i < N ; i ++ ) { preMin [ i ] = Math . min ( preMin [ i - 1 ] , arr [ i ] ) ; } var st = [ ] ; var j ; for ( j = N - 1 ; j >= 0 ; j -- ) { if ( arr [ j ] > preMin [ j ] ) { while ( st . length > 0 && st [ st . length - 1 ] <= preMin [ j ] ) { st . pop ( ) ; } if ( st . length > 0 && st [ st . length - 1 ] < arr [ j ] ) { return " " ; } st . push ( arr [ j ] ) ; } } return " " ; } var arr = [ 4 , 7 , 11 , 5 , 13 , 2 ] ; var size = arr . length ; document . write ( recreationalSpot ( arr , size ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function reverseBin ( N ) { let S = " " ; let i ; for ( i = 0 ; i < 32 ; i ++ ) { if ( N & ( 1 << i ) ) S += ' ' ; else S += ' ' ; } S = S . split ( " " ) . reverse ( ) . join ( " " ) ; let M = 0 ; for ( i = 0 ; i < 32 ; i ++ ) { if ( S [ i ] == ' ' ) M += ( 1 << i ) ; } return M ; } function maximumOfTwo ( N ) { let M = reverseBin ( N ) ; return Math . max ( N , M ) ; } let N = 6 ; document . write ( maximumOfTwo ( N ) ) ;

Index/key variables: i
Accumulator variables: M, S, i
Iterator variables: i
Javascript program:
function minTime ( A , n , K ) { let max_ability = A [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { max_ability = Math . max ( max_ability , A [ i ] ) ; } let tmp = Array . from ( { length : max_ability + 1 } , ( _ , i ) => 0 ) ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { tmp [ A [ i ] ] ++ ; } for ( let i = max_ability ; i >= 0 ; i -- ) { if ( tmp [ i ] != 0 ) { if ( tmp [ i ] * i < K ) { K -= ( i * tmp [ i ] ) ; tmp [ ( i / 2 ) ] += tmp [ i ] ; count += tmp [ i ] ; if ( K <= 0 ) { return count ; } } else { if ( K % i != 0 ) { count += Math . floor ( K / i ) + 1 ; } else { count += Math . floor ( K / i ) ; } return count ; } } } return - 1 ; } let arr = [ 3 , 1 , 7 , 2 , 4 ] ; let N = arr . length ; let K = 15 ; document . write ( minTime ( arr , N , K ) ) ;

Index/key variables: i
Accumulator variables: K, count, i
Iterator variables: i
Javascript program:
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function findlcm ( arr , n ) { let ans = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { ans = ( ( ( arr [ i ] * ans ) ) / ( gcd ( arr [ i ] , ans ) ) ) ; } return ans ; } function minimumRod ( A , N ) { document . write ( N * findlcm ( A , N ) ) ; } let arr = [ 1 , 2 ] ; let N = arr . length ; minimumRod ( arr , N ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function findDigit ( N , D ) { while ( N > 0 ) { let a = N % 10 ; if ( a == D ) { return true ; } N = Math . floor ( N / 10 ) ; } return false ; } function check ( N , D ) { while ( N > 0 ) { if ( findDigit ( N , D ) == true ) { return true ; } N -= D ; } return false ; } let N = 24 ; let D = 7 ; if ( check ( N , D ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }

Accumulator variables: N
Javascript program:
function minOperations ( N ) { let arr = Array . from ( { length : N } , ( _ , i ) => 0 ) ; let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { arr [ i ] = ( 2 * i ) + 1 ; sum = sum + arr [ i ] ; } let mid = 0 ; if ( N % 2 == 0 ) { mid = sum / N ; } else { mid = arr [ N / 2 ] ; } let ans = 0 ; for ( let i = 0 ; i < N / 2 ; i ++ ) { ans += mid - arr [ i ] ; } return ans ; } let N = 6 ; document . write ( minOperations ( N ) ) ;

Index/key variables: i
Accumulator variables: ans, i
Iterator variables: i
Javascript program:
function findXORS ( arr1 , arr2 , N , M ) { let res = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { let temp = arr1 [ i ] & arr2 [ j ] ; res ^= temp ; } } return res ; } let arr1 = [ 1 , 2 , 3 ] ; let arr2 = [ 6 , 5 ] ; let N = arr1 . length ; let M = arr2 . length ; document . write ( findXORS ( arr1 , arr2 , N , M ) ) ;

Index/key variables: i, j
Accumulator variables: i, j, res
Iterator variables: i, j
Javascript program:
function findXORS ( arr1 , arr2 , N , M ) { let XORS1 = 0 ; let XORS2 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { XORS1 ^= arr1 [ i ] ; } for ( let i = 0 ; i < M ; i ++ ) { XORS2 ^= arr2 [ i ] ; } return XORS1 && XORS2 ; } let arr1 = [ 1 , 2 , 3 ] ; let arr2 = [ 6 , 5 ] ; let N = arr1 . length ; let M = arr2 . length ; document . write ( findXORS ( arr1 , arr2 , N , M ) ) ;

Index/key variables: i
Accumulator variables: XORS1, XORS2, i
Iterator variables: i
Javascript program:
function isPowerof2 ( n ) { return ( ( n & ( n - 1 ) ) && n ) ; } function countNum ( N ) { let count = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { let flag = isPowerof2 ( i ) ; if ( ! flag ) { count ++ ; } } document . write ( count + " " ) ; } let N = 100 ; countNum ( N ) ;

Accumulator variables: count, i
Iterator variables: i
Javascript program:
class ListNode { constructor ( ) { this . val = 0 ; this . next = null ; } } ; function pushIn ( head_ref , node_val ) { var new_node = new ListNode ( ) ; new_node . val = node_val ; new_node . next = ( head_ref ) ; head_ref = new_node ; return head_ref ; } function splitListInParts ( head , K ) { var ans = [ ] ; if ( ! head ) { while ( K -- ) ans . push ( null ) ; } var N = 0 ; var p = head ; while ( p ) { p = p . next ; N ++ ; } var len = parseInt ( N / K ) ; var rem = N % K ; p = head ; while ( K > 0 && p ) { var x = len ; var curr_head = p ; var last = p ; if ( rem > 0 ) { p = p . next ; rem -- ; } while ( x -- ) { if ( last == p ) p = p . next ; else { last . next = p ; last = p ; p = p . next ; } } last . next = null ; ans . push ( curr_head ) ; K -- ; } while ( K > 0 ) { ans . push ( null ) ; K -- ; } document . write ( " " ) ; for ( var i = 0 ; i < ans . length ; i ++ ) { document . write ( " " ) ; while ( ans [ i ] ) { document . write ( ans [ i ] . val + " " ) ; ans [ i ] = ans [ i ] . next ; } document . write ( " " ) ; if ( i != ans . length - 1 ) document . write ( " " ) ; } document . write ( " " ) ; } var root = null ; root = pushIn ( root , 8 ) ; root = pushIn ( root , 7 ) ; root = pushIn ( root , 6 ) ; root = pushIn ( root , 5 ) ; root = pushIn ( root , 4 ) ; root = pushIn ( root , 3 ) ; root = pushIn ( root , 2 ) ; root = pushIn ( root , 1 ) ; var K = 3 ; splitListInParts ( root , K ) ;

Index/key variables: i
Accumulator variables: K, N, i, rem, x
Iterator variables: i
Class/struct names: ListNode
Javascript program:
function nearestPow ( x , base ) { let k = Math . floor ( Math . log ( x ) / Math . log ( base ) ) ; if ( Math . abs ( Math . pow ( base , k ) - x ) < Math . abs ( Math . pow ( base , ( k + 1 ) ) - x ) ) return Math . pow ( base , k ) ; else return Math . pow ( base , ( k + 1 ) ) ; } function replaceWithNearestPowerOfK ( arr , K , n ) { for ( let i = 0 ; i < n ; i ++ ) { let strEle = arr [ i ] . toString ( ) ; for ( let c = 0 ; c < strEle . length ; c ++ ) { if ( ( strEle - ' ' ) == K ) { arr [ i ] = nearestPow ( arr [ i ] , K ) ; break ; } } } for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) } let arr = [ 432 , 953 , 232 , 333 ] ; let n = arr . length ; let K = 3 ; replaceWithNearestPowerOfK ( arr , K , n )

Index/key variables: i
Accumulator variables: c, i
Iterator variables: c, i
Javascript program:
function ceilDifference ( arr , n , x ) { var totalSum = 0 ; var perElementSum = 0 ; for ( var i = 0 ; i < n ; i ++ ) { totalSum += arr [ i ] ; perElementSum += parseInt ( Math . ceil ( ( arr [ i ] ) / ( x ) ) ) ; } var totalCeilSum = parseInt ( Math . ceil ( ( totalSum ) / ( x ) ) ) ; return Math . abs ( perElementSum - totalCeilSum ) ; } var arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; var K = 4 ; var N = arr . length ; document . write ( ceilDifference ( arr , N , K ) ) ;

Index/key variables: i
Accumulator variables: i, perElementSum, totalSum
Iterator variables: i
Javascript program:
function removeTailing ( A , B ) { let N = A + B ; let s = new Array ( ) ; let strsum = N . toString ( ) ; for ( let i = 0 ; i < strsum . length ; i ++ ) { s . push ( strsum . charAt ( i ) ) ; } while ( s [ s . length - 1 ] === ' ' ) { s . pop ( ) ; } let res = " " ; while ( s . length != 0 ) { res = res . concat ( s [ s . length - 1 ] ) ; s . pop ( ) ; } let str = " " ; str = str . concat ( res ) str = str . split ( " " ) . reverse ( ) . join ( " " ) ; return str . toString ( ) ; } let A = 130246 , B = 450164 ; document . write ( removeTailing ( A , B ) ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function countTriplets ( N ) { let ans = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = 1 ; j <= N ; j ++ ) { if ( i * j > N ) break ; ans += Math . floor ( N / ( i * j ) ) ; } } return ans ; } let N = 10 ; document . write ( countTriplets ( N ) ) ;

Accumulator variables: ans, i, j
Iterator variables: i, j
Javascript program:
function countNumbers ( N ) { var count = 0 ; for ( var i = 1 ; i <= N ; i ++ ) { if ( __gcd ( i , N ) != 1 ) count ++ ; } document . write ( count ) ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } var N = 5 ; countNumbers ( N ) ;

Accumulator variables: count, i
Iterator variables: i
Javascript program:
function phi ( N ) { let result = N ; for ( let p = 2 ; p * p <= N ; ++ p ) { if ( N % p == 0 ) { while ( N % p == 0 ) N /= p ; result -= result / p ; } } if ( N > 1 ) result -= result / N ; return result ; } function countNumbers ( N ) { let count = N - phi ( N ) ; document . write ( count ) ; } let N = 5 ; countNumbers ( N ) ;

Accumulator variables: N, result
Iterator variables: p
Javascript program:
function Query ( arr , N , Q ) { let mul = 1 ; let add = 0 ; for ( let i = 0 ; i < Q . length ; i ++ ) { if ( Q [ i ] [ 0 ] == 0 ) { add = add + Q [ i ] [ 1 ] ; } else if ( Q [ i ] [ 0 ] == 1 ) { mul = mul * Q [ i ] [ 1 ] ; add = add * Q [ i ] [ 1 ] ; } else { let ans = arr [ Q [ i ] [ 1 ] ] * mul + add ; document . write ( ans + " " ) ; } } } let arr = [ 3 , 1 , 23 , 45 , 100 ] ; let N = arr . length ; let Q = [ [ 1 , 2 ] , [ 0 , 10 ] , [ 2 , 3 ] , [ 1 , 5 ] , [ 2 , 4 ] ] ; Query ( arr , N , Q ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function nthFibo ( n ) { let a = ( Math . pow ( 5 , 0.5 ) + 1 ) / 2 ; let b = ( - 1 * ( Math . pow ( 5 , 0.5 ) ) + 1 ) / 2 ; let r = Math . pow ( 5 , 0.5 ) ; let ans = ( Math . pow ( a , n ) - Math . pow ( b , n ) ) / r ; return Math . floor ( ans ) ; } function nearFibo ( X ) { let a = ( Math . pow ( 5 , 0.5 ) + 1 ) / 2 ; let n = Math . floor ( Math . log ( ( Math . pow ( 5 , 0.5 ) ) * X ) / Math . log ( a ) ) ; let nth = nthFibo ( n ) ; let nplus = nthFibo ( n + 1 ) ; if ( Math . abs ( X - nth ) < Math . abs ( X - nplus ) ) return nth ; else return nplus ; } function getCost ( arr , n ) { let cost = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let fibo = nearFibo ( arr [ i ] ) ; cost += Math . abs ( arr [ i ] - fibo ) ; } return cost ; } let arr = [ 56 , 34 , 23 , 98 , 7 ] ; let n = arr . length ; document . write ( getCost ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: cost, i
Iterator variables: i
Javascript program:
function getMaxPairSum ( arr , N , K ) { var preMax = Array ( N ) ; preMax [ 0 ] = arr [ 0 ] ; for ( var i = 1 ; i < N ; i ++ ) { preMax [ i ] = Math . max ( preMax [ i - 1 ] , arr [ i ] ) ; } var res = - 1000000000 ; for ( var i = K ; i < N ; i ++ ) { res = Math . max ( res , arr [ i ] + preMax [ i - K ] ) ; } return res ; } var arr = [ 1 , 2 , 4 , 8 , 6 , 3 ] ; var K = 3 ; var N = arr . length ; document . write ( getMaxPairSum ( arr , N , K ) ) ;

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

Index/key variables: i, j
Accumulator variables: count, i, j
Iterator variables: i, j