dhyuti-n/xlcost-variable-roles huggingface dataset:
Javascript program:
let MAX = 1000 ; let log = 10 ; let level = new Array ( MAX ) ; let lca = new Array ( MAX ) ; let dist = new Array ( MAX ) ; let graph = [ ] ; function addEdge ( u , v , cost ) { graph [ u ] . push ( [ v , cost ] ) ; graph [ v ] . push ( [ u , cost ] ) ; } function dfs ( node , parent , h , cost ) { lca [ node ] [ 0 ] = parent ; level [ node ] = h ; if ( parent != - 1 ) { dist [ node ] [ 0 ] = cost ; } for ( let i = 1 ; i < log ; i ++ ) { if ( lca [ node ] [ i - 1 ] != - 1 ) { lca [ node ] [ i ] = lca [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ; dist [ node ] [ i ] = dist [ node ] [ i - 1 ] + dist [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ; } } for ( let i = 0 ; i < graph [ node ] . length ; i ++ ) { if ( graph [ node ] [ i ] [ 0 ] == parent ) continue ; dfs ( graph [ node ] [ i ] [ 0 ] , node , h + 1 , graph [ node ] [ i ] [ 1 ] ) ; } } function findDistance ( u , v ) { let ans = 0 ; if ( level [ u ] > level [ v ] ) { let temp = u ; u = v ; v = temp ; } for ( let i = log - 1 ; i >= 0 ; i -- ) { if ( lca [ v ] [ i ] != - 1 && level [ lca [ v ] [ i ] ] >= level [ u ] ) { ans += dist [ v ] [ i ] ; v = lca [ v ] [ i ] ; } } if ( v == u ) { document . write ( ans + " " ) ; } else { for ( let i = log - 1 ; i >= 0 ; i -- ) { if ( lca [ v ] [ i ] != lca [ u ] [ i ] ) { ans += dist [ u ] [ i ] + dist [ v ] [ i ] ; v = lca [ v ] [ i ] ; u = lca [ u ] [ i ] ; } } ans += dist [ u ] [ 0 ] + dist [ v ] [ 0 ] ; document . write ( ans + " " ) ; } } let n = 5 ; for ( let i = 0 ; i < MAX ; i ++ ) { graph . push ( [ ] ) ; } addEdge ( 1 , 2 , 2 ) ; addEdge ( 1 , 3 , 3 ) ; addEdge ( 2 , 4 , 5 ) ; addEdge ( 2 , 5 , 7 ) ; for ( let i = 1 ; i <= n ; i ++ ) { lca [ i ] = new Array ( log ) ; dist [ i ] = new Array ( log ) ; for ( let j = 0 ; j < log ; j ++ ) { lca [ i ] [ j ] = - 1 ; dist [ i ] [ j ] = 0 ; } } dfs ( 1 , - 1 , 0 , 0 ) ; findDistance ( 1 , 3 ) ; findDistance ( 2 , 3 ) ; findDistance ( 3 , 5 ) ;

Index/key variables: i, j, node, u, v
Accumulator variables: ans, i, j
Iterator variables: i, j
Javascript program:
var MAX = 1000 ; var weight = Array ( MAX ) ; var level = Array ( MAX ) ; var par = Array ( MAX ) ; var prime = Array ( MAX + 1 ) . fill ( true ) ; var graph = Array . from ( Array ( MAX ) , ( ) => Array ( ) ) ; function SieveOfEratosthenes ( ) { for ( var p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( var i = p * p ; i <= MAX ; i += p ) prime [ i ] = false ; } } } function dfs ( node , parent , h ) { par [ node ] = parent ; level [ node ] = h ; graph [ node ] . forEach ( child => { if ( child != parent ) dfs ( child , node , h + 1 ) ; } ) ; } function findPrimeOnPath ( u , v ) { var count = 0 ; if ( level [ u ] > level [ v ] ) { [ u , v ] = [ v , u ] } var d = level [ v ] - level [ u ] ; while ( d -- ) { if ( prime [ weight [ v ] ] ) count ++ ; v = par [ v ] ; } if ( v == u ) { if ( prime [ weight [ v ] ] ) count ++ ; return count ; } while ( v != u ) { if ( prime [ weight [ v ] ] ) count ++ ; if ( prime [ weight [ u ] ] ) count ++ ; u = par [ u ] ; v = par [ v ] ; } if ( prime [ weight [ v ] ] ) count ++ ; return count ; } SieveOfEratosthenes ( ) ; weight [ 1 ] = 5 ; weight [ 2 ] = 10 ; weight [ 3 ] = 11 ; weight [ 4 ] = 8 ; weight [ 5 ] = 6 ; graph [ 1 ] . push ( 2 ) ; graph [ 2 ] . push ( 3 ) ; graph [ 2 ] . push ( 4 ) ; graph [ 1 ] . push ( 5 ) ; dfs ( 1 , - 1 , 0 ) ; var u = 3 , v = 5 ; document . write ( findPrimeOnPath ( u , v ) ) ;

Index/key variables: i, node, p, u, v
Accumulator variables: count, d, i, p
Iterator variables: i, p
Javascript program:
let MAX = 1000 ; let log = 10 ; let level = new Array ( MAX ) ; level . fill ( 0 ) ; let lca = new Array ( MAX ) ; let minWeight = new Array ( MAX ) ; let maxWeight = new Array ( MAX ) ; let graph = new Array ( MAX ) ; let weight = new Array ( MAX ) ; weight . fill ( 0 ) ; function addEdge ( u , v ) { graph [ u ] . push ( v ) ; graph [ v ] . push ( u ) ; } function dfs ( node , parent , h ) { lca [ node ] [ 0 ] = parent ; level [ node ] = h ; if ( parent != - 1 ) { minWeight [ node ] [ 0 ] = Math . min ( weight [ node ] , weight [ parent ] ) ; maxWeight [ node ] [ 0 ] = Math . max ( weight [ node ] , weight [ parent ] ) ; } for ( let i = 1 ; i < log ; i ++ ) { if ( lca [ node ] [ i - 1 ] != - 1 ) { lca [ node ] [ i ] = lca [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ; minWeight [ node ] [ i ] = Math . min ( minWeight [ node ] [ i - 1 ] , minWeight [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ) ; maxWeight [ node ] [ i ] = Math . max ( maxWeight [ node ] [ i - 1 ] , maxWeight [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ) ; } } for ( let i = 0 ; i < graph [ node ] . length ; i ++ ) { if ( graph [ node ] [ i ] == parent ) continue ; dfs ( graph [ node ] [ i ] , node , h + 1 ) ; } } function findMinMaxWeight ( u , v ) { let minWei = Number . MAX_VALUE ; let maxWei = Number . MIN_VALUE ; if ( level [ u ] > level [ v ] ) { let temp = u ; u = v ; v = temp ; } for ( let i = log - 1 ; i >= 0 ; i -- ) { if ( lca [ v ] [ i ] != - 1 && level [ lca [ v ] [ i ] ] >= level [ u ] ) { minWei = Math . min ( minWei , minWeight [ v ] [ i ] ) ; maxWei = Math . max ( maxWei , maxWeight [ v ] [ i ] ) ; v = lca [ v ] [ i ] ; } } if ( v == u ) { document . write ( minWei + " " + maxWei + " " ) ; } else { for ( let i = log - 1 ; i >= 0 ; i -- ) { if ( v == - 1 ) v ++ ; if ( lca [ v ] [ i ] != lca [ u ] [ i ] ) { minWei = Math . min ( minWei , Math . min ( minWeight [ v ] [ i ] , minWeight [ u ] [ i ] ) ) ; maxWei = Math . max ( maxWei , Math . max ( maxWeight [ v ] [ i ] , maxWeight [ u ] [ i ] ) ) ; v = lca [ v ] [ i ] ; u = lca [ u ] [ i ] ; } } if ( u == - 1 ) u ++ ; minWei = Math . min ( minWei , Math . min ( minWeight [ v ] [ 0 ] , minWeight [ u ] [ 0 ] ) ) ; maxWei = Math . max ( maxWei , Math . max ( maxWeight [ v ] [ 0 ] , maxWeight [ u ] [ 0 ] ) ) ; document . write ( minWei + " " + maxWei + " " ) ; } } let n = 5 ; for ( let i = 0 ; i < graph . length ; i ++ ) graph [ i ] = [ ] ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 5 ) ; addEdge ( 2 , 4 ) ; addEdge ( 2 , 3 ) ; weight [ 1 ] = - 1 ; weight [ 2 ] = 5 ; weight [ 3 ] = - 1 ; weight [ 4 ] = 3 ; weight [ 5 ] = - 2 ; for ( let i = 1 ; i <= n ; i ++ ) { lca [ i ] = new Array ( log ) ; minWeight [ i ] = new Array ( log ) ; maxWeight [ i ] = new Array ( log ) ; for ( let j = 0 ; j < log ; j ++ ) { lca [ i ] [ j ] = - 1 ; minWeight [ i ] [ j ] = Number . MAX_VALUE ; maxWeight [ i ] [ j ] = Number . MIN_VALUE ; } } dfs ( 1 , - 1 , 0 ) ; findMinMaxWeight ( 1 , 3 ) ; findMinMaxWeight ( 2 , 4 ) ; findMinMaxWeight ( 3 , 5 ) ;

Index/key variables: i, j, node, parent, u, v
Accumulator variables: i, j, u, v
Iterator variables: i, j
Javascript program:
let T = 1 ; function dfs ( node , parent , g , level , t_in , t_out ) { if ( parent == - 1 ) { level [ node ] = 1 ; } else { level [ node ] = level [ parent ] + 1 ; } t_in [ node ] = T ; for ( let i = 0 ; i < g [ node ] . length ; i ++ ) { if ( g [ node ] [ i ] != parent ) { T ++ ; dfs ( g [ node ] [ i ] , node , g , level , t_in , t_out ) ; } } T ++ ; t_out [ node ] = T ; } function findLCA ( n , g , v ) { let level = new Array ( n + 1 ) ; let t_in = new Array ( n + 1 ) ; let t_out = new Array ( n + 1 ) ; dfs ( 1 , - 1 , g , level , t_in , t_out ) ; let mint = Number . MAX_VALUE , maxt = Number . MIN_VALUE ; let minv = - 1 , maxv = - 1 ; for ( let i = 0 ; i < v . length ; i ++ ) { if ( t_in [ v [ i ] ] < mint ) { mint = t_in [ v [ i ] ] ; minv = v [ i ] ; } if ( t_out [ v [ i ] ] > maxt ) { maxt = t_out [ v [ i ] ] ; maxv = v [ i ] ; } } if ( minv == maxv ) { return minv ; } let lev = Math . min ( level [ minv ] , level [ maxv ] ) ; let node = 0 , l = Number . MIN_VALUE ; for ( let i = 1 ; i <= n ; i ++ ) { if ( level [ i ] > lev ) continue ; if ( t_in [ i ] <= mint && t_out [ i ] >= maxt && level [ i ] > l ) { node = i ; l = level [ i ] ; } } return node ; } let n = 10 ; let g = new Array ( n + 1 ) ; for ( let i = 0 ; i < g . length ; i ++ ) g [ i ] = [ ] ; g [ 1 ] . push ( 2 ) ; g [ 2 ] . push ( 1 ) ; g [ 1 ] . push ( 3 ) ; g [ 3 ] . push ( 1 ) ; g [ 1 ] . push ( 4 ) ; g [ 4 ] . push ( 1 ) ; g [ 2 ] . push ( 5 ) ; g [ 5 ] . push ( 2 ) ; g [ 2 ] . push ( 6 ) ; g [ 6 ] . push ( 2 ) ; g [ 3 ] . push ( 7 ) ; g [ 7 ] . push ( 3 ) ; g [ 4 ] . push ( 10 ) ; g [ 10 ] . push ( 4 ) ; g [ 8 ] . push ( 7 ) ; g [ 7 ] . push ( 8 ) ; g [ 9 ] . push ( 7 ) ; g [ 7 ] . push ( 9 ) ; let v = [ ] ; v . push ( 7 ) ; v . push ( 3 ) ; v . push ( 8 ) ; document . write ( findLCA ( n , g , v ) + " " ) ;

Index/key variables: i, maxv, minv, node, parent
Accumulator variables: T, i
Iterator variables: i
Javascript program:
let MAX_SIZE = 100005 , MAX_CHAR = 26 ; let nodeCharactersCount = new Array ( MAX_SIZE ) ; let tree = new Array ( MAX_SIZE ) ; function canFormPalindrome ( charArray ) { let oddCount = 0 ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) { if ( charArray [ i ] % 2 == 1 ) oddCount ++ ; } if ( oddCount >= 2 ) return false ; else return true ; } function LCA ( currentNode , x , y ) { if ( currentNode == x ) return x ; if ( currentNode == y ) return y ; let xLca , yLca ; xLca = yLca = - 1 ; let gotLca = - 1 ; for ( let l = 0 ; l < tree [ currentNode ] . length ; l ++ ) { let nextNode = tree [ currentNode ] [ l ] ; let out_ = LCA ( nextNode , x , y ) ; if ( out_ == x ) xLca = out_ ; if ( out_ == y ) yLca = out_ ; if ( xLca != - 1 && yLca != - 1 ) return currentNode ; if ( out_ != - 1 ) gotLca = out_ ; } return gotLca ; } function buildTree ( i ) { for ( let l = 0 ; l < tree [ i ] . length ; l ++ ) { let nextNode = tree [ i ] [ l ] ; for ( let j = 0 ; j < MAX_CHAR ; j ++ ) { nodeCharactersCount [ nextNode ] [ j ] += nodeCharactersCount [ i ] [ j ] ; } buildTree ( nextNode ) ; } } function canFormPalindromicPath ( x , y ) { let lcaNode ; if ( x == y ) lcaNode = x ; else lcaNode = LCA ( 1 , x , y ) ; let charactersCountFromXtoY = new Array ( MAX_CHAR ) ; charactersCountFromXtoY . fill ( 0 ) ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) { charactersCountFromXtoY [ i ] = nodeCharactersCount [ x ] [ i ] + nodeCharactersCount [ y ] [ i ] - 2 * nodeCharactersCount [ lcaNode ] [ i ] ; } if ( canFormPalindrome ( charactersCountFromXtoY ) ) return true ; return false ; } function updateNodeCharactersCount ( str , v ) { for ( let i = 0 ; i < str . length ; i ++ ) nodeCharactersCount [ v ] [ str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; } function performQueries ( queries , q ) { let i = 0 ; while ( i < q ) { let x = queries [ i ] [ 0 ] ; let y = queries [ i ] [ 1 ] ; if ( canFormPalindromicPath ( x , y ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; i ++ ; } } for ( let i = 0 ; i < MAX_SIZE ; i ++ ) { nodeCharactersCount [ i ] = new Array ( MAX_CHAR ) ; for ( let j = 0 ; j < MAX_CHAR ; j ++ ) { nodeCharactersCount [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < MAX_SIZE ; i ++ ) { tree [ i ] = [ ] ; } tree [ 1 ] . push ( 2 ) ; updateNodeCharactersCount ( " " , 2 ) ; tree [ 1 ] . push ( 3 ) ; updateNodeCharactersCount ( " " , 3 ) ; buildTree ( 1 ) ; let queries = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 1 ] , [ 3 , 3 ] ] ; let q = queries . length ; performQueries ( queries , q ) ;

Index/key variables: currentNode, i, j, l, lcaNode, nextNode, v, x, y
Accumulator variables: i, j, l, oddCount
Iterator variables: i, j, l
Javascript program:
class Node { constructor ( key ) { this . left = null ; this . right = null ; this . data = key ; } } function newNode ( key ) { let node = new Node ( key ) ; return node ; } let path = [ ] ; function FindPath ( root , key ) { if ( root == null ) return false ; path . push ( root . data ) ; if ( root . data == key ) return true ; if ( FindPath ( root . left , key ) || FindPath ( root . right , key ) ) return true ; path . pop ( ) ; return false ; } function minMaxNodeInPath ( root , a , b ) { path = [ ] ; let flag = true ; let Path2 = [ ] , Path1 = [ ] ; let min1 = Number . MAX_VALUE ; let max1 = Number . MIN_VALUE ; let min2 = Number . MAX_VALUE ; let max2 = Number . MIN_VALUE ; let i = 0 ; let j = 0 ; flag = FindPath ( root , a ) ; Path1 = path ; path = [ ] ; flag &= FindPath ( root , b ) ; Path2 = path ; if ( flag ) { for ( i = 0 ; i < Path1 . length && i < Path2 . length ; i ++ ) if ( Path1 [ i ] != Path2 [ i ] ) break ; i -- ; j = i ; for ( ; i < Path1 . length ; i ++ ) { if ( min1 > Path1 [ i ] ) min1 = Path1 [ i ] ; if ( max1 < Path1 [ i ] ) max1 = Path1 [ i ] ; } for ( ; j < Path2 . length ; j ++ ) { if ( min2 > Path2 [ j ] ) min2 = Path2 [ j ] ; if ( max2 < Path2 [ j ] ) max2 = Path2 [ j ] ; } document . write ( " " + Math . min ( min1 , min2 ) + " " ) ; document . write ( " " + Math . max ( max1 , max2 ) + " " ) ; } else document . write ( " " + " " ) ; return 0 ; } let root = newNode ( 20 ) ; root . left = newNode ( 8 ) ; root . right = newNode ( 22 ) ; root . left . left = newNode ( 5 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 4 ) ; root . right . right = newNode ( 25 ) ; root . left . right . left = newNode ( 10 ) ; root . left . right . right = newNode ( 14 ) ; let a = 5 ; let b = 14 ; minMaxNodeInPath ( root , a , b ) ;

Index/key variables: i, j
Accumulator variables: flag, i, j
Iterator variables: i
Boolean variables: flag
Class/struct names: Node
Javascript program:
class Node { constructor ( data ) { this . left = null ; this . right = null ; this . data = data ; } } function newNode ( data ) { let node = new Node ( data ) ; return node ; } function getPath ( root , arr , x ) { if ( root == null ) return false ; arr . push ( root . data ) ; if ( root . data == x ) return true ; if ( getPath ( root . left , arr , x ) || getPath ( root . right , arr , x ) ) return true ; arr . pop ( ) ; return false ; } function sumOddNodes ( root , n1 , n2 ) { let path1 = [ ] ; let path2 = [ ] ; getPath ( root , path1 , n1 ) ; getPath ( root , path2 , n2 ) ; let intersection = - 1 ; let i = 0 , j = 0 ; while ( i != path1 . length j != path2 . length ) { if ( i == j && path1 [ i ] == path2 [ j ] ) { i ++ ; j ++ ; } else { intersection = j - 1 ; break ; } } let sum = 0 ; for ( i = path1 . length - 1 ; i > intersection ; i -- ) if ( path1 [ i ] % 2 != 0 ) sum += path1 [ i ] ; for ( i = intersection ; i < path2 . length ; i ++ ) if ( path2 [ i ] % 2 != 0 ) sum += path2 [ i ] ; return sum ; } let root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; let node1 = 5 ; let node2 = 6 ; document . write ( sumOddNodes ( root , node1 , node2 ) ) ;

Index/key variables: i, j
Accumulator variables: i, j, sum
Iterator variables: i
Class/struct names: Node
Javascript program:
const MAX = 1000 ; function findLCA ( n1 , n2 , parent ) { let visited = new Array ( MAX ) . fill ( false ) ; visited [ n1 ] = true ; while ( parent [ n1 ] != - 1 ) { visited [ n1 ] = true ; n1 = parent [ n1 ] ; } visited [ n1 ] = true ; while ( ! visited [ n2 ] ) n2 = parent [ n2 ] ; return n2 ; } function insertAdj ( parent , i , j ) { parent [ i ] = j ; } let parent = new Array ( MAX ) ; parent [ 20 ] = - 1 ; insertAdj ( parent , 8 , 20 ) ; insertAdj ( parent , 22 , 20 ) ; insertAdj ( parent , 4 , 8 ) ; insertAdj ( parent , 12 , 8 ) ; insertAdj ( parent , 10 , 12 ) ; insertAdj ( parent , 14 , 12 ) ; document . write ( findLCA ( 10 , 14 , parent ) ) ;

Index/key variables: i, n1, n2
Javascript program:
let MAXN = 100001 ; let tree = new Array ( MAXN ) ; let path = new Array ( 3 ) ; for ( let i = 0 ; i < 3 ; i ++ ) { path [ i ] = new Array ( MAXN ) ; for ( let j = 0 ; j < MAXN ; j ++ ) { path [ i ] [ j ] = 0 ; } } let flag ; / function dfs ( cur , prev , pathNumber , ptr , node ) { for ( let i = 0 ; i < tree [ cur ] . length ; i ++ ) { if ( tree [ cur ] [ i ] != prev && ! flag ) { path [ pathNumber ] [ ptr ] = tree [ cur ] [ i ] ; if ( tree [ cur ] [ i ] == node ) { flag = true ; path [ pathNumber ] [ ptr + 1 ] = - 1 ; return ; } dfs ( tree [ cur ] [ i ] , cur , pathNumber , ptr + 1 , node ) ; } } } function LCA ( a , b ) { if ( a == b ) return a ; path [ 1 ] [ 0 ] = path [ 2 ] [ 0 ] = 1 ; flag = false ; dfs ( 1 , 0 , 1 , 1 , a ) ; flag = false ; dfs ( 1 , 0 , 2 , 1 , b ) ; let i = 0 ; while ( i < MAXN && path [ 1 ] [ i ] == path [ 2 ] [ i ] ) i ++ ; return path [ 1 ] [ i - 1 ] ; } function addEdge ( a , b ) { tree [ a ] . push ( b ) ; tree [ b ] . push ( a ) ; } for ( let i = 0 ; i < MAXN ; i ++ ) tree [ i ] = [ ] ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 2 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 2 , 6 ) ; addEdge ( 3 , 7 ) ; addEdge ( 3 , 8 ) ; document . write ( " " + LCA ( 4 , 7 ) + " " ) ; document . write ( " " + LCA ( 4 , 6 ) + " " ) ;

Index/key variables: a, b, cur, i, j, pathNumber, ptr
Accumulator variables: i, j
Iterator variables: i, j
Boolean variables: flag
Javascript program:
var MAXN = 100000 ; var level = 18 ; var tree = Array . from ( Array ( MAXN ) , ( ) => Array ( ) ) ; var depth = Array ( MAXN ) . fill ( 0 ) ; var parent = Array . from ( Array ( MAXN ) , ( ) => Array ( level ) . fill ( - 1 ) ) ; function dfs ( cur , prev ) { depth [ cur ] = depth [ prev ] + 1 ; parent [ cur ] [ 0 ] = prev ; for ( var i = 0 ; i < tree [ cur ] . length ; i ++ ) { if ( tree [ cur ] [ i ] != prev ) dfs ( tree [ cur ] [ i ] , cur ) ; } } function precomputeSparseMatrix ( n ) { for ( var i = 1 ; i < level ; i ++ ) { for ( var node = 1 ; node <= n ; node ++ ) { if ( parent [ node ] [ i - 1 ] != - 1 ) parent [ node ] [ i ] = parent [ parent [ node ] [ i - 1 ] ] [ i - 1 ] ; } } } function lca ( u , v ) { if ( depth [ v ] < depth [ u ] ) { u = u + v ; v = u - v ; u = u - v ; } var diff = depth [ v ] - depth [ u ] ; for ( var i = 0 ; i < level ; i ++ ) if ( ( ( diff >> i ) & 1 ) == 1 ) v = parent [ v ] [ i ] ; if ( u == v ) return u ; for ( var i = level - 1 ; i >= 0 ; i -- ) if ( parent [ u ] [ i ] != parent [ v ] [ i ] ) { u = parent [ u ] [ i ] ; v = parent [ v ] [ i ] ; } return parent [ u ] [ 0 ] ; } function addEdge ( u , v ) { tree [ u ] . push ( v ) ; tree [ v ] . push ( u ) ; } function memset ( value ) { for ( var i = 0 ; i < MAXN ; i ++ ) { for ( var j = 0 ; j < level ; j ++ ) { parent [ i ] [ j ] = - 1 ; } } } memset ( - 1 ) ; var n = 8 ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 2 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 2 , 6 ) ; addEdge ( 3 , 7 ) ; addEdge ( 3 , 8 ) ; depth [ 0 ] = 0 ; dfs ( 1 , 0 ) ; precomputeSparseMatrix ( n ) ; document . write ( " " + lca ( 4 , 7 ) + " " ) ; document . write ( " " + lca ( 4 , 6 ) + " " ) ;

Index/key variables: cur, i, j, node, prev, u, v
Accumulator variables: i, j, node
Iterator variables: i, j, node
Javascript program:
class Query { constructor ( L , R ) { this . L = L ; this . R = R ; } } function LCE ( str , n , L , R ) { let length = 0 ; while ( R + length < n && str [ L + length ] == str [ R + length ] ) length ++ ; return ( length ) ; } function LCEQueries ( str , n , q , m ) { for ( let i = 0 ; i < m ; i ++ ) { let L = q [ i ] . L ; let R = q [ i ] . R ; document . write ( " " + L + " " + R + " " + LCE ( str , n , L , R ) + " " ) ; } return ; } let str = " " ; let n = str . length ; let q = new Array ( 3 ) ; q [ 0 ] = new Query ( 1 , 2 ) ; q [ 1 ] = new Query ( 1 , 6 ) ; q [ 2 ] = new Query ( 0 , 5 ) ; let m = q . length ; LCEQueries ( str , n , q , m ) ;

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Class/struct names: Query
Javascript program:
function findPair ( a , n , z ) { for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < n ; j ++ ) if ( i != j && a [ i ] + a [ j ] == z ) return true ; return false ; } let a = [ 1 , - 2 , 1 , 0 , 5 ] ; let z = 0 ; let n = a . length ; if ( findPair ( a , n , z ) ) document . write ( " " ) ; else document . write ( " " ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function print ( a , n ) { for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( i != j ) document . write ( a [ i ] + " " + a [ j ] + " " ) ; } } } let a = [ 1 , 2 , 3 ] ; let n = a . length ; print ( a , n ) ;

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Javascript program:
function maximumSum ( arr , N ) { let Sum = 0 ; for ( let i = 1 ; i < N ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) Sum += ( arr [ i ] - arr [ i - 1 ] ) ; } return Sum ; } let arr = [ 1 , 5 , 3 ] ; let N = arr . length ; document . write ( maximumSum ( arr , N ) ) ;

