dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
function uniqueElement ( arr , n ) { var set = new Set ( ) ; for ( var i = 0 ; i < n ; i ++ ) { set . add ( arr [ i ] ) ; } if ( set . size == 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var arr = [ 9 , 9 , 9 , 9 , 9 , 9 , 9 ] ; var n = arr . length ; uniqueElement ( arr , n ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
let ans = 0 , allsum = 0 ; let edges = new Array ( 100001 ) ; function addedge ( a , b ) { edges [ a ] . push ( b ) ; edges [ b ] . push ( a ) ; } function findCost ( r , p , arr ) { let i , cur ; for ( i = 0 ; i < edges [ r ] . length ; i ++ ) { cur = edges [ r ] [ i ] ; if ( cur == p ) continue ; findCost ( cur , r , arr ) ; arr [ r ] += arr [ cur ] ; } let t1 = arr [ r ] ; let t2 = allsum - t1 ; if ( t1 * t2 > ans ) { ans = t1 * t2 ; } } function maximumCost ( r , p , N , M , arr , Edges ) { for ( let i = 0 ; i < N ; i ++ ) { allsum += arr [ i ] ; } for ( let i = 0 ; i < M ; i ++ ) { addedge ( Edges [ i ] [ 0 ] , Edges [ i ] [ 1 ] ) ; } findCost ( r , p , arr ) ; } let N = 6 ; let arr = [ 13 , 8 , 7 , 4 , 5 , 9 ] ; let M = 5 ; let Edges = [ [ 0 , 1 ] , [ 1 , 2 ] , [ 1 , 4 ] , [ 3 , 4 ] , [ 4 , 5 ] ] ; for ( let i = 0 ; i < edges . length ; i ++ ) edges [ i ] = [ ] ; maximumCost ( 1 , - 1 , N , M , arr , Edges ) ; document . write ( ans ) ;

Index/key variables: a, b, cur, i, r
Accumulator variables: allsum, i
Iterator variables: i
Javascript program:
let X = 1 ; let diameter = 0 ; let mp = new Map ( ) ; function dfs ( current_node , prev_node , len , add_to_map , adj ) { if ( len > diameter ) { diameter = len ; X = current_node ; } if ( add_to_map && len == diameter ) { mp . set ( current_node , true ) ; } for ( let it = 0 ; it < adj [ current_node ] . length ; it ++ ) { if ( adj [ current_node ] [ it ] != prev_node ) dfs ( adj [ current_node ] [ it ] , current_node , len + 1 , add_to_map , adj ) ; } } function dfsUtility ( adj ) { dfs ( 1 , - 1 , 0 , false , adj ) ; let farthest_node = X ; dfs ( farthest_node , - 1 , 0 , false , adj ) ; dfs ( farthest_node , - 1 , 0 , true , adj ) ; dfs ( X , - 1 , 0 , true , adj ) ; } function printDiameters ( adj ) { dfsUtility ( adj ) ; for ( let i = 1 ; i <= 6 ; i ++ ) { if ( mp . has ( i ) && mp . get ( i ) == true ) document . write ( diameter + 1 + " " ) ; else document . write ( diameter + " " ) ; } } let adj = new Array ( 7 ) ; for ( let i = 0 ; i < adj . length ; i ++ ) adj [ i ] = [ ] ; adj [ 1 ] . push ( 2 ) ; adj [ 2 ] . push ( 1 ) ; adj [ 1 ] . push ( 3 ) ; adj [ 3 ] . push ( 1 ) ; adj [ 2 ] . push ( 4 ) ; adj [ 4 ] . push ( 2 ) ; adj [ 2 ] . push ( 5 ) ; adj [ 5 ] . push ( 2 ) ; adj [ 2 ] . push ( 6 ) ; adj [ 6 ] . push ( 2 ) ; printDiameters ( adj ) ;

Index/key variables: current_node, i, it
Accumulator variables: i, it
Iterator variables: i, it
Javascript program:
let mp = new Map ( ) ; class Node { constructor ( key ) { this . left = this . right = null ; this . key = key ; } } function newNode ( key ) { let temp = new Node ( key ) ; return ( temp ) ; } function deleteNode ( nodeVal ) { return mp . has ( nodeVal ) ; } function treePruning ( root , result ) { if ( root == null ) return null ; root . left = treePruning ( root . left , result ) ; root . right = treePruning ( root . right , result ) ; if ( deleteNode ( root . key ) ) { if ( root . left != null ) { result . push ( root . left ) ; } if ( root . right != null ) { result . push ( root . right ) ; } return null ; } return root ; } function printInorderTree ( root ) { if ( root == null ) return ; printInorderTree ( root . left ) ; document . write ( root . key + " " ) ; printInorderTree ( root . right ) ; } function printForests ( root , arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { mp . set ( arr [ i ] , true ) ; } let result = [ ] ; if ( treePruning ( root , result ) != null ) result . push ( root ) ; for ( let i = 0 ; i < result . length ; i ++ ) { printInorderTree ( result [ i ] ) ; document . write ( " " ) ; } } let root = newNode ( 1 ) ; root . left = newNode ( 12 ) ; root . right = newNode ( 13 ) ; root . right . left = newNode ( 14 ) ; root . right . right = newNode ( 15 ) ; root . right . left . left = newNode ( 21 ) ; root . right . left . right = newNode ( 22 ) ; root . right . right . left = newNode ( 23 ) ; root . right . right . right = newNode ( 24 ) ; let arr = [ 14 , 23 , 1 ] ; let n = arr . length ; printForests ( root , arr , n ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Class/struct names: Node
Javascript program:
function knapSack ( nums , S ) { let sum = 0 ; for ( let i = 0 ; i < nums . length ; i ++ ) sum += nums [ i ] ; if ( sum < S || - sum > - S || ( S + sum ) % 2 == 1 ) return 0 ; let dp = new Array ( parseInt ( ( S + sum ) / 2 , 10 ) + 1 ) ; dp . fill ( 0 ) ; dp [ 0 ] = 1 ; for ( let num = 0 ; num < nums . length ; num ++ ) { for ( let i = dp . length - 1 ; i >= nums [ num ] ; i -- ) { dp [ i ] += dp [ i - nums [ num ] ] ; } } return dp [ dp . length - 1 ] ; } let S = 3 ; let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let answer = knapSack ( arr , S ) ; document . write ( answer ) ;

Index/key variables: i, num
Accumulator variables: i, num, sum
Iterator variables: i, num
Javascript program:
function maxDistance ( arr , K , N ) { var map = new Map ( ) ; var maxDist = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( ! map . has ( arr [ i ] ) ) map . set ( arr [ i ] , i ) ; if ( map . has ( arr [ i ] + K ) ) { maxDist = Math . max ( maxDist , i - map . get ( arr [ i ] + K ) ) ; } if ( map . has ( arr [ i ] - K ) ) { maxDist = Math . max ( maxDist , i - map . get ( arr [ i ] - K ) ) ; } } if ( maxDist == 0 ) return - 1 ; else return maxDist ; } var arr = [ 11 , 2 , 3 , 8 , 5 , 2 ] ; var N = arr . length ; var K = 2 ; document . write ( maxDistance ( arr , K , N ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function no_of_subString ( s , N ) { var fre = Array . from ( { length : 26 } , ( _ , i ) => 0 ) ; var str_len ; str_len = parseInt ( s . length ) ; var count = 0 ; for ( i = 0 ; i < str_len ; i ++ ) { fre = Array ( 26 ) . fill ( 0 ) ; var max_index = 0 ; var dist = 0 ; for ( j = i ; j < str_len ; j ++ ) { var x = s . charAt ( j ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( fre [ x ] == 0 ) dist ++ ; fre [ x ] ++ ; max_index = Math . max ( max_index , fre [ x ] ) ; if ( dist >= N && ( ( max_index * dist ) == ( j - i + 1 ) ) ) count ++ ; } } return count ; } var s = " " ; var N = 3 ; document . write ( no_of_subString ( s , N ) ) ;

Index/key variables: x
Accumulator variables: count, dist, i, j
Iterator variables: i, j
Javascript program:
function solve ( n ) { var ans = 0 ; for ( y = n + 1 ; y <= n * n + n ; y ++ ) { if ( ( n * n ) % ( y - n ) == 0 ) { ans += 1 ; } } document . write ( ans ) ; } var n = 5 ; solve ( n ) ;

Accumulator variables: ans, y
Iterator variables: y
Javascript program:
let adj = new Array ( 100005 ) ; let visited = new Array ( 100005 ) ; visited . fill ( 0 ) ; let ans = 0 ; function dfs ( node , count , m , arr , k ) { visited [ node ] = 1 ; if ( arr [ node - 1 ] == k ) { count ++ ; } else { count = 0 ; } if ( count > m ) { return ; } if ( adj [ node ] . length == 1 && node != 1 ) { ans ++ ; } for ( let x = 0 ; x < adj [ node ] . length ; x ++ ) { if ( visited [ adj [ node ] [ x ] ] == 0 ) { dfs ( adj [ node ] [ x ] , count , m , arr , k ) ; } } } let arr = [ 2 , 1 , 3 , 2 , 1 , 2 , 1 ] ; let K = 2 , M = 2 ; for ( let i = 0 ; i < adj . length ; i ++ ) adj [ i ] = [ ] ; adj [ 1 ] . push ( 2 ) ; adj [ 2 ] . push ( 1 ) ; adj [ 1 ] . push ( 3 ) ; adj [ 3 ] . push ( 1 ) ; adj [ 2 ] . push ( 4 ) ; adj [ 4 ] . push ( 2 ) ; adj [ 2 ] . push ( 5 ) ; adj [ 5 ] . push ( 2 ) ; adj [ 3 ] . push ( 6 ) ; adj [ 6 ] . push ( 3 ) ; adj [ 3 ] . push ( 7 ) ; adj [ 7 ] . push ( 3 ) ; let counter = 0 ; dfs ( 1 , counter , M , arr , K ) ; document . write ( ans + " " ) ;

Index/key variables: i, node, x
Accumulator variables: ans, count, i, x
Iterator variables: i, x
Javascript program:
function reverse ( num ) { var rev = 0 ; while ( num > 0 ) { var r = parseInt ( ( num % 10 ) ) ; rev = rev * 10 + r ; num = parseInt ( num / 10 ) ; } return rev ; } function count ( N , A , B ) { var l = Math . pow ( 10 , N - 1 ) , r = Math . pow ( 10 , N ) - 1 ; if ( l == 1 ) l = 0 ; var ans = 0 ; for ( i = l ; i <= r ; i ++ ) { var even_sum = 0 , odd_sum = 0 ; var itr = 0 , num = reverse ( i ) ; while ( num > 0 ) { if ( itr % 2 == 0 ) odd_sum += num % 10 ; else even_sum += num % 10 ; num = parseInt ( num / 10 ) ; itr ++ ; } if ( even_sum % A == 0 && odd_sum % B == 0 ) ans ++ ; } return ans ; } var N = 2 , A = 5 , B = 3 ; document . write ( count ( N , A , B ) ) ;

Accumulator variables: ans, even_sum, i, itr, odd_sum
Iterator variables: i
Javascript program:
function calc_distance ( A , B , n ) { var distance_traveled_A = 0 ; var distance_traveled_B = 0 ; var answer = 0 ; for ( i = 0 ; i < 5 ; i ++ ) { distance_traveled_A += A [ i ] ; distance_traveled_B += B [ i ] ; if ( ( distance_traveled_A == distance_traveled_B ) && ( A [ i ] == B [ i ] ) ) { answer += A [ i ] ; } } return answer ; } var A = [ 1 , 2 , 3 , 2 , 4 ] ; var B = [ 2 , 1 , 3 , 1 , 4 ] ; var N = A . length ; document . write ( calc_distance ( A , B , N ) ) ;

Index/key variables: i
Accumulator variables: answer, distance_traveled_A, distance_traveled_B, i
Iterator variables: i
Javascript program:
function constructTree ( n , edges ) { let adjl = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { adjl . push ( [ ] ) ; } for ( let e = 0 ; e < edges . length ; e ++ ) { let u = edges [ e ] [ 0 ] ; let v = edges [ e ] [ 1 ] ; adjl [ u ] . push ( v ) ; adjl [ v ] . push ( u ) ; } return adjl ; } function DFS ( adjl , parent , u , p ) { parent [ u ] = p ; for ( let v = 0 ; v < adjl [ u ] . length ; v ++ ) { if ( adjl [ u ] [ v ] != p ) { DFS ( adjl , parent , adjl [ u ] [ v ] , u ) ; } } } function valuesFromChildren ( parent , values ) { let valuesChildren = new Array ( parent . length ) ; for ( let i = 0 ; i < parent . length ; i ++ ) { valuesChildren [ i ] = 0 ; } for ( let i = 0 ; i < parent . length ; i ++ ) { if ( parent [ i ] == - 1 ) continue ; else { let p = parent [ i ] ; valuesChildren [ p ] += values [ i ] ; } } return valuesChildren ; } function findSumOfValues ( v , parent , valuesChildren ) { let cur_node = v ; let sum = 0 ; while ( cur_node != - 1 ) { sum += valuesChildren [ cur_node ] ; cur_node = parent [ cur_node ] ; } return sum ; } let n = 8 ; let edges = [ [ 0 , 1 ] , [ 0 , 2 ] , [ 0 , 3 ] , [ 1 , 4 ] , [ 1 , 5 ] , [ 4 , 7 ] , [ 3 , 6 ] ] ; let v = 7 ; let values = [ 1 , 2 , 3 , 0 , 0 , 4 , 3 , 6 ] ; let adjl = constructTree ( n , edges ) ; let parent = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { parent [ i ] = 0 ; } DFS ( adjl , parent , 0 , - 1 ) ; let valuesChildren = valuesFromChildren ( parent , values ) ; let sum = findSumOfValues ( v , parent , valuesChildren ) ; sum += values [ 0 ] ; document . write ( sum ) ;

Index/key variables: cur_node, e, i, p, u, v
Accumulator variables: e, i, sum, v
Iterator variables: e, i, v
Javascript program:
function maxProduct ( arr , N ) { if ( N < 3 ) { return ; } let a = arr [ 0 ] , b = arr [ 1 ] ; let c = 0 , d = 0 ; for ( let i = 0 ; i < N ; i ++ ) for ( let j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] * arr [ j ] > a * b ) { c = a , d = b ; a = arr [ i ] , b = arr [ j ] ; } if ( arr [ i ] * arr [ j ] < a * b && arr [ i ] * arr [ j ] > c * d ) c = arr [ i ] , d = arr [ j ] ; } document . write ( c + " " + d ) ; } let arr = [ 5 , 2 , 67 , 45 , 160 , 78 ] ; let N = arr . length ; maxProduct ( arr , N ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function isVowel ( c ) { if ( c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' ) return true ; return false ; } function countSubstrings ( s , n ) { let result = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let count = 0 ; for ( let j = i ; j < n ; j ++ ) { if ( isVowel ( s [ j ] ) ) { count ++ ; } if ( count % 2 == 0 ) result ++ ; } } document . write ( result ) ; } let n = 5 ; let s = " " ; countSubstrings ( s , n ) ;

Index/key variables: j
Accumulator variables: count, i, j, result
Iterator variables: i, j
Javascript program:
function isVowel ( c ) { if ( c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' ) return true ; return false ; } function countSubstrings ( s , n ) { let temp = [ 1 , 0 ] ; let result = 0 , sum = 0 ; for ( let i = 0 ; i <= n - 1 ; i ++ ) { sum += ( isVowel ( s [ i ] ) ? 1 : 0 ) ; sum %= 2 ; temp [ sum ] ++ ; } result += ( ( temp [ 0 ] * ( temp [ 0 ] - 1 ) ) / 2 ) ; result += ( ( temp [ 1 ] * ( temp [ 1 ] - 1 ) ) / 2 ) ; document . write ( result ) ; } let n = 5 ; let s = " " ; countSubstrings ( s , n ) ;

Index/key variables: i, sum
Accumulator variables: i, result, sum
Iterator variables: i
Javascript program:
class Node { constructor ( ) { this . data = 0 ; this . left = null ; this . right = null ; } } ; class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } function newNode ( data ) { var temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } function findLevel ( root , k , level ) { var qt = [ ] ; qt . push ( new pair ( root , 0 ) ) ; var count = 1 , b = 0 , a = 0 ; while ( qt . length != 0 ) { var temp = qt . shift ( ) ; if ( count == 1 ) { b = temp . second ; } if ( temp . first . left != null ) { qt . push ( new pair ( temp . first . left , 2 * temp . second ) ) ; } if ( temp . first . right != null ) { qt . push ( new pair ( temp . first . right , 2 * temp . second + 1 ) ) ; } count -- ; if ( count == 0 ) { if ( ( b - a + 1 ) == k ) return level ; var secondLabel = qt [ 0 ] ; a = secondLabel . second ; level += 1 ; count = qt . length ; } } return - 1 ; } var root = newNode ( 5 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 2 ) ; root . right . right = newNode ( 8 ) ; root . left . left = newNode ( 7 ) ; root . left . left . left = newNode ( 5 ) ; root . left . right = newNode ( 3 ) ; root . left . right . right = newNode ( 4 ) ; var k = 4 ; document . write ( findLevel ( root , k , 1 ) ) ;

Accumulator variables: count, level
Class/struct names: Node, pair
Javascript program:
function possible ( arr , N , mid , K ) { let add = 0 ; for ( let i = parseInt ( N / 2 , 10 ) - ( N + 1 ) % 2 ; i < N ; ++ i ) { if ( mid - arr [ i ] > 0 ) { add += ( mid - arr [ i ] ) ; if ( add > K ) return false ; } } if ( add <= K ) return true ; else return false ; } function findMaxMedian ( arr , N , K ) { let low = 1 ; let mx = 0 ; for ( let i = 0 ; i < N ; ++ i ) { mx = Math . max ( mx , arr [ i ] ) ; } let high = K + mx ; while ( low <= high ) { let mid = parseInt ( ( high + low ) / 2 , 10 ) ; if ( possible ( arr , N , mid , K ) ) { low = mid + 1 ; } else { high = mid - 1 ; } } if ( N % 2 == 0 ) { if ( low - 1 < arr [ parseInt ( N / 2 ) ] ) { return parseInt ( ( arr [ parseInt ( N / 2 ) ] + low - 1 ) / 2 , 10 ) ; } } return low - 1 ; } let arr = [ 1 , 3 , 6 ] ; let K = 10 ; let N = arr . length ; arr . sort ( ) ; document . write ( findMaxMedian ( arr , N , K ) ) ;

Index/key variables: i
Accumulator variables: add
Iterator variables: i
Javascript program:
function countLessThanMid ( mid , N , M , K ) { let count = 0 ; for ( let i = 1 ; i <= Math . min ( N , mid ) ; ++ i ) { count = count + Math . min ( parseInt ( mid / i ) , M ) ; } if ( count >= K ) return false ; else return true ; } function findKthElement ( N , M , K ) { let low = 1 , high = N * M ; while ( low <= high ) { let mid = low + parseInt ( ( high - low ) / 2 ) ; if ( countLessThanMid ( mid , N , M , K ) ) low = mid + 1 ; else high = mid - 1 ; } return high + 1 ; } let N = 2 , M = 3 ; let K = 5 ; document . write ( findKthElement ( N , M , K ) ) ;

Iterator variables: i
Javascript program:
function countSubString ( S , C ) { var count = 0 ; var conCount = 0 ; for ( var i = 0 ; i < S . length ; i ++ ) { var ch = S [ i ] ; if ( ch === C ) conCount ++ ; else { count += ( conCount * ( conCount + 1 ) ) / 2 ; conCount = 0 ; } } count += ( conCount * ( conCount + 1 ) ) / 2 ; document . write ( count ) ; } var S = " " ; var C = " " ; countSubString ( S , C ) ;

Index/key variables: i
Accumulator variables: conCount, count, i
Iterator variables: i
Javascript program:
function checkPalindrome ( S ) { var N = S . length ; var first_half = true ; var second_half = true ; var cnt = parseInt ( ( N / 2 ) ) - 1 ; for ( i = 0 ; i < ( N / 2 ) ; i ++ ) { if ( S . charAt ( i ) != S . charAt ( cnt ) ) { first_half = false ; break ; } if ( S . charAt ( N / 2 + i ) != S . charAt ( N / 2 + cnt ) ) { second_half = false ; break ; } cnt -- ; } if ( first_half && second_half ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var S = " " ; checkPalindrome ( S ) ;

Accumulator variables: cnt, i
Iterator variables: i
Boolean variables: first_half, second_half
Javascript program:
class Node { constructor ( ) { this . key = 0 ; this . left = null ; this . right = null ; } } ; var kth_smallest ; function newNode ( item ) { var temp = new Node ( ) ; temp . key = item ; temp . left = temp . right = null ; return temp ; } function insert ( node , key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insert ( node . left , key ) ; else if ( key > node . key ) node . right = insert ( node . right , key ) ; return node ; } function KSmallestPerfectBST ( root , k , treeSize ) { if ( root == null ) return false ; var median_loc = parseInt ( treeSize / 2 ) + 1 ; if ( k == median_loc ) { kth_smallest = root . key ; return true ; } var newTreeSize = parseInt ( treeSize / 2 ) ; if ( k < median_loc ) { return KSmallestPerfectBST ( root . left , k , newTreeSize ) ; } var newK = k - median_loc ; return KSmallestPerfectBST ( root . right , newK , newTreeSize ) ; } var root = null ; root = insert ( root , 50 ) ; insert ( root , 30 ) ; insert ( root , 20 ) ; insert ( root , 40 ) ; insert ( root , 70 ) ; insert ( root , 60 ) ; insert ( root , 80 ) ; insert ( root , 14 ) ; insert ( root , 25 ) ; insert ( root , 35 ) ; insert ( root , 45 ) ; insert ( root , 55 ) ; insert ( root , 65 ) ; insert ( root , 75 ) ; insert ( root , 85 ) ; var n = 15 , k = 5 ; if ( KSmallestPerfectBST ( root , k , n ) ) { document . write ( kth_smallest + " " ) ; }

Class/struct names: Node
Javascript program:
function isNudeNum ( n ) { let copy , length , flag = 0 ; copy = n ; let temp ; temp = copy . toString ( ) ; length = temp . length ; for ( let i = 0 ; i < length ; i ++ ) { let num = temp [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; if ( num == 0 n % num != 0 ) { flag = 1 ; } } if ( flag == 1 ) return false ; else return true ; } function longestNudeSubseq ( arr , n ) { let answer = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( isNudeNum ( arr [ i ] ) ) answer ++ ; } return answer ; } let arr = [ 34 , 34 , 2 , 2 , 3 , 333 , 221 , 32 ] ; let n = arr . length ; document . write ( longestNudeSubseq ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: answer, i
Iterator variables: i
Javascript program:
function longestSubarray ( arr , n , k ) { let i , j , Max = 1 ; let s = new Set ( ) ; for ( i = 0 ; i < n - 1 ; i ++ ) { s . add ( arr [ i ] ) ; for ( j = i + 1 ; j < n ; j ++ ) { if ( Math . abs ( arr [ i ] - arr [ j ] ) == 0 || Math . abs ( arr [ i ] - arr [ j ] ) == k ) { if ( ! s . has ( arr [ j ] ) ) { if ( s . size == 2 ) break ; else s . add ( arr [ j ] ) ; } } else break ; } if ( s . size == 2 ) { Max = Math . max ( Max , j - i ) ; s . clear ; } else s . clear ; } return Max ; } let arr = [ 1 , 0 , 2 , 2 , 5 , 5 , 5 ] ; let N = arr . length ; let K = 1 ; let length = longestSubarray ( arr , N , K ) ; if ( length == 1 ) document . write ( - 1 ) ; else document . write ( length ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function longestSubarray ( a , n ) { var l = Array ( n ) . fill ( 0 ) ; var r = Array ( n ) . fill ( 0 ) ; for ( i = 0 , count = 0 ; i < n ; i ++ ) { if ( a [ i ] == 1 ) count ++ ; else { l [ i ] = count ; count = 0 ; } } for ( i = n - 1 , count = 0 ; i >= 0 ; i -- ) { if ( a [ i ] == 1 ) count ++ ; else { r [ i ] = count ; count = 0 ; } } var ans = - 1 ; for ( i = 0 ; i < n ; ++ i ) { if ( a [ i ] == 0 ) ans = Math . max ( ans , l [ i ] + r [ i ] ) ; } return ans < 0 ? n : ans ; } var arr = [ 0 , 1 , 1 , 1 , 0 , 1 , 0 , 1 , 1 ] ; var n = arr . length ; document . write ( longestSubarray ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: count, i
Iterator variables: i
Javascript program:
function findMaxValue ( N , K ) { let packages ; let maxi = 1 ; for ( let i = 1 ; i <= K ; i ++ ) { if ( N % i == 0 ) maxi = Math . max ( maxi , i ) ; } packages = parseInt ( N / maxi ) ; document . write ( packages ) ; } let N = 8 , K = 7 ; findMaxValue ( N , K ) ;

Accumulator variables: i
Iterator variables: i
Javascript program:
function maxSide ( a , n ) { let sideLength = 0 ; a . sort ( ) ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( a [ i ] > sideLength ) { sideLength ++ ; } else { break ; } } document . write ( sideLength ) ; } let N = 6 ; let arr = [ 3 , 2 , 1 , 5 , 2 , 4 ] ; maxSide ( arr , N ) ;

Index/key variables: i
Accumulator variables: i, sideLength
Iterator variables: i
Javascript program:
function minDiff ( a , n ) { for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) a [ i ] *= 2 ; } a . sort ( ( a , b ) => a - b ) ; let mindifference = a [ 1 ] - a [ 0 ] ; for ( let i = 1 ; i < a . length ; i ++ ) { mindifference = Math . min ( mindifference , a [ i ] - a [ i - 1 ] ) ; } document . write ( mindifference ) ; } let arr = [ 3 , 8 , 13 , 30 , 50 ] ; let n = arr . length ; minDiff ( arr , n ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
let v = [ ] ; function bin ( val , k , n ) { let lo = 0 ; let hi = n ; let mid ; let ans = - 1 ; while ( lo <= hi ) { mid = lo + parseInt ( ( hi - lo ) / 2 ) ; if ( v [ mid ] - val <= k ) { lo = mid + 1 ; ans = mid ; } else hi = mid - 1 ; } return ans ; } function findSubarraySumK ( arr , N , K ) { let sum = 0 ; v . push ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; v . push ( sum ) ; } let l = 0 , ans = 0 , r ; for ( let i = 0 ; i < N ; i ++ ) { r = bin ( v [ i ] , K , N ) ; ans = Math . max ( ans , r - i ) ; } document . write ( ans ) ; } let arr = [ 6 , 8 , 14 , 9 , 4 , 11 , 10 ] ; let N = arr . length ; let K = 13 ; findSubarraySumK ( arr , N , K ) ;

Index/key variables: i, mid
Accumulator variables: i, sum
Iterator variables: i
Javascript program:
function splitArray ( arr , N ) { arr . sort ( ) ; let result = Number . MAX_VALUE ; for ( let i = 1 ; i < N ; i ++ ) { result = Math . min ( result , arr [ i ] - arr [ i - 1 ] ) ; } return result ; } let arr = [ 3 , 1 , 2 , 6 , 4 ] ; let N = arr . length ; document . write ( splitArray ( arr , N ) ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Javascript program:
function findValue ( R , C ) { let k = ( R * ( R - 1 ) ) / 2 + 1 ; let diff = R + 1 ; for ( let i = 1 ; i < C ; i ++ ) { k = ( k + diff ) ; diff ++ ; } return k ; } let R = 4 ; let C = 4 ; let k = findValue ( R , C ) ; document . write ( k ) ;

Accumulator variables: diff, i
Iterator variables: i
Javascript program:
function makeGroups ( a , n ) { let v = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { v [ a [ i ] ] ++ ; } let no_of_groups = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { no_of_groups += Math . floor ( v [ i ] / i ) ; v [ i ] = v [ i ] % i ; } let i = 1 ; let total = 0 ; for ( i = 1 ; i <= n ; i ++ ) { if ( v [ i ] != 0 ) { total = v [ i ] ; break ; } } i ++ ; while ( i <= n ) { if ( v [ i ] != 0 ) { total += v [ i ] ; if ( total >= i ) { let rem = total - i ; no_of_groups ++ ; total = rem ; } } i ++ ; } document . write ( no_of_groups + " " ) ; } let arr = [ 2 , 3 , 1 , 2 , 2 ] ; let size = arr . length ; makeGroups ( arr , size ) ;

Index/key variables: i
Accumulator variables: i, no_of_groups, total
Iterator variables: i