dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
function findMissingPositive ( arr , n ) { let m = 1 ; let x = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( m < arr [ i ] ) { x . add ( arr [ i ] ) ; } else if ( m == arr [ i ] ) { m = m + 1 ; while ( x . has ( m ) ) { x . delete ( m ) ; m = m + 1 ; } } } return m ; } let arr = [ 2 , 3 , - 7 , 6 , 8 , 1 , - 10 , 15 ] ; let n = arr . length ; document . write ( findMissingPositive ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function prime ( n ) { if ( n == 1 ) return false ; for ( let i = 2 ; i * i <= n ; ++ i ) if ( n % i == 0 ) return false ; return true ; } function Min_Sum ( n ) { if ( prime ( n ) ) return 1 ; if ( n % 2 == 0 ) return 2 ; else { if ( prime ( n - 2 ) ) return 2 ; else return 3 ; } } let n = 27 ; document . write ( Min_Sum ( n ) ) ;

Iterator variables: i
Javascript program:
function find_maximum_value ( a , n ) { var sum = 0 ; var minimum = Number . MAX_VALUE ; var pos = 0 , neg = 0 ; for ( i = 0 ; i < n ; i ++ ) { minimum = Math . min ( minimum , Math . abs ( a [ i ] ) ) ; sum += Math . abs ( a [ i ] ) ; if ( a [ i ] >= 0 ) pos += 1 ; else neg += 1 ; } if ( pos > 0 && neg > 0 ) return sum ; return ( sum - 2 * minimum ) ; } var a = [ 5 , 4 , 6 , 2 ] ; var n = a . length ; document . write ( find_maximum_value ( a , n ) ) ;

Index/key variables: i
Accumulator variables: i, neg, pos, sum
Iterator variables: i
Javascript program:
function find_number ( N , K ) { var r ; if ( N % 100 == 0 ) { N += 1 ; r = N . toString ( ) ; } else { r = N . toString ( ) ; } for ( var i = 1 ; i <= K ; i ++ ) r += " " ; return r ; } var N = 1000 , K = 2 ; var ans = find_number ( N , K ) ; document . write ( ans ) ;

Accumulator variables: N, i, r
Iterator variables: i
Javascript program:
function countPairs ( s ) { let st = new Set ( ) ; st . add ( ' ' ) ; st . add ( ' ' ) ; st . add ( ' ' ) ; st . add ( ' ' ) ; st . add ( ' ' ) ; let count = 0 ; let n = s . length ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( st . has ( s [ i ] ) && ! st . has ( s [ i + 1 ] ) ) count ++ ; } return count ; } let s = " " ; document . write ( countPairs ( s ) ) ;

Index/key variables: i
Accumulator variables: count, i
Iterator variables: i
Javascript program:
function maxSize ( v , n ) { v . push ( 0 ) ; v . push ( 100001 ) ; n += 2 ; v . sort ( ( a , b ) => a - b ) ; let mx = 0 ; for ( let i = 1 ; i < n - 1 ; i ++ ) { let L = v [ i - 1 ] + 1 ; let R = v [ i + 1 ] - 1 ; let cnt = R - L + 1 ; mx = Math . max ( mx , cnt ) ; } return mx ; } let v = [ 200 , 10 , 5 ] ; let n = v . length ; document . write ( maxSize ( v , n ) ) ;

Accumulator variables: i, n
Iterator variables: i
Javascript program:
function maxLength ( a , n ) { let sum = 0 , len = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += a [ i ] ; return n ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) len = Math . max ( len , Math . max ( n - i - 1 , i ) ) ; } return len ; } let a = [ 1 , 2 , 3 , 2 ] ; let n = a . length ; document . write ( maxLength ( a , n ) + " " ) ;

