dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
let prime = new Array ( 100001 ) ; function SieveOfEratosthenes ( ) { for ( let i = 0 ; i < 100001 ; i ++ ) prime [ i ] = 1 ; let p = 2 ; while ( p * p <= 100000 ) { if ( prime [ p ] == 1 ) { for ( let i = p * p ; i < 100001 ; i += p ) prime [ i ] = 0 ; } p += 1 ; } } function join ( a , b ) { let mul = 1 ; let bb = b ; while ( b != 0 ) { mul *= 10 ; b = parseInt ( b / 10 , 10 ) ; } a *= mul ; a += bb ; return a ; } function fibonacciOfPrime ( n1 , n2 ) { SieveOfEratosthenes ( ) ; let initial = [ ] ; for ( let i = n1 ; i <= n2 ; i ++ ) if ( prime [ i ] == 1 ) initial . push ( i ) ; let now = [ ] ; for ( let i = 0 ; i < initial . length ; i ++ ) { for ( let j = 0 ; j < initial . length ; j ++ ) { let a = initial [ i ] ; let b = initial [ j ] ; if ( a != b ) { let c = join ( a , b ) ; now . push ( c ) ; } } } let current = [ ] ; for ( let i = 0 ; i < now . length ; i ++ ) if ( prime [ now [ i ] ] == 1 ) current . push ( now [ i ] ) ; let cnt = new Array ( 1000009 ) ; for ( let i = 0 ; i < 1000001 ; i ++ ) cnt [ i ] = 0 ; let current_set = [ ] ; for ( let i = 0 ; i < current . length ; i ++ ) { cnt [ current [ i ] ] ++ ; if ( cnt [ current [ i ] ] == 1 ) current_set . push ( current [ i ] ) ; } let first = 1000000000 ; for ( let i = 0 ; i < current_set . length ; i ++ ) first = Math . min ( first , current_set [ i ] ) ; let second = 0 ; for ( let i = 0 ; i < current_set . length ; i ++ ) second = Math . max ( second , current_set [ i ] ) ; let count = current_set . length - 1 ; let curr = 1 ; let c = 0 ; while ( curr < count ) { c = first + second ; first = second ; second = c ; curr += 1 ; } document . write ( c ) ; } let x = 2 ; let y = 40 ; fibonacciOfPrime ( x , y ) ;

Index/key variables: i, j, p
Accumulator variables: a, curr, i, j, mul, p
Iterator variables: i, j
Javascript program:
function countElements ( arr , n , k ) { var s = [ ] ; var next_greater = new Array ( n + 1 ) . fill ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( s . length === 0 ) { s . push ( i ) ; continue ; } while ( s . length !== 0 && arr [ s [ s . length - 1 ] ] < arr [ i ] ) { next_greater [ s [ s . length - 1 ] ] = i ; s . pop ( ) ; } s . push ( i ) ; } var count = 0 ; var maxi = - 2147483648 ; for ( var i = 0 ; i < n ; i ++ ) { if ( next_greater [ i ] - i > k && maxi < arr [ i ] ) { maxi = Math . max ( maxi , arr [ i ] ) ; count ++ ; } } return count ; } var arr = [ 4 , 2 , 3 , 6 , 4 , 3 , 2 ] ; var K = 2 ; var n = arr . length ; document . write ( countElements ( arr , n , K ) ) ;

Index/key variables: i
Accumulator variables: count, i
Iterator variables: i
Javascript program:
function maxLengthOf1s ( arr , n ) { let prefix = new Array ( n ) ; prefix . fill ( 0 ) ; for ( let i = 2 ; i < n ; i ++ ) { if ( arr [ i - 2 ] == 1 ) prefix [ i ] = prefix [ i - 1 ] + 1 ; else prefix [ i ] = 0 ; } let suffix = new Array ( n ) ; suffix . fill ( 0 ) ; for ( let i = n - 3 ; i >= 0 ; i -- ) { if ( arr [ i + 2 ] == 1 ) suffix [ i ] = suffix [ i + 1 ] + 1 ; else suffix [ i ] = 0 ; } let ans = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { ans = Math . max ( ans , prefix [ i + 1 ] + suffix [ i ] ) ; } document . write ( ans ) ; } let n = 6 ; let arr = [ 1 , 1 , 1 , 0 , 1 , 1 ] ; maxLengthOf1s ( arr , n ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function minOperations ( s ) { let n = s . length ; let pos = - 1 ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) { pos = i ; break ; } } if ( pos == - 1 ) { return 0 ; } let last = 1 ; let ans = 1 ; for ( let i = pos + 1 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) { if ( last == 1 ) { ans ++ ; last = 0 ; } } else { if ( last == 0 ) { ans ++ ; last = 1 ; } } } return ans ; } let s = " " ; document . write ( minOperations ( s ) ) ;

