dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
function printMatrix ( grid , m , n ) { let A = [ ] ; for ( let i = 0 ; i < m ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( j == n - 1 || ( i == m - 1 ) j == 0 i == 0 ) A . push ( grid [ i ] [ j ] ) ; } } A . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( A [ i ] + " " ) ; document . write ( " " ) for ( let i = 0 ; i < m - 2 ; i ++ ) { document . write ( A [ A . length - i - 1 ] + " " ) ; for ( let j = 1 ; j < n - 1 ; j ++ ) document . write ( grid [ i + 1 ] [ j ] + " " ) ; document . write ( A [ n + i ] + " " ) } document . write ( A . slice ( n + m - 2 , n + m - 2 + n ) . reverse ( ) . join ( " " ) ) ; [ n + m - 2 : n + m - 2 + n ] << endl ; } let m = 4 , n = 5 ; let grid = [ [ 1 , 2 , 3 , 4 , 0 ] , [ 1 , 1 , 1 , 1 , 2 ] , [ 1 , 2 , 2 , 2 , 4 ] , [ 1 , 9 , 3 , 1 , 7 ] ] ; printMatrix ( grid , m , n ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function findLargest ( s ) { let len = s . length ; let loccur = new Array ( 26 ) ; loccur . fill ( - 1 ) ; for ( let i = len - 1 ; i >= 0 ; -- i ) { let chI = s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; if ( loccur [ chI ] == - 1 ) { loccur [ chI ] = i ; } } let sorted_s = s . join ( " " ) . split ( ' ' ) ; sorted_s . sort ( ) ; sorted_s . reverse ( ) ; for ( let i = 0 ; i < len ; ++ i ) { if ( s [ i ] != sorted_s [ i ] ) { let chI = sorted_s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; let last_occ = loccur [ chI ] ; let temp = s [ i ] ; s [ i ] = s [ last_occ ] ; s [ last_occ ] = temp ; break ; } } return ( s . join ( " " ) ) ; } let str = " " ; let s = str . split ( ' ' ) ; document . write ( findLargest ( s ) ) ;

Index/key variables: chI, i, last_occ
Iterator variables: i
Javascript program:
function pairInSortedRotated ( arr , n , x ) { let i ; for ( i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] > arr [ i + 1 ] ) break ; let l = ( i + 1 ) % n ; let r = i ; while ( l != r ) { if ( arr [ l ] + arr [ r ] == x ) return true ; if ( arr [ l ] + arr [ r ] < x ) l = ( l + 1 ) % n ; else r = ( n + r - 1 ) % n ; } return false ; } let arr = [ 11 , 15 , 6 , 8 , 9 , 10 ] ; let sum = 16 ; let n = arr . length ; if ( pairInSortedRotated ( arr , n , sum ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;

Index/key variables: i, l, r
Accumulator variables: i
Iterator variables: i
Javascript program:
function pairsInSortedRotated ( arr , n , x ) { let i ; for ( i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] > arr [ i + 1 ] ) break ; let l = ( i + 1 ) % n ; let r = i ; let cnt = 0 ; while ( l != r ) { if ( arr [ l ] + arr [ r ] == x ) { cnt ++ ; if ( l == ( r - 1 + n ) % n ) { return cnt ; } l = ( l + 1 ) % n ; r = ( r - 1 + n ) % n ; } else if ( arr [ l ] + arr [ r ] < x ) l = ( l + 1 ) % n ; else r = ( n + r - 1 ) % n ; } return cnt ; } let arr = [ 11 , 15 , 6 , 7 , 9 , 10 ] ; let sum = 16 ; let n = arr . length ; document . write ( pairsInSortedRotated ( arr , n , sum ) ) ;

Index/key variables: i, l, r
Accumulator variables: cnt, i
Iterator variables: i
Javascript program:
function maxSum ( arr , n ) { let arrSum = 0 ; let currVal = 0 ; for ( let i = 0 ; i < n ; i ++ ) { arrSum = arrSum + arr [ i ] ; currVal = currVal + ( i * arr [ i ] ) ; } let maxVal = currVal ; for ( let j = 1 ; j < n ; j ++ ) { currVal = currVal + arrSum - n * arr [ n - j ] ; if ( currVal > maxVal ) maxVal = currVal ; } return maxVal ; } let arr = [ 10 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ; let n = arr . length ; document . write ( " " + maxSum ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function maxSum ( arr , n ) { var res = Number . MIN_VALUE ; for ( i = 0 ; i < n ; i ++ ) { var curr_sum = 0 ; for ( j = 0 ; j < n ; j ++ ) { var index = ( i + j ) % n ; curr_sum += j * arr [ index ] ; } res = Math . max ( res , curr_sum ) ; } return res ; } var arr = [ 8 , 3 , 1 , 2 ] ; var n = arr . length ; document . write ( maxSum ( arr , n ) ) ;

Index/key variables: index
Accumulator variables: curr_sum, i, j
Iterator variables: i, j
Javascript program:
function maxSum ( arr , n ) { let cum_sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) cum_sum += arr [ i ] ; let curr_val = 0 ; for ( let i = 0 ; i < n ; i ++ ) curr_val += i * arr [ i ] ; let res = curr_val ; for ( let i = 1 ; i < n ; i ++ ) { let next_val = curr_val - ( cum_sum - arr [ i - 1 ] ) + arr [ i - 1 ] * ( n - 1 ) ; curr_val = next_val ; res = Math . max ( res , next_val ) ; } return res ; } let arr = [ 8 , 3 , 1 , 2 ] ; let n = arr . length ; document . write ( maxSum ( arr , n ) + " " ) ;

Index/key variables: i
Accumulator variables: cum_sum, curr_val, i
Iterator variables: i
Javascript program:
function solve ( arr , n ) { let k = arr . length ; let mp = Array . from ( { length : n + 2 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i <= n ; i ++ ) { mp [ i ] = 0 ; } for ( let i = 0 ; i < k ; i ++ ) { mp [ arr [ i ] ] = 1 ; } let leftSegment = arr [ 0 ] - 1 ; for ( let i = 1 ; i < k ; i ++ ) { leftSegment = Math . max ( leftSegment , arr [ i ] - arr [ i - 1 ] - 1 ) ; } let rightSegment = n - arr [ k - 1 ] ; let maxSegment = Math . max ( leftSegment , rightSegment ) ; let tim ; if ( ( maxSegment & 1 ) == 1 ) tim = ( maxSegment / 2 ) + 1 ; else tim = maxSegment / 2 ; return tim ; } let N = 5 ; let arr = [ 1 , 4 ] ; document . write ( solve ( arr , N ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function countRotations ( arr , n ) { let min = arr [ 0 ] , min_index = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( min > arr [ i ] ) { min = arr [ i ] ; min_index = i ; } } return min_index ; } let arr = [ 15 , 18 , 2 , 3 , 6 , 12 ] ; let n = arr . length ; document . write ( countRotations ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function countRotations ( arr , low , high ) { if ( high < low ) return 0 ; if ( high == low ) return low ; let mid = Math . floor ( low + ( high - low ) / 2 ) ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) return ( mid + 1 ) ; if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) return mid ; if ( arr [ high ] > arr [ mid ] ) return countRotations ( arr , low , mid - 1 ) ; return countRotations ( arr , mid + 1 , high ) ; } let arr = [ 15 , 18 , 2 , 3 , 6 , 12 ] ; let n = arr . length ; document . write ( countRotations ( arr , 0 , n - 1 ) ) ;

Index/key variables: high, mid
Javascript program:
function preprocess ( arr , n , temp ) { for ( i = 0 ; i < n ; i ++ ) temp [ i ] = temp [ i + n ] = arr [ i ] ; } function leftRotate ( arr , n , k , temp ) { var start = k % n ; for ( i = start ; i < start + n ; i ++ ) document . write ( temp [ i ] + " " ) ; document . write ( " " ) ; } var arr = [ 1 , 3 , 5 , 7 , 9 ] ; var n = arr . length ; var temp = Array ( 2 * n ) . fill ( 0 ) ; preprocess ( arr , n , temp ) ; var k = 2 ; leftRotate ( arr , n , k , temp ) ; k = 3 ; leftRotate ( arr , n , k , temp ) ; k = 4 ; leftRotate ( arr , n , k , temp ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function Desired_Array ( v ) { let n = v . length ; v . sort ( function ( a , b ) { return a - b } ) ; let i = n - 1 ; while ( i > 0 ) { let p = v [ n - 1 ] ; for ( let j = n - 1 ; j >= i ; j -- ) { v [ j ] = v [ j - 1 ] ; } v [ i ] = p ; i -- ; } for ( let x = 0 ; x < v . length ; x ++ ) { document . write ( v [ x ] + " " ) ; } document . write ( " " ) ; } let v = [ 1 , 2 , 3 , 4 , 5 ] ; Desired_Array ( v ) ; let v1 = [ 1 , 12 , 2 , 10 , 4 , 16 , 6 ] ; Desired_Array ( v1 ) ;

Index/key variables: i, j, x
Accumulator variables: i, j, x
Iterator variables: j, x
Javascript program:
function leftRotate ( arr , n , k ) { for ( let i = k ; i < k + n ; i ++ ) document . write ( arr [ i % n ] + " " ) ; } let arr = [ 1 , 3 , 5 , 7 , 9 ] ; n = arr . length ; k = 2 ; leftRotate ( arr , n , k ) ; document . write ( " " ) ; k = 3 ; leftRotate ( arr , n , k ) ; document . write ( " " ) ; k = 4 ; leftRotate ( arr , n , k ) ; document . write ( " " ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function sortArrays ( arr ) { let length = arr . length ; for ( let j = 0 ; j < length - 1 ; j ++ ) { if ( arr [ j ] > arr [ j + 1 ] ) { let temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; j = - 1 ; } } return arr ; } let arr = [ 1 , 2 , 99 , 9 , 8 , 7 , 6 , 0 , 5 , 4 , 3 ] ; document . write ( " " + ( arr ) . join ( " " ) + " " ) ; arr = sortArrays ( arr ) ; document . write ( " " + arr . join ( " " ) + " " ) ;

Index/key variables: j
Accumulator variables: j
Iterator variables: j
Javascript program:
function sortArrays ( arr ) { let length = arr . length ; for ( let j = 0 ; j < length - 1 ; j ++ ) { let d1 = arr [ j ] ; let d2 = arr [ j + 1 ] ; if ( d1 > d2 ) { let temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; j = - 1 ; } } return arr ; } let geeks = " " ; let arr = geeks . split ( " " ) ; document . write ( " " + arr . join ( " " ) + " " ) ; arr = sortArrays ( arr ) ; document . write ( " " + ( arr ) . join ( " " ) + " " ) ;

Index/key variables: j
Accumulator variables: j
Iterator variables: j
Javascript program:
class Node { constructor ( item ) { this . left = null ; this . right = null ; this . data = item ; } } function findLevel ( root , data ) { if ( data < root . data ) return - 1 ; if ( data == root . data ) return 0 ; let cur_level = 0 ; while ( true ) { cur_level ++ ; root = root . left ; if ( root . data == data ) return - 2 ; if ( root . data < data && ( root . left == null root . left . data > data ) ) { break ; } } return cur_level ; } function traversePath ( root , path ) { for ( let i = 0 ; i < path . length ; i ++ ) { let direction = path [ i ] ; if ( direction == 0 ) { if ( root . left == null ) return - 1 ; root = root . left ; } else { if ( root . right == null ) return - 1 ; root = root . right ; } } return root . data ; } function generateGray ( n , x ) { let gray = [ ] ; let i = 0 ; while ( x > 0 ) { gray . push ( x % 2 ) ; x = parseInt ( x / 2 , 10 ) ; i ++ ; } gray . reverse ( ) ; for ( let j = 0 ; j < n - i ; j ++ ) gray . push ( 0 , 0 ) ; return gray ; } function binarySearch ( root , start , end , data , level ) { if ( end >= start ) { let mid = parseInt ( ( start + end ) / 2 , 10 ) ; let encoding = generateGray ( level , mid ) ; let element_found = traversePath ( root , encoding ) ; if ( element_found == - 1 ) return binarySearch ( root , start , mid - 1 , data , level ) ; if ( element_found == data ) return true ; if ( element_found < data ) return binarySearch ( root , mid + 1 , end , data , level ) ; else return binarySearch ( root , start , mid - 1 , data , level ) ; } return false ; } function findKey ( root , data ) { let level = findLevel ( root , data ) ; if ( level == - 1 ) return false ; if ( level == - 2 ) return true ; return binarySearch ( root , 0 , Math . pow ( 2 , level ) - 1 , data , level ) ; } let root = new Node ( 5 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 10 ) ; root . left . left = new Node ( 13 ) ; root . left . right = new Node ( 23 ) ; root . right . left = new Node ( 25 ) ; root . right . right = new Node ( 30 ) ; root . left . left . left = new Node ( 32 ) ; root . left . left . right = new Node ( 40 ) ; root . left . right . left = new Node ( 50 ) ; let arr = [ 5 , 8 , 9 ] ; let n = arr . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( findKey ( root , arr [ i ] ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; }

Index/key variables: i
Accumulator variables: cur_level, i, j
Iterator variables: i, j
Class/struct names: Node
Javascript program:
let MAX = 1000 ; function ToSpiral ( m , n , Sorted , a ) { let index = 0 ; let k = 0 , l = 0 ; while ( k < m && l < n ) { for ( let i = l ; i < n ; ++ i ) { a [ k ] [ i ] = Sorted [ index ] ; index ++ ; } k ++ ; for ( let i = k ; i < m ; ++ i ) { a [ i ] [ n - 1 ] = Sorted [ index ] ; index ++ ; } n -- ; if ( k < m ) { for ( let i = n - 1 ; i >= l ; -- i ) { a [ m - 1 ] [ i ] = Sorted [ index ] ; index ++ ; } m -- ; } if ( l < n ) { for ( let i = m - 1 ; i >= k ; -- i ) { a [ i ] [ l ] = Sorted [ index ] ; index ++ ; } l ++ ; } } } function convert2Dto1D ( y , m , n ) { let index = 0 ; let x = new Array ( m * n ) ; for ( let i = 0 ; i < m ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { x [ index ] = y [ i ] [ j ] ; index ++ ; } } return x ; } function PrintMatrix ( a , m , n ) { for ( let i = 0 ; i < m ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { document . write ( a [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } function SortArray ( x ) { x . sort ( function ( a , b ) { return a - b ; } ) ; return x ; } function convertMatrixToSortedSpiral ( y , m , n ) { let a = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) a [ i ] = new Array ( MAX ) ; let x = new Array ( m * n ) ; x = convert2Dto1D ( y , m , n ) ; x = SortArray ( x ) ; ToSpiral ( m , n , x , a ) ; PrintMatrix ( a , m , n ) ; } let m = 4 , n = 3 ; let y = [ [ 2 , 5 , 12 ] , [ 22 , 45 , 55 ] , [ 1 , 6 , 8 ] , [ 13 , 56 , 10 ] ] ; convertMatrixToSortedSpiral ( y , m , n ) ;

Index/key variables: i, index, j, k, l
Accumulator variables: i, index, j, k, l, m, n
Iterator variables: i, j
Javascript program:
function reverseArray ( arr , start , end ) { while ( start < end ) { let temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; start ++ ; end -- ; } return arr ; } function rightRotate ( arr , d , n ) { arr = reverseArray ( arr , 0 , n - 1 ) ; arr = reverseArray ( arr , 0 , d - 1 ) ; arr = reverseArray ( arr , d , n - 1 ) ; return arr ; } function printArray ( arr , size ) { for ( let i = 0 ; i < size ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ; let n = arr . length ; let k = 3 ; arr = rightRotate ( arr , k , n ) ; printArray ( arr , n ) ;

Index/key variables: end, i, start
Accumulator variables: end, i, start
Iterator variables: i
Javascript program:
function maxHamming ( arr , n ) { let brr = new Array ( 2 * n + 1 ) ; for ( let i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; for ( let i = 0 ; i < n ; i ++ ) brr [ n + i ] = arr [ i ] ; let maxHam = 0 ; for ( let i = 1 ; i < n ; i ++ ) { let currHam = 0 ; for ( let j = i , k = 0 ; j < ( i + n ) ; j ++ , k ++ ) if ( brr [ j ] != arr [ k ] ) currHam ++ ; if ( currHam == n ) return n ; maxHam = max ( maxHam , currHam ) ; } return maxHam ; } let arr = [ 2 , 4 , 6 , 8 ] ; let n = arr . length ; document . write ( maxHamming ( arr , n ) ) ;

Index/key variables: i, j, k
Accumulator variables: currHam, i, j, k
Iterator variables: i, j
Javascript program:
function leftRotate ( arr , n , k ) { let mod = k % n ; for ( let i = 0 ; i < n ; i ++ ) document . write ( ( arr [ ( mod + i ) % n ] ) + " " ) ; document . write ( " " ) ; } let arr = [ 1 , 3 , 5 , 7 , 9 ] ; let n = arr . length ; let k = 2 ; leftRotate ( arr , n , k ) ; document . write ( " " ) ; k = 3 ; leftRotate ( arr , n , k ) ; document . write ( " " ) ; k = 4 ; leftRotate ( arr , n , k ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
let findElement = ( arr , ranges , rotations , index ) => { for ( let i = rotations - 1 ; i >= 0 ; i -- ) { let left = ranges [ i ] [ 0 ] ; let right = ranges [ i ] [ 1 ] ; if ( left <= index && right >= index ) { if ( index == left ) index = right ; else index -- ; } } return arr [ index ] ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let rotations = 2 ; let ranges = [ [ 0 , 2 ] , [ 0 , 3 ] ] ; let index = 1 ; document . write ( findElement ( arr , ranges , rotations , index ) ) ;

Index/key variables: i, index
Accumulator variables: i, index
Iterator variables: i
Javascript program:
function sort ( s ) { var c = s . split ( ' ' ) ; c . sort ( ) ; var al_c = 0 , nu_c = 0 ; while ( c [ al_c ] . charCodeAt ( 0 ) < 97 ) al_c ++ ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] . charCodeAt ( 0 ) < 97 ) s = s . substring ( 0 , i ) + c [ nu_c ++ ] + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + c [ al_c ++ ] + s . substring ( i + 1 ) ; } return s ; } var s = " " ; document . write ( sort ( s ) ) ;

Index/key variables: al_c, i
Accumulator variables: al_c, i, nu_c
Iterator variables: i
Javascript program:
function splitArr ( arr , n , k ) { for ( let i = 0 ; i < k ; i ++ ) { let x = arr [ 0 ] ; for ( let j = 0 ; j < n - 1 ; ++ j ) arr [ j ] = arr [ j + 1 ] ; arr [ n - 1 ] = x ; } } let arr = [ 12 , 10 , 5 , 6 , 52 , 36 ] ; let n = arr . length ; let position = 2 ; splitArr ( arr , 6 , position ) ; for ( let i = 0 ; i < n ; ++ i ) document . write ( arr [ i ] + " " ) ;

Index/key variables: i, j
Accumulator variables: i
Iterator variables: i, j
Javascript program:
function SplitAndAdd ( A , Length , rotation ) { let tmp = new Array ( Length * 2 ) ; for ( let i = 0 ; i < tmp . length ; i ++ ) { tmp [ i ] = 0 ; } for ( let i = 0 ; i < Length ; i ++ ) { tmp [ i ] = A [ i ] tmp [ i + Length ] = A [ i ] } for ( let i = rotation ; i < rotation + Length ; i ++ ) { A [ i - rotation ] = tmp [ i ] ; } } let arr = [ 12 , 10 , 5 , 6 , 52 , 36 ] ; let n = arr . length ; let position = 2 ; SplitAndAdd ( arr , n , position ) ; for ( let i = 0 ; i < n ; ++ i ) document . write ( arr [ i ] + " " ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function swap ( arr , i , j ) { let temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } function sort_shell_metzner ( arr , n ) { let i , j , k , l , m , temp ; m = n ; while ( m > 0 ) { m = Math . floor ( m / 2 ) ; k = n - m ; j = 0 ; do { i = j ; do { l = i + m ; if ( l < n && arr [ i ] > arr [ l ] ) { swap ( arr , i , l ) ; i -= m ; } else { break ; } } while ( i >= 0 ) ; j ++ ; } while ( j <= k ) ; } } function printArray ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } let arr = [ 0 , - 2 , 8 , 5 , 1 ] ; let n = arr . length ; sort_shell_metzner ( arr , n ) ; printArray ( arr , n ) ;

Index/key variables: i, j, l
Accumulator variables: i, j
Iterator variables: i
Javascript program:
function fixArray ( ar , n ) { var i , j , temp ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( ar [ j ] == i ) { temp = ar [ j ] ; ar [ j ] = ar [ i ] ; ar [ i ] = temp ; break ; } } } for ( i = 0 ; i < n ; i ++ ) { if ( ar [ i ] != i ) { ar [ i ] = - 1 ; } } document . write ( " " ) ; document . write ( " " ) ; for ( i = 0 ; i < n ; i ++ ) { document . write ( ar [ i ] + " " ) ; } } var n , ar = [ - 1 , - 1 , 6 , 1 , 9 , 3 , 2 , - 1 , 4 , - 1 ] ; n = ar . length ; fixArray ( ar , n ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
const getFinalSum = ( a , n , queries , q , k ) => { let answer = 0 ; for ( let i = 0 ; i < n ; i ++ ) answer += a [ i ] ; let contribution = [ ] ; for ( let i = 0 ; i < q ; i ++ ) { contribution . push ( queries [ i ] [ 1 ] - queries [ i ] [ 0 ] + 1 ) ; } contribution . sort ( ( a , b ) => b - a ) ; let i = 0 ; while ( i < k ) { answer += contribution [ i ] ; i ++ ; } return answer ; } const a = [ 1 , 1 , 2 , 2 , 2 , 3 ] ; const n = a . length ; const queries = [ [ 0 , 4 ] , [ 1 , 2 ] , [ 2 , 5 ] , [ 2 , 3 ] , [ 2 , 4 ] ] ; const q = queries . length ; let k = 3 ; document . write ( getFinalSum ( a , n , queries , q , k ) ) ;

Index/key variables: i
Accumulator variables: answer, i
Iterator variables: i
Javascript program:
function answerQuery ( arr , l , r ) { let answer = Math . abs ( arr [ l - 1 ] [ 1 ] - arr [ r - 1 ] [ 1 ] ) ; return answer ; } function solveQueries ( a , n , q , m ) { let arr = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { arr [ i ] = new Array ( n ) . fill ( 0 ) } for ( let i = 0 ; i < n ; i ++ ) { arr [ i ] [ 0 ] = a [ i ] ; arr [ i ] [ 1 ] = i ; } arr . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ; for ( let i = 0 ; i < m ; i ++ ) document . write ( answerQuery ( arr , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) + " " ) ; } let a = [ 1 , 5 , 4 , 2 , 8 , 6 , 7 ] ; let n = a . length ; let q = [ [ 2 , 5 ] , [ 1 , 3 ] , [ 1 , 5 ] , [ 3 , 6 ] ] ; let m = q . length ; solveQueries ( a , n , q , m ) ;

Index/key variables: i
Accumulator variables: 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 b - a ; } ) ; for ( let i = 0 ; i < k ; ++ i ) { document . write ( heap [ i ] + " " ) ; } } 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 kThLexString ( st , k , n ) { var z = new Set ( ) ; for ( var i = 0 ; i < n ; i ++ ) { var pp = " " ; for ( var j = i ; j < i + k ; j ++ ) { if ( j >= n ) break ; pp += st [ j ] ; z . add ( pp ) ; } } var fin = [ ] ; z . forEach ( element => { fin . push ( element ) ; } ) ; fin . sort ( ) ; document . write ( fin [ k - 1 ] ) ; } var s = " " ; var k = 5 ; var n = s . length ; kThLexString ( s , k , n ) ;

Index/key variables: j
Accumulator variables: i, j, pp
Iterator variables: i, j
Javascript program:
function rearrangeArr ( arr , n ) { let evenPos = Math . floor ( n / 2 ) ; let oddPos = n - evenPos ; let tempArr = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) tempArr [ i ] = arr [ i ] ; tempArr . sort ( ) ; let j = oddPos - 1 ; for ( let i = 0 ; i < n ; i += 2 ) { arr [ i ] = tempArr [ j ] ; j -- ; } j = oddPos ; for ( let i = 1 ; i < n ; i += 2 ) { arr [ i ] = tempArr [ j ] ; j ++ ; } for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ; let size = arr . length ; rearrangeArr ( arr , size ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i
Javascript program:
class Node { constructor ( val ) { this . data = val ; this . prev = null ; this . next = null ; } } function countTriplets ( head , x ) { var ptr1 , ptr2 , ptr3 ; var count = 0 ; for ( ptr1 = head ; ptr1 != null ; ptr1 = ptr1 . next ) for ( ptr2 = ptr1 . next ; ptr2 != null ; ptr2 = ptr2 . next ) for ( ptr3 = ptr2 . next ; ptr3 != null ; ptr3 = ptr3 . next ) if ( ( ptr1 . data * ptr2 . data * ptr3 . data ) == x ) count ++ ; return count ; } function insert ( head , val ) { temp = new Node ( val ) ; if ( head == null ) head = temp ; else { temp . next = head ; head . prev = temp ; head = temp ; } return head ; } head = null ; head = insert ( head , 9 ) ; head = insert ( head , 8 ) ; head = insert ( head , 6 ) ; head = insert ( head , 5 ) ; head = insert ( head , 4 ) ; head = insert ( head , 2 ) ; head = insert ( head , 1 ) ; var x = 8 ; document . write ( " " + countTriplets ( head , x ) ) ;

Accumulator variables: count
Iterator variables: ptr1, ptr2, ptr3
Class/struct names: Node
Javascript program:
prime = new Array ( 100005 ) ; function SieveOfEratosthenes ( n ) { prime . fill ( true ) ; prime [ 1 ] = false ; for ( var p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( var i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } } function sortedArray ( arr , n ) { SieveOfEratosthenes ( 100005 ) ; var v = [ ] ; for ( var i = 0 ; i < n ; ++ i ) { if ( prime [ arr [ i ] ] == 0 ) v . push ( arr [ i ] ) ; } v . sort ( ) ; var j = 0 ; for ( var i = 0 ; i < n ; ++ i ) { if ( prime [ arr [ i ] ] == true ) document . write ( arr [ i ] + " " ) ; else { document . write ( v [ j ] + " " ) ; j ++ ; } } } var n = 6 ; var arr = [ 100 , 11 , 500 , 2 , 17 , 1 ] ; sortedArray ( arr , n ) ;

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