Index/key variables: i
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function solve ( n ) { let initial_array = new Array ( n - 1 ) ; for ( var i = 0 ; i < initial_array . length ; i ++ ) { initial_array [ i ] = new Array ( 2 ) ; } let final_array = new Array ( n ) ; for ( var i = 0 ; i < final_array . length ; i ++ ) { final_array [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < n - 1 ; ++ i ) initial_array [ 0 ] [ i ] = i + 1 ; for ( let i = 1 ; i < n - 1 ; ++ i ) for ( let j = 0 ; j < n - 1 ; ++ j ) initial_array [ i ] [ j ] = initial_array [ i - 1 ] [ ( j + 1 ) % ( n - 1 ) ] ; for ( let i = 0 ; i < n - 1 ; ++ i ) for ( let j = 0 ; j < n - 1 ; ++ j ) final_array [ i ] [ j ] = initial_array [ i ] [ j ] ; for ( let i = 0 ; i < n ; ++ i ) final_array [ i ] [ n - 1 ] = final_array [ n - 1 ] [ i ] = 0 ; for ( let i = 0 ; i < n ; ++ i ) { let t0 = final_array [ i ] [ i ] ; let t1 = final_array [ i ] [ n - 1 ] ; let s = final_array [ i ] [ i ] ; final_array [ i ] [ i ] = final_array [ i ] [ n - 1 ] ; final_array [ i ] [ n - 1 ] = s ; final_array [ n - 1 ] [ i ] = t0 ; } for ( let i = 0 ; i < n ; ++ i ) { for ( let j = 0 ; j < n ; ++ j ) document . write ( final_array [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } let n = 5 ; solve ( n ) ;

Index/key variables: i, j
Accumulator variables: i
Iterator variables: i, j
Javascript program:
function findPerm ( n , differences ) { let ans = [ ] ; ans . push ( 0 ) ; let x = 0 ; for ( let i = 0 ; i <= n - 2 ; ++ i ) { let diff = differences [ i ] ; x = x + diff ; ans . push ( x ) ; } let anss = [ ] ; for ( let obj = 0 ; obj < ans . length ; obj ++ ) anss . push ( ans [ obj ] ) ; ans . sort ( function ( a , b ) { return a - b ; } ) ; let flag = - 1 ; for ( let i = 1 ; i <= n - 1 ; ++ i ) { let res = ans [ i ] - ans [ i - 1 ] ; if ( res != 1 ) { flag = 0 ; } } if ( flag == 0 ) { document . write ( - 1 ) ; return ; } else { let mpp = new Map ( ) ; let j = 1 ; let value_at_index = [ ] ; for ( let x1 = 0 ; x1 < ans . length ; x1 ++ ) { mpp . set ( ans [ x1 ] , j ) ; ++ j ; } for ( let x2 = 0 ; x2 < anss . length ; x2 ++ ) { value_at_index . push ( mpp . get ( anss [ x2 ] ) ) ; } for ( let x3 = 0 ; x3 < value_at_index . length ; x3 ++ ) { document . write ( value_at_index [ x3 ] + " " ) ; } document . write ( " " ) ; } } let differences = [ ] ; differences . push ( 2 ) ; differences . push ( - 3 ) ; differences . push ( 2 ) ; let n = differences . length + 1 ; findPerm ( n , differences ) ;

Index/key variables: i, obj, x1, x2, x3
Accumulator variables: obj, x1, x2, x3
Iterator variables: i, obj, x1, x2, x3
Javascript program:
function isPalindrome ( str , i , j ) { while ( i < j ) { if ( str [ i ] != str [ j ] ) return false ; i ++ ; j -- ; } return true ; } function countPrimePalindrome ( str , len ) { var prime = Array ( len + 1 ) . fill ( true ) ; prime [ 0 ] = prime [ 1 ] = false ; for ( var p = 2 ; p * p <= len ; p ++ ) { if ( prime [ p ] ) { for ( var i = p * p ; i <= len ; i += p ) prime [ i ] = false ; } } var count = 0 ; for ( var j = 2 ; j <= len ; j ++ ) { if ( prime [ j ] ) { for ( var i = 0 ; i + j - 1 < len ; i ++ ) { if ( isPalindrome ( str , i , i + j - 1 ) ) count ++ ; } } } return count ; } var s = " " ; var len = s . length ; document . write ( countPrimePalindrome ( s , len ) ) ;

Index/key variables: i, j, p
Accumulator variables: count, i, j, p
Iterator variables: i, j, p
Javascript program:
function getProbability ( p , q ) { p /= 100 ; q /= 100 ; let probability = ( p * p + q * q ) / ( p + q ) ; return probability ; } let p = 80.0 ; let q = 40.0 ; document . write ( getProbability ( p , q ) . toPrecision ( 15 ) ) ;

Accumulator variables: p, q
Javascript program:
function check ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 ) return true ; } return false ; } let arr = [ 3 , 9 , 12 , 13 , 15 ] ; let n = arr . length ; if ( check ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function findMaximum ( arr , size ) { let i = 0 , j = size - 1 , mid ; while ( i <= j ) { mid = i + Math . floor ( ( j - i ) / 2 ) ; if ( arr [ mid ] < 0 ) i = mid + 1 ; else if ( arr [ mid ] > 0 ) j = mid - 1 ; } return Math . max ( i , size - i ) ; } let arr = [ - 9 , - 7 , - 4 , 1 , 5 , 8 , 9 ] ; let N = arr . length ; document . write ( findMaximum ( arr , N ) ) ;

Index/key variables: mid
Javascript program:
function solve ( n , m , queries ) { let countE1 = 0 ; let removed = new Set ( ) ; for ( let i = 0 ; i < m ; i ++ ) { if ( queries [ i ] [ 0 ] == 1 ) ++ countE1 ; else if ( queries [ i ] [ 0 ] == 2 ) removed . add ( queries [ i ] [ 1 ] ) ; else { let position = queries [ i ] [ 1 ] - countE1 ; if ( removed . has ( queries [ i ] [ 1 ] ) != false position <= 0 ) document . write ( " " + " " ) ; else { for ( let it of removed ) { if ( it > queries [ i ] [ 1 ] ) break ; position -- ; } document . write ( position + " " ) ; } } } } let N = 5 , Q = 3 ; let queries = new Array ( Q ) ; for ( let i = 0 ; i < Q ; i ++ ) { queries [ i ] = new Array ( 2 ) ; } queries [ 0 ] [ 0 ] = 1 ; queries [ 0 ] [ 1 ] = 0 ; queries [ 1 ] [ 0 ] = 3 ; queries [ 1 ] [ 1 ] = 3 ; queries [ 2 ] [ 0 ] = 2 ; queries [ 2 ] [ 1 ] = 2 ; solve ( N , Q , queries ) ;

Index/key variables: i
Accumulator variables: i, position
Iterator variables: i, it
Javascript program:
function lowerBound ( Ans , target ) { const targetRange = [ - 1 , - 1 ] const leftIdx = extremeInsertionIndex ( Ans , target , true ) if ( leftIdx === Ans . length Ans [ leftIdx ] != target ) return targetRange targetRange [ 0 ] = leftIdx targetRange [ 1 ] = extremeInsertionIndex ( Ans , target , false ) - 1 return targetRange function extremeInsertionIndex ( Ans , target , left ) { let lo = 0 , hi = Ans . length while ( lo < hi ) { const mid = lo + Math . floor ( ( hi - lo ) / 2 ) if ( Ans [ mid ] > target || ( left && target === Ans [ mid ] ) ) hi = mid else lo = mid + 1 } return lo } } function maxIndex ( N , B ) { let maximumIndexReached = 0 ; let Ans = [ ] ; for ( let i = 1 ; i <= N ; i ++ ) { maximumIndexReached += i ; Ans . push ( i ) ; } Ans . reverse ( ) ; for ( let i = 1 ; i < Ans . length ; i ++ ) { Ans [ i ] += Ans [ i - 1 ] ; } while ( maximumIndexReached ) { let it = lowerBound ( Ans , maximumIndexReached - B ) ; if ( it == - 1 ) { break ; } if ( it == maximumIndexReached - B ) { maximumIndexReached -- ; } else { break ; } } return maximumIndexReached ; } let N = 3 , B = 2 ; document . write ( maxIndex ( N , B ) ) ;

Index/key variables: i, leftIdx, mid
Accumulator variables: i, maximumIndexReached
Iterator variables: i
Javascript program:
function maxKth ( arr , N , C , K ) { if ( N + C < K ) { return - 1 ; } let iter = 300 ; let l = 0 , r = 1000000000.0 ; while ( iter -- ) { let mid = ( l + r ) * 0.5 ; let a = 0 ; let b = 0 ; for ( let i = 0 ; i < N ; i ++ ) { a += Math . floor ( arr [ i ] / mid ) ; if ( arr [ i ] >= mid ) { b ++ ; } } if ( a >= K && b + C >= K ) { l = mid ; } else { r = mid ; } } return l ; } let arr = [ 5 , 8 ] ; let K = 1 , C = 1 ; let N = arr . length ; document . write ( maxKth ( arr , N , C , K ) ) ;

Index/key variables: i
Accumulator variables: a, b, i, iter
Iterator variables: i
Javascript program:
function isPossibleToSelect ( arr , N , dif , K ) { let count = 1 ; let prev = arr [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { if ( arr [ i ] >= ( prev + dif ) ) { count ++ ; if ( count == K ) return true ; prev = arr [ i ] ; } } return false ; } function binarySearch ( arr , left , right , K , N ) { let ans = 1 ; while ( left <= right ) { let dif = left + Math . floor ( ( right - left ) / 2 ) ; if ( isPossibleToSelect ( arr , N , dif , K ) ) { ans = Math . max ( ans , dif ) ; left = dif + 1 ; } else right = dif - 1 ; } return ans ; } let N , K ; N = 7 , K = 4 ; let arr = [ 1 , 4 , 9 , 0 , 2 , 13 , 3 ] ; arr . sort ( function ( a , b ) { return a - b } ) document . write ( binarySearch ( arr , 0 , arr [ N - 1 ] , K , N ) + ' ' ) ;

Index/key variables: i
Accumulator variables: count, i
Iterator variables: i
Javascript program:
function cal ( arr , mid ) { let chocolate = 0 for ( let i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] >= mid ) chocolate += arr [ i ] - mid } return chocolate } function maximumCut ( arr , K ) { let low = 0 let high = arr [ 0 ] ; for ( let i = 1 ; i < arr . length ; i ++ ) { high = Math . max ( high , arr [ i ] ) ; } while ( low <= high ) { mid = Math . floor ( ( low + high ) / 2 ) ; chocolate = cal ( arr , mid ) if ( chocolate == K ) { return mid } else if ( chocolate < K ) { high = mid - 1 } else { low = mid + 1 if ( mid > high ) high = mid } } return high } let N = 4 ; let K = 7 ; let arr = [ 15 , 20 , 8 , 17 ] ; document . write ( maximumCut ( arr , K ) )

Index/key variables: i
Accumulator variables: chocolate, i
Iterator variables: i
Javascript program:
let edges = [ ] ; function add_edge ( x , y ) { edges . push ( [ x , y ] ) ; } function merge ( a , b ) { let res = [ ] ; let i = 0 , j = 0 ; let n = a . length , m = b . length ; while ( i < n && j < m ) { if ( a [ i ] < b [ j ] ) res . push ( a [ i ++ ] ) ; else if ( b [ j ] < a [ i ] ) res . push ( b [ j ++ ] ) ; } while ( i < n ) res . push ( a [ i ++ ] ) ; while ( j < m ) res . push ( b [ j ++ ] ) ; return res ; } function help ( tree , x , p , c , sol ) { let res = [ ] ; res . push ( c [ x ] ) ; for ( let i of tree [ x ] ) { if ( i != p ) { let tmp = help ( tree , i , x , c , sol ) ; res = merge ( res , tmp ) ; } } let l = 0 , r = res . length - 1 ; let ans = res . length ; while ( l <= r ) { let mid = Math . floor ( ( l + r ) / 2 ) ; if ( res [ mid ] > mid ) r = mid - 1 ; else { ans = mid + 1 ; l = mid + 1 ; } } if ( res [ 0 ] != 0 ) ans = 0 ; sol [ x ] = ans ; return res ; } function solve ( A , C ) { let n = A ; let tree = new Array ( n + 1 ) . fill ( 0 ) . map ( ( ) => [ ] ) ; for ( let i of edges ) { tree [ i [ 0 ] ] . push ( i [ 1 ] ) ; tree [ i [ 1 ] ] . push ( i [ 0 ] ) ; } let sol = new Array ( n ) . fill ( 0 ) ; help ( tree , 0 , - 1 , C , sol ) ; for ( let i of sol ) document . write ( i + " " ) ; } let N = 6 ; add_edge ( 0 , 1 ) ; add_edge ( 1 , 2 ) ; add_edge ( 0 , 3 ) ; add_edge ( 3 , 4 ) ; add_edge ( 3 , 5 ) ; let val = [ 4 , 3 , 5 , 1 , 0 , 2 ] ; solve ( N , val ) ;

Index/key variables: i, j, mid, x
Accumulator variables: i, j
Iterator variables: i
Javascript program:
function check ( bananas , mid_val , H ) { let time = 0 ; for ( let i = 0 ; i < bananas . length ; i ++ ) { if ( bananas [ i ] % mid_val != 0 ) { time += Math . floor ( bananas [ i ] / mid_val ) + 1 ; } else { time += Math . floor ( bananas [ i ] / mid_val ) ; } } if ( time <= H ) { return true ; } else { return false ; } } function minEatingSpeed ( piles , H ) { let start = 1 ; let end = [ ... piles ] . sort ( ( a , b ) => b - a ) [ 0 ] ; while ( start < end ) { let mid = start + Math . floor ( ( end - start ) / 2 ) ; if ( check ( piles , mid , H ) == true ) { end = mid ; } else { start = mid + 1 ; } } return end ; } let piles = [ 30 , 11 , 23 , 4 , 20 ] ; let H = 6 ; document . write ( minEatingSpeed ( piles , H ) ) ;

Index/key variables: i
Accumulator variables: i, time
Iterator variables: i
Javascript program:
function minSumPair ( arr , N ) { if ( N < 5 ) { return - 1 ; } let prefixMin = new Array ( N ) . fill ( 0 ) ; prefixMin [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < N - 1 ; i ++ ) { prefixMin [ i ] = Math . min ( arr [ i ] , prefixMin [ i - 1 ] ) ; } let ans = Number . MAX_VALUE ; for ( let i = 3 ; i < N - 1 ; i ++ ) { ans = Math . min ( ans , arr [ i ] + prefixMin [ i - 2 ] ) ; } return ans ; } let arr = [ 5 , 2 , 4 , 6 , 3 , 7 ] ; let N = arr . length ; document . write ( minSumPair ( arr , N ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function check ( H , A , mid , N , M , L ) { let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let speed = mid * A [ i ] + H [ i ] ; if ( speed >= L ) { sum += speed ; } } return sum ; } function buzzTime ( N , M , L , H , A ) { let low = 0 , high = 1e10 ; let ans = 0 ; while ( high >= low ) { let mid = Math . floor ( low + ( high - low ) / 2 ) ; if ( check ( H , A , mid , N , M , L ) >= M ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } let M = 400 , L = 120 ; let H = [ 20 , 50 , 20 ] ; let A = [ 20 , 70 , 90 ] ; let N = A . length ; document . write ( buzzTime ( N , M , L , H , A ) ) ;

Index/key variables: i
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function findCount ( number ) { let sum = 0 ; for ( let i = 0 ; i < number . length ; ++ i ) { sum += number [ i ] - 48 ; } let count = 0 ; for ( let i = 0 ; i < number . length ; ++ i ) { let remaining_sum = sum - ( number [ i ] - 48 ) ; for ( let j = 0 ; j <= 9 ; ++ j ) { if ( ( remaining_sum + j ) % 3 == 0 && j != number [ i ] - 48 ) { ++ count ; } } } document . write ( count ) ; } let number = " " ; findCount ( number ) ;

Index/key variables: i
Accumulator variables: sum
Iterator variables: i, j
Javascript program:
function is_possible ( teams , T , k ) { let sum = 0 ; for ( let i = 0 ; i < teams . length ; i ++ ) { sum += Math . min ( T , teams [ i ] ) ; } return sum >= T * k ; } function countOfTeams ( teams_list , N , K ) { let lb = 0 , ub = 1e9 ; while ( lb <= ub ) { let mid = Math . floor ( lb + ( ub - lb ) / 2 ) ; if ( is_possible ( teams_list , mid , K ) ) { if ( ! is_possible ( teams_list , mid + 1 , K ) ) { return mid ; } else { lb = mid + 1 ; } } else { ub = mid - 1 ; } } return 0 ; } let arr = [ 2 , 3 , 4 ] ; let K = 2 ; let N = arr . length ; document . write ( countOfTeams ( arr , N , K ) ) ;

Index/key variables: i
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function minimumOperations ( arr , N ) { let mx = 0 , pos = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= mx ) { mx = arr [ i ] ; pos = i ; } } document . write ( ( mx - 1 ) * N + pos + 1 ) ; } let arr = [ 2 , 0 , 2 ] ; let N = arr . length ; minimumOperations ( arr , N ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
let n = 0 , m = 0 , k = 0 , l = 0 , r = 0 , i = 0 ; function check ( v , a ) { let tec = 0 , ans = 0 ; let b = new Array ( n + k + 1 ) . fill ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { tec -= b [ i ] ; if ( a [ i ] + tec < v ) { let mov = v - a [ i ] - tec ; ans = ans + mov ; tec += mov ; b [ i + k ] = mov ; } } return ans <= m ; } function FindLargest ( a ) { l = 1 ; r = Math . pow ( 10 , 10 ) ; while ( r - l > 0 ) { let tm = Math . floor ( ( l + r + 1 ) / 2 ) ; if ( check ( tm , a ) ) l = tm ; else r = tm - 1 ; } return l ; } let a = [ 2 , 2 , 2 , 2 , 1 , 1 ] ; m = 2 ; k = 3 ; n = a . length ; document . write ( FindLargest ( a ) ) ;

Index/key variables: i
Accumulator variables: i, tec
Iterator variables: i
Javascript program:
function printFreq ( arr , N ) { let freq = 1 ; for ( let i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) { freq ++ ; } else { document . write ( " " + parseInt ( arr [ i - 1 ] ) + " " + parseInt ( freq ) + " " ) ; freq = 1 ; } } document . write ( " " + parseInt ( arr [ N - 1 ] ) + " " + parseInt ( freq ) + " " ) ; } let arr = [ 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 ] ; let N = arr . length ; printFreq ( arr , N ) ;

Index/key variables: i
Accumulator variables: freq, i
Iterator variables: i
Javascript program:
function isMaximumMedian ( arr , N , K , mid ) { let Pre = Array ( N + 5 ) . fill ( ) . map ( ( ) => Array ( N + 5 ) . fill ( 0 ) ) ; for ( let i = 0 ; i < N ; ++ i ) { for ( let j = 0 ; j < N ; ++ j ) { Pre [ i + 1 ] [ j + 1 ] = Pre [ i + 1 ] [ j ] + Pre [ i ] [ j + 1 ] - Pre [ i ] [ j ] ; if ( arr [ i ] [ j ] <= mid ) Pre [ i + 1 ] [ j + 1 ] ++ ; } } let required = Math . floor ( ( K * K + 1 ) / 2 ) ; let flag = 0 ; for ( let i = K ; i <= N ; ++ i ) { for ( let j = K ; j <= N ; ++ j ) { let X = Pre [ i ] [ j ] - Pre [ i - K ] [ j ] - Pre [ i ] [ j - K ] + Pre [ i - K ] [ j - K ] ; if ( X < required ) flag = 1 ; } } return flag ; } function maximumMedian ( arr , N , K ) { let low = 0 , high = 1e9 ; while ( low < high ) { let mid = low + Math . floor ( ( high - low ) / 2 ) ; if ( isMaximumMedian ( arr , N , K , mid ) ) { low = mid + 1 ; } else { high = mid ; } } return low ; } let arr = [ [ 1 , 5 , 12 ] , [ 6 , 7 , 11 ] , [ 8 , 9 , 10 ] ] ; let N = arr . length ; let K = 2 ; document . write ( maximumMedian ( arr , N , K ) ) ;

Index/key variables: i, j
Iterator variables: i, j
Javascript program:
function findMinDays ( arr , R , N , X , K ) { let low = 0 , high = X ; let minDays ; while ( low <= high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let temp = arr [ i ] + R [ i ] * mid ; if ( temp >= K ) { sum += temp ; } } if ( sum >= X ) { minDays = mid ; high = mid - 1 ; } else { low = mid + 1 ; } } document . write ( minDays ) ; } let X = 100 , K = 45 ; let arr = [ 2 , 5 , 2 , 6 ] ; let R = [ 10 , 13 , 15 , 12 ] ; let N = arr . length findMinDays ( arr , R , N , X , K ) ;

Index/key variables: i
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function getPairsCount ( a , n ) { let count = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( ( a [ i ] * a [ j ] ) == Math . abs ( i - j ) ) count ++ ; } } return count ; } let arr = [ 1 , 1 , 2 , 4 ] ; let N = arr . length ; document . write ( getPairsCount ( arr , N ) ) ;