Index/key variables: i
Accumulator variables: ans, i
Iterator variables: i
Javascript program:
function countClumps ( arr , N ) { let clumps = 0 ; for ( let i = 0 ; i < N - 1 ; i ++ ) { let flag = 0 ; while ( arr [ i ] == arr [ i + 1 ] ) { flag = 1 ; i ++ ; } if ( flag ) clumps ++ ; } return clumps ; } let arr = [ 13 , 15 , 66 , 66 , 66 , 37 , 37 , 8 , 8 , 11 , 11 ] ; let N = arr . length ; document . write ( countClumps ( arr , N ) ) ;

Index/key variables: i
Accumulator variables: clumps, i
Iterator variables: i
Javascript program:
function findPivot ( arr , low , high ) { if ( high < low ) return - 1 ; if ( high == low ) return low ; var mid = parseInt ( ( low + high ) / 2 ) ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) { return mid ; } if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) { return mid - 1 ; } if ( arr [ low ] > arr [ mid ] ) { return findPivot ( arr , low , mid - 1 ) ; } else { return findPivot ( arr , mid + 1 , high ) ; } } function isRotated ( arr , n ) { var l = 0 ; var r = n - 1 ; var pivot = - 1 ; if ( arr [ l ] > arr [ r ] ) { pivot = findPivot ( arr , l , r ) ; var temp = pivot ; if ( l < pivot ) { while ( pivot > l ) { if ( arr [ pivot ] < arr [ pivot - 1 ] ) { return false ; } pivot -- ; } } else { pivot = temp ; pivot ++ ; while ( pivot < r ) { if ( arr [ pivot ] > arr [ pivot + 1 ] ) { return false ; } pivot ++ ; } } return true ; } else { return false ; } } var arr = [ 4 , 5 , 1 , 3 , 2 ] ; if ( isRotated ( arr , 5 ) ) document . write ( " " ) ; else document . write ( " " ) ;