Index/key variables: i
Accumulator variables: Sum, i
Iterator variables: i
Javascript program:
function stoogesort ( arr , l , h ) { if ( l >= h ) return ; if ( arr [ l ] > arr [ h ] ) { let temp = arr [ l ] ; arr [ l ] = arr [ h ] ; arr [ h ] = temp ; } if ( h - l + 1 > 2 ) { let t = Math . floor ( ( h - l + 1 ) / 3 ) ; stoogesort ( arr , l , h - t ) ; stoogesort ( arr , l + t , h ) ; stoogesort ( arr , l , h - t ) ; } } let arr = [ 2 , 4 , 5 , 3 , 1 ] ; let N = arr . length ; stoogesort ( arr , 0 , N - 1 ) ; for ( let i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + " " ) ; }

Index/key variables: h, i, l
Accumulator variables: i
Iterator variables: i
Javascript program:
function countSubarrays ( arr , n ) { var count = 0 ; var i , j ; for ( i = 0 ; i < n ; i ++ ) { var sum = 0 ; for ( j = i ; j < n ; j ++ ) { if ( ( j - i ) % 2 == 0 ) sum += arr [ j ] ; else sum -= arr [ j ] ; if ( sum == 0 ) count ++ ; } } document . write ( count ) ; } var arr = [ 2 , 4 , 6 , 4 , 2 ] ; var n = arr . length ; countSubarrays ( arr , n ) ;

