dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
function subArraySum ( arr , n , sum ) { let curr_sum = arr [ 0 ] , start = 0 , i ; for ( i = 1 ; i <= n ; i ++ ) { while ( curr_sum > sum && start < i - 1 ) { curr_sum = curr_sum - arr [ start ] ; start ++ ; } if ( curr_sum == sum ) { let p = i - 1 ; document . write ( " " + start + " " + p + " " ) ; return 1 ; } if ( i < n ) curr_sum = curr_sum + arr [ i ] ; } document . write ( " " ) ; return 0 ; } let arr = [ 15 , 2 , 4 , 8 , 9 , 5 , 10 , 23 ] ; let n = arr . length ; let sum = 23 ; subArraySum ( arr , n , sum ) ;

Index/key variables: i, start
Accumulator variables: i, start
Iterator variables: i
Javascript program:
function maximum ( a , b , c ) { return Math . max ( Math . max ( a , b ) , c ) ; } function minimum ( a , b , c ) { return Math . min ( Math . min ( a , b ) , c ) ; } function smallestDifferenceTriplet ( arr1 , arr2 , arr3 , n ) { arr1 . sort ( function ( a , b ) { return a - b } ) ; arr2 . sort ( function ( a , b ) { return a - b } ) ; arr3 . sort ( function ( a , b ) { return a - b } ) ; let res_min = 0 , res_max = 0 , res_mid = 0 ; let i = 0 , j = 0 , k = 0 ; let diff = 2147483647 ; while ( i < n && j < n && k < n ) { let sum = arr1 [ i ] + arr2 [ j ] + arr3 [ k ] ; let max = maximum ( arr1 [ i ] , arr2 [ j ] , arr3 [ k ] ) ; let min = minimum ( arr1 [ i ] , arr2 [ j ] , arr3 [ k ] ) ; if ( min == arr1 [ i ] ) i ++ ; else if ( min == arr2 [ j ] ) j ++ ; else k ++ ; if ( diff > ( max - min ) ) { diff = max - min ; res_max = max ; res_mid = sum - ( max + min ) ; res_min = min ; } } document . write ( res_max + " " + res_mid + " " + res_min ) ; } let arr1 = [ 5 , 2 , 8 ] ; let arr2 = [ 10 , 7 , 12 ] ; let arr3 = [ 9 , 14 , 6 ] ; let n = arr1 . length ; smallestDifferenceTriplet ( arr1 , arr2 , arr3 , n ) ;

Index/key variables: i, j, k
Accumulator variables: i, j, k
Javascript program:
function find3Numbers ( A , arr_size , sum ) { let l , r ; for ( let i = 0 ; i < arr_size - 2 ; i ++ ) { for ( let j = i + 1 ; j < arr_size - 1 ; j ++ ) { for ( let k = j + 1 ; k < arr_size ; k ++ ) { if ( A [ i ] + A [ j ] + A [ k ] == sum ) { document . write ( " " + A [ i ] + " " + A [ j ] + " " + A [ k ] ) ; return true ; } } } } return false ; } let A = [ 1 , 4 , 45 , 6 , 10 , 8 ] ; let sum = 22 ; let arr_size = A . length ; find3Numbers ( A , arr_size , sum ) ;

Index/key variables: i, j, k
Accumulator variables: i, j, k
Iterator variables: i, j, k
Javascript program:
function find3Numbers ( A , arr_size , sum ) { let l , r ; A . sort ( ( a , b ) => a - b ) ; for ( let i = 0 ; i < arr_size - 2 ; i ++ ) { l = i + 1 ; r = arr_size - 1 ; while ( l < r ) { if ( A [ i ] + A [ l ] + A [ r ] == sum ) { document . write ( " " + A [ i ] + " " + A [ l ] + " " + A [ r ] ) ; return true ; } else if ( A [ i ] + A [ l ] + A [ r ] < sum ) l ++ ; else r -- ; } } return false ; } let A = [ 1 , 4 , 45 , 6 , 10 , 8 ] ; let sum = 22 ; let arr_size = A . length ; find3Numbers ( A , arr_size , sum ) ;

Index/key variables: i, l, r
Accumulator variables: i, l, r
Iterator variables: i
Javascript program:
let arr = [ 1 , 2 , 3 , 4 ] ; function subArray ( n ) { for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i ; j < n ; j ++ ) { for ( let k = i ; k <= j ; k ++ ) document . write ( arr [ k ] + " " ) ; document . write ( " " ) ; } } } document . write ( " " + " " ) ; subArray ( arr . length ) ;