Index/key variables: l, low, mid, pivot, r
Accumulator variables: pivot
Javascript program:
function nameRank ( names , marks , updates , n ) { let x = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { x [ i ] = new Array ( 3 ) ; for ( let j = 0 ; j < 3 ; j ++ ) { x [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < n ; i ++ ) { x [ i ] [ 0 ] = names [ i ] x [ i ] [ 1 ] = marks [ i ] + updates [ i ] x [ i ] [ 2 ] = i + 1 } let highest = x [ 0 ] ; for ( let j = 1 ; j < n ; j ++ ) { if ( x [ j ] [ 1 ] >= highest [ 1 ] ) highest = x [ j ] } document . write ( " " , highest [ 0 ] , " " , Math . abs ( highest [ 2 ] - 1 ) , sep = " " ) } let names = [ " " , " " , " " ] ; let marks = [ 80 , 79 , 75 ] ; let updates = [ 0 , 5 , - 9 ] ; let n = marks . length ; nameRank ( names , marks , updates , n )

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function countDigits ( n ) { let count = 0 ; while ( n > 0 ) { count += 1 ; n = parseInt ( n / 10 , 10 ) ; } return count ; } function getDistinct ( d , count ) { let num = 0 ; count = parseInt ( Math . pow ( 10 , count - 1 ) , 10 ) ; while ( count > 0 ) { num += ( count * d ) ; count = parseInt ( count / 10 , 10 ) ; } return num ; } function findCount ( L , R ) { let count = 0 ; let countDigitsL = countDigits ( L ) ; let countDigitsR = countDigits ( R ) ; let firstDigitL = parseInt ( L / parseInt ( Math . pow ( 10 , countDigitsL - 1 ) , 10 ) , 10 ) ; let firstDigitR = parseInt ( R / parseInt ( Math . pow ( 10 , countDigitsR - 1 ) , 10 ) , 10 ) ; if ( countDigitsL < countDigitsR ) { count += ( 9 * ( countDigitsR - countDigitsL - 1 ) ) ; if ( getDistinct ( firstDigitL , countDigitsL ) >= L ) count += ( 9 - firstDigitL + 1 ) ; else count += ( 9 - firstDigitL ) ; if ( getDistinct ( firstDigitR , countDigitsR ) <= R ) count += firstDigitR ; else count += ( firstDigitR - 1 ) ; } else { if ( getDistinct ( firstDigitL , countDigitsL ) >= L ) count += ( 9 - firstDigitL + 1 ) ; else count += ( 9 - firstDigitL ) ; if ( getDistinct ( firstDigitR , countDigitsR ) <= R ) count -= ( 9 - firstDigitR ) ; else count -= ( 9 - firstDigitR + 1 ) ; } return count ; } let L = 10 , R = 50 ; document . write ( findCount ( L , R ) ) ;

Accumulator variables: count, num
Javascript program:
let N = 4 ; let M = 4 ; function isPairWithDiff ( mat , k ) { let s = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] > k ) { let m = mat [ i ] [ j ] - k ; if ( s . includes ( m ) ) { return true ; } } else { let m = k - mat [ i ] [ j ] ; if ( s . includes ( m ) ) { return true ; } } s . push ( mat [ i ] [ j ] ) ; } } return false ; } let mat = [ [ 5 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 100 ] ] ; let k = 85 ; document . write ( isPairWithDiff ( mat , k ) == true ? " " : " " ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function printNSE ( arr , n ) { let s = [ ] ; let mp = new Map ( ) ; s . push ( arr [ 0 ] ) ; for ( let i = 1 ; i < n ; i ++ ) { if ( s . length == 0 ) { s . push ( arr [ i ] ) ; continue ; } while ( s . length != 0 && s [ s . length - 1 ] > arr [ i ] ) { mp [ s [ s . length - 1 ] ] = arr [ i ] ; s . pop ( ) ; } s . push ( arr [ i ] ) ; } while ( s . length != 0 ) { mp [ s [ s . length - 1 ] ] = - 1 ; s . pop ( ) ; } for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " + mp [ arr [ i ] ] + " " ) ; } let arr = [ 11 , 13 , 21 , 3 ] ; let n = arr . length ; printNSE ( arr , n ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function ElementsCalculationFunc ( pre , maxx , x , k , n ) { for ( let i = 0 , j = x ; j <= n ; j ++ , i ++ ) { if ( x * maxx [ j ] - ( pre [ j ] - pre [ i ] ) <= k ) return true ; } return false ; } function MaxNumberOfElements ( a , n , k ) { a . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i <= n ; ++ i ) { pre [ i ] = 0 ; maxx [ i ] = 0 ; } for ( let i = 1 ; i <= n ; i ++ ) { pre [ i ] = pre [ i - 1 ] + a [ i - 1 ] ; maxx [ i ] = Math . max ( maxx [ i - 1 ] , a [ i - 1 ] ) ; } let l = 1 , r = n , ans = 0 ; while ( l < r ) { let mid = Math . floor ( ( l + r ) / 2 ) ; if ( ElementsCalculationFunc ( pre , maxx , mid - 1 , k , n ) ) { ans = mid ; l = mid + 1 ; } else r = mid - 1 ; } document . write ( ans + " " ) ; } let arr = [ 2 , 4 , 9 ] ; let n = arr . length ; let k = 3 ; MaxNumberOfElements ( arr , n , k ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i
Javascript program:
class Interval { constructor ( a , b ) { this . s = a ; this . e = b ; } } function kthSmallestNum ( merged , k ) { let n = merged . length ; for ( let j = 0 ; j < n ; j ++ ) { if ( k <= Math . abs ( merged [ j ] . e - merged [ j ] . s + 1 ) ) return ( merged [ j ] . s + k - 1 ) ; k = k - Math . abs ( merged [ j ] . e - merged [ j ] . s + 1 ) ; } if ( k != 0 ) return - 1 ; return 0 ; } function mergeIntervals ( merged , arr , n ) { arr . sort ( function ( a , b ) { return a . s - b . s ; } ) ; merged . push ( arr [ 0 ] ) ; for ( let i = 1 ; i < n ; i ++ ) { let prev = merged [ merged . length - 1 ] ; let curr = arr [ i ] ; if ( ( curr . s >= prev . s && curr . s <= prev . e ) && ( curr . e > prev . e ) ) merged [ merged . length - 1 ] . e = curr . e ; else { merged . push ( curr ) ; } } return merged ; } let arr = [ new Interval ( 2 , 6 ) , new Interval ( 4 , 7 ) ] ; let n = arr . length ; let query = [ 5 , 8 ] ; let q = query . length ; let merged = [ ] ; merged = mergeIntervals ( merged , arr , n ) ; for ( let i = 0 ; i < q ; i ++ ) document . write ( kthSmallestNum ( merged , query [ i ] ) + " " ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Class/struct names: Interval
Javascript program:
function countCountries ( ans , N ) { let total_countries = 0 , i = 0 ; let invalid = false ; while ( i < N ) { let curr_size = ans [ i ] ; let num = ans [ i ] ; while ( num > 0 ) { if ( ans [ i ] != curr_size ) { document . write ( " " ) ; return ; } else num -- ; i ++ ; } total_countries ++ ; } document . write ( " " + total_countries + " " ) ; } let ans = [ 1 , 1 , 2 , 2 , 4 , 4 , 4 , 4 ] ; let n = 8 ; countCountries ( ans , n ) ;

Index/key variables: i
Accumulator variables: i, num, total_countries
Boolean variables: invalid
Javascript program:
function check_elements ( arr , n , A , B ) { let range = B - A ; for ( let i = 0 ; i < n ; i ++ ) { if ( Math . abs ( arr [ i ] ) >= A && Math . abs ( arr [ i ] ) <= B ) { let z = Math . abs ( arr [ i ] ) - A ; if ( arr [ z ] > 0 ) { arr [ z ] = arr [ z ] * - 1 ; } } } let count = 0 ; for ( let i = 0 ; i <= range && i < n ; i ++ ) { if ( arr [ i ] > 0 ) return false ; else count ++ ; } if ( count != ( range + 1 ) ) return false ; return true ; } let arr = [ 1 , 4 , 5 , 2 , 7 , 8 , 3 ] ; let n = arr . length ; let A = 2 , B = 5 ; if ( check_elements ( arr , n , A , B ) ) document . write ( " " ) ; else document . write ( " " ) ;

Index/key variables: i, z
Accumulator variables: count, i
Iterator variables: i
Javascript program:
function isPossible ( arr , n , m , curr_min ) { let studentsRequired = 1 ; let curr_sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > curr_min ) return false ; if ( curr_sum + arr [ i ] > curr_min ) { studentsRequired ++ ; curr_sum = arr [ i ] ; if ( studentsRequired > m ) return false ; } else curr_sum += arr [ i ] ; } return true ; } function findPages ( arr , n , m ) { let sum = 0 ; if ( n < m ) return - 1 ; for ( let i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; let start = 0 , end = sum ; let result = Number . MAX_VALUE ; while ( start <= end ) { let mid = Math . floor ( ( start + end ) / 2 ) ; if ( isPossible ( arr , n , m , mid ) ) { result = Math . min ( result , mid ) ; end = mid - 1 ; } else start = mid + 1 ; } return result ; } let arr = [ 12 , 34 , 67 , 90 ] ; document . write ( " " + findPages ( arr , arr . length , m ) ) ;

Index/key variables: i
Accumulator variables: curr_sum, i, studentsRequired, sum
Iterator variables: i
Javascript program:
function countPlates ( plates , lastLength , lastWidth , i , n ) { if ( i == n ) return 0 ; var taken = 0 , notTaken = 0 ; if ( lastLength > plates [ i ] [ 0 ] && lastWidth > plates [ i ] [ 1 ] ) { taken = 1 + countPlates ( plates , plates [ i ] [ 0 ] , plates [ i ] [ 1 ] , i + 1 , n ) ; notTaken = countPlates ( plates , lastLength , lastWidth , i + 1 , n ) ; } else notTaken = countPlates ( plates , lastLength , lastWidth , i + 1 , n ) ; return Math . max ( taken , notTaken ) ; } var plates = [ [ 6 , 4 ] , [ 5 , 7 ] , [ 1 , 2 ] , [ 3 , 3 ] , [ 7 , 9 ] ] ; var n = plates . length ; plates . sort ( ( v1 , v2 ) => v2 [ 0 ] * v2 [ 1 ] - v1 [ 0 ] * v1 [ 1 ] ) ; var lastLength = 1000000000 ; var lastWidth = 1000000000 ; document . write ( countPlates ( plates , lastLength , lastWidth , 0 , n ) ) ;

Index/key variables: i
Javascript program:
function maximizeSum ( arr , n , k ) { let cnt = new Array ( 30 ) . fill ( 0 ) ; let X = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < 30 ; j ++ ) { if ( arr [ i ] & ( 1 << j ) ) cnt [ j ] ++ ; } } let v = new Array ( ) ; for ( let i = 0 ; i < 30 ; i ++ ) { let gain = cnt [ i ] * ( 1 << i ) ; v . push ( [ i , gain ] ) ; } v . sort ( ( a , b ) => { if ( a [ 1 ] != b [ 1 ] ) return b [ 1 ] - a [ 1 ] ; return a [ 0 ] - b [ 0 ] ; } ) ; for ( let i = 0 ; i < k ; i ++ ) { X |= ( 1 << v [ i ] [ 0 ] ) ; } document . write ( X ) ; } let arr = [ 3 , 4 , 5 , 1 ] ; let K = 1 ; let N = arr . length ; maximizeSum ( arr , N , K ) ;

