dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
function printArray ( arr , n ) { for ( i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function printMaxMean ( arr , n ) { var newArr = Array ( n ) . fill ( 0 ) ; arr . sort ( ( a , b ) => a - b ) ; for ( i = 0 ; i < n ; i ++ ) newArr [ i ] = arr [ i + n ] ; printArray ( newArr , n ) ; } var arr = [ 4 , 8 , 3 , 1 , 3 , 7 , 0 , 4 ] ; var n = arr . length ; printMaxMean ( arr , n / 2 ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function average ( arr , n , k ) { var total = 0 ; if ( 2 * k >= n ) return 0 ; arr . sort ( ) ; var start = k , end = n - k - 1 ; for ( i = start ; i <= end ; i ++ ) total += arr [ i ] ; return ( total / ( n - 2 * k ) ) ; } var arr = [ 1 , 2 , 4 , 4 , 5 , 6 ] ; var n = arr . length ; var k = 2 ; document . write ( average ( arr , n , k ) ) ;

Index/key variables: i
Accumulator variables: i, total
Iterator variables: i
Javascript program:
function min_sum ( n , k , a ) { a . sort ( ) ; if ( a [ 0 ] < 0 ) return - 1 ; if ( k == 0 ) { if ( a [ 0 ] == a [ n - 1 ] ) return ( n * a [ 0 ] ) ; else return - 1 ; } else { let f = 0 ; for ( let i = 1 ; i < n ; i ++ ) { let p = a [ i ] - a [ 0 ] ; if ( p % k == 0 ) continue ; else { f = 1 ; break ; } } if ( f > 0 ) return - 1 ; else { if ( k == 1 ) return n ; else return ( n * ( a [ 0 ] % k ) ) ; } } } let arr = [ 2 , 3 , 4 , 5 ] ; let K = 1 ; let N = arr . length ; document . write ( min_sum ( N , K , arr ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function merge ( arr , start , mid , end ) { let start2 = mid + 1 ; if ( arr [ mid ] <= arr [ start2 ] ) { return ; } while ( start <= mid && start2 <= end ) { if ( arr [ start ] <= arr [ start2 ] ) { start ++ ; } else { let value = arr [ start2 ] ; let index = start2 ; while ( index != start ) { arr [ index ] = arr [ index - 1 ] ; index -- ; } arr [ start ] = value ; start ++ ; mid ++ ; start2 ++ ; } } } function mergeSort ( arr , l , r ) { if ( l < r ) { let m = l + Math . floor ( ( r - l ) / 2 ) ; mergeSort ( arr , l , m ) ; mergeSort ( arr , m + 1 , r ) ; merge ( arr , l , m , r ) ; } } function printArray ( A , size ) { let i ; for ( i = 0 ; i < size ; i ++ ) document . write ( A [ i ] + " " ) ; document . write ( " " ) ; } let arr = [ 12 , 11 , 13 , 5 , 6 , 7 ] ; let arr_size = arr . length ; mergeSort ( arr , 0 , arr_size - 1 ) ; printArray ( arr , arr_size ) ;

Index/key variables: i, index, mid, start, start2
Accumulator variables: i, index, mid, start, start2
Iterator variables: i
Javascript program:
function minCost ( A , n ) { var cost = 0 ; A . sort ( ) ; var K = A [ parseInt ( n / 2 ) ] ; var i ; for ( i = 0 ; i < n ; ++ i ) cost += Math . abs ( A [ i ] - K ) ; if ( n % 2 == 0 ) { var tempCost = 0 ; K = A [ parseInt ( n / 2 ) - 1 ] ; for ( i = 0 ; i < n ; ++ i ) tempCost += Math . abs ( A [ i ] - K ) ; cost = Math . min ( cost , tempCost ) ; } return cost ; } var A = [ 1 , 6 , 7 , 10 ] ; var n = A . length ; document . write ( minCost ( A , n ) ) ;

Index/key variables: i
Accumulator variables: cost, tempCost
Iterator variables: i
Javascript program:
function printArray ( arr , n ) { arr . sort ( ) ; let l = 0 , r = n - 1 , flag = 0 , i ; let k = 2 ; while ( l <= r ) { if ( flag == 0 ) { for ( i = l ; i < l + k && i <= r ; i ++ ) document . write ( arr [ i ] + " " ) ; flag = 1 ; l = i ; } else { for ( i = r ; i > r - k && i >= l ; i -- ) document . write ( arr [ i ] + " " ) ; flag = 0 ; r = i ; } k ++ ; } } let n = 6 ; let arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; printArray ( arr , n ) ;

Index/key variables: i
Accumulator variables: i, k
Iterator variables: i
Javascript program:
class Node { constructor ( ) { let data ; let next , prev ; } } function countPairs ( first , second , value ) { let count = 0 ; while ( first != null && second != null && first != second && second . next != first ) { if ( ( first . data * second . data ) == value ) { count ++ ; first = first . next ; second = second . prev ; } else if ( ( first . data * second . data ) > value ) second = second . prev ; else first = first . next ; } return count ; } function countTriplets ( head , x ) { if ( head == null ) return 0 ; let current , first , last ; let count = 0 ; last = head ; while ( last . next != null ) last = last . next ; for ( current = head ; current != null ; current = current . next ) { first = current . next ; count += countPairs ( first , last , x / current . data ) ; } return count ; } function insert ( head , data ) { let temp = new Node ( ) ; temp . data = data ; temp . next = temp . prev = null ; if ( ( head ) == null ) ( head ) = temp ; else { temp . next = head ; ( head ) . prev = temp ; ( head ) = temp ; } return head ; } let 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 ) ; let x = 8 ; document . write ( " " + countTriplets ( head , x ) ) ;

Accumulator variables: count
Iterator variables: current
Class/struct names: Node
Javascript program:
function isAlphabaticOrder ( s ) { let n = s . length ; for ( let i = 1 ; i < n ; i ++ ) { if ( s [ i ] < s [ i - 1 ] ) return false ; } return true ; } let s = " " ; if ( isAlphabaticOrder ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function findProduct ( arr , n ) { arr . sort ( ) ; var prod = 1 * arr [ 0 ] ; for ( i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) { prod = prod * arr [ i + 1 ] ; } } return prod ; } var arr = [ 1 , 2 , 3 , 1 , 1 , 4 , 5 , 6 ] ; var n = arr . length ; document . write ( findProduct ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function rotateRec ( n , L , B ) { var m = Number . MAX_VALUE for ( var i = 0 ; i < n ; i ++ ) { if ( Math . max ( L [ i ] , B [ i ] ) <= m ) m = Math . max ( L [ i ] , B [ i ] ) ; else if ( Math . min ( L [ i ] , B [ i ] ) <= m ) m = Math . min ( L [ i ] , B [ i ] ) ; else { return 0 ; } } return 1 ; } var n = 3 ; var L = [ 5 , 5 , 6 ] ; var B = [ 6 , 7 , 8 ] ; if ( rotateRec ( n , L , B ) == 1 ) document . write ( " " ) ; else document . write ( " " ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function minDistance ( n , k , points ) { for ( let i = 0 ; i < k ; i ++ ) ( point [ i ] ) . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < k ; i ++ ) document . write ( point [ i ] [ Math . ceil ( ( n / 2 ) - 1 ) ] + " " ) ; } let n = 4 ; let k = 4 ; let point = [ [ 1 , 5 , 2 , 4 ] , [ 6 , 2 , 0 , 6 ] , [ 9 , 5 , 1 , 3 ] , [ 6 , 7 , 5 , 9 ] ] ; minDistance ( n , k , point ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function printOrder ( arr , n , k ) { let len1 = k , len2 = n - k ; let arr1 = new Array ( k ) ; let arr2 = new Array ( n - k ) ; for ( let i = 0 ; i < k ; i ++ ) arr1 [ i ] = arr [ i ] ; for ( let i = k ; i < n ; i ++ ) arr2 [ i - k ] = arr [ i ] ; arr1 . sort ( function ( a , b ) { return a - b ; } ) ; arr2 . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( i < k ) arr [ i ] = arr1 [ i ] ; else { arr [ i ] = arr2 [ len2 - 1 ] ; len2 -- ; } } for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } let arr = [ 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , - 1 ] ; let k = 4 ; let n = arr . length ; printOrder ( arr , n , k ) ;

Index/key variables: i
Accumulator variables: i, len2
Iterator variables: i
Javascript program:
function findMaxNum ( arr , n ) { var hash = Array ( 10 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { hash [ arr [ i ] ] ++ ; } for ( var i = 9 ; i >= 0 ; i -- ) { for ( var j = 0 ; j < hash [ i ] ; j ++ ) document . write ( i ) ; } } var arr = [ 1 , 2 , 3 , 4 , 5 , 0 ] ; var n = arr . length ; findMaxNum ( arr , n ) ;

Index/key variables: i
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function sortK ( arr , n , k ) { ( arr ) . sort ( function ( a , b ) { return a - b ; } ) ; } function printArray ( arr , size ) { for ( let i = 0 ; i < size ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; } let k = 3 ; let arr = [ 2 , 6 , 3 , 12 , 56 , 8 ] ; let n = arr . length ; sortK ( arr , n , k ) ; document . write ( " " ) ; printArray ( arr , n ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function distribution ( arr , n ) { arr . sort ( ( a , b ) => a - b ) ; var count = 1 ; for ( var i = 1 ; i < n ; i ++ ) if ( arr [ i ] > arr [ i - 1 ] ) count ++ ; return Math . min ( count , parseInt ( n / 2 ) ) ; } var arr = [ 1 , 1 , 2 , 1 , 3 , 4 ] ; var n = arr . length ; document . write ( distribution ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: count, i
Iterator variables: i
Javascript program:
function printPairs ( arr , n ) { let pairs = new Set ( ) ; let pair_exists = false ; for ( let i = 0 ; i < n ; i ++ ) if ( arr [ i ] > 0 ) pairs . add ( arr [ i ] ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) if ( pairs . has ( - arr [ i ] ) ) { document . write ( arr [ i ] + " " + - arr [ i ] + " " ) ; pair_exists = true ; } } if ( pair_exists == false ) document . write ( " " ) ; } let arr = [ 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 ] ; let n = arr . length ; printPairs ( arr , n ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Boolean variables: pair_exists
Javascript program:
let arr = [ 10 , 12 , 5 ] ; arr . sort ( ( a , b ) => a - b ) ; for ( let i = 0 ; i < 3 ; i ++ ) document . write ( arr [ i ] + " " ) ;

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

Index/key variables: i, j, k
Accumulator variables: i, j, k
Iterator variables: i, j, k
Javascript program:
function countTriplets ( arr , n , a , b ) { var ans = 0 ; for ( var i = 0 ; i < n - 2 ; i ++ ) { for ( var j = i + 1 ; j < n - 1 ; j ++ ) { for ( var k = j + 1 ; k < n ; k ++ ) if ( arr [ i ] + arr [ j ] + arr [ k ] >= a && arr [ i ] + arr [ j ] + arr [ k ] <= b ) ans ++ ; } } return ans ; } var arr = [ 2 , 7 , 5 , 3 , 8 , 4 , 1 , 9 ] ; var n = arr . length ; var a = 8 , b = 16 ; document . write ( countTriplets ( arr , n , a , b ) ) ;

Index/key variables: i, j, k
Accumulator variables: ans, i, j, k
Iterator variables: i, j, k
Javascript program:
function countTripletsLessThan ( arr , n , val ) { arr . sort ( ) ; var ans = 0 ; var j , k ; var sum ; for ( var i = 0 ; i < n - 2 ; i ++ ) { j = i + 1 ; k = n - 1 ; while ( j != k ) { sum = arr [ i ] + arr [ j ] + arr [ k ] ; if ( sum > val ) k -- ; else { ans += k - j ; j ++ ; } } } return ans ; } function countTriplets ( arr , n , a , b ) { var res ; res = countTripletsLessThan ( arr , n , b ) - countTripletsLessThan ( arr , n , a - 1 ) ; return res ; } var arr = [ 2 , 7 , 5 , 3 , 8 , 4 , 1 , 9 ] ; var n = arr . length ; var a = 8 , b = 16 ; document . write ( " " + countTriplets ( arr , n , a , b ) ) ;

Index/key variables: i, j, k
Accumulator variables: ans, i, j, k
Iterator variables: i
Javascript program:
function areSame ( a , b ) { if ( a . length != b . length ) return false ; var m1 , m2 ; m1 = { } ; m2 = { } ; for ( var i = 0 ; i < a . length ; i ++ ) { if ( m1 . hasOwnProperty ( a [ i ] ) ) { m1 [ a [ i ] ] = m1 [ a [ i ] ] + 1 ; } else { m1 [ a [ i ] ] = 1 ; } if ( m2 . hasOwnProperty ( b [ i ] ) ) { m2 [ b [ i ] ] = m2 [ b [ i ] ] + 1 ; } else { m2 [ b [ i ] ] = 1 ; } } for ( const [ key , value ] of Object . entries ( m1 ) ) { if ( ! m2 . hasOwnProperty ( key ) m2 [ key ] != value ) return false ; } return true ; } var a = [ 7 , 7 , 5 ] ; var b = [ 7 , 7 , 5 ] ; if ( areSame ( a , b ) ) document . write ( " " ) ; else document . write ( " " ) ;

Index/key variables: i, key
Accumulator variables: i
Iterator variables: i
Javascript program:
function MaxTotalRectangleArea ( a , n ) { a . sort ( ) ; a . reverse ( ) ; let sum = 0 ; let flag = false ; let len ; for ( let i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] == a [ i + 1 ] a [ i ] - a [ i + 1 ] == 1 ) && ( ! flag ) ) { flag = true ; len = a [ i + 1 ] ; i ++ ; } else if ( ( a [ i ] == a [ i + 1 ] a [ i ] - a [ i + 1 ] == 1 ) && ( flag ) ) { sum = sum + a [ i + 1 ] * len ; flag = false ; i ++ ; } } return sum ; } let a = [ 10 , 10 , 10 , 10 , 11 , 10 , 11 , 10 , 9 , 9 , 8 , 8 ] ; let n = a . length ; document . write ( MaxTotalRectangleArea ( a , n ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Boolean variables: flag
Javascript program:
function evenOddInsertionSort ( arr , n ) { for ( let i = 2 ; i < n ; i ++ ) { let j = i - 2 ; let temp = arr [ i ] ; if ( ( ( i + 1 ) & 1 ) == 1 ) { while ( j >= 0 && temp >= arr [ j ] ) { arr [ j + 2 ] = arr [ j ] ; j -= 2 ; } arr [ j + 2 ] = temp ; } else { while ( j >= 0 && temp <= arr [ j ] ) { arr [ j + 2 ] = arr [ j ] ; j -= 2 ; } arr [ j + 2 ] = temp ; } } } function printArray ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( ) ; } let arr = [ 12 , 11 , 13 , 5 , 6 ] ; let n = arr . length ; evenOddInsertionSort ( arr , n ) ; printArray ( arr , n ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i
Javascript program:
function print ( a , n ) { for ( let i = 0 ; i <= n ; i ++ ) document . write ( a [ i ] + " " ) ; document . write ( " " ) ; } function sort ( a , n ) { for ( let i = n ; i >= 0 ; i -- ) for ( let j = n ; j > n - i ; j -- ) if ( a [ j ] > a [ j - 1 ] ) { let tempswap = a [ j ] ; a [ j ] = a [ j - 1 ] ; a [ j - 1 ] = tempswap ; } print ( a , n ) ; } let n = 6 ; let a = [ ] ; a . push ( 2 ) ; a . push ( 4 ) ; a . push ( 3 ) ; a . push ( 2 ) ; a . push ( 4 ) ; a . push ( 5 ) ; a . push ( 3 ) ; sort ( a , n ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function operations ( arr , n , k ) { arr . sort ( ) ; let i = 0 , sum = 0 ; while ( k -- > 0 ) { while ( i < n && arr [ i ] - sum == 0 ) i ++ ; if ( i < n && arr [ i ] - sum > 0 ) { document . write ( arr [ i ] - sum + " " ) ; sum = arr [ i ] ; } else document . write ( " " ) ; } } let k = 5 ; let arr = [ 3 , 6 , 4 , 2 ] ; let n = arr . length ; operations ( arr , n , k ) ;

Index/key variables: i
Accumulator variables: i, k
Javascript program:
function sort012 ( arr , n ) { let count0 = 0 , count1 = 0 ; let count2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) count0 ++ ; if ( arr [ i ] == 1 ) count1 ++ ; if ( arr [ i ] == 2 ) count2 ++ ; } for ( let i = 0 ; i < count0 ; i ++ ) arr [ i ] = 0 ; for ( let i = count0 ; i < ( count0 + count1 ) ; i ++ ) arr [ i ] = 1 ; for ( let i = ( count0 + count1 ) ; i < n ; i ++ ) arr [ i ] = 2 ; printArray ( arr , n ) ; } function printArray ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( ) ; } let arr = [ 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 ] ; let n = 12 ; sort012 ( arr , n ) ;

Index/key variables: i
Accumulator variables: count0, count1, count2, i
Iterator variables: i
Javascript program:
const MAX = 26 ; function alternateSort ( s1 ) { var n = s1 . length ; var l = 0 , j = 0 , k = 0 ; var s = s1 . split ( " " ) ; var lCount = new Array ( MAX ) . fill ( 0 ) ; var uCount = new Array ( MAX ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( s [ i ] === s [ i ] . toUpperCase ( ) ) uCount [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; else lCount [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } while ( k < n ) { while ( l < MAX && uCount [ l ] === 0 ) l ++ ; if ( l < MAX ) { s [ k ++ ] = String . fromCharCode ( " " . charCodeAt ( 0 ) + l ) ; uCount [ l ] -- ; } while ( j < MAX && lCount [ j ] === 0 ) j ++ ; if ( j < MAX ) { s [ k ++ ] = String . fromCharCode ( " " . charCodeAt ( 0 ) + j ) ; lCount [ j ] -- ; } } return s . join ( " " ) ; } var str = " " ; document . write ( alternateSort ( str ) ) ;

Index/key variables: i, j, l
Accumulator variables: i, j, k, l
Iterator variables: i
Javascript program:
function distancesum ( x , y , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = i + 1 ; j < n ; j ++ ) sum += ( Math . abs ( x [ i ] - x [ j ] ) + Math . abs ( y [ i ] - y [ j ] ) ) ; return sum ; } let x = [ - 1 , 1 , 3 , 2 ] ; let y = [ 5 , 6 , 5 , 3 ] ; let n = x . length ; document . write ( distancesum ( x , y , n ) ) ;

Index/key variables: i, j
Accumulator variables: i, j, sum
Iterator variables: i, j
Javascript program:
function distancesum ( arr , n ) { arr . sort ( ) ; let res = 0 , sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { res += ( arr [ i ] * i - sum ) ; sum += arr [ i ] ; } return res ; } function totaldistancesum ( x , y , n ) { return distancesum ( x , n ) + distancesum ( y , n ) ; } let x = [ - 1 , 1 , 3 , 2 ] ; let y = [ 5 , 6 , 5 , 3 ] ; let n = x . length ; document . write ( totaldistancesum ( x , y , n ) ) ;

Index/key variables: i
Accumulator variables: i, res, sum
Iterator variables: i
Javascript program:
function sortRec ( arr , index , k , n ) { if ( k == 0 ) { for ( let i = 1 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( 999 ) ; return ; } else if ( k > 0 && index == 0 ) { index = n - 2 ; for ( let i = 1 ; i <= index ; i ++ ) { arr [ i - 1 ] = arr [ i ] ; } arr [ index ] = 999 ; } if ( index - 1 >= 0 && index + 1 < n && arr [ index - 1 ] > arr [ index + 1 ] ) { swap ( arr , index , index + 1 ) ; swap ( arr , index - 1 , index + 1 ) ; } else swap ( arr , index , index - 1 ) ; sortRec ( arr , index - 1 , k - 1 , n ) ; } function swap ( arr , i , j ) { let temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } function sort ( arr , n ) { let index = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 999 ) { index = i ; break ; } } if ( index == - 1 ) return ; sortRec ( arr , index , n , n ) ; } let arr = [ 3 , 2 , 999 , 1 ] ; let n = arr . length ; sort ( arr , n ) ;

Index/key variables: i, index, j
Accumulator variables: i
Iterator variables: i
Javascript program:
var k ; function unsort ( l , r , a ) { if ( k < 1 l + 1 == r ) return ; k -= 2 ; var mid = parseInt ( ( l + r ) / 2 ) ; [ a [ mid - 1 ] , a [ mid ] ] = [ a [ mid ] , a [ mid - 1 ] ] ; unsort ( l , mid , a ) ; unsort ( mid , r , a ) ; } function arrayWithKCalls ( n ) { if ( k % 2 == 0 ) { document . write ( " " ) ; return ; } var a = Array ( n + 1 ) ; a [ 0 ] = 1 ; for ( var i = 1 ; i < n ; i ++ ) a [ i ] = i + 1 ; k -- ; unsort ( 0 , n , a ) ; for ( var i = 0 ; i < n ; ++ i ) document . write ( a [ i ] + ' ' ) ; } var n = 10 k = 17 ; arrayWithKCalls ( n ) ;

Index/key variables: i, mid
Accumulator variables: i, k
Iterator variables: i
Javascript program:
let MAX_CHAR = 26 ; function sortString ( str ) { let charCount = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < charCount . length ; i ++ ) { charCount [ i ] = 0 ; } for ( let i = 0 ; i < str . length ; i ++ ) { charCount [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } for ( let i = MAX_CHAR - 1 ; i >= 0 ; i -- ) { for ( let j = 0 ; j < charCount [ i ] ; j ++ ) { document . write ( String . fromCharCode ( ' ' . charCodeAt ( 0 ) + i ) ) ; } } } let s = " " ; sortString ( s ) ;

Index/key variables: i
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function swap ( arr , xp , yp ) { var temp = arr [ xp ] ; arr [ xp ] = arr [ yp ] ; arr [ yp ] = temp ; } function minMaxSelectionSort ( arr , n ) { for ( var i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { var min = arr [ i ] ; var max = arr [ i ] ; var min_i = i ; var max_i = i ; for ( var k = i ; k <= j ; k ++ ) { if ( arr [ k ] > max ) { max = arr [ k ] ; max_i = k ; } else if ( arr [ k ] < min ) { min = arr [ k ] ; min_i = k ; } } swap ( arr , i , min_i ) ; if ( arr [ min_i ] == max ) swap ( arr , j , min_i ) ; else swap ( arr , j , max_i ) ; } } var arr = [ 23 , 78 , 45 , 8 , 32 , 56 , 1 ] ; var n = 7 ; minMaxSelectionSort ( arr , n ) ; document . write ( " " ) ; for ( var i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ;

Index/key variables: i, k, min_i, xp, yp
Accumulator variables: i, j, k
Iterator variables: i, k
Javascript program:
function mergeArray ( a , b , n , m ) { let mx = 0 ; for ( let i = 0 ; i < n ; i ++ ) { mx = Math . max ( mx , a [ i ] ) ; } for ( let i = 0 ; i < m ; i ++ ) { mx = Math . max ( mx , b [ i ] ) ; } mx ++ ; let i = 0 , j = 0 , k = 0 ; while ( i < n && j < m && k < ( n + m ) ) { let e1 = a [ i ] % mx ; let e2 = b [ j ] % mx ; if ( e1 <= e2 ) { if ( k < n ) a [ k ] += ( e1 * mx ) ; else b [ k - n ] += ( e1 * mx ) ; i ++ ; k ++ ; } else { if ( k < n ) a [ k ] += ( e2 * mx ) ; else b [ k - n ] += ( e2 * mx ) ; j ++ ; k ++ ; } } while ( i < n ) { let el = a [ i ] % mx ; if ( k < n ) a [ k ] += ( el * mx ) ; else b [ k - n ] += ( el * mx ) ; i ++ ; k ++ ; } while ( j < m ) { let el = b [ j ] % mx ; if ( k < n ) a [ k ] += ( el * mx ) ; else b [ k - n ] += ( el * mx ) ; j ++ ; k ++ ; } for ( let i = 0 ; i < n ; i ++ ) a [ i ] = parseInt ( a [ i ] / mx ) ; for ( let i = 0 ; i < m ; i ++ ) b [ i ] = parseInt ( b [ i ] / mx ) ; return ; } var a = [ 3 , 5 , 6 , 8 , 12 ] ; var b = [ 1 , 4 , 9 , 13 ] ; var n = a . length ; var m = b . length ; mergeArray ( a , b , n , m ) ; document . write ( " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( a [ i ] + " " ) ; document . write ( ' ' ) document . write ( " " ) ; for ( let i = 0 ; i < m ; i ++ ) document . write ( b [ i ] + " " ) ;

Index/key variables: i, j, k
Accumulator variables: i, j, k, mx
Iterator variables: i
Javascript program:
function arrange ( arr , n , x ) { for ( let i = 1 ; i < n ; i ++ ) { let diff = Math . abs ( arr [ i ] - x ) ; let j = i - 1 ; if ( Math . abs ( arr [ j ] - x ) > diff ) { let temp = arr [ i ] ; while ( j >= 0 && Math . abs ( arr [ j ] - x ) > diff ) { arr [ j + 1 ] = arr [ j ] ; j -- ; } arr [ j + 1 ] = temp ; } } } function print ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 10 , 5 , 3 , 9 , 2 ] ; let n = arr . length ; let x = 7 ; arrange ( arr , n , x ) ; print ( arr , n ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i
Javascript program:
function check ( v , len ) { let n = v . length ; let tempArray ; for ( let i = 0 ; i < n ; i ++ ) { tempArray = v [ i ] . split ( " " ) ; ( tempArray ) . sort ( ) ; v [ i ] = ( tempArray ) . join ( " " ) ; } for ( let i = 0 ; i < len - 1 ; i ++ ) for ( let j = 0 ; j < n ; j ++ ) if ( v [ i ] [ j ] > v [ i + 1 ] [ j ] ) return false ; return true ; } let v = [ " " , " " , " " , " " , " " ] ; if ( check ( v , len ) ) document . write ( " " ) ; else document . write ( " " ) ;

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

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