Index/key variables: k
Accumulator variables: i, j, k
Iterator variables: i, j, k
Javascript program:
function printSubsequences ( arr , n ) { let opsize = parseInt ( Math . pow ( 2 , n ) , 10 ) ; for ( let counter = 1 ; counter < opsize ; counter ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( ( counter & ( 1 << j ) ) != 0 ) document . write ( arr [ j ] + " " ) ; } document . write ( " " ) ; } } let arr = [ 1 , 2 , 3 , 4 ] ; let n = arr . length ; document . write ( " " + " " ) ; printSubsequences ( arr , n ) ;

Index/key variables: j
Accumulator variables: counter, j
Iterator variables: counter, j
Javascript program:
function productArray ( arr , n ) { if ( n == 1 ) { document . write ( " " ) ; return ; } var i , temp = 1 ; var prod = Array ( n ) . fill ( 0 ) ; for ( j = 0 ; j < n ; j ++ ) prod [ j ] = 1 ; for ( i = 0 ; i < n ; i ++ ) { prod [ i ] = temp ; temp *= arr [ i ] ; } temp = 1 ; for ( i = n - 1 ; i >= 0 ; i -- ) { prod [ i ] *= temp ; temp *= arr [ i ] ; } for ( i = 0 ; i < n ; i ++ ) document . write ( prod [ i ] + " " ) ; return ; } var arr = [ 10 , 3 , 5 , 6 , 2 ] ; var n = arr . length ; document . write ( " " ) ; productArray ( arr , n ) ;

Index/key variables: i, j
Accumulator variables: i, j, temp
Iterator variables: i, j
Javascript program:
function productExceptSelf ( a , n ) { let prod = 1 ; let flag = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) flag ++ ; else prod *= a [ i ] ; } let arr = Array ( n ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( flag > 1 ) { arr [ i ] = 0 ; } else if ( flag == 0 ) arr [ i ] = ( prod / a [ i ] ) ; else if ( flag == 1 && a [ i ] != 0 ) { arr [ i ] = 0 ; } else arr [ i ] = prod ; } return arr ; } let n = 5 ; let array = [ 10 , 3 , 5 , 6 , 2 ] ; let ans = Array ( n ) . fill ( 0 ) ; ans = productExceptSelf ( array , n ) ; for ( let i = 0 ; i < n ; i ++ ) { document . write ( ans [ i ] + " " ) ; }

Index/key variables: i
Accumulator variables: flag, i, prod
Iterator variables: i
Javascript program:
function areConsecutive ( arr , n ) { if ( n < 1 ) return false ; let min = getMin ( arr , n ) ; let max = getMax ( arr , n ) ; if ( max - min + 1 == n ) { let visited = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { visited [ i ] = false ; } let i ; for ( i = 0 ; i < n ; i ++ ) { if ( visited [ arr [ i ] - min ] != false ) { return false ; } visited [ arr [ i ] - min ] = true ; } return true ; } return false ; } function getMin ( arr , n ) { let min = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < min ) min = arr [ i ] ; } return min ; } function getMax ( arr , n ) { let max = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } return max ; } let arr = [ 5 , 4 , 2 , 3 , 1 , 6 ] let n = arr . length ; if ( areConsecutive ( arr , n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function areConsecutive ( arr , n ) { if ( n < 1 ) return false ; let min = getMin ( arr , n ) ; let max = getMax ( arr , n ) ; if ( max - min + 1 == n ) { let i ; for ( i = 0 ; i < n ; i ++ ) { let j ; if ( arr [ i ] < 0 ) j = - arr [ i ] - min ; else j = arr [ i ] - min ; if ( arr [ j ] > 0 ) arr [ j ] = - arr [ j ] ; else return false ; } return true ; } return false ; } function getMin ( arr , n ) { let min = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < min ) min = arr [ i ] ; } return min ; } function getMax ( arr , n ) { let max = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } return max ; } let arr = [ 5 , 4 , 2 , 3 , 1 , 6 ] ; let n = arr . length ; if ( areConsecutive ( arr , n ) == true ) document . write ( " " ) ; else document . write ( " " ) ;