Index/key variables: i, j
Accumulator variables: count, i, j
Iterator variables: i, j
Javascript program:
function lexicographicallySmallestString ( S , N ) { var LastZe = - 1 ; var ans = " " ; for ( var i = N - 1 ; i >= 0 ; i -- ) { if ( S . charAt ( i ) == ' ' ) { LastZe = i ; break ; } } for ( var i = 0 ; i < N ; i ++ ) { if ( i <= LastZe && S . charAt ( i ) == ' ' ) ans += S . charAt ( i ) ; else if ( i > LastZe ) ans += S . charAt ( i ) ; } return ans ; } var S = " " ; var N = S . length ; document . write ( lexicographicallySmallestString ( S , N ) ) ;

Accumulator variables: ans, i
Iterator variables: i
Javascript program:
function minSwaps ( N , M , A , B ) { let count01 = 0 ; let count10 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( A [ i ] [ j ] != B [ i ] [ j ] ) { if ( A [ i ] [ j ] == 1 ) count10 ++ ; else count01 ++ ; } } } if ( count01 == count10 ) return count01 ; else return - 1 ; } let A = [ [ 1 , 1 , 0 ] , [ 0 , 0 , 1 ] , [ 0 , 1 , 0 ] ] ; let B = [ [ 0 , 0 , 1 ] , [ 0 , 1 , 0 ] , [ 1 , 1 , 0 ] ] ; let N = A . length ; let M = B [ 0 ] . length ; document . write ( minSwaps ( N , M , A , B ) ) ;