Index/key variables: i, j
Accumulator variables: X, i, j
Iterator variables: i, j
Javascript program:
class Node { constructor ( item ) { this . data = item ; this . left = this . right = null ; } } let root ; function minimumDepth ( ) { return minimumDepth ( root ) ; } function minimumDepth ( root ) { if ( root == null ) return 0 ; if ( root . left == null && root . right == null ) return 1 ; if ( root . left == null ) return minimumDepth ( root . right ) + 1 ; if ( root . right == null ) return minimumDepth ( root . left ) + 1 ; return Math . min ( minimumDepth ( root . left ) , minimumDepth ( root . right ) ) + 1 ; } root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; document . write ( " " + " " + minimumDepth ( root ) ) ;

Class/struct names: Node
Javascript program:
function maxFreqElements ( arr , N , K ) { var mp = { } ; for ( var i = 0 ; i < N ; i ++ ) { if ( mp . hasOwnProperty ( arr [ i ] ) ) { mp [ arr [ i ] ] ++ ; } else { mp [ arr [ i ] ] = 1 ; } } while ( K > 0 ) { var max = 0 ; var element = 0 ; for ( const [ key , value ] of Object . entries ( mp ) ) { if ( value > max ) { max = value ; element = key ; } } document . write ( element + " " ) ; if ( mp . hasOwnProperty ( element ) ) { mp [ element ] -- ; } else { mp [ element ] = - 1 ; } K -- ; } } var arr = [ 1 , 3 , 2 , 1 , 4 , 1 ] ; var N = arr . length ; var K = 2 ; maxFreqElements ( arr , N , K ) ;