Index/key variables: i, j
Accumulator variables: i
Iterator variables: i
Javascript program:
function relativeComplement ( arr1 , arr2 , n , m ) { let i = 0 , j = 0 ; while ( i < n && j < m ) { if ( arr1 [ i ] < arr2 [ j ] ) { document . write ( arr1 [ i ] + " " ) ; i ++ ; } else if ( arr1 [ i ] > arr2 [ j ] ) { j ++ ; } else if ( arr1 [ i ] == arr2 [ j ] ) { i ++ ; j ++ ; } } while ( i < n ) document . write ( arr1 [ i ] + " " ) ; } let arr1 = [ 3 , 6 , 10 , 12 , 15 ] ; let arr2 = [ 1 , 3 , 5 , 10 , 16 ] ; let n = arr1 . length ; let m = arr2 . length ; relativeComplement ( arr1 , arr2 , n , m ) ;

Index/key variables: i, j
Accumulator variables: i, j
Javascript program:
function minOps ( arr , n , k ) { var max = arr [ 0 ] ; for ( var i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } var res = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( ( max - arr [ i ] ) % k != 0 ) return - 1 ; else res += ( max - arr [ i ] ) / k ; } return res ; } var arr = [ 21 , 33 , 9 , 45 , 63 ] ; var n = arr . length ; var k = 6 ; document . write ( minOps ( arr , n , k ) ) ;

Index/key variables: i
Accumulator variables: i, res
Iterator variables: i
Javascript program:
function solve ( A , B , C ) { let i , j , k ; i = A . length - 1 ; j = B . length - 1 ; k = C . length - 1 ; let min_diff , current_diff , max_term ; min_diff = Math . abs ( Math . max ( A [ i ] , Math . max ( B [ j ] , C [ k ] ) ) - Math . min ( A [ i ] , Math . min ( B [ j ] , C [ k ] ) ) ) ; while ( i != - 1 && j != - 1 && k != - 1 ) { current_diff = Math . abs ( Math . max ( A [ i ] , Math . max ( B [ j ] , C [ k ] ) ) - Math . min ( A [ i ] , Math . min ( B [ j ] , C [ k ] ) ) ) ; if ( current_diff < min_diff ) min_diff = current_diff ; max_term = Math . max ( A [ i ] , Math . max ( B [ j ] , C [ k ] ) ) ; if ( A [ i ] == max_term ) i -= 1 ; else if ( B [ j ] == max_term ) j -= 1 ; else k -= 1 ; } return min_diff ; } let D = [ 5 , 8 , 10 , 15 ] ; let E = [ 6 , 9 , 15 , 78 , 89 ] ; let F = [ 2 , 3 , 6 , 6 , 8 , 8 , 10 ] ; document . write ( solve ( D , E , F ) ) ;