Index/key variables: i, j
Accumulator variables: count01, count10, i, j
Iterator variables: i, j
Javascript program:
function Count ( N , K ) { if ( K == 0 ) return 0 ; let sum = 0 ; let res = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { sum += i ; if ( sum <= K ) res ++ ; else break ; } return res ; } let N = 6 , K = 14 ; document . write ( Count ( N , K ) ) ;

Accumulator variables: i, res, sum
Iterator variables: i
Javascript program:
function maximumIndices ( arr , N ) { let temp = new Array ( N ) . fill ( 0 ) ; let maxIndices = parseInt ( ( N - 1 ) / 2 ) ; arr . sort ( function ( a , b ) { return a - b ; } ) for ( let i = 0 ; i < maxIndices ; i ++ ) { temp [ 2 * i + 1 ] = arr [ i ] ; } let j = 0 ; for ( let i = maxIndices ; i < N ; ) { if ( temp [ j ] == 0 ) { temp [ j ] = arr [ i ] ; i ++ ; } j ++ ; } for ( let i = 0 ; i < N ; i ++ ) { document . write ( temp [ i ] + " " ) ; } } let arr = [ 1 , 2 , 3 , 4 ] ; let N = arr . length ; maximumIndices ( arr , N ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i
Javascript program:
function repeatingElement ( arr , N ) { let M = 0 , sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; M = Math . max ( M , arr [ i ] ) ; } let sum1 = parseInt ( M * ( M + 1 ) / 2 ) ; let ans = parseInt ( ( sum - sum1 ) / ( N - M ) ) ; return ans ; } let arr = [ 2 , 6 , 4 , 3 , 1 , 5 , 2 ] ; let N = arr . length ; document . write ( repeatingElement ( arr , N ) ) ;

Index/key variables: i
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function calculateMax ( N , M , K ) { var ans = - 1 ; var low = 0 , high = M ; while ( low <= high ) { var mid = parseInt ( ( low + high ) / 2 ) ; var val = 0 ; var L = K - 1 ; var R = N - K ; val += mid ; if ( mid >= L ) { val += ( L ) * ( ( 2 * mid - L - 1 ) / 2 ) ; } else { val += mid * parseInt ( ( mid - 1 ) / 2 ) + ( L - mid ) ; } if ( mid >= R ) { val += ( R ) * ( 2 * mid - R - 1 ) / 2 ; } else { val += mid * parseInt ( ( mid - 1 ) / 2 ) + ( R - mid ) ; } if ( val <= M ) { ans = Math . max ( ans , mid ) ; low = mid + 1 ; } else high = mid - 1 ; } return ans ; } let N = 7 , M = 100 , K = 6 ; document . write ( calculateMax ( N , M , K ) ) ;

Accumulator variables: val