dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
function getSingle ( arr , n ) { let ones = 0 , twos = 0 ; let common_bit_mask ; for ( let i = 0 ; i < n ; i ++ ) { twos = twos | ( ones & arr [ i ] ) ; ones = ones ^ arr [ i ] ; common_bit_mask = ~ ( ones & twos ) ; ones &= common_bit_mask ; twos &= common_bit_mask ; } return ones ; } let arr = [ 3 , 3 , 2 , 3 ] ; let n = arr . length ; document . write ( " " + getSingle ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: i, ones, twos
Iterator variables: i
Javascript program:
let INT_SIZE = 32 ; function getSingle ( arr , n ) { let result = 0 ; let x , sum ; for ( let i = 0 ; i < INT_SIZE ; i ++ ) { sum = 0 ; x = ( 1 << i ) ; for ( let j = 0 ; j < n ; j ++ ) { if ( arr [ j ] & x ) sum ++ ; } if ( ( sum % 3 ) != 0 ) result |= x ; } return result ; } let arr = [ 12 , 1 , 12 , 3 , 12 , 1 , 1 , 2 , 3 , 2 , 2 , 3 , 7 ] ; let n = arr . length ; document . write ( " " + getSingle ( arr , n ) ) ;

Index/key variables: j
Accumulator variables: i, j, result, sum
Iterator variables: i, j
Javascript program:
function countSetBits ( n ) { let bitCount = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { bitCount += countSetBitsUtil ( i ) ; } return bitCount ; } function countSetBitsUtil ( x ) { if ( x <= 0 ) { return 0 ; } return ( x % 2 == 0 ? 0 : 1 ) + countSetBitsUtil ( Math . floor ( x / 2 ) ) ; } let n = 4 ; document . write ( " " ) ; document . write ( countSetBits ( n ) ) ;

Accumulator variables: bitCount, i
Iterator variables: i
Javascript program:
function countSetBits ( n ) { let i = 0 ; let ans = 0 ; while ( ( 1 << i ) <= n ) { let k = 0 ; let change = 1 << i ; for ( let j = 0 ; j <= n ; j ++ ) { ans += k ; if ( change == 1 ) { k = ! k ; change = 1 << i ; } else { change -- ; } } i ++ ; } return ans ; } let n = 17 ; document . write ( countSetBits ( n ) ) ;

Accumulator variables: ans, change, i, j
Iterator variables: j
Javascript program:
function getSetBitsFromOneToN ( N ) { var two = 2 var ans = 0 var n = N while ( n != 0 ) { ans += Math . floor ( N / two ) * ( two >> 1 ) if ( ( N & ( two - 1 ) ) > ( two >> 1 ) - 1 ) ans += ( N & ( two - 1 ) ) - ( two >> 1 ) + 1 two <<= 1 ; n >>= 1 ; } return ans }

Accumulator variables: ans
Javascript program:
function swapBits ( num , p1 , p2 , n ) { let shift1 , shift2 , value1 , value2 ; while ( n -- > 0 ) { shift1 = 1 << p1 ; shift2 = 1 << p2 ; value1 = ( ( num & shift1 ) ) ; value2 = ( ( num & shift2 ) ) ; if ( ( value1 == 0 && value2 != 0 ) || ( value2 == 0 && value1 != 0 ) ) { if ( value1 != 0 ) { num = num & ( ~ shift1 ) ; num = num | shift2 ; } else { num = num & ( ~ shift2 ) ; num = num | shift1 ; } } p1 ++ ; p2 ++ ; } return num ; } let res = swapBits ( 28 , 0 , 3 , 2 ) ; document . write ( " " + res ) ;

Accumulator variables: n, p1, p2
Javascript program:
function smallest ( x , y , z ) { let c = 0 ; while ( x && y && z ) { x -- ; y -- ; z -- ; c ++ ; } return c ; } let x = 12 , y = 15 , z = 5 ; document . write ( " " + smallest ( x , y , z ) ) ;

Accumulator variables: c, x, y, z
Javascript program:
function multiplyWith3Polet5 ( x ) { let r = 0 ; let x1Shift = x << 1 ; let x2Shifts = x << 2 ; r = ( x ^ x1Shift ) ^ x2Shifts ; let c = ( x & x1Shift ) | ( x & x2Shifts ) | ( x1Shift & x2Shifts ) ; while ( c > 0 ) { c <<= 1 ; let t = r ; r ^= c ; c &= t ; } r = r >> 1 ; return r ; } document . write ( multiplyWith3Polet5 ( 5 ) ) ;

Accumulator variables: c, r
Javascript program:
function isPowerOfFour ( n ) { let count = 0 ; if ( n && ! ( n & ( n - 1 ) ) ) { while ( n > 1 ) { n >>= 1 ; count += 1 ; } return ( count % 2 == 0 ) ? 1 : 0 ; } return 0 ; } let test_no = 64 ; if ( isPowerOfFour ( test_no ) ) document . write ( test_no + " " ) ; else document . write ( test_no + " " ) ;

Accumulator variables: count
Javascript program:
function UniqueNumbers2 ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum = ( sum ^ arr [ i ] ) ; } sum = ( sum & - sum ) ; let sum1 = 0 ; let sum2 = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( ( arr [ i ] & sum ) > 0 ) { sum1 = ( sum1 ^ arr [ i ] ) ; } else { sum2 = ( sum2 ^ arr [ i ] ) ; } } document . write ( " " + " " + sum1 + " " + sum2 ) ; } let arr = [ 2 , 3 , 7 , 9 , 11 , 2 , 3 , 11 ] ; let n = arr . length ; UniqueNumbers2 ( arr , n ) ;

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