Index/key variables: element, i
Accumulator variables: K, i
Iterator variables: i
Javascript program:
function reverse ( a ) { var i , n = a . length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } function maxDiffLargSmallOper ( arr , N , K ) { var maxDiff = 0 ; arr . sort ( ) ; arr = reverse ( arr ) ; for ( i = 0 ; i <= Math . min ( K , N - 1 ) ; i ++ ) { maxDiff += arr [ i ] ; } return maxDiff ; } var arr = [ 7 , 7 , 7 , 7 ] ; var N = arr . length ; var K = 1 ; document . write ( maxDiffLargSmallOper ( arr , N , K ) ) ;

Index/key variables: i
Accumulator variables: i, maxDiff
Iterator variables: i
Javascript program:
function minCost ( str , N , R , C ) { str = str . split ( ' ' ) ; var pq = [ ] ; var flip = 0 ; var cost = 0 ; for ( var i = 0 ; i < N ; i ++ ) { while ( pq . length > 0 && pq [ pq . length - 1 ] < i ) { pq . pop ( ) ; flip -- ; } if ( flip % 2 == 1 ) { str [ i ] = String . fromCharCode ( ' ' . charCodeAt ( 0 ) - str [ i ] . charCodeAt ( 0 ) + ' ' . charCodeAt ( 0 ) ) ; } if ( str [ i ] == ' ' ) { flip ++ ; cost += C [ i ] ; pq . push ( R [ i ] ) ; } pq . sort ( ( a , b ) => b - a ) ; } return cost ; } var str = " " ; var R = [ 1 , 2 , 2 , 3 ] ; var C = [ 3 , 1 , 2 , 3 ] ; var N = str . length ; document . write ( minCost ( str , N , R , C ) ) ;