Index/key variables: i, j, k
Accumulator variables: i, j, k
Javascript program:
function binarySearch ( arr , l , r , x ) { if ( r >= l ) { let mid = l + Math . floor ( ( r - l ) / 2 ) ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; } let arr = [ 2 , 3 , 4 , 10 , 40 ] ; let x = 10 ; let n = arr . length let result = binarySearch ( arr , 0 , n - 1 , x ) ; ( result == - 1 ) ? document . write ( " " ) : document . write ( " " + result ) ;

Index/key variables: mid
Javascript program:
function jumpSearch ( arr , x , n ) { let step = Math . sqrt ( n ) ; let prev = 0 ; while ( arr [ Math . min ( step , n ) - 1 ] < x ) { prev = step ; step += Math . sqrt ( n ) ; if ( prev >= n ) return - 1 ; } while ( arr [ prev ] < x ) { prev ++ ; if ( prev == Math . min ( step , n ) ) return - 1 ; } if ( arr [ prev ] == x ) return prev ; return - 1 ; } let arr = [ 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377 , 610 ] ; let x = 55 ; let n = arr . length ; let index = jumpSearch ( arr , x , n ) ; document . write ( ` ${ x } ${ index } ` ) ;

Index/key variables: prev
Accumulator variables: prev, step
Javascript program:
function interpolationSearch ( arr , lo , hi , x ) { let pos ; if ( lo <= hi && x >= arr [ lo ] && x <= arr [ hi ] ) { pos = lo + Math . floor ( ( ( hi - lo ) / ( arr [ hi ] - arr [ lo ] ) ) * ( x - arr [ lo ] ) ) ; ; if ( arr [ pos ] == x ) { return pos ; } if ( arr [ pos ] < x ) { return interpolationSearch ( arr , pos + 1 , hi , x ) ; } if ( arr [ pos ] > x ) { return interpolationSearch ( arr , lo , pos - 1 , x ) ; } } return - 1 ; } let arr = [ 10 , 12 , 13 , 16 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 33 , 35 , 42 , 47 ] ; let n = arr . length ; let x = 18 let index = interpolationSearch ( arr , 0 , n - 1 , x ) ; if ( index != - 1 ) { document . write ( ` ${ index } ` ) } else { document . write ( " " ) ; }

Index/key variables: hi, lo, pos
Javascript program:
function exponentialSearch ( arr , n , x ) { if ( arr [ 0 ] == x ) return 0 ; let i = 1 ; while ( i < n && arr [ i ] <= x ) i = i * 2 ; return binarySearch ( arr , i / 2 , Math . min ( i , n - 1 ) , x ) ; } function binarySearch ( arr , l , r , x ) { if ( r >= l ) { let mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; } let arr = [ 2 , 3 , 4 , 10 , 40 ] ; let n = arr . length ; let x = 10 ; let result = exponentialSearch ( arr , n , x ) ; if ( result == - 1 ) document . write ( " " ) ; else document . write ( " " + result ) ;

Index/key variables: i, mid
Javascript program:
function merge ( arr , l , m , r ) { var n1 = m - l + 1 ; var n2 = r - m ; var L = new Array ( n1 ) ; var R = new Array ( n2 ) ; for ( var i = 0 ; i < n1 ; i ++ ) L [ i ] = arr [ l + i ] ; for ( var j = 0 ; j < n2 ; j ++ ) R [ j ] = arr [ m + 1 + j ] ; var i = 0 ; var j = 0 ; var k = l ; while ( i < n1 && j < n2 ) { if ( L [ i ] <= R [ j ] ) { arr [ k ] = L [ i ] ; i ++ ; } else { arr [ k ] = R [ j ] ; j ++ ; } k ++ ; } while ( i < n1 ) { arr [ k ] = L [ i ] ; i ++ ; k ++ ; } while ( j < n2 ) { arr [ k ] = R [ j ] ; j ++ ; k ++ ; } } function mergeSort ( arr , l , r ) { if ( l >= r ) { return ; } var m = l + parseInt ( ( r - l ) / 2 ) ; mergeSort ( arr , l , m ) ; mergeSort ( arr , m + 1 , r ) ; merge ( arr , l , m , r ) ; } function printArray ( A , size ) { for ( var i = 0 ; i < size ; i ++ ) document . write ( A [ i ] + " " ) ; } var arr = [ 12 , 11 , 13 , 5 , 6 , 7 ] ; var arr_size = arr . length ; document . write ( " " ) ; printArray ( arr , arr_size ) ; mergeSort ( arr , 0 , arr_size - 1 ) ; document . write ( " " ) ; printArray ( arr , arr_size ) ;

Index/key variables: i, j, k
Accumulator variables: i, j, k
Iterator variables: i, j
Javascript program:
function getNextGap ( gap ) { gap = parseInt ( ( gap * 10 ) / 13 , 10 ) ; if ( gap < 1 ) return 1 ; return gap ; } function sort ( arr ) { let n = arr . length ; let gap = n ; let swapped = true ; while ( gap != 1 swapped == true ) { gap = getNextGap ( gap ) ; swapped = false ; for ( let i = 0 ; i < n - gap ; i ++ ) { if ( arr [ i ] > arr [ i + gap ] ) { let temp = arr [ i ] ; arr [ i ] = arr [ i + gap ] ; arr [ i + gap ] = temp ; swapped = true ; } } } } let arr = [ 8 , 4 , 1 , 56 , 3 , - 44 , 23 , - 6 , 28 , 0 ] ; sort ( arr ) ; document . write ( " " + " " ) ; for ( let i = 0 ; i < arr . length ; ++ i ) document . write ( arr [ i ] + " " ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Boolean variables: swapped
Javascript program:
function cycleSort ( arr , n ) { let writes = 0 ; for ( let cycle_start = 0 ; cycle_start <= n - 2 ; cycle_start ++ ) { let item = arr [ cycle_start ] ; let pos = cycle_start ; for ( let i = cycle_start + 1 ; i < n ; i ++ ) if ( arr [ i ] < item ) pos ++ ; if ( pos == cycle_start ) continue ; while ( item == arr [ pos ] ) pos += 1 ; if ( pos != cycle_start ) { let temp = item ; item = arr [ pos ] ; arr [ pos ] = temp ; writes ++ ; } while ( pos != cycle_start ) { pos = cycle_start ; for ( let i = cycle_start + 1 ; i < n ; i ++ ) if ( arr [ i ] < item ) pos += 1 ; while ( item == arr [ pos ] ) pos += 1 ; if ( item != arr [ pos ] ) { let temp = item ; item = arr [ pos ] ; arr [ pos ] = temp ; writes ++ ; } } } } let arr = [ 1 , 8 , 3 , 9 , 10 , 10 , 2 , 4 ] ; let n = arr . length ; cycleSort ( arr , n ) ; document . write ( " " + " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;

Index/key variables: cycle_start, i, pos
Accumulator variables: cycle_start, i, pos, writes
Iterator variables: cycle_start, i
Javascript program:
class Node { constructor ( val ) { this . data = val ; this . next = null ; } } function mergeSort ( head ) { if ( head . next == null ) return head ; var mid = findMid ( head ) ; var head2 = mid . next ; mid . next = null ; var newHead1 = mergeSort ( head ) ; var newHead2 = mergeSort ( head2 ) ; var finalHead = merge ( newHead1 , newHead2 ) ; return finalHead ; } function merge ( head1 , head2 ) { var merged = new Node ( - 1 ) ; var temp = merged ; while ( head1 != null && head2 != null ) { if ( head1 . data < head2 . data ) { temp . next = head1 ; head1 = head1 . next ; } else { temp . next = head2 ; head2 = head2 . next ; } temp = temp . next ; } while ( head1 != null ) { temp . next = head1 ; head1 = head1 . next ; temp = temp . next ; } while ( head2 != null ) { temp . next = head2 ; head2 = head2 . next ; temp = temp . next ; } return merged . next ; } function findMid ( head ) { var slow = head , fast = head . next ; while ( fast != null && fast . next != null ) { slow = slow . next ; fast = fast . next . next ; } return slow ; } function printList ( head ) { while ( head != null ) { document . write ( head . data + " " ) ; head = head . next ; } } var head = new Node ( 7 ) ; var temp = head ; temp . next = new Node ( 10 ) ; temp = temp . next ; temp . next = new Node ( 5 ) ; temp = temp . next ; temp . next = new Node ( 20 ) ; temp = temp . next ; temp . next = new Node ( 3 ) ; temp = temp . next ; temp . next = new Node ( 2 ) ; temp = temp . next ; head = mergeSort ( head ) ; document . write ( " " ) ; printList ( head ) ;

Class/struct names: Node
Javascript program:
function countSort ( arr , n , exp ) { let output = new Array ( n ) ; let count = new Array ( n ) ; count . fill ( 0 ) ; output . fill ( 0 ) ; let i ; for ( i = 0 ; i < n ; i ++ ) count [ i ] = 0 ; for ( i = 0 ; i < n ; i ++ ) count [ parseInt ( arr [ i ] / exp , 10 ) % n ] ++ ; for ( i = 1 ; i < n ; i ++ ) count [ i ] += count [ i - 1 ] ; for ( i = n - 1 ; i >= 0 ; i -- ) { output [ count [ parseInt ( arr [ i ] / exp , 10 ) % n ] - 1 ] = arr [ i ] ; count [ parseInt ( arr [ i ] / exp , 10 ) % n ] -- ; } for ( i = 0 ; i < n ; i ++ ) arr [ i ] = output [ i ] ; } function sort ( arr , n ) { countSort ( arr , n , 1 ) ; countSort ( arr , n , n ) ; } function printArr ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 40 , 12 , 45 , 32 , 33 , 1 , 22 ] ; let n = arr . length ; document . write ( " " + " " ) ; printArr ( arr , n ) ; sort ( arr , n ) ; document . write ( " " + " " ) ; printArr ( arr , n ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function printClosest ( ar1 , ar2 , m , n , x ) { let diff = Number . MAX_VALUE ; let res_l , res_r ; let l = 0 , r = n - 1 ; while ( l < m && r >= 0 ) { if ( Math . abs ( ar1 [ l ] + ar2 [ r ] - x ) < diff ) { res_l = l ; res_r = r ; diff = Math . abs ( ar1 [ l ] + ar2 [ r ] - x ) ; } if ( ar1 [ l ] + ar2 [ r ] > x ) r -- ; else l ++ ; } document . write ( " " + ar1 [ res_l ] + " " + ar2 [ res_r ] + " " ) ; } let ar1 = [ 1 , 4 , 5 , 7 ] ; let ar2 = [ 10 , 20 , 30 , 40 ] ; let m = ar1 . length ; let n = ar2 . length ; let x = 38 ; printClosest ( ar1 , ar2 , m , n , x ) ;

Index/key variables: l, r, res_l, res_r
Accumulator variables: l, r
Javascript program:
function printClosest ( arr , n , x ) { let res_l = 0 , res_r = 0 ; let l = 0 , r = n - 1 , diff = Number . MAX_VALUE ; while ( r > l ) { if ( Math . abs ( arr [ l ] + arr [ r ] - x ) < diff ) { res_l = l ; res_r = r ; diff = Math . abs ( arr [ l ] + arr [ r ] - x ) ; } if ( arr [ l ] + arr [ r ] > x ) r -- ; else l ++ ; } document . write ( " " + arr [ res_l ] + " " + arr [ res_r ] ) ; } let arr = [ 10 , 22 , 28 , 29 , 30 , 40 ] , x = 54 ; let n = arr . length ; printClosest ( arr , n , x ) ;

Index/key variables: l, r, res_l, res_r
Accumulator variables: l, r
Javascript program:
function countOnes ( arr , low , high ) { if ( high >= low ) { let mid = Math . trunc ( low + ( high - low ) / 2 ) ; if ( ( mid == high arr [ mid + 1 ] == 0 ) && ( arr [ mid ] == 1 ) ) return mid + 1 ; if ( arr [ mid ] == 1 ) return countOnes ( arr , ( mid + 1 ) , high ) ; return countOnes ( arr , low , ( mid - 1 ) ) ; } return 0 ; } let arr = [ 1 , 1 , 1 , 1 , 0 , 0 , 0 ] ; let n = arr . length ; document . write ( " " + countOnes ( arr , 0 , n - 1 ) ) ;

Index/key variables: mid
Javascript program:
function solve ( a , n ) { let maxx = - 1 , minn = a [ 0 ] , l = 0 , r = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] > maxx ) { maxx = a [ i ] ; l = i ; } if ( a [ i ] <= minn ) { minn = a [ i ] ; r = i ; } } if ( r < l ) document . write ( l + ( n - r - 2 ) ) ; else document . write ( l + ( n - r - 1 ) ) ; } let a = [ 5 , 6 , 1 , 3 ] ; let n = a . length ; solve ( a , n ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function printMaxActivities ( s , f , n ) { let i , j ; document . write ( " " ) ; i = 0 ; document . write ( i + " " ) ; for ( j = 1 ; j < n ; j ++ ) { if ( s [ j ] >= f [ i ] ) { document . write ( j + " " ) ; i = j ; } } } let s = [ 1 , 3 , 0 , 5 , 8 , 5 ] let f = [ 2 , 4 , 6 , 7 , 9 , 9 ] let n = s . length ; printMaxActivities ( s , f , n ) ;

Index/key variables: i, j
Accumulator variables: j
Iterator variables: j
Javascript program:
function MaxActivities ( arr , n ) { let selected = [ ] ; Activity = Activity . sort ( function ( a , b ) { return a [ 1 ] - b [ 1 ] ; } ) ; let i = 0 selected . push ( arr [ i ] ) ; for ( let j = 1 ; j < n ; j ++ ) { if ( arr [ j ] [ 0 ] >= arr [ i ] [ 1 ] ) { selected . push ( arr [ j ] ) ; i = j ; } } return selected ; } Activity = [ [ 5 , 9 ] , [ 1 , 2 ] , [ 3 , 4 ] , [ 0 , 6 ] , [ 5 , 7 ] , [ 8 , 9 ] ] ; n = Activity . length ; selected = MaxActivities ( Activity , n ) ; document . write ( " " ) console . log ( selected ) for ( let i = 0 ; i < selected . length ; i ++ ) document . write ( " " + selected [ i ] + " " )

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function lis ( arr , n ) { let lis = Array ( n ) . fill ( 0 ) ; let i , j , max = 0 ; for ( i = 0 ; i < n ; i ++ ) lis [ i ] = 1 ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; for ( i = 0 ; i < n ; i ++ ) if ( max < lis [ i ] ) max = lis [ i ] ; return max ; } let arr = [ 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 ] ; let n = arr . length ; document . write ( " " + lis ( arr , n ) + " " ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function min ( x , y , z ) { if ( x < y ) return ( x < z ) ? x : z ; else return ( y < z ) ? y : z ; } function minCost ( cost , m , n ) { if ( n < 0 m < 0 ) return Number . MAX_VALUE ; else if ( m == 0 && n == 0 ) return cost [ m ] [ n ] ; else return cost [ m ] [ n ] + min ( minCost ( cost , m - 1 , n - 1 ) , minCost ( cost , m - 1 , n ) , minCost ( cost , m , n - 1 ) ) ; } var cost = [ [ 1 , 2 , 3 ] , [ 4 , 8 , 2 ] , [ 1 , 5 , 3 ] ] ; document . write ( minCost ( cost , 2 , 2 ) ) ;

Index/key variables: m, n
Javascript program:
function minCost ( cost , m , n ) { let i , j ; let tc = new Array ( m + 1 ) ; for ( let k = 0 ; k < m + 1 ; k ++ ) { tc [ k ] = new Array ( n + 1 ) ; } tc [ 0 ] [ 0 ] = cost [ 0 ] [ 0 ] ; for ( i = 1 ; i <= m ; i ++ ) tc [ i ] [ 0 ] = tc [ i - 1 ] [ 0 ] + cost [ i ] [ 0 ] ; for ( j = 1 ; j <= n ; j ++ ) tc [ 0 ] [ j ] = tc [ 0 ] [ j - 1 ] + cost [ 0 ] [ j ] ; for ( i = 1 ; i <= m ; i ++ ) for ( j = 1 ; j <= n ; j ++ ) tc [ i ] [ j ] = Math . min ( tc [ i - 1 ] [ j - 1 ] , tc [ i - 1 ] [ j ] , tc [ i ] [ j - 1 ] ) + cost [ i ] [ j ] ; return tc [ m ] [ n ] ; } function min ( x , y , z ) { if ( x < y ) return ( x < z ) ? x : z ; else return ( y < z ) ? y : z ; } let cost = [ [ 1 , 2 , 3 ] , [ 4 , 8 , 2 ] , [ 1 , 5 , 3 ] ] ; document . write ( minCost ( cost , 2 , 2 ) ) ;

Index/key variables: i, j, k, m, n
Accumulator variables: i, j, k
Iterator variables: i, j, k
Javascript program:
function max ( a , b ) { return ( a > b ) ? a : b ; } function knapSack ( W , wt , val , n ) { let i , w ; let K = new Array ( n + 1 ) ; for ( i = 0 ; i <= n ; i ++ ) { K [ i ] = new Array ( W + 1 ) ; for ( w = 0 ; w <= W ; w ++ ) { if ( i == 0 w == 0 ) K [ i ] [ w ] = 0 ; else if ( wt [ i - 1 ] <= w ) K [ i ] [ w ] = max ( val [ i - 1 ] + K [ i - 1 ] [ w - wt [ i - 1 ] ] , K [ i - 1 ] [ w ] ) ; else K [ i ] [ w ] = K [ i - 1 ] [ w ] ; } } return K [ n ] [ W ] ; } let val = [ 60 , 100 , 120 ] ; let wt = [ 10 , 20 , 30 ] ; let W = 50 ; let n = val . length ; document . write ( knapSack ( W , wt , val , n ) ) ;

Index/key variables: W, i, n, w
Accumulator variables: i, w
Iterator variables: i, w
Javascript program:
function maxDivide ( a , b ) { while ( a % b == 0 ) a = a / b ; return a ; } function isUgly ( no ) { no = maxDivide ( no , 2 ) ; no = maxDivide ( no , 3 ) ; no = maxDivide ( no , 5 ) ; return ( no == 1 ) ? 1 : 0 ; } function getNthUglyNo ( n ) { var i = 1 ; var count = 1 ; while ( n > count ) { i ++ ; if ( isUgly ( i ) == 1 ) count ++ ; } return i ; } var no = getNthUglyNo ( 150 ) ; document . write ( " " + " " + no ) ;

Accumulator variables: count, i