dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
function sum ( arr , from , to ) { let total = 0 ; for ( let i = from ; i <= to ; i ++ ) total += arr [ i ] ; return total ; } function findMax ( arr , n , k ) { let dp = new Array ( k + 1 ) ; for ( let i = 0 ; i < k + 1 ; i ++ ) { dp [ i ] = new Array ( n + 1 ) ; } for ( let i = 1 ; i <= n ; i ++ ) dp [ 1 ] [ i ] = sum ( arr , 0 , i - 1 ) ; for ( let i = 1 ; i <= k ; i ++ ) dp [ i ] [ 1 ] = arr [ 0 ] ; for ( let j = 2 ; j <= n ; j ++ ) { let best = Number . MAX_VALUE ; for ( let p = 1 ; p <= j ; p ++ ) best = Math . min ( best , Math . max ( dp [ i - 1 ] [ p ] , sum ( arr , p , j - 1 ) ) ) ; dp [ i ] [ j ] = best ; } } return dp [ k ] [ n ] ; } let arr = [ 10 , 20 , 60 , 50 , 30 , 40 ] ; let n = arr . length ; let k = 3 ; document . write ( findMax ( arr , n , k ) ) ;

Index/key variables: i, j, k, n, p
Accumulator variables: i, j, p, total
Iterator variables: i, j, p
Javascript program:
function countCrossLine ( arr , n ) { let count_crossline = 0 ; let i , key , j ; for ( i = 1 ; i < n ; i ++ ) { key = arr [ i ] ; j = i - 1 ; while ( j >= 0 && arr [ j ] > key ) { arr [ j + 1 ] = arr [ j ] ; j = j - 1 ; count_crossline ++ ; } arr [ j + 1 ] = key ; } return count_crossline ; } let arr = [ 4 , 3 , 1 , 2 ] ; let n = arr . length ; document . write ( countCrossLine ( arr , n ) + " " ) ;

Index/key variables: i, j
Accumulator variables: count_crossline, i
Iterator variables: i
Javascript program:
function findAmountPlayers ( arr , N ) { arr . sort ( ( a , b ) => b - a ) let amountA = 0 ; let amountB = 0 ; if ( N == 1 ) { amountA += arr [ 0 ] ; document . write ( " " + amountA + " " + " " + amountB + " " ) ; return ; } amountA = arr [ 0 ] ; amountB = arr [ 1 ] ; for ( let i = 2 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { amountB += arr [ i ] ; } else { amountA += arr [ i ] ; } } document . write ( " " + amountA + " " + " " + amountB + " " ) ; } let arr = [ 1 , 1 , 1 ] ; let N = arr . length findAmountPlayers ( arr , N ) ;

Index/key variables: i
Accumulator variables: amountA, amountB, i
Iterator variables: i
Javascript program:
function mycmp ( a , b ) { return a [ 0 ] > b [ 0 ] ; } function minSwapsAsc ( arr , n ) { let arrPos = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) arrPos [ i ] = new Array ( 2 ) ; for ( let i = 0 ; i < n ; i ++ ) { arrPos [ i ] [ 0 ] = arr [ i ] ; arrPos [ i ] [ 1 ] = i ; } arrPos . sort ( function ( a , b ) { return a [ 0 ] - b [ 0 ] } ) let vis = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) vis [ i ] = false let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( vis [ i ] arrPos [ i ] [ 1 ] == i ) continue ; let cycle_size = 0 ; let j = i ; while ( ! vis [ j ] ) { vis [ j ] = 1 ; j = arrPos [ j ] [ 1 ] ; cycle_size ++ ; } if ( cycle_size > 0 ) { ans += ( cycle_size - 1 ) ; } } return ans ; } function minSwapsDes ( arr , n ) { let arrPos = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) arrPos [ i ] = new Array ( 2 ) ; for ( let i = 0 ; i < n ; i ++ ) { arrPos [ i ] [ 0 ] = arr [ i ] ; arrPos [ i ] [ 1 ] = i ; } arrPos . sort ( function ( a , b ) { return b [ 0 ] - a [ 0 ] } ) let vis = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) vis [ i ] = false let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( vis [ i ] arrPos [ i ] [ 1 ] == i ) continue ; let cycle_size = 0 ; let j = i ; while ( ! vis [ j ] ) { vis [ j ] = 1 ; j = arrPos [ j ] [ 1 ] ; cycle_size ++ ; } if ( cycle_size > 0 ) { ans += ( cycle_size - 1 ) ; } } return ans ; } function minimumSwaps ( arr ) { let S1 = minSwapsAsc ( arr , arr . length ) ; let S2 = minSwapsDes ( arr , arr . length ) ; return Math . min ( S1 , S2 ) ; } let arr = [ 3 , 4 , 2 , 5 , 1 ] ; document . write ( minimumSwaps ( arr ) ) ;

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