Index/key variables: j
Accumulator variables: count, i, j, sum
Iterator variables: i, j
Javascript program:
function findCountRooks ( row , col , n , k ) { let res = n - k ; document . write ( res + " " ) ; let ri = 0 , ci = 0 ; while ( res -- > 0 ) { while ( ri < k && row [ ri ] == 1 ) { ri ++ ; } while ( ci < k && col [ ci ] == 1 ) { ci ++ ; } document . write ( ( ri + 1 ) + " " + ( ci + 1 ) + " " ) ; ri ++ ; ci ++ ; } } let n = 4 ; let k = 2 ; let row = [ 1 , 2 ] ; let col = [ 4 , 2 ] ; findCountRooks ( row , col , n , k ) ;

Index/key variables: ci, ri
Accumulator variables: ci, res, ri
Javascript program:
function checkIfDivisible ( str , num ) { let powerOf2 = ( Math . log ( num ) / Math . log ( 2 ) ) ; if ( str . length < powerOf2 ) return false ; if ( powerOf2 == 0 ) return true ; let i , number = 0 ; let len = str . length ; for ( i = len - powerOf2 ; i < len ; i ++ ) { number += ( str [ i ] - ' ' ) * Math . pow ( 10 , powerOf2 - 1 ) ; powerOf2 -- ; } if ( number % num != 0 ) return false ; else return true ; } let str = " " ; let num = 4 ; if ( checkIfDivisible ( str , num ) ) document . write ( " " ) ; else document . write ( " " ) ;