Index/key variables: i
Accumulator variables: cost, flip, i
Iterator variables: i
Javascript program:
function dist ( p1 , p2 ) { var x0 = p1 [ 0 ] - p2 [ 0 ] ; var y0 = p1 [ 1 ] - p2 [ 1 ] ; return x0 * x0 + y0 * y0 ; } function maxDist ( p ) { var n = p . length ; var maxm = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { maxm = Math . max ( maxm , dist ( p [ i ] , p [ j ] ) ) ; } } return Math . sqrt ( maxm ) ; } var n = 5 ; var p = [ ] ; p . push ( [ 4 , 0 ] ) ; p . push ( [ 0 , 2 ] ) ; p . push ( [ - 1 , - 7 ] ) ; p . push ( [ 1 , 10 ] ) ; p . push ( [ 2 , - 3 ] ) ; document . write ( maxDist ( p ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function printArr ( arr , n ) { arr . sort ( ) ; if ( arr [ 0 ] == arr [ n - 1 ] ) { document . write ( " " ) ; } else { document . write ( " " ) ; for ( i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } } var arr = [ 1 , 2 , 2 , 1 , 3 , 1 ] ; var N = arr . length ; printArr ( arr , N ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function maximumSum ( a , b , k , n ) { let i , j ; a . sort ( ) ; b . sort ( ) ; for ( i = 0 , j = n - 1 ; i < k ; i ++ , j -- ) { if ( a [ i ] < b [ j ] ) { let temp = a [ i ] ; a [ i ] = b [ j ] ; b [ j ] = temp ; } else break ; } let sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += a [ i ] ; document . write ( sum ) ; } let K = 1 ; let A = [ 2 , 3 , 4 ] ; let B = [ 6 , 8 , 5 ] ; let N = A . length ; maximumSum ( A , B , K , N ) ;

Index/key variables: i, j
Accumulator variables: i, j, sum
Iterator variables: i
Javascript program:
function CheckSort ( arr , k , n ) { var sortarr = arr . sort ( ( a , b ) => a - b ) ; if ( k === 1 ) document . write ( " " ) ; else { var flag = 0 ; for ( var i = 0 ; i < n ; i ++ ) { flag = 0 ; for ( var j = i ; j < n ; j += k ) { if ( sortarr [ i ] === arr [ j ] ) { var tmp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = tmp ; flag = 1 ; break ; } if ( j + k >= n ) { break ; } } if ( flag === 0 ) { break ; } } if ( flag === 0 ) document . write ( " " ) ; else document . write ( " " ) ; } } var k = 3 ; var arr = [ 1 , 5 , 6 , 9 , 2 , 3 , 5 , 9 ] ; var n = arr . length ; CheckSort ( arr , k , n ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function check ( arr , n ) { let modify = 0 ; if ( arr [ n - 1 ] >= arr [ n - 2 ] ) { arr [ n - 1 ] = arr [ n - 2 ] - 1 ; modify ++ ; } if ( arr [ 0 ] <= arr [ 1 ] ) { arr [ 0 ] = arr [ 1 ] + 1 ; modify ++ ; } for ( let i = n - 2 ; i > 0 ; i -- ) { if ( ( arr [ i - 1 ] <= arr [ i ] && arr [ i + 1 ] <= arr [ i ] ) || ( arr [ i - 1 ] >= arr [ i ] && arr [ i + 1 ] >= arr [ i ] ) ) { arr [ i ] = parseInt ( ( arr [ i - 1 ] + arr [ i + 1 ] ) / 2 , 10 ) ; modify ++ ; if ( arr [ i ] == arr [ i - 1 ] arr [ i ] == arr [ i + 1 ] ) return false ; } } if ( modify > 1 ) return false ; return true ; } let arr = [ 10 , 5 , 11 , 3 ] ; let n = arr . length ; if ( check ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;

Index/key variables: i
Accumulator variables: i, modify
Iterator variables: i
Javascript program:
function maxDisjointIntervals ( list ) { list . sort ( ( a , b ) => a [ 1 ] - b [ 1 ] ) ; document . write ( " " + list [ 0 ] [ 0 ] + " " + list [ 0 ] [ 1 ] + " " + " " ) ; var r1 = list [ 0 ] [ 1 ] ; for ( var i = 1 ; i < list . length ; i ++ ) { var l1 = list [ i ] [ 0 ] ; var r2 = list [ i ] [ 1 ] ; if ( l1 > r1 ) { document . write ( " " + l1 + " " + r2 + " " + " " ) ; r1 = r2 ; } } } var N = 4 ; var intervals = [ [ 1 , 4 ] , [ 2 , 3 ] , [ 4 , 6 ] , [ 8 , 9 ] ] ; maxDisjointIntervals ( intervals ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
var next_pos = 1 ; function printIndices ( n , a ) { for ( var i = 0 ; i < n ; i ++ ) document . write ( a [ i ] [ 1 ] + " " ) ; document . write ( " " ) ; } function printPermutations ( n , a , k ) { var arr = Array . from ( Array ( n ) , ( ) => Array ( 2 ) ) ; for ( var i = 0 ; i < n ; i ++ ) { arr [ i ] [ 0 ] = a [ i ] ; arr [ i ] [ 1 ] = i ; } arr . sort ( ) ; var count = 1 ; for ( var i = 1 ; i < n ; i ++ ) if ( arr [ i ] [ 0 ] == arr [ i - 1 ] [ 0 ] ) count ++ ; if ( count < k ) { document . write ( " " ) ; return ; } for ( var i = 0 ; i < k - 1 ; i ++ ) { printIndices ( n , arr ) ; for ( var j = next_pos ; j < n ; j ++ ) { if ( arr [ j ] [ 0 ] == arr [ j - 1 ] [ 0 ] ) { [ arr [ j ] , arr [ j - 1 ] ] = [ arr [ j - 1 ] , arr [ j ] ] ; next_pos = j + 1 ; break ; } } } printIndices ( n , arr ) ; } var a = [ 1 , 3 , 3 , 1 ] ; var n = a . length ; var k = 3 ; printPermutations ( n , a , k ) ;

Index/key variables: i, j
Accumulator variables: count, i, j
Iterator variables: i, j
Javascript program:
function countDistinct ( arr , n ) { let hs = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) { hs . add ( arr [ i ] ) ; } return hs . size ; } let arr = [ 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 ] ; document . write ( countDistinct ( arr , arr . length ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function nextGap ( gap ) { if ( gap <= 1 ) return 0 ; return Math . floor ( Math . ceil ( gap / 2.0 ) ) ; } function swap ( nums , i , j ) { let temp = nums [ i ] ; nums [ i ] = nums [ j ] ; nums [ j ] = temp ; } function inPlaceMerge ( nums , start , end ) { let gap = end - start + 1 ; for ( gap = nextGap ( gap ) ; gap > 0 ; gap = nextGap ( gap ) ) { for ( let i = start ; i + gap <= end ; i ++ ) { let j = i + gap ; if ( nums [ i ] > nums [ j ] ) swap ( nums , i , j ) ; } } } function mergeSort ( nums , s , e ) { if ( s == e ) return ; let mid = Math . floor ( ( s + e ) / 2 ) ; mergeSort ( nums , s , mid ) ; mergeSort ( nums , mid + 1 , e ) ; inPlaceMerge ( nums , s , e ) ; } let nums = [ 12 , 11 , 13 , 5 , 6 , 7 ] ; mergeSort ( nums , 0 , nums . length - 1 ) ; document . write ( ( nums ) . join ( " " ) ) ;

Index/key variables: i, j
Accumulator variables: i
Iterator variables: gap, i
Javascript program:
function findOptimalSolution ( a , N ) { a . sort ( function ( a , b ) { return a - b } ) ; let points = 0 ; for ( let i = 0 ; i < N ; i ++ ) { points += a [ i ] * i ; } return points ; } let a = [ 1 , 4 , 2 , 3 , 9 ] ; let N = a . length ; document . write ( findOptimalSolution ( a , N ) ) ;

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