Index/key variables: i, j, mid
Accumulator variables: i, j, mid
Iterator variables: i
Javascript program:
function check ( arr , n ) { var count = 0 ; var index = - 1 ; for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i - 1 ] >= arr [ i ] ) { count ++ ; index = i ; } } if ( count > 1 ) return false ; if ( count == 0 ) return true ; if ( index == n - 1 index == 1 ) return true ; if ( arr [ index - 1 ] < arr [ index + 1 ] ) return true ; if ( arr [ index - 2 ] < arr [ index ] ) return true ; return false ; } var arr = [ 1 , 2 , 5 , 3 , 5 ] ; var N = arr . length ; if ( check ( arr , N ) ) document . write ( " " ) ; else document . write ( " " ) ;

Index/key variables: i, index
Accumulator variables: count, i
Iterator variables: i
Javascript program:
function sortVowels ( S ) { var n = S . length ; var vow = " " ; for ( var i = 0 ; i < n ; i ++ ) { if ( S [ i ] == ' ' S [ i ] == ' ' S [ i ] == ' ' S [ i ] == ' ' S [ i ] == ' ' ) { vow += S [ i ] ; } } if ( vow . length == 0 ) { document . write ( S ) ; return ; } vow = vow . split ( ' ' ) . sort ( ) ; var j = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( S [ i ] == ' ' S [ i ] == ' ' S [ i ] == ' ' S [ i ] == ' ' S [ i ] == ' ' ) { S [ i ] = vow [ j ++ ] ; } } document . write ( S . join ( ' ' ) ) ; } var S = " " . split ( ' ' ) ; sortVowels ( S ) ;

Index/key variables: i
Accumulator variables: i, j, vow
Iterator variables: i
Javascript program:
function getBuckets ( arr , N ) { let availableWater = N * ( N - 1 ) / 2 ; arr . sort ( function ( a , b ) { return a - b } ) ; let i = 0 , sum = 0 ; while ( sum <= availableWater ) { sum += arr [ i ] ; i ++ ; } document . write ( i - 1 ) ; } let arr = [ 1 , 5 , 3 , 4 , 7 , 9 ] ; let N = arr . length ; getBuckets ( arr , N ) ;

Index/key variables: i
Accumulator variables: i, sum
Javascript program:
function minimumIncreasingSubsequences ( arr , N ) { arr . sort ( function ( a , b ) { return a - b } ) ; let count = 0 ; let i = 0 ; while ( i < N ) { let x = arr [ i ] ; let freqX = 0 ; while ( i < N && arr [ i ] == x ) { freqX ++ ; i ++ ; } count = Math . max ( count , freqX ) ; } document . write ( count ) ; } let arr = [ 2 , 1 , 2 , 1 , 4 , 3 ] ; let N = arr . length ; minimumIncreasingSubsequences ( arr , N ) ;