Index/key variables: i
Accumulator variables: i, number, powerOf2
Iterator variables: i
Javascript program:
function check_order ( arr ) { var cnt = 0 ; for ( i = 0 ; i < arr . length - 1 ; i ++ ) { if ( Math . abs ( arr [ i + 1 ] - arr [ i ] ) > 1 ) cnt ++ ; } if ( Math . abs ( arr [ 0 ] - arr [ arr . length - 1 ] ) > 1 ) cnt ++ ; if ( cnt > 1 ) return false ; return true ; } var arr = [ 2 , 3 , 4 , 5 , 1 ] ; if ( check_order ( arr ) ) document . write ( " " ) ; else document . write ( " " ) ;

Index/key variables: i
Accumulator variables: cnt, i
Iterator variables: i
Javascript program:
function maxProfit ( prices , n ) { if ( n <= 1 ) return 0 ; let rest = [ ] ; for ( let k = 0 ; k < n ; k ++ ) rest . push ( 0 ) ; let hold = [ ] ; for ( let k = 0 ; k < n ; k ++ ) hold . push ( 0 ) ; let sold = [ ] ; for ( let k = 0 ; k < n ; k ++ ) sold . push ( 0 ) ; rest [ 0 ] = 0 ; hold [ 0 ] = - prices [ 0 ] ; sold [ 0 ] = 0 ; for ( let i = 1 ; i < n ; i ++ ) { rest [ i ] = Math . max ( rest [ i - 1 ] , sold [ i - 1 ] ) ; hold [ i ] = Math . max ( hold [ i - 1 ] , rest [ i - 1 ] - prices [ i ] ) ; sold [ i ] = hold [ i - 1 ] + prices [ i ] ; } return Math . max ( rest [ n - 1 ] , sold [ n - 1 ] ) ; } let price = [ 2 , 4 , 5 , 0 , 2 ] ; let n = price . length ; document . write ( maxProfit ( price , n ) , ' ' ) ;

Index/key variables: i
Accumulator variables: i, k
Iterator variables: i, k
Javascript program:
function longest_subarray ( arr , d ) { var i = 0 , j = 1 , e = 0 ; for ( i = 0 ; i < d - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { j = j + 1 ; } else { j = 1 ; } if ( e < j ) { e = j ; } } return e ; } var arr = [ 1 , 2 , 3 , 4 ] ; var N = arr . length ; document . write ( longest_subarray ( arr , N ) ) ;

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