Index/key variables: i, j
Accumulator variables: count, i, j
Iterator variables: i, j
Javascript program:
function getOddOccurrence ( arr , n ) { let hmap = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( hmap . has ( arr [ i ] ) ) { let val = hmap . get ( arr [ i ] ) ; hmap . set ( arr [ i ] , val + 1 ) ; } else { hmap . set ( arr [ i ] , 1 ) ; } } for ( let [ key , value ] of hmap . entries ( ) ) { if ( hmap . get ( key ) % 2 != 0 ) return key ; } return - 1 ; } let arr = [ 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 ] ; let n = arr . length ; document . write ( getOddOccurrence ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function countSetBits ( n ) { var count = 0 ; while ( n != 0 ) { count ++ ; n &= ( n - 1 ) ; } return count ; } function FlippedCount ( a , b ) { return countSetBits ( a ^ b ) ; } var a = 10 ; var b = 20 ; document . write ( FlippedCount ( a , b ) ) ;

Accumulator variables: count, n
Javascript program:
function PositionRightmostSetbit ( n ) { let position = 1 ; let m = 1 ; while ( ( n & m ) == 0 ) { m = m << 1 ; position ++ ; } return position ; } let n = 16 ; document . write ( PositionRightmostSetbit ( n ) ) ;

Accumulator variables: position
Javascript program:
let INT_SIZE = 32 ; function Right_most_setbit ( num ) { let pos = 1 ; for ( let i = 0 ; i < INT_SIZE ; i ++ ) { if ( ( num & ( 1 << i ) ) == 0 ) pos ++ ; else break ; } return pos ; } let num = 18 ; let pos = Right_most_setbit ( num ) ; document . write ( pos ) ;

Accumulator variables: i, pos
Iterator variables: i
Javascript program:
function Last_set_bit ( n ) { let p = 1 ; while ( n > 0 ) { if ( ( n & 1 ) > 0 ) { return p ; } p ++ ; n = n >> 1 ; } return - 1 ; } let n = 18 ; let pos = Last_set_bit ( n ) ; if ( pos != - 1 ) { document . write ( pos ) ; } else { document . write ( 0 ) ; }

Accumulator variables: p
Javascript program:
function bin ( n ) { let i ; document . write ( " " ) ; for ( i = 1 << 30 ; i > 0 ; i = Math . floor ( i / 2 ) ) { if ( ( n & i ) != 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } } bin ( 7 ) ; document . write ( " " ) ; bin ( 4 ) ;

Iterator variables: i
Javascript program:
function isPowerOfTwo ( n ) { return ( n > 0 && ( ( n & ( n - 1 ) ) == 0 ) ) ? true : false ; } function findPosition ( n ) { if ( isPowerOfTwo ( n ) == false ) return - 1 ; var i = 1 ; var pos = 1 ; while ( ( i & n ) == 0 ) { i = i << 1 ; pos += 1 ; } return pos ; } var n = 16 ; var pos = findPosition ( n ) ; if ( pos == - 1 ) document . write ( " " + n + " " ) ; else document . write ( " " + n + " " + pos ) ; document . write ( " " ) ; n = 12 ; pos = findPosition ( n ) ; if ( pos == - 1 ) document . write ( " " + n + " " ) ; else document . write ( " " + n + " " , pos ) ; document . write ( " " ) ; n = 128 ; pos = findPosition ( n ) ; if ( pos == - 1 ) document . write ( " " + n + " " ) ; else document . write ( " " + n + " " + pos ) ;

Accumulator variables: pos
Javascript program:
function isPowerOfTwo ( n ) { return ( n && ( ! ( n & ( n - 1 ) ) ) ) } function findPosition ( n ) { if ( ! isPowerOfTwo ( n ) ) return - 1 var count = 0 while ( n ) { n = n >> 1 count += 1 } return count } var n = 0 var pos = findPosition ( n ) if ( pos == - 1 ) document . write ( " " , n , " " ) else document . write ( " " , n , " " , pos ) document . write ( " " ) n = 12 pos = findPosition ( n ) if ( pos == - 1 ) document . write ( " " , n , " " ) else document . write ( " " , n , " " , pos ) document . write ( " " ) n = 128 pos = findPosition ( n ) if ( pos == - 1 ) document . write ( " " , n , " " ) else document . write ( " " , n , " " , pos ) document . write ( " " )

Accumulator variables: count
Javascript program:
function checkSentence ( str ) { var len = str . length ; if ( str [ 0 ] . charCodeAt ( 0 ) < " " . charCodeAt ( 0 ) || str [ 0 ] . charCodeAt ( 0 ) > " " . charCodeAt ( 0 ) ) return false ; if ( str [ len - 1 ] !== " " ) return false ; var prev_state = 0 , curr_state = 0 ; var index = 1 ; while ( index <= str . length ) { if ( str [ index ] . charCodeAt ( 0 ) >= " " . charCodeAt ( 0 ) && str [ index ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) curr_state = 0 ; else if ( str [ index ] === " " ) curr_state = 1 ; else if ( str [ index ] . charCodeAt ( 0 ) >= " " . charCodeAt ( 0 ) && str [ index ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) curr_state = 2 ; else if ( str [ index ] === " " ) curr_state = 3 ; if ( prev_state === curr_state && curr_state !== 2 ) return false ; if ( prev_state === 2 && curr_state === 0 ) return false ; if ( curr_state === 3 && prev_state !== 1 ) return index + 1 == str . length ; index ++ ; prev_state = curr_state ; } return false ; } var str = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , ] ; var str_size = str . length ; var i = 0 ; for ( i = 0 ; i < str_size ; i ++ ) { var temp = str [ i ] . split ( " " ) ; if ( checkSentence ( temp ) ) document . write ( ' ' + str [ i ] + ' ' + " " + " " ) ; else document . write ( ' ' + str [ i ] + ' ' + " " + " " ) ; }

Index/key variables: i, index
Accumulator variables: i, index
Iterator variables: i
Javascript program:
function maxOnesIndex ( arr , n ) { let max_count = 0 ; let max_index = 0 ; let prev_zero = - 1 ; let prev_prev_zero = - 1 ; for ( let curr = 0 ; curr < n ; ++ curr ) { if ( arr [ curr ] == 0 ) { if ( curr - prev_prev_zero > max_count ) { max_count = curr - prev_prev_zero ; max_index = prev_zero ; } prev_prev_zero = prev_zero ; prev_zero = curr ; } } if ( n - prev_prev_zero > max_count ) max_index = prev_zero ; return max_index ; } let arr = [ 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 1 ] ; let n = arr . length ; document . write ( " " + maxOnesIndex ( arr , n ) ) ;

Index/key variables: curr
Iterator variables: curr
Javascript program:
function min ( x , y ) { return ( x < y ) ? x : y ; } function max ( x , y ) { return ( x > y ) ? x : y ; } function findLength ( arr , n ) { let max_len = 1 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { let mn = arr [ i ] , mx = arr [ i ] ; for ( let j = i + 1 ; j < n ; j ++ ) { mn = min ( mn , arr [ j ] ) ; mx = max ( mx , arr [ j ] ) ; if ( ( mx - mn ) == j - i ) max_len = Math . max ( max_len , mx - mn + 1 ) ; } } return max_len ; } let arr = [ 1 , 56 , 58 , 57 , 90 , 92 , 94 , 93 , 91 , 45 ] ; let n = arr . length ; document . write ( " " + findLength ( arr , n ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function printArr ( arr , k ) { for ( let i = 0 ; i < k ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; } function printSeqUtil ( n , k , len , arr ) { if ( len == k ) { printArr ( arr , k ) ; return ; } let i = ( len == 0 ) ? 1 : arr [ len - 1 ] + 1 ; len ++ ; while ( i <= n ) { arr [ len - 1 ] = i ; printSeqUtil ( n , k , len , arr ) ; i ++ ; } len -- ; } function printSeq ( n , k ) { let arr = new Array ( k ) ; let len = 0 ; printSeqUtil ( n , k , len , arr ) ; } let k = 3 , n = 7 ; printSeq ( n , k ) ;

Index/key variables: i
Accumulator variables: i, len
Iterator variables: i
Javascript program:
function find3Numbers ( nums ) { if ( nums . length < 3 ) { document . write ( " " ) ; return ; } let seq = 1 ; let min_num = nums [ 0 ] ; let max_seq = Number . MIN_VALUE ; let store_min = min_num ; for ( let i = 1 ; i < nums . length ; i ++ ) { if ( nums [ i ] == min_num ) continue ; else if ( nums [ i ] < min_num ) { min_num = nums [ i ] ; continue ; } else if ( nums [ i ] < max_seq ) { max_seq = nums [ i ] ; store_min = min_num ; } else if ( nums [ i ] > max_seq ) { seq ++ ; if ( seq == 3 ) { document . write ( " " + store_min + " " + max_seq + " " + nums [ i ] ) ; return ; } max_seq = nums [ i ] ; } } document . write ( " " ) ; } let nums = [ 1 , 2 , - 1 , 7 , 5 ] ; find3Numbers ( nums ) ;

Index/key variables: i
Accumulator variables: i, seq
Iterator variables: i
Javascript program:
function maxSubarrayProduct ( arr , n ) { let result = arr [ 0 ] ; for ( let i = 0 ; i < n ; i ++ ) { let mul = arr [ i ] ; for ( let j = i + 1 ; j < n ; j ++ ) { result = Math . max ( result , mul ) ; mul *= arr [ j ] ; } result = Math . max ( result , mul ) ; } return result ; } let arr = [ 1 , - 2 , - 3 , 0 , 7 , - 8 , - 2 ] ; let n = arr . length ; document . write ( " " + maxSubarrayProduct ( arr , n ) ) ;

Index/key variables: i, j
Accumulator variables: i, j, mul
Iterator variables: i, j
Javascript program:
function nextGreatest ( arr , size ) { max_from_right = arr [ size - 1 ] ; arr [ size - 1 ] = - 1 ; for ( let i = size - 2 ; i >= 0 ; i -- ) { temp = arr [ i ] ; arr [ i ] = max_from_right ; if ( max_from_right < temp ) max_from_right = temp ; } } function printArray ( arr , size ) { var i ; for ( let i = 0 ; i < size ; i ++ ) document . write ( arr [ i ] + " " ) ; } arr = new Array ( 16 , 17 , 4 , 3 , 5 , 2 ) ; size = arr . length ; nextGreatest ( arr , size ) ; document . write ( " " + " " + " " ) ; printArray ( arr , size ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function maxCircularSum ( a , n ) { if ( n == 1 ) return a [ 0 ] ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } let curr_max = a [ 0 ] , max_so_far = a [ 0 ] , curr_min = a [ 0 ] , min_so_far = a [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { curr_max = Math . max ( curr_max + a [ i ] , a [ i ] ) ; max_so_far = Math . max ( max_so_far , curr_max ) ; curr_min = Math . min ( curr_min + a [ i ] , a [ i ] ) ; min_so_far = Math . min ( min_so_far , curr_min ) ; } if ( min_so_far == sum ) return max_so_far ; return Math . max ( max_so_far , sum - min_so_far ) ; } let a = [ 11 , 10 , - 20 , 5 , - 3 , - 5 , 8 , - 13 , 10 ] ; let n = a . length ; document . write ( " " + maxCircularSum ( a , n ) ) ;

Index/key variables: i
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function GetCeilIndex ( arr , T , l , r , key ) { while ( r - l > 1 ) { let m = l + parseInt ( ( r - l ) / 2 , 10 ) ; if ( arr [ T [ m ] ] >= key ) r = m ; else l = m ; } return r ; } function LongestIncreasingSubsequence ( arr , n ) { let tailIndices = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) tailIndices [ i ] = 0 ; let prevIndices = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) prevIndices [ i ] = - 1 ; let len = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ tailIndices [ 0 ] ] ) tailIndices [ 0 ] = i ; else if ( arr [ i ] > arr [ tailIndices [ len - 1 ] ] ) { prevIndices [ i ] = tailIndices [ len - 1 ] ; tailIndices [ len ++ ] = i ; } else { let pos = GetCeilIndex ( arr , tailIndices , - 1 , len - 1 , arr [ i ] ) ; prevIndices [ i ] = tailIndices [ pos - 1 ] ; tailIndices [ pos ] = i ; } } document . write ( " " + " " ) ; for ( let i = tailIndices [ len - 1 ] ; i >= 0 ; i = prevIndices [ i ] ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; return len ; } let arr = [ 2 , 5 , 3 , 7 , 11 , 8 , 10 , 13 , 6 ] ; let n = arr . length ; document . write ( " " + LongestIncreasingSubsequence ( arr , n ) ) ;

Index/key variables: i, m, pos
Accumulator variables: i, len
Iterator variables: i
Javascript program:
function maxSum ( arr , n ) { let sum = 0 ; arr . sort ( ) ; for ( let i = 0 ; i < n / 2 ; i ++ ) { sum -= ( 2 * arr [ i ] ) ; sum += ( 2 * arr [ n - i - 1 ] ) ; } return sum ; } let arr = [ 4 , 2 , 1 , 8 ] ; let n = arr . length ; document . write ( maxSum ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function threeWayPartition ( arr , n , lowVal , highVal ) { let start = 0 , end = n - 1 ; for ( let i = 0 ; i <= end ; ) { if ( arr [ i ] < lowVal ) { let temp = arr [ start ] ; arr [ start ] = arr [ i ] ; arr [ i ] = temp ; start ++ ; i ++ ; } else if ( arr [ i ] > highVal ) { let temp = arr [ end ] ; arr [ end ] = arr [ i ] ; arr [ i ] = temp ; end -- ; } else i ++ ; } } let arr = [ 1 , 14 , 5 , 20 , 4 , 2 , 54 , 20 , 87 , 98 , 3 , 1 , 32 ] ; let n = arr . length ; threeWayPartition ( arr , n , 10 , 20 ) ; document . write ( " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;

Index/key variables: end, i, start
Accumulator variables: end, i, start
Iterator variables: i
Javascript program:
function generateUtil ( A , B , C , i , j , m , n , len , flag ) { if ( flag ) { if ( len != 0 ) printArr ( C , len + 1 ) ; for ( var k = i ; k < m ; k ++ ) { if ( len == 0 ) { C [ len ] = A [ k ] ; generateUtil ( A , B , C , k + 1 , j , m , n , len , ! flag ) ; } else if ( A [ k ] > C [ len ] ) { C [ len + 1 ] = A [ k ] ; generateUtil ( A , B , C , k + 1 , j , m , n , len + 1 , ! flag ) ; } } } else { for ( var l = j ; l < n ; l ++ ) { if ( B [ l ] > C [ len ] ) { C [ len + 1 ] = B [ l ] ; generateUtil ( A , B , C , i , l + 1 , m , n , len + 1 , ! flag ) ; } } } } function generate ( A , B , m , n ) { var C = Array ( m + n ) . fill ( 0 ) ; generateUtil ( A , B , C , 0 , 0 , m , n , 0 , true ) ; } function printArr ( arr , n ) { for ( i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; } var A = [ 10 , 15 , 25 ] ; var B = [ 5 , 20 , 30 ] ; var n = A . length ; var m = B . length ; generate ( A , B , n , m ) ;

Index/key variables: i, k, l, len
Accumulator variables: i, k, l
Iterator variables: i, k, l
Javascript program:
function updateindex ( index , a , ai , b , bi ) { index [ a ] = ai ; index [ b ] = bi ; } function minSwapsUtil ( arr , pairs , index , i , n ) { if ( i > n ) return 0 ; if ( pairs [ arr [ i ] ] == arr [ i + 1 ] ) return minSwapsUtil ( arr , pairs , index , i + 2 , n ) ; var one = arr [ i + 1 ] ; var indextwo = i + 1 ; var indexone = index [ pairs [ arr [ i ] ] ] ; var two = arr [ index [ pairs [ arr [ i ] ] ] ] ; arr [ i + 1 ] = arr [ i + 1 ] ^ arr [ indexone ] ^ ( arr [ indexone ] = arr [ i + 1 ] ) ; updateindex ( index , one , indexone , two , indextwo ) ; var a = minSwapsUtil ( arr , pairs , index , i + 2 , n ) ; arr [ i + 1 ] = arr [ i + 1 ] ^ arr [ indexone ] ^ ( arr [ indexone ] = arr [ i + 1 ] ) ; updateindex ( index , one , indextwo , two , indexone ) ; one = arr [ i ] ; indexone = index [ pairs [ arr [ i + 1 ] ] ] ; two = arr [ index [ pairs [ arr [ i + 1 ] ] ] ] ; indextwo = i ; arr [ i ] = arr [ i ] ^ arr [ indexone ] ^ ( arr [ indexone ] = arr [ i ] ) ; updateindex ( index , one , indexone , two , indextwo ) ; var b = minSwapsUtil ( arr , pairs , index , i + 2 , n ) ; arr [ i ] = arr [ i ] ^ arr [ indexone ] ^ ( arr [ indexone ] = arr [ i ] ) ; updateindex ( index , one , indextwo , two , indexone ) ; return 1 + Math . min ( a , b ) ; } function minSwaps ( n , pairs , arr ) { var index = Array ( 2 * n + 1 ) . fill ( 0 ) ; for ( i = 1 ; i <= 2 * n ; i ++ ) index [ arr [ i ] ] = i ; return minSwapsUtil ( arr , pairs , index , 1 , 2 * n ) ; } var arr = [ 0 , 3 , 5 , 6 , 4 , 1 , 2 ] ; var pairs = [ 0 , 3 , 6 , 1 , 5 , 4 , 2 ] ; var m = pairs . length ; var n = m / 2 ; document . write ( " " + minSwaps ( n , pairs , arr ) ) ;

Index/key variables: a, b, i, indexone
Accumulator variables: i
Iterator variables: i
Javascript program:
function replace_elements ( arr , n ) { let pos = 0 ; for ( let i = 0 ; i < n ; i ++ ) { arr [ pos ++ ] = arr [ i ] ; while ( pos > 1 && arr [ pos - 2 ] == arr [ pos - 1 ] ) { pos -- ; arr [ pos - 1 ] ++ ; } } for ( let i = 0 ; i < pos ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 6 , 4 , 3 , 4 , 3 , 3 , 5 ] ; let n = arr . length replace_elements ( arr , n ) ;

Index/key variables: i
Accumulator variables: i, pos
Iterator variables: i
Javascript program:
function arrangeString ( str , x , y ) { let count_0 = 0 ; let count_1 = 0 ; let len = str . length ; for ( let i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' ' ) count_0 ++ ; else count_1 ++ ; } while ( count_0 > 0 count_1 > 0 ) { for ( let j = 0 ; j < x && count_0 > 0 ; j ++ ) { if ( count_0 > 0 ) { document . write ( " " ) ; count_0 -- ; } } for ( let j = 0 ; j < y && count_1 > 0 ; j ++ ) { if ( count_1 > 0 ) { document . write ( " " ) ; count_1 -- ; } } } } let str = " " ; let x = 1 ; let y = 2 ; arrangeString ( str , x , y ) ;

Index/key variables: i
Accumulator variables: count_0, count_1, i, j
Iterator variables: i, j
Javascript program:
function rearrange ( arr , n ) { if ( arr == null n % 2 == 1 ) return ; let currIdx = Math . floor ( ( n - 1 ) / 2 ) ; while ( currIdx > 0 ) { let count = currIdx , swapIdx = currIdx ; while ( count -- > 0 ) { let temp = arr [ swapIdx + 1 ] ; arr [ swapIdx + 1 ] = arr [ swapIdx ] ; arr [ swapIdx ] = temp ; swapIdx ++ ; } currIdx -- ; } } let arr = [ 1 , 3 , 5 , 2 , 4 , 6 ] ; let n = arr . length ; rearrange ( arr , n ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( " " + arr [ i ] ) ;

Index/key variables: i, swapIdx
Accumulator variables: count, currIdx, i, swapIdx
Iterator variables: i
Javascript program:
function maxDiff ( arr , arr_size ) { let max_diff = arr [ 1 ] - arr [ 0 ] ; for ( let i = 0 ; i < arr_size ; i ++ ) { for ( let j = i + 1 ; j < arr_size ; j ++ ) { if ( arr [ j ] - arr [ i ] > max_diff ) max_diff = arr [ j ] - arr [ i ] ; } } return max_diff ; } let arr = [ 1 , 2 , 90 , 10 , 110 ] ; let n = arr . length ; document . write ( " " + maxDiff ( arr , n ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function maxDiff ( arr , n ) { let maxDiff = - 1 ; let maxRight = arr [ n - 1 ] ; for ( let i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > maxRight ) maxRight = arr [ i ] ; else { let diff = maxRight - arr [ i ] ; if ( diff > maxDiff ) { maxDiff = diff ; } } } return maxDiff ; } let arr = [ 1 , 2 , 90 , 10 , 110 ] ; let n = arr . length ; document . write ( " " + maxDiff ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function maxDiff ( arr , n ) { let diff = arr [ 1 ] - arr [ 0 ] ; let curr_sum = diff ; let max_sum = curr_sum ; for ( let i = 1 ; i < n - 1 ; i ++ ) { diff = arr [ i + 1 ] - arr [ i ] ; if ( curr_sum > 0 ) curr_sum += diff ; else curr_sum = diff ; if ( curr_sum > max_sum ) max_sum = curr_sum ; } return max_sum ; } let arr = [ 80 , 2 , 6 , 3 , 100 ] ; let n = arr . length ; document . write ( " " + maxDiff ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: curr_sum, i
Iterator variables: i
Javascript program:
function findMaximum ( arr , low , high ) { var max = arr [ low ] ; var i ; for ( i = low + 1 ; i <= high ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; else break ; } return max ; } var arr = [ 1 , 30 , 40 , 50 , 60 , 70 , 23 , 20 ] ; var n = arr . length ; document . write ( " " + findMaximum ( arr , 0 , n - 1 ) ) ;

Index/key variables: i, low
Accumulator variables: i
Iterator variables: i
Javascript program:
function findMaximum ( arr , low , high ) { if ( low == high ) return arr [ low ] ; if ( ( high == low + 1 ) && arr [ low ] >= arr [ high ] ) return arr [ low ] ; if ( ( high == low + 1 ) && arr [ low ] < arr [ high ] ) return arr [ high ] ; mid = ( low + high ) / 2 ; if ( arr [ mid ] > arr [ mid + 1 ] && arr [ mid ] > arr [ mid - 1 ] ) return arr [ mid ] ; if ( arr [ mid ] > arr [ mid + 1 ] && arr [ mid ] < arr [ mid - 1 ] ) return findMaximum ( arr , low , mid - 1 ) ; return findMaximum ( arr , mid + 1 , high ) ; } arr = new Array ( 1 , 3 , 50 , 10 , 9 , 7 , 6 ) ; n = arr . length ; document . write ( " " + " " + findMaximum ( arr , 0 , n - 1 ) ) ;

Index/key variables: high, low, mid