dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
function is_prime ( n ) { if ( n == 1 ) { return false ; } for ( var i = 2 ; i <= parseInt ( Math . sqrt ( n ) ) ; i ++ ) { if ( n % i == 0 ) { return false ; } } return true ; } function consecutive_primes ( n ) { var first = - 1 , second = - 1 ; for ( var i = parseInt ( Math . sqrt ( n ) ) ; i >= 2 ; i -- ) { if ( is_prime ( i ) ) { first = i ; break ; } } for ( var i = parseInt ( Math . sqrt ( n ) ) + 1 ; i <= parseInt ( n / 2 ) ; i ++ ) { if ( is_prime ( i ) ) { second = i ; break ; } } if ( first * second >= n ) { document . write ( first + " " + second ) ; } else { for ( var i = second + 1 ; i <= n ; i ++ ) { if ( is_prime ( i ) ) { document . write ( second + " " + i ) ; return ; } } } } var n = 14 ; consecutive_primes ( n ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function construct_digits ( s ) { let k = [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] ; let l = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let c = [ 0 , 2 , 4 , 6 , 8 , 3 , 1 , 5 , 7 , 9 ] ; let ans = [ ] ; let d = new Map ( ) ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( ! d . has ( s [ i ] ) ) d . set ( s [ i ] , 0 ) ; d . set ( s [ i ] , d . get ( s [ i ] ) + 1 ) ; } for ( let i = 0 ; i < k . length ; i ++ ) { let x = 0 ; if ( d . has ( k [ i ] ) ) x = d . get ( k [ i ] ) ; for ( let j = 0 ; j < l [ i ] . length ; j ++ ) { if ( d . has ( l [ i ] [ j ] ) ) d . set ( l [ i ] [ j ] , d . get ( l [ i ] [ j ] ) - x ) ; } for ( let j = 0 ; j < x ; j ++ ) ans . push ( c [ i ] ) ; } ans . sort ( ) ; return ans . join ( " " ) ; } let s = " " ; document . write ( construct_digits ( s ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
const n = 3 ; const m = 3 ; function countDecrements ( arr ) { let count_1 = 0 ; let count_2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { if ( ( i + j ) % 2 == arr [ i ] [ j ] % 2 ) count_1 ++ ; if ( 1 - ( i + j ) % 2 == arr [ i ] [ j ] % 2 ) count_2 ++ ; } } document . write ( Math . min ( count_1 , count_2 ) ) ; } let arr = [ [ 1 , 2 , 3 ] , [ 1 , 2 , 3 ] , [ 1 , 2 , 3 ] ] ; countDecrements ( arr ) ;

Index/key variables: i, j
Accumulator variables: count_1, count_2, i, j
Iterator variables: i, j
Javascript program:
function evenXorSubarray ( arr , n ) { let ans = 0 ; let freq = [ 0 , 0 ] ; let XOR = 0 ; for ( let i = 0 ; i < n ; i ++ ) { XOR = XOR ^ arr [ i ] ; if ( XOR % 2 == 0 ) { ans += freq [ 0 ] + 1 ; freq [ 0 ] ++ ; } else { ans += freq [ 1 ] ; freq [ 1 ] ++ ; } } document . write ( ans ) ; } let arr = [ 1 , 2 , 3 , 4 ] ; let N = arr . length ; evenXorSubarray ( arr , N ) ;

Index/key variables: i
Accumulator variables: ans, i
Iterator variables: i
Javascript program:
function countOccurrences ( N , X ) { var count = 0 ; for ( var i = 1 ; i < Math . sqrt ( X ) ; i ++ ) { if ( X % i == 0 ) { var a = i ; var b = X / i ; if ( a <= N && b <= N ) { if ( a == b ) count += 1 ; else count += 2 ; } } } return count ; } var N = 7 ; var X = 12 ; document . write ( countOccurrences ( N , X ) ) ;

Accumulator variables: count, i
Iterator variables: i
Javascript program:
function findArrayWithMaxProduct ( arr , N ) { for ( var i = 0 ; i < N ; i ++ ) if ( arr [ i ] >= 0 ) { arr [ i ] = - arr [ i ] - 1 ; } if ( N % 2 === 1 ) { var max_element = - 1 ; var index = - 1 ; for ( var i = 0 ; i < N ; i ++ ) if ( Math . abs ( arr [ i ] ) > max_element ) { max_element = Math . abs ( arr [ i ] ) ; index = i ; } arr [ index ] = - arr [ index ] - 1 ; } for ( var i = 0 ; i < N ; i ++ ) document . write ( arr [ i ] + " " ) ; } var arr = [ - 3 , 0 , 1 ] ; var N = arr . length ; findArrayWithMaxProduct ( arr , N ) ;

Index/key variables: i, index
Accumulator variables: i
Iterator variables: i
Javascript program:
function findPerfectIndex ( N , K ) { var i = 0 ; for ( ; i < K ; i ++ ) { document . write ( ( N - K + 1 ) + i + " " ) ; } for ( ; i < N ; i ++ ) { document . write ( i - K + 1 + " " ) ; } } var N = 10 , K = 3 ; findPerfectIndex ( N , K ) ;

Accumulator variables: i
Javascript program:
var res = - 1 ; function isDiv ( N , X , M ) { if ( X == 0 ) { if ( N % M == 0 ) { res = N ; return true ; } return false ; } for ( var i = 0 ; i <= 9 ; i ++ ) { if ( isDiv ( N * 10 + i , X - 1 , M ) ) { return true ; } } } var N = 4 , M = 50 , X = 2 ; isDiv ( N , X , M , res ) ; document . write ( res ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function minimumMoves ( arr , N ) { let sum = 0 ; let maxelement = - 1 ; if ( N == 2 ) { document . write ( " " ) ; return ; } for ( let i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; maxelement = Math . max ( maxelement , arr [ i ] ) ; } let k = ( sum + N - 2 ) / ( N - 1 ) ; k = Math . max ( maxelement , k ) ; let ans = k * ( N - 1 ) - sum ; document . write ( ans ) ; } let arr = [ 2 , 3 , 7 ] ; let N = arr . length ; minimumMoves ( arr , N ) ;

Index/key variables: i
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function printSubsequence ( N ) { document . write ( " " ) ; for ( let i = 1 ; i < N - 1 ; i ++ ) { document . write ( i + " " ) ; } document . write ( N - 1 + " " + " " ) ; document . write ( " " + N + " " + " " ) ; } let N = 8 ; printSubsequence ( N ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function array_divisbleby_k ( N , K ) { let flag = false ; let d1 = 0 , d2 = 0 ; for ( let i = 2 ; i * i <= K ; i ++ ) { if ( K % i == 0 ) { flag = true ; d1 = i ; d2 = K / i ; break ; } } if ( flag ) { for ( let i = 0 ; i < N ; i ++ ) { if ( i % 2 == 1 ) { document . write ( d2 + " " ) ; } else { document . write ( d1 + " " ) ; } } } else { document . write ( - 1 ) ; } } let N = 5 , K = 21 ; array_divisbleby_k ( N , K ) ;

Accumulator variables: i
Iterator variables: i
Boolean variables: flag
Javascript program:
function addEdge ( adj , u , v ) { adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } let cnt = 0 ; let visited ; function DFS ( u , adj , fre , S ) { visited [ u ] = true ; cnt ++ ; fre [ S [ u ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; for ( let i = 0 ; i < adj [ u ] . length ; i ++ ) { if ( ! visited [ adj [ u ] [ i ] ] ) { DFS ( adj [ u ] [ i ] , adj , fre , S ) ; } } } function minimumOperations ( S , m ) { let V = 100 ; let adj = [ ] ; for ( let i = 0 ; i < V ; i ++ ) { adj . push ( [ ] ) ; } let total = 0 , N = S . length ; for ( let i = 0 ; i < adj . length ; i ++ ) adj [ i ] = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { addEdge ( adj , i , N - i - 1 ) ; addEdge ( adj , N - i - 1 , i ) ; } for ( let i = 0 ; i < N - m ; i ++ ) { addEdge ( adj , i , i + m ) ; addEdge ( adj , i + m , i ) ; } visited = new Array ( V ) ; visited . fill ( false ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( ! visited [ i ] ) { let fre = new Array ( 26 ) ; fre . fill ( 0 ) ; cnt = 0 ; let maxx = - 1 ; DFS ( i , adj , fre , S ) ; for ( let j = 0 ; j < 26 ; j ++ ) maxx = Math . max ( maxx , fre [ j ] ) ; total += cnt - maxx ; } } document . write ( total ) ; } let S = " " ; let K = 2 ; minimumOperations ( S , K ) ;

Index/key variables: i, j, u, v
Accumulator variables: cnt, i, j, total
Iterator variables: i, j
Javascript program:
function print ( arr , n , m ) { for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { let a = arr [ i ] [ j ] ; if ( ( i + j ) % 2 == 0 && a == ' ' ) { arr [ i ] [ j ] = ' ' ; } else if ( a == ' ' ) { arr [ i ] [ j ] = ' ' ; } } } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { document . write ( arr [ i ] [ j ] ) ; } document . write ( " " ) ; } } let n = 4 , m = 4 ; let arr = [ [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] ] ; print ( arr , n , m ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function removeDuplicateLetters ( s ) { var cnt = Array ( 26 ) . fill ( 0 ) ; var vis = Array ( 26 ) . fill ( false ) ; var n = s . length ; for ( var i = 0 ; i < n ; i ++ ) cnt [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; var res = " " ; for ( var i = 0 ; i < n ; i ++ ) { cnt [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] -- ; if ( ! vis [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ) { while ( res . length > 0 && res [ res . length - 1 ] . charCodeAt ( 0 ) > s [ i ] . charCodeAt ( 0 ) && cnt [ res [ res . length - 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] > 0 ) { vis [ res [ res . length - 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] = 0 ; res = res . substring ( 0 , res . length - 1 ) ; } res += s [ i ] ; vis [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] = 1 ; } } return res ; } var S = " " ; document . write ( removeDuplicateLetters ( S ) ) ;

Index/key variables: i
Accumulator variables: i, res
Iterator variables: i
Javascript program:
function countPairs ( arr , N ) { let countPowerof2 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( Number ( arr [ i ] . toString ( 2 ) . split ( " " ) . sort ( ) . join ( " " ) ) . toString ( ) . length == 1 ) countPowerof2 ++ ; } let desiredPairs = ( countPowerof2 * ( countPowerof2 - 1 ) ) / 2 ; document . write ( desiredPairs + " " ) ; } let arr = [ 2 , 4 , 7 , 2 ] ; let N = arr . length ; countPairs ( arr , N ) ;

Index/key variables: i
Accumulator variables: countPowerof2, i
Iterator variables: i
Javascript program:
var MAXN = 1000000 var is_prime = Array ( MAXN ) . fill ( 0 ) ; var count_of_primes = Array ( MAXN ) . fill ( 0 ) ; function sieve ( ) { for ( var i = 3 ; i < MAXN ; i += 2 ) { is_prime [ i ] = 1 ; } for ( var i = 3 ; i * i < MAXN ; i += 2 ) { if ( is_prime [ i ] ) for ( var j = i * i ; j < MAXN ; j += i ) { is_prime [ j ] = 0 ; } } is_prime [ 2 ] = 1 ; for ( var i = 1 ; i < MAXN ; i ++ ) count_of_primes [ i ] = count_of_primes [ i - 1 ] + is_prime [ i ] ; } function power ( x , y , p ) { var result = 1 ; while ( y > 0 ) { if ( y & 1 == 1 ) result = ( result * x ) % p ; x = ( x * x ) % p ; y >>= 1 ; } return result ; } function numberOfWays ( N ) { var count = count_of_primes [ N ] - 1 ; var mod = 1000000007 ; var answer = power ( 2 , count , mod ) ; if ( N == 1 ) answer = 0 ; document . write ( answer ) ; } sieve ( ) ; var N = 7 ; numberOfWays ( N ) ;

Index/key variables: N, i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function maxSumOfSquares ( N , S ) { var res = 0 ; if ( S < N S > 9 * N ) { document . write ( - 1 ) ; return ; } S = S - N ; var c = 0 ; while ( S > 0 ) { c ++ ; if ( parseInt ( S / 8 ) > 0 ) { res += 9 * 9 ; S -= 8 ; } else { res += ( S + 1 ) * ( S + 1 ) ; break ; } } res = res + ( N - c ) ; document . write ( res ) ; } var N = 3 ; var S = 12 ; maxSumOfSquares ( N , S ) ;

Accumulator variables: S, c, res
Javascript program:
function calculate_steps ( arr , n , minimum ) { var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { var val = arr [ i ] ; if ( arr [ i ] > minimum ) { arr [ i ] = arr [ i ] - minimum ; count += parseInt ( arr [ i ] / 5 ) ; arr [ i ] = arr [ i ] % 5 ; count += parseInt ( arr [ i ] / 2 ) ; arr [ i ] = arr [ i ] % 2 ; if ( arr [ i ] ) { count ++ ; } } arr [ i ] = val ; } return count ; } function solve ( arr , n ) { arr . sort ( ( a , b ) => b - a ) ; var minimum = arr [ n - 1 ] ; var count1 = 0 , count2 = 0 , count3 = 0 ; count1 = calculate_steps ( arr , n , minimum ) ; count2 = calculate_steps ( arr , n , minimum - 1 ) ; count3 = calculate_steps ( arr , n , minimum - 2 ) ; return Math . min ( count1 , Math . min ( count2 , count3 ) ) ; } var arr = [ 3 , 6 , 6 ] ; var N = arr . length ; document . write ( solve ( arr , N ) ) ;

Index/key variables: i
Accumulator variables: count, i
Iterator variables: i
Javascript program:
function countOfSubarray ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) arr [ i ] -- ; let pref = [ ] ; pref [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) pref [ i ] = pref [ i - 1 ] + arr [ i ] ; let mp = new Map ; let answer = 0 ; if ( mp [ 0 ] ) mp [ 0 ] ++ ; else mp [ 0 ] = 1 ; for ( let i = 0 ; i < N ; i ++ ) { if ( mp [ pref [ i ] ] ) { answer += mp [ pref [ i ] ] ; mp [ pref [ i ] ] ++ ; } } return answer ; } let arr = [ 1 , 1 , 0 ] ; let N = arr . length ; document . write ( countOfSubarray ( arr , N ) ) ;

Index/key variables: i
Accumulator variables: answer, i
Iterator variables: i
Javascript program:
function printsubset ( n , k ) { let count = 0 , x = 0 ; let vec = [ ] ; while ( n != 0 ) { x = n & 1 ; if ( x != 0 ) { vec . push ( Math . pow ( k , count ) ) ; } n = n >> 1 ; count ++ ; } for ( let i = 0 ; i < vec . length ; i ++ ) document . write ( vec [ i ] + " " ) ; } let n = 7 , k = 4 ; printsubset ( n , k ) ;

Index/key variables: i
Accumulator variables: count, i
Iterator variables: i
Javascript program:
function solve ( s , x , y ) { let res = 0 ; for ( let c of s ) { if ( c - ' ' == x ) { res ++ ; x = x + y ; y = x - y ; x = x - y ; } } if ( x != y && res % 2 == 1 ) -- res ; return res ; } function find_min ( s ) { let count = 0 ; for ( let i = 0 ; i < 10 ; i ++ ) { for ( let j = 0 ; j < 10 ; j ++ ) { count = Math . max ( count , solve ( s , i , j ) ) ; } } return count ; } let s = " " ; let n = s . length ; let answer = find_min ( s ) ; document . write ( n - answer ) ;

Accumulator variables: i, j, res
Iterator variables: c, i, j
Javascript program:
function countSetBitsUtil ( x ) { if ( x <= 0 ) return 0 ; return ( ( x % 2 == 0 ? 0 : 1 ) + countSetBitsUtil ( parseInt ( x / 2 ) ) ) ; } function countSetBits ( L , R ) { var bitCount = 0 ; for ( var i = L ; i <= R ; i ++ ) { bitCount += countSetBitsUtil ( i ) ; } return bitCount ; } var L = 3 , R = 5 ; document . write ( " " + countSetBits ( L , R ) ) ;

Accumulator variables: bitCount, i
Iterator variables: i
Javascript program:
function values_of_Q ( X ) { let val_Q = [ ] ; for ( let i = 1 ; i <= X ; i ++ ) { if ( ( ( ( X + i ) * X ) ) % i == 0 ) { val_Q . push ( X + i ) ; } } document . write ( val_Q . length + " " ) ; for ( let i = 0 ; i < val_Q . length ; i ++ ) { document . write ( val_Q [ i ] + " " ) ; } } let X = 3 ; values_of_Q ( X ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function gcd ( str1 , str2 ) { if ( str1 . length < str2 . length ) { return gcd ( str2 , str1 ) ; } else if ( ! str1 . startsWith ( str2 ) ) { return " " ; } else if ( str2 . length == 0 ) { return str1 ; } else { return gcd ( str1 . substr ( str2 . length ) , str2 ) ; } } function findGCD ( arr , n ) { let result = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { result = gcd ( result , arr [ i ] ) ; } return result ; } let arr = [ " " , " " , " " ] ; let n = arr . length ; document . write ( findGCD ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
let N = 4 ; let M = 4 ; function balancedMatrix ( mat ) { let is_balanced = true ; for ( let i = 0 ; i < N && is_balanced ; i ++ ) { for ( let j = 0 ; j < M && is_balanced ; j ++ ) { if ( ( i == 0 i == N - 1 ) && ( j == 0 j == M - 1 ) ) { if ( mat [ i ] [ j ] >= 2 ) is_balanced = false ; } else if ( i == 0 i == N - 1 j == 0 j == M - 1 ) { if ( mat [ i ] [ j ] >= 3 ) is_balanced = false ; } else { if ( mat [ i ] [ j ] >= 4 ) is_balanced = false ; } } } if ( is_balanced ) return " " ; else return " " ; } let mat = [ [ 1 , 2 , 3 , 4 ] , [ 3 , 5 , 2 , 6 ] , [ 5 , 3 , 6 , 1 ] , [ 9 , 5 , 6 , 0 ] ] ; document . write ( balancedMatrix ( mat ) ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Boolean variables: is_balanced
Javascript program:
function unixTimeToHumanReadable ( seconds ) { let ans = " " ; let daysOfMonth = [ 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ] ; let currYear , daysTillNow , extraTime , extraDays , index , date , month , hours , minutes , secondss , flag = 0 ; daysTillNow = parseInt ( seconds / ( 24 * 60 * 60 ) , 10 ) ; extraTime = seconds % ( 24 * 60 * 60 ) ; currYear = 1970 ; while ( daysTillNow >= 365 ) { if ( currYear % 400 == 0 || ( currYear % 4 == 0 && currYear % 100 != 0 ) ) { daysTillNow -= 366 ; } else { daysTillNow -= 365 ; } currYear += 1 ; } extraDays = daysTillNow + 1 ; if ( currYear % 400 == 0 || ( currYear % 4 == 0 && currYear % 100 != 0 ) ) flag = 1 ; month = 0 ; index = 0 ; if ( flag == 1 ) { while ( true ) { if ( index == 1 ) { if ( extraDays - 29 < 0 ) break ; month += 1 ; extraDays -= 29 ; } else { if ( extraDays - daysOfMonth [ index ] < 0 ) { break ; } month += 1 ; extraDays -= daysOfMonth [ index ] ; } index += 1 ; } } else { while ( true ) { if ( extraDays - daysOfMonth [ index ] < 0 ) { break ; } month += 1 ; extraDays -= daysOfMonth [ index ] ; index += 1 ; } } if ( extraDays > 0 ) { month += 1 ; date = extraDays ; } else { if ( month == 2 && flag == 1 ) date = 29 ; else { date = daysOfMonth [ month - 1 ] ; } } hours = parseInt ( extraTime / 3600 , 10 ) ; minutes = parseInt ( ( extraTime % 3600 ) / 60 , 10 ) ; secondss = parseInt ( ( extraTime % 3600 ) % 60 , 10 ) ; ans += date . toString ( ) ; ans += " " ; ans += month . toString ( ) ; ans += " " ; ans += currYear . toString ( ) ; ans += " " ; ans += hours . toString ( ) ; ans += " " ; ans += minutes . toString ( ) ; ans += " " ; ans += secondss . toString ( ) ; return ans ; } let T = 1595497956 ; let ans = unixTimeToHumanReadable ( T ) ; document . write ( ans + " " ) ;

Index/key variables: index
Accumulator variables: ans, currYear, daysTillNow, extraDays, index, month
Javascript program:
function downToZero ( n ) { if ( n <= 3 ) return n ; let dp = new Array ( n + 1 ) dp . fill ( - 1 ) ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; dp [ 2 ] = 2 ; dp [ 3 ] = 3 ; let sqr ; for ( let i = 4 ; i <= n ; i ++ ) { sqr = Math . sqrt ( i ) ; let best = Number . MAX_VALUE ; while ( sqr > 1 ) { if ( i % sqr == 0 ) { best = Math . min ( best , 1 + dp [ sqr ] ) ; } sqr -- ; } best = Math . min ( best , 1 + dp [ i - 1 ] ) ; dp [ i ] = best ; } return dp [ n ] ; } let n = 4 ; document . write ( downToZero ( n ) ) ;

Index/key variables: i, n, sqr
Accumulator variables: i, sqr
Iterator variables: i
Javascript program:
function minimumrequired ( A , N ) { let K = N ; let ans ; while ( K > 0 ) { if ( K % 2 == 1 ) { ans = K ; break ; } let ispalindrome = true ; for ( let i = 0 ; i < parseInt ( K / 2 , 10 ) ; i ++ ) { if ( A [ i ] != A [ K - 1 - i ] ) ispalindrome = false ; } if ( ispalindrome ) { ans = parseInt ( K / 2 , 10 ) ; K = parseInt ( K / 2 , 10 ) ; } else { ans = K ; break ; } } return ans ; } let a = [ 1 , 2 , 2 , 1 , 1 , 2 , 2 , 1 ] ; let N = a . length ; document . write ( minimumrequired ( a , N ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Boolean variables: ispalindrome
Javascript program:
function sumOfFactors ( N ) { var ans = 0 ; for ( i = 1 ; i <= N ; i ++ ) { var first = i ; var last = parseInt ( N / i ) * i ; var factors = parseInt ( ( last - first ) / i ) + 1 ; var totalContribution = parseInt ( ( ( factors ) * ( factors + 1 ) ) / 2 ) * i ; ans += totalContribution ; } return ans ; } var N = 3 ; document . write ( sumOfFactors ( N ) ) ;

Accumulator variables: ans, i
Iterator variables: i
Javascript program:
function getMaxDifference ( N ) { var M = - 1 ; var maxDiff = 0 ; for ( i = 0 ; i < N ; i ++ ) { var diff = ( N ^ i ) - ( N & i ) ; if ( diff >= maxDiff ) { maxDiff = diff ; M = i ; } } return M ; } var N = 6 ; document . write ( getMaxDifference ( N ) ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function findM ( N ) { var M = 0 ; var MSB = parseInt ( Math . log ( N ) ) ; for ( i = 0 ; i < MSB ; i ++ ) { if ( ( N & ( 1 << i ) ) == 0 ) M += ( 1 << i ) ; } return M ; } var N = 6 ; document . write ( findM ( N ) ) ;

Accumulator variables: M, i
Iterator variables: i
Javascript program:
function findTriplet ( arr ) { var n = arr . length ; var st = [ ] ; var h3 = - 1000000000 , h1 = 1000000000 ; for ( var i = n - 1 ; i >= 0 ; i -- ) { h1 = arr [ i ] ; while ( st . length != 0 && st [ st . length - 1 ] < arr [ i ] ) { h3 = st [ st . length - 1 ] ; st . pop ( ) ; } st . push ( arr [ i ] ) ; if ( h1 < h3 ) { return true ; } } return false ; } var arr = [ 4 , 7 , 5 , 6 ] ; if ( findTriplet ( arr ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function distinctNumbers ( arr , m , n ) { var count = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) if ( count . has ( arr [ i ] ) ) count . set ( arr [ i ] , count . get ( arr [ i ] ) + 1 ) else count . set ( arr [ i ] , 1 ) var fre_arr = Array ( n + 1 ) . fill ( 0 ) ; count . forEach ( ( value , key ) => { fre_arr [ value ] ++ ; } ) ; var ans = count . size ; for ( var i = 1 ; i <= n ; i ++ ) { var temp = fre_arr [ i ] ; if ( temp == 0 ) continue ; var t = Math . min ( temp , parseInt ( m / i ) ) ; ans -= t ; m -= i * t ; } return ans ; } var arr = [ 2 , 4 , 1 , 5 , 3 , 5 , 1 , 3 ] ; var n = arr . length ; var m = 2 ; document . write ( distinctNumbers ( arr , m , n ) ) ;

Index/key variables: i, value
Accumulator variables: ans, i, m
Iterator variables: i
Javascript program:
let M = 4 ; let N = 5 ; function no_of_moves ( Matrix , x , y ) { let moves = 0 ; for ( let i = 0 ; i < M ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) { if ( Matrix [ i ] [ j ] != 0 ) { moves += Math . abs ( x - i ) ; moves += Math . abs ( y - j ) ; } } } document . write ( moves ) ; } let x = 3 ; let y = 2 ; let Matrix = [ [ 1 , 0 , 1 , 1 , 0 ] , [ 0 , 1 , 1 , 0 , 1 ] , [ 0 , 0 , 1 , 1 , 0 ] , [ 1 , 1 , 1 , 0 , 0 ] ] ; let num = 1 ; no_of_moves ( Matrix , x , y ) ;

Index/key variables: i, j
Accumulator variables: i, j, moves
Iterator variables: i, j
Javascript program:
function FindSubarray ( arr , n ) { if ( n == 1 ) { document . write ( " " ) ; } let vis = new Array ( n + 1 ) ; for ( let i = 0 ; i < ( n + 1 ) ; i ++ ) vis [ i ] = - 1 ; vis [ arr [ 0 ] ] = 0 ; let len = Number . MAX_VALUE , flag = 0 ; let start = 0 , end = 0 ; for ( let i = 1 ; i < n ; i ++ ) { let t = arr [ i ] ; if ( vis [ t ] != - 1 ) { let distance = i - vis [ t ] + 1 ; if ( distance < len ) { len = distance ; start = vis [ t ] ; end = i ; } flag = 1 ; } vis [ t ] = i ; } if ( flag == 0 ) document . write ( " " ) ; else { for ( let i = start ; i <= end ; i ++ ) document . write ( arr [ i ] + " " ) ; } } let arr = [ 2 , 3 , 2 , 4 , 5 ] ; let n = arr . length ; FindSubarray ( arr , n ) ;

Index/key variables: i, t
Accumulator variables: i
Iterator variables: i
Javascript program:
function countSubstrings ( s ) { var n = s . length ; var answer = ( n * ( n - 1 ) ) / 2 ; var cnt = 1 ; var v = [ ] ; for ( var i = 1 ; i < n ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i - 1 ) ) cnt ++ ; else { v . push ( cnt ) ; cnt = 1 ; } } if ( cnt > 0 ) v . push ( cnt ) ; for ( var i = 0 ; i < v . length - 1 ; i ++ ) { answer -= ( v [ i ] + v [ i + 1 ] - 1 ) ; } return answer ; } var s = " " ; document . write ( countSubstrings ( s ) ) ;

Index/key variables: i
Accumulator variables: answer, cnt, i
Iterator variables: i
Javascript program:
function maximumSubarraySum ( a , n , subarrays ) { let i , maxsum = 0 ; let prefixArray = new Array ( n ) ; prefixArray . fill ( 0 ) ; for ( i = 0 ; i < subarrays . length ; ++ i ) { prefixArray [ subarrays [ i ] [ 0 ] - 1 ] ++ ; prefixArray [ subarrays [ i ] [ 1 ] ] -- ; } for ( i = 1 ; i < n ; i ++ ) { prefixArray [ i ] += prefixArray [ i - 1 ] ; } prefixArray . sort ( ) ; a . sort ( ) ; for ( i = 0 ; i < n ; i ++ ) maxsum += a [ i ] * prefixArray [ i ] ; return maxsum ; } let n = 6 ; let a = [ 4 , 1 , 2 , 1 , 9 , 2 ] ; let subarrays = [ ] ; subarrays . push ( [ 1 , 2 ] ) ; subarrays . push ( [ 1 , 3 ] ) ; subarrays . push ( [ 1 , 4 ] ) ; subarrays . push ( [ 3 , 4 ] ) ; document . write ( maximumSubarraySum ( a , n , subarrays ) ) ;

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