Index/key variables: i
Accumulator variables: freqX, i
Javascript program:
function getCount ( arr , N ) { arr . sort ( ) ; var count = 0 ; if ( N < 3 ) return 0 ; for ( var b = 0 ; b < N ; b ++ ) { var a = 0 , c = N - 1 ; var d = arr [ b ] * arr [ b ] / 4 ; while ( a < c ) { if ( a == b ) { a ++ ; continue ; } if ( c == b ) { c -- ; continue ; } if ( arr [ a ] * arr <= d ) { if ( a < b && b < c ) { count += c - a - 1 ; } else { count += c - a ; } a ++ ; } else { c -- ; } } } return count * 2 ; } var arr = [ 3 , 6 , 10 , 13 , 21 ] ; var N = arr . length ; document . write ( getCount ( arr , N ) ) ;

Index/key variables: a, b
Accumulator variables: a, b, c, count
Iterator variables: b
Javascript program:
function findMaxDifference ( arr , n ) { arr . sort ( ) ; let Leftsum = 0 ; let Totalsum = 0 ; let Min = Number . MAX_VALUE , Max = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) Totalsum += arr [ i ] ; for ( let i = 0 ; i < n ; i ++ ) { let leftNumbers = i ; let rightNumbers = n - i - 1 ; Totalsum = Totalsum - arr [ i ] ; let sum = ( leftNumbers * arr [ i ] ) - Leftsum + Totalsum - ( rightNumbers * arr [ i ] ) ; Min = Math . min ( Min , sum ) ; Max = Math . max ( Max , sum ) ; Leftsum += arr [ i ] ; } document . write ( Max - Min ) ; } let arr = [ 1 , 2 , 4 , 7 ] ; let N = arr . length ; findMaxDifference ( arr , N ) ;

Index/key variables: i
Accumulator variables: Leftsum, Totalsum, i
Iterator variables: i
Javascript program:
function maxcntPairsSumKRemoved ( arr , k ) { var cntPairs = 0 ; if ( arr . length <= 1 ) return cntPairs ; arr . sort ( ) ; var left = 0 ; var right = arr . length - 1 ; while ( left < right ) { var s = arr [ left ] + arr [ right ] ; if ( s == k ) { cntPairs += 1 ; left += 1 ; right -= 1 ; } else if ( s > k ) right -= 1 ; else left += 1 ; } return cntPairs ; } var arr = [ 1 , 2 , 3 , 4 ] ; var K = 5 ; document . write ( maxcntPairsSumKRemoved ( arr , K ) ) ;

Index/key variables: left, right
Accumulator variables: cntPairs, left, right
Javascript program:
function reverse ( a , n ) { var i , k , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } function getMinCost ( arr , n , k ) { arr . sort ( ( a , b ) => b - a ) ; var min_cost = 0 ; var X = 0 ; for ( var i = 0 ; i < n ; i += k ) { for ( var j = i ; j < i + k && j < n ; j ++ ) { min_cost += arr [ j ] * ( X + 1 ) ; } X ++ ; } return min_cost ; } var arr = [ 9 , 20 , 7 , 8 ] ; var K = 2 ; var N = arr . length ; document . write ( getMinCost ( arr , N , K ) ) ;

Index/key variables: i, j
Accumulator variables: X, i, j, min_cost
Iterator variables: i, j
Javascript program:
function minDiff ( A , K ) { A . sort ( function ( a , b ) { return a - b } ) ; let n = A . length ; if ( n <= K ) return 0 ; let mindiff = A [ n - 1 ] - A [ 0 ] ; if ( K == 0 ) return mindiff ; for ( let i = 0 , j = n - 1 - K ; j < n ; ) { mindiff = Math . min ( mindiff , A [ j ] - A [ i ] ) ; i ++ ; j ++ ; } return mindiff ; } let A = [ - 1 , 3 , - 1 , 8 , 5 , 4 ] ; let K = 3 ; document . write ( minDiff ( A , K ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i
Javascript program:
function minDiff ( A , K , N ) { if ( N <= K + 1 ) return 0 ; var maxHeap = [ ] ; var minHeap = [ ] ; for ( var i = 0 ; i < N ; i ++ ) { maxHeap . push ( A [ i ] ) ; maxHeap . sort ( ( a , b ) => b - a ) ; if ( maxHeap . length > K + 1 ) maxHeap . pop ( ) ; minHeap . push ( A [ i ] ) ; minHeap . sort ( ( a , b ) => a - b ) ; if ( minHeap . length > K + 1 ) minHeap . pop ( ) ; } var maxList = [ ] ; while ( maxHeap . length > 0 ) { maxList . push ( maxHeap [ maxHeap . length - 1 ] ) ; maxHeap . pop ( ) ; } var minList = [ ] ; while ( minHeap . length > 0 ) { minList . push ( minHeap [ minHeap . length - 1 ] ) ; minHeap . pop ( ) ; } var mindiff = 1000000000 ; for ( var i = 0 ; i <= K ; i ++ ) { mindiff = Math . min ( mindiff , maxList [ i ] - minList [ K - i ] ) ; } return mindiff ; } var A = [ - 1 , 3 , - 1 , 8 , 5 , 4 ] ; var N = A . length ; var K = 3 ; document . write ( minDiff ( A , K , N ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function sortArrayInDescAsc ( arr , N , K ) { arr . sort ( ( b , a ) => a - b ) var temp = 0 ; for ( var i = K ; i < N ; i ++ ) { for ( var j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] > arr [ j ] ) { temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } } arr . forEach ( element => { document . write ( element + " " ) ; } ) ; } var arr = [ 7 , 6 , 8 , 9 , 0 , 1 , 2 , 2 , 1 , 8 , 9 , 6 , 7 ] ; var N = arr . length ; var K = 6 ; sortArrayInDescAsc ( arr , N , K ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function findMedianOfsubSum ( arr , N ) { var sum = 0 ; for ( var i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } arr . sort ( ( a , b ) => a - b ) var dp = Array . from ( Array ( N ) , ( ) => Array ( sum + 1 ) . fill ( 0 ) ) ; for ( var i = 0 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = 1 ; } dp [ 0 ] [ arr [ 0 ] ] = 1 ; for ( var i = 1 ; i < N ; i ++ ) { for ( var j = 1 ; j <= sum ; j ++ ) { if ( j >= arr [ i ] ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j - arr [ i ] ] ; } else { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } } } var sumSub = [ ] ; for ( var j = 1 ; j <= sum ; j ++ ) { var M = dp [ N - 1 ] [ j ] ; for ( var i = 1 ; i <= M ; i ++ ) { sumSub . push ( j ) ; } } var mid = sumSub [ parseInt ( sumSub . length / 2 ) ] ; return mid ; } var arr = [ 2 , 3 , 3 ] ; var N = arr . length document . write ( findMedianOfsubSum ( arr , N ) ) ;

Index/key variables: i, j
Accumulator variables: i, j, sum
Iterator variables: i, j
Javascript program:
function printIndex ( arr , N ) { let B = [ ] ; let save = - 1 ; for ( let i = 0 ; i < N ; i ++ ) { B [ i ] = arr [ i ] ; } B . sort ( ) ; for ( let i = N - 1 ; i >= 1 ; i -- ) { if ( B [ i ] != B [ i - 1 ] ) { save = B [ i ] ; break ; } } if ( save == - 1 ) { document . write ( - 1 + " " ) ; return ; } if ( save == arr [ 0 ] && save != arr [ 1 ] ) { document . write ( 1 ) ; } else if ( save == arr [ N - 1 ] && save != arr [ N - 2 ] ) { document . write ( N ) ; } for ( let i = 1 ; i < N - 1 ; i ++ ) { if ( save == arr [ i ] && ( save != arr [ i - 1 ] save != arr [ i + 1 ] ) ) { document . write ( i + 1 ) ; break ; } } } let arr = [ 5 , 3 , 4 , 4 , 5 ] ; let N = arr . length ; printIndex ( arr , N ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function firstSubsequence ( s ) { let allsubseq = [ ] ; for ( let i = 0 ; i < s . length ; i ++ ) { let k = " " ; for ( let j = 0 ; j < s . length ; j ++ ) { if ( i != j ) { k += s [ j ] ; } } allsubseq . push ( k ) ; } ( allsubseq ) . sort ( ) ; document . write ( allsubseq [ 0 ] ) ; } let S = " " ; firstSubsequence ( S ) ;

Index/key variables: j
Accumulator variables: i, j, k
Iterator variables: i, j
Javascript program:
function evenSumK ( arr , N , K ) { if ( K > N ) { return - 1 ; } var maxSum = 0 ; var Even = [ ] ; var Odd = [ ] ; for ( var i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 ) { Odd . push ( arr [ i ] ) ; } else { Even . push ( arr [ i ] ) ; } } Odd . sort ( ( a , b ) => a - b ) ; Even . sort ( ( a , b ) => a - b ) ; var i = Even . length - 1 ; var j = Odd . length - 1 ; while ( K > 0 ) { if ( K % 2 == 1 ) { if ( i >= 0 ) { maxSum += Even [ i ] ; i -- ; } else { return - 1 ; } K -- ; } else if ( i >= 1 && j >= 1 ) { if ( Even [ i ] + Even [ i - 1 ] <= Odd [ j ] + Odd [ j - 1 ] ) { maxSum += Odd [ j ] + Odd [ j - 1 ] ; j -= 2 ; } else { maxSum += Even [ i ] + Even [ i - 1 ] ; i -= 2 ; } K -= 2 ; } else if ( i >= 1 ) { maxSum += Even [ i ] + Even [ i - 1 ] ; i -= 2 ; K -= 2 ; } else if ( j >= 1 ) { maxSum += Odd [ j ] + Odd [ j - 1 ] ; j -= 2 ; K -= 2 ; } } return maxSum ; } var arr = [ 2 , 4 , 10 , 3 , 5 ] ; var N = arr . length ; var K = 3 ; document . write ( evenSumK ( arr , N , K ) ) ;

Index/key variables: i, j
Accumulator variables: K, i, j, maxSum
Iterator variables: i
Javascript program:
function weightedMedian ( arr , W ) { let pr = [ ] ; for ( let index = 0 ; index < arr . length ; index ++ ) pr . push ( [ arr [ index ] , W [ index ] ] ) ; ( pr ) . sort ( function ( a , b ) { return a [ 1 ] - b [ 1 ] ; } ) ; if ( arr . length % 2 != 0 ) { let sums = 0 ; for ( let element = 0 ; element < pr . length ; element ++ ) { sums += pr [ element ] [ 1 ] ; if ( sums > 0.5 ) document . write ( " " + pr [ element ] [ 0 ] + " " ) ; } } else { let sums = 0 ; for ( let element = 0 ; element < pr . length ; element ++ ) { sums += pr [ element ] [ 1 ] ; if ( sums <= 0.5 ) { document . write ( " " + pr [ element ] [ 0 ] + " " ) ; break ; } } sums = 0 ; for ( let index = pr . length - 1 ; index >= 0 ; index -- ) { let element = pr [ index ] [ 0 ] ; let weight = pr [ index ] [ 1 ] ; sums += weight ; if ( sums >= 0.5 ) { document . write ( " " + element ) ; break ; } } } } let arr = [ ] ; arr . push ( 4 ) ; arr . push ( 1 ) ; arr . push ( 3 ) ; arr . push ( 2 ) ; let W = [ ] ; W . push ( 0.25 ) ; W . push ( 0.49 ) ; W . push ( 0.25 ) ; W . push ( 0.01 ) ; weightedMedian ( arr , W ) ;

Index/key variables: element, index
Accumulator variables: element, index, sums
Iterator variables: element, index
Javascript program:
function KthSmallestNum ( arr , n , k ) { var pq = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { pq . push ( [ arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ] ) ; } var cnt = 1 ; while ( cnt < k ) { pq . sort ( ( a , b ) => { if ( a [ 0 ] == b [ 0 ] ) return a [ 1 ] - b [ 1 ] return a [ 0 ] - b [ 0 ] } ) ; var interval = pq [ 0 ] ; pq . shift ( ) ; if ( interval [ 0 ] < interval [ 1 ] ) { pq . push ( [ interval [ 0 ] + 1 , interval [ 1 ] ] ) ; } cnt += 1 ; } pq . sort ( ( a , b ) => { if ( a [ 0 ] == b [ 0 ] ) return a [ 1 ] - b [ 1 ] return a [ 0 ] - b [ 0 ] } ) ; return ( pq [ 0 ] ) [ 0 ] ; } var arr = [ [ 5 , 11 ] , [ 10 , 15 ] , [ 12 , 20 ] ] ; var n = arr . length ; var k = 12 ; document . write ( KthSmallestNum ( arr , n , k ) ) ;

Index/key variables: i
Accumulator variables: cnt, i
Iterator variables: i
Javascript program:
function LongestSequence ( a , n ) { var m = new Map ( ) ; var ar = Array ( n + 1 ) . fill ( 0 ) , i , j ; for ( i = 1 ; i <= n ; i ++ ) { ar [ i ] = a [ i - 1 ] ; } a . sort ( ( a , b ) => a - b ) ; var c = 1 ; m . set ( a [ 0 ] , c ) ; for ( i = 1 ; i <= n ; i ++ ) { if ( a [ i ] != a [ i - 1 ] ) { c ++ ; m . set ( a [ i ] , c ) ; } } var cnt = new Map ( ) ; var dp = Array . from ( Array ( n + 1 ) , ( ) => Array ( 3 ) . fill ( 0 ) ) ; cnt . set ( 0 , 0 ) ; for ( i = 1 ; i <= n ; i ++ ) { ar [ i ] = m . get ( ar [ i ] ) ; if ( cnt . has ( ar [ i ] ) ) cnt . set ( ar [ i ] , cnt . get ( ar [ i ] ) + 1 ) else cnt . set ( ar [ i ] , 1 ) } var ans = 0 , x ; for ( i = 1 ; i <= n ; i ++ ) { x = ar [ i ] ; if ( dp [ x ] [ 0 ] == 0 ) { if ( dp [ x - 1 ] [ 0 ] == cnt . get ( x - 1 ) ) { dp [ x ] [ 1 ] = dp [ x - 1 ] [ 1 ] ; dp [ x ] [ 2 ] = dp [ x - 1 ] [ 1 ] ; } else { dp [ x ] [ 1 ] = dp [ x - 1 ] [ 0 ] ; } } dp [ x ] [ 2 ] = Math . max ( dp [ x - 1 ] [ 0 ] , dp [ x ] [ 2 ] ) ; if ( dp [ x - 1 ] [ 0 ] == cnt [ x - 1 ] ) { dp [ x ] [ 2 ] = Math . max ( dp [ x ] [ 2 ] , dp [ x - 1 ] [ 1 ] ) ; } for ( j = 0 ; j < 3 ; j ++ ) { dp [ x ] [ j ] ++ ; ans = Math . max ( ans , dp [ x ] [ j ] ) ; } } return ans ; } var arr = [ 2 , 6 , 4 , 8 , 2 , 9 ] ; var N = arr . length ; document . write ( LongestSequence ( arr , N ) ) ;

Index/key variables: i, j, x
Accumulator variables: c, i, j
Iterator variables: i, j
Javascript program:
function findMatrix ( N , K ) { let mat = new Array ( N ) ; for ( var i = 0 ; i < mat . length ; i ++ ) { mat [ i ] = new Array ( 2 ) ; } let element = 1 ; for ( let i = 0 ; i < N ; ++ i ) { for ( let j = 0 ; j < K - 1 ; ++ j ) { mat [ i ] [ j ] = element ++ ; } } for ( let i = 0 ; i < N ; ++ i ) { for ( let j = K - 1 ; j < N ; ++ j ) { mat [ i ] [ j ] = element ++ ; } } return mat ; } function prletMatrix ( mat , N ) { for ( let i = 0 ; i < N ; ++ i ) { for ( let j = 0 ; j < N ; ++ j ) { document . write ( mat [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let N = 3 , K = 2 ; let mat = findMatrix ( N , K ) ; prletMatrix ( mat , N ) ;

Index/key variables: i, j
Accumulator variables: element, i
Iterator variables: i, j
Javascript program:
function range_sum ( arr , a , b ) { let sum = 0 ; if ( a - 2 < 0 ) sum = arr [ b - 1 ] ; else sum = arr [ b - 1 ] - arr [ a - 2 ] ; return sum ; } function precompute_sum ( arr , brr ) { let N = arr . length ; for ( let i = 1 ; i < N ; i ++ ) { arr [ i ] = arr [ i ] + arr [ i - 1 ] ; brr [ i ] = brr [ i ] + brr [ i - 1 ] ; } } function find_sum ( arr , q , Queries ) { let brr = [ ... arr ] ; let N = arr . length ; brr . sort ( function ( a , b ) { return a - b ; } ) ; precompute_sum ( arr , brr ) ; for ( let i = 0 ; i < q ; i ++ ) { let m = Queries [ i ] [ 0 ] ; let a = Queries [ i ] [ 1 ] ; let b = Queries [ i ] [ 2 ] ; if ( m == 1 ) { document . write ( range_sum ( arr , a , b ) + " " ) ; } else if ( m == 2 ) { document . write ( range_sum ( brr , a , b ) + " " ) ; } } } let arr = [ 0 , 6 , 4 , 2 , 7 , 2 , 7 ] ; let Q = 1 ; let Queries = [ [ 2 , 3 , 6 ] ] ; find_sum ( arr , Q , Queries ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function lower_bound ( a , low , high , element ) { while ( low < high ) { let middle = low + Math . floor ( ( high - low ) / 2 ) ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } function XorSum ( A , B , N ) { let maxBit = 29 ; let ans = 0 ; for ( let k = 0 ; k < maxBit ; k ++ ) { let C = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { C [ i ] = B [ i ] % ( 1 << ( k + 1 ) ) ; } C . sort ( function ( x , y ) { return x - y ; } ) ; let count = 0 ; let l , r ; for ( let i = 0 ; i < N ; i ++ ) { let x = A [ i ] % ( 1 << ( k + 1 ) ) ; l = lower_bound ( C , 0 , N , ( 1 << k ) - x ) ; r = lower_bound ( C , 0 , N , ( 1 << k ) * 2 - x ) ; count += ( r - l ) ; l = lower_bound ( C , 0 , N , ( 1 << k ) * 3 - x ) ; r = lower_bound ( C , 0 , N , ( 1 << k ) * 4 - x ) ; count += ( r - l ) ; } if ( ( count & 1 ) != 0 ) ans += ( 1 << k ) ; } return ans ; } let A = [ 4 , 6 , 0 , 0 , 3 , 3 ] ; let B = [ 0 , 5 , 6 , 5 , 0 , 3 ] ; let N = A . length ; document . write ( XorSum ( A , B , N ) + " " ) ;

Index/key variables: i, middle
Accumulator variables: ans, count, i, k
Iterator variables: i, k
Javascript program:
function findCount ( arr , N , K ) { arr . sort ( ) ; let result = 1 ; let cur_max = arr [ 0 ] ; let cur_min = arr [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { cur_max = arr [ i ] ; if ( cur_max - cur_min > K ) { result ++ ; cur_max = arr [ i ] ; cur_min = arr [ i ] ; } } return result ; } let arr = [ 1 , 10 , 8 , 3 , 9 ] ; let K = 3 ; let N = arr . length ; document . write ( findCount ( arr , N , K ) ) ;

Index/key variables: i
Accumulator variables: i, result
Iterator variables: i
Javascript program:
var mod = 1000000007 ; function findMinValue ( a , b ) { var n = a . length ; var pro = Array ( n ) ; for ( var i = 0 ; i < n ; ++ i ) { pro [ i ] = ( ( i + 1 ) * ( n - i ) ) ; pro [ i ] *= ( 1 * a [ i ] ) ; ; } b . sort ( ( a , b ) => b - a ) pro . sort ( ( a , b ) => a - b ) var ans = 0 ; for ( var i = 0 ; i < n ; ++ i ) { ans += ( pro [ i ] % mod * b [ i ] ) % mod ; ans %= mod ; } return ans ; } var a = [ 1 , 2 , 3 ] ; var b = [ 2 , 3 , 2 ] ; document . write ( findMinValue ( a , b ) ) ;

Index/key variables: i
Accumulator variables: ans
Iterator variables: i
Javascript program:
function findTriplets ( ar , n ) { var mp = new Map ( ) ; for ( var x = 0 ; x < n ; x ++ ) { if ( mp . has ( ar [ x ] ) ) mp . set ( ar [ x ] , mp . get ( ar [ x ] ) + 1 ) else mp . set ( ar [ x ] , 1 ) } var pq = [ ] ; for ( var pa of mp ) pq . push ( [ pa [ 1 ] , pa [ 0 ] ] ) ; var ans = [ ] ; pq . sort ( ( a , b ) => { if ( a [ 0 ] == b [ 0 ] ) return a [ 1 ] - b [ 1 ] ; return a [ 0 ] - b [ 0 ] ; } ) ; while ( pq . length >= 3 ) { var ar = Array . from ( Array ( 3 ) , ( ) => Array ( 2 ) . fill ( 0 ) ) ; for ( var x = 0 ; x < 3 ; x ++ ) { ar [ x ] = pq [ pq . length - 1 ] ; pq . pop ( ) ; } ans . push ( [ ar [ 0 ] [ 1 ] , ar [ 1 ] [ 1 ] , ar [ 2 ] [ 1 ] ] ) ; for ( var x = 0 ; x < 3 ; x ++ ) { ar [ x ] [ 0 ] -- ; if ( ar [ x ] [ 0 ] ) pq . push ( ar [ x ] ) ; } pq . sort ( ( a , b ) => { if ( a [ 0 ] == b [ 0 ] ) return a [ 1 ] - b [ 1 ] ; return a [ 0 ] - b [ 0 ] ; } ) ; } document . write ( " " + " " ) ; document . write ( ans . length + " " ) ; for ( var pa of ans ) { for ( var v of pa ) document . write ( v + " " ) ; document . write ( " " ) ; } } var arr = [ 2 , 2 , 3 , 3 , 4 , 4 , 4 , 4 , 5 ] ; var n = arr . length ; findTriplets ( arr , n ) ;

Index/key variables: x
Accumulator variables: x
Iterator variables: pa, v, x
Javascript program:
function rearrange ( a ) { var i = 0 , j = a . length - 1 ; var min = 0 , k , x = 0 , temp ; while ( i < j ) { for ( k = i ; k <= j ; k ++ ) { if ( a [ k ] < a [ min ] ) min = k ; } if ( x % 2 == 0 ) { temp = a [ i ] ; a [ i ] = a [ min ] ; a [ min ] = temp ; i ++ ; min = i ; } else { temp = a [ j ] ; a [ j ] = a [ min ] ; a [ min ] = temp ; j -- ; min = j ; } x ++ ; } for ( i = 0 ; i < a . length ; i ++ ) document . write ( a [ i ] + " " ) ; } var arr = [ 1 , 3 , 3 , 4 , 5 ] ; rearrange ( arr ) ;

Index/key variables: i, j, k, min
Accumulator variables: i, j, k, x
Iterator variables: i, k