codeparrot/xlcost-text-to-code huggingface dataset:
Find Nth term of the series 0 , 2 , 4 , 8 , 12 , 18. . . | Calculate Nth term of series ; Driver Function
function nthTerm ( N ) { return parseInt ( ( N + N * ( N - 1 ) ) / 2 ) ; } let N = 5 ; document . write ( nthTerm ( N ) ) ;
Number of substrings of one string present in other | javascript program to count number of substrings of s1 present in s2 . ; s3 stores all substrings of s1 ; check the presence of s3 in s2 ; Driver code
function countSubstrs ( s1 , s2 ) { var ans = 0 ; for ( var i = 0 ; i < s1 . length ; i ++ ) { var s3 = " " ; var s4 = s1 ; for ( var j = i ; j < s1 . length ; j ++ ) { s3 += s4 [ j ] ; if ( s2 . indexOf ( s3 ) != - 1 ) ans ++ ; } } return ans ; } var s1 = " " , s2 = " " ; document . write ( countSubstrs ( s1 , s2 ) ) ;
Find Nth term of series 1 , 4 , 15 , 72 , 420. . . | Function to find factorial of N ; return factorial of N ; calculate Nth term of series ; Driver code
function factorial ( N ) { let fact = 1 ; for ( let i = 1 ; i <= N ; i ++ ) fact = fact * i ; return fact ; } function nthTerm ( N ) { return ( factorial ( N ) * ( N + 2 ) / 2 ) ; } let N = 6 ; document . write ( nthTerm ( N ) ) ;
Match Expression where a single special character in pattern can match one or more characters | Returns true if pat matches with text . ; i is used as an index in pattern and j as an index in text . ; Traverse through pattern ; If current character of pattern is not ' # ' ; If does not match with text . ; If matches , increment i and j ; Current character is ' # ' ; At least one character must match with # ; Match characters with # until a matching character is found . ; Matching with # is over , move ahead in pattern ; Driver code
function regexMatch ( text , pat ) { let lenText = text . length ; let lenPat = pat . length ; let Text = text . split ( " " ) ; let Pat = pat . split ( " " ) ; let i = 0 , j = 0 ; while ( i < lenPat ) { if ( Pat [ i ] != ' ' ) { if ( Pat [ i ] != Text [ j ] ) return false ; i ++ ; j ++ ; } else { j ++ ; while ( Text [ j ] != Pat [ i + 1 ] ) j ++ ; i ++ ; } } return ( j == lenText ) ; } let str = " " ; let pat = " " ; if ( regexMatch ( str , pat ) ) document . write ( " " ) ; else document . write ( " " ) ;
Replace all occurrences of string AB with C without using extra space | Efficient javascript program to replace all occurrences of " AB " with " C " ; Index in modified string ; Index in original string ; Traverse string ; Replace occurrence of " AB " with " C " ; Increment j by 2 ; Add a null character to terminate string ; Driver code
function translate ( str ) { var len = str . length ; if ( len < 2 ) return ; var i = 0 ; var j = 0 ; while ( j < len - 1 ) { if ( str [ j ] == ' ' && str [ j + 1 ] == ' ' ) { j = j + 2 ; let firstPart = str . substr ( 0 , i ) ; let lastPart = str . substr ( i + 1 ) ; str = firstPart + ' ' + lastPart ; i += 1 ; continue ; } let firstPart = str . substr ( 0 , i ) ; let lastPart = str . substr ( i + 1 ) ; str = firstPart + str [ j ] + lastPart ; i += 1 ; j += 1 ; } if ( j == len - 1 ) { let firstPart = str . substr ( 0 , i ) ; let lastPart = str . substr ( i + 1 ) ; str = firstPart + str [ j ] + lastPart ; i += 1 ; } let firstPart = str . substr ( 0 , i ) ; let lastPart = str . substr ( i + 1 ) ; str = firstPart + ' ' + lastPart ; firstPart = str . substr ( 0 , len - 1 ) ; lastPart = str . substr ( len ) ; str = firstPart + ' ' + lastPart ; str [ len - 1 ] = ' ' ; return str ; } var str = " " ; document . write ( " " + " " + translate ( str ) ) ;
Search a Word in a 2D Grid of characters | Rows and columns in the given grid ; For searching in all 8 direction ; This function searches in all 8 - direction from point ( row , col ) in grid [ ] [ ] ; If first character of word doesn 't match with  given starting point in grid. ; Search word in all 8 directions starting from ( row , col ) ; Initialize starting point for current direction ; First character is already checked , match remaining characters ; If out of bound break ; If not matched , break ; Moving in particular direction ; If all character matched , then value of must be equal to length of word ; Searches given word in a given matrix in all 8 directions ; Consider every point as starting point and search given word ; Driver code
let R , C ; let x = [ - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 ] ; let y = [ - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 ] ; function search2D ( grid , row , col , word ) { if ( grid [ row ] [ col ] != word [ 0 ] ) return false ; let len = word . length ; for ( let dir = 0 ; dir < 8 ; dir ++ ) { let k , rd = row + x [ dir ] , cd = col + y [ dir ] ; for ( k = 1 ; k < len ; k ++ ) { if ( rd >= R rd < 0 cd >= C cd < 0 ) break ; if ( grid [ rd ] [ cd ] != word [ k ] ) break ; rd += x [ dir ] ; cd += y [ dir ] ; } if ( k == len ) return true ; } return false ; } function patternSearch ( grid , word ) { for ( let row = 0 ; row < R ; row ++ ) { for ( let col = 0 ; col < C ; col ++ ) { if ( search2D ( grid , row , col , word ) ) document . write ( " " + row + " " + col + " " ) ; } } } R = 3 ; C = 13 ; let grid = [ [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] ] ; patternSearch ( grid , " " ) ; document . write ( " " ) ; patternSearch ( grid , " " ) ;
Boyer Moore Algorithm for Pattern Searching | Javascript Program for Bad Character Heuristic of Boyer Moore String Matching Algorithm ; A utility function to get maximum of two integers ; The preprocessing function for Boyer Moore 's bad character heuristic ; Initialize all occurrences as - 1 ; Fill the actual value of last occurrence of a character ( indices of table are ascii and values are index of occurence ) ; A pattern searching function that uses Bad Character Heuristic of Boyer Moore Algorithm ; Fill the bad character array by calling the preprocessing function badCharHeuristic ( ) for given pattern ; s is shift of the pattern with respect to text there are n - m + 1 potential allignments ; Keep reducing index j of pattern while characters of pattern and text are matching at this shift s ; If the pattern is present at current shift , then index j will become - 1 after the above loop ; Shift the pattern so that the next character in text aligns with the last occurrence of it in pattern . The condition s + m < n is necessary for the case when pattern occurs at the end of text ; Shift the pattern so that the bad character in text aligns with the last occurrence of it in pattern . The max function is used to make sure that we get a positive shift . We may get a negative shift if the last occurrence of bad character in pattern is on the right side of the current character . ; Driver program to test above function
let NO_OF_CHARS = 256 ; function max ( a , b ) { return ( a > b ) ? a : b ; } function badCharHeuristic ( str , size , badchar ) { for ( let i = 0 ; i < NO_OF_CHARS ; i ++ ) badchar [ i ] = - 1 ; for ( i = 0 ; i < size ; i ++ ) badchar [ str [ i ] . charCodeAt ( 0 ) ] = i ; } function search ( txt , pat ) { let m = pat . length ; let n = txt . length ; let badchar = new Array ( NO_OF_CHARS ) ; badCharHeuristic ( pat , m , badchar ) ; let s = 0 ; while ( s <= ( n - m ) ) { let j = m - 1 ; while ( j >= 0 && pat [ j ] == txt [ s + j ] ) j -- ; if ( j < 0 ) { document . write ( " " + s ) ; s += ( s + m < n ) ? m - badchar [ txt [ s + m ] . charCodeAt ( 0 ) ] : 1 ; } else s += max ( 1 , j - badchar [ txt [ s + j ] . charCodeAt ( 0 ) ] ) ; } } let txt = " " . split ( " " ) ; let pat = " " . split ( " " ) ; search ( txt , pat ) ;
Maximize cost of forming a set of words using given set of characters | Function to find the maximum cost of any valid set of words formed by using the given letters ; Stores frequency of characters ; Find the maximum cost ; Utility function to find maximum cost of generating any possible subsets of strings ; Base Case ; Stores the current cost ; Stores the cost of by the current word ; Create a copy of the letterCounts array ; Traverse the current word ; Store the current index & check if its frequency is 0 or not ; If true , then update wordScore to - 1 ; Otherwise , add the cost of the current index to wordScore ; Decrease its frequency ; If wordScore > 0 , then recursively call for next index ; Find the cost by not including current word ; Return the maximum score ; Given arrays ; Function Call
function maxScoreWords ( words , letters , score ) { let letterCounts = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { letterCounts [ i ] = 0 ; } for ( let letter = 0 ; letter < letters . length ; letter ++ ) letterCounts [ letters [ letter ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; return helper ( words , 0 , letterCounts , score ) ; } function helper ( words , start , letterCounts , score ) { if ( start == words . length ) return 0 ; let currScore = 0 ; let wordScore = 0 ; let nextCounts = [ ... letterCounts ] ; for ( let i = 0 ; i < words [ start ] . length ; ++ i ) { let idx = words [ start ] [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( nextCounts [ idx ] == 0 ) { wordScore = - 1 ; break ; } wordScore += score [ idx ] ; nextCounts [ idx ] -- ; } if ( wordScore > 0 ) currScore = helper ( words , start + 1 , nextCounts , score ) + wordScore ; currScore = Math . max ( currScore , helper ( words , start + 1 , letterCounts , score ) ) ; return currScore ; } let words = [ " " , " " , " " , " " ] ; let letters = [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] ; let score = [ 1 , 0 , 9 , 5 , 0 , 0 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ; document . write ( maxScoreWords ( words , letters , score ) ) ;
Modify string by sorting characters after removal of characters whose frequency is not equal to power of 2 | Function to remove all the characters from a string that whose frequencies are not equal to a perfect power of 2 ; Stores the frequency of each character in S ; Iterate over characters of string ; Update frequency of current character in the array freq [ ] ; Traverse the array freq [ ] ; Check if the i - th letter is absent from string S ; Calculate log of frequency of the current character in the string S ; Calculate power of 2 of lg ; Check if freq [ i ] is a power of 2 ; Print letter i + ' a ' freq [ i ] times ; Driver Code
function countFrequency ( S , N ) { var freq = new Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { freq [ S [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } for ( var i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] === 0 ) continue ; var lg = parseInt ( Math . log2 ( freq [ i ] ) ) ; var a = Math . pow ( 2 , lg ) ; if ( a === freq [ i ] ) { while ( freq [ i ] -- ) document . write ( String . fromCharCode ( i + " " . charCodeAt ( 0 ) ) ) ; } } } var S = " " ; var N = S . length ; countFrequency ( S , N ) ;
Length of longest subsequence whose difference between maximum and minimum ASCII value of characters is exactly one | Function to find the maximum length of subsequence having difference of ASCII value of longest and smallest character as 1 ; Stores frequency of characters ; Iterate over characters of the string ; Stores the resultant length of subsequence ; Check if there exists any elements with ASCII value one less than character ch ; Size of current subsequence ; Update the value of ans ; Print the resultant count ; Driver Code
function maximumLengthSubsequence ( str ) { var mp = { } ; var temp = str . split ( " " ) ; for ( const ch of temp ) { if ( mp . hasOwnProperty ( ch ) ) mp [ ch ] = mp [ ch ] + 1 ; else mp [ ch ] = 1 ; } var ans = 0 ; for ( const ch of temp ) { if ( mp . hasOwnProperty ( String . fromCharCode ( ch . charCodeAt ( 0 ) - 1 ) ) ) { var curr_max = mp [ ch ] + mp [ String . fromCharCode ( ch . charCodeAt ( 0 ) - 1 ) ] ; ans = Math . max ( ans , curr_max ) ; } } document . write ( ans ) ; } var S = " " ; maximumLengthSubsequence ( S ) ;
Minimum increments by 1 or K required to convert a string into another given string | Function to count minimum increments by 1 or K required to convert X to Y ; Traverse the string X ; Case 1 ; Case 2 ; Add the difference / K to the count ; Add the difference % K to the count ; Case 3 ; Add the difference / K to the count ; Add the difference % K to the count ; Print the answer ; Driver Code
function countOperations ( X , Y , K ) { var count = 0 ; for ( var i = 0 ; i < X . length ; i ++ ) { var c = 0 ; if ( X [ i ] === Y [ i ] ) continue ; else if ( X [ i ] . charCodeAt ( 0 ) < Y [ i ] . charCodeAt ( 0 ) ) { if ( Y [ i ] . charCodeAt ( 0 ) - X [ i ] . charCodeAt ( 0 ) >= K ) { c = ( Y [ i ] . charCodeAt ( 0 ) - X [ i ] . charCodeAt ( 0 ) ) / K ; } c += ( Y [ i ] . charCodeAt ( 0 ) - X [ i ] . charCodeAt ( 0 ) ) % K ; } else { var t = 90 - X [ i ] . charCodeAt ( 0 ) ; t += Y [ i ] . charCodeAt ( 0 ) - 65 + 1 ; if ( t >= K ) { c = parseInt ( t / K ) ; } c += parseInt ( t % K ) ; } count = parseInt ( count + c ) ; } document . write ( count + " " ) ; } var X = " " , Y = " " ; var K = 6 ; countOperations ( X , Y , K ) ;
Minimum number of swaps required such that a given substring consists of exactly K 1 s | Function to find the minimum number of swaps required such that the substring { s [ l ] , . . , s [ r ] } consists of exactly k 1 s ; Store the size of the string ; Store the total number of 1 s and 0 s in the entire string ; Traverse the string S to find the frequency of 1 and 0 ; Store the number of 1 s and 0 s in the substring s [ l , r ] ; Traverse the substring S [ l , r ] to find the frequency of 1 s and 0 s in it ; Store the count of 1 s and 0 s outside substring s [ l , r ] ; Check if the sum of the substring is at most K ; Store number of 1 s required ; Check if there are enough 1 s remaining to be swapped ; If the count of 1 s in the substring exceeds k ; Store the number of 0 s required ; Check if there are enough 0 s remaining to be swapped ; In all other cases , print - 1 ; Driver Code
function minimumSwaps ( s , l , r , k ) { var n = s . length ; var tot_ones = 0 , tot_zeros = 0 ; for ( i = 0 ; i < s . length ; i ++ ) { if ( s . charAt ( i ) == ' ' ) tot_ones ++ ; else tot_zeros ++ ; } var ones = 0 , zeros = 0 , sum = 0 ; for ( var i = l - 1 ; i < r ; i ++ ) { if ( s . charAt ( i ) == ' ' ) { ones ++ ; sum ++ ; } else zeros ++ ; } var rem_ones = tot_ones - ones ; var rem_zeros = tot_zeros - zeros ; if ( k >= sum ) { var rem = k - sum ; if ( zeros >= rem && rem_ones >= rem ) return rem ; } else if ( k < sum ) { var rem = sum - k ; if ( ones >= rem && rem_zeros >= rem ) return rem ; } return - 1 ; } var S = " " ; var L = 5 , R = 8 , K = 2 ; document . write ( minimumSwaps ( S , L , R , K ) ) ;
Check if given number contains only “ 01 ” and “ 10 ” as substring in its binary representation | javascript Program to implement the above approach ; Function to generate all numbers having "01" and "10" as a substring ; Insert 2 and 5 ; Iterate till x is 10 ^ 15 ; Multiply x by 2 ; Update x as x * 2 + 1 ; Function to check if binary representation of N contains only "01" and "10" as substring ; Function Call to generate all such numbers ; Check if a number N exists in Ans or not ; If the number exists ; Driver Code
var N = 200000 ; var Ans = Array . from ( { length : N } , ( _ , i ) => 0 ) ; var index = 0 ; function populateNumber ( ) { Ans [ index ++ ] = ( 2 ) ; Ans [ index ++ ] = ( 5 ) ; var x = 5 ; var inf = 1000000000001 ; while ( x < inf ) { x *= 2 ; Ans [ index ++ ] = ( x ) ; x = x * 2 + 1 ; Ans [ index ++ ] = ( x ) ; } } function checkString ( N ) { populateNumber ( ) ; for ( i = 0 ; i < index ; i ++ ) { if ( Ans [ i ] == N ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } N = 5 ; checkString ( N ) ;
Minimize length by removing subsequences forming valid parenthesis from a given string | Function to find the minimum count of remaining characters left into the string by removing the valid subsequences ; Length of the string ; Stores opening parenthesis ' ( ' of the given string ; Stores square parenthesis ' [ ' of the given string ; Stores count of opening parenthesis ' ( ' in valid subsequences ; Stores count of opening parenthesis ' [ ' in valid subsequences ; Iterate over each characters of S ; If current character is ' [ ' ; insert into stack ; If i is equal to ' ] ' ; If stack is not empty and top element of stack is ' [ ' ; Remove top element from stack ; Update squareCount ; If current character is ' ( ' ; Insert into stack ; If i is equal to ' ) ' ; If stack is not empty and top element of stack is ' ( ' ; Remove top element from stack ; Update roundCount ; Print the minimum number of remaining characters left into S ; input string ; function call
function deleteSubseq ( s ) { var N = s . length ; var roundStk = [ ] ; var squareStk = [ ] ; var roundCount = 0 ; var squareCount = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( s [ i ] == ' ' ) { squareStk . push ( s [ i ] ) ; } else if ( s [ i ] == ' ' ) { if ( squareStk . length != 0 && squareStk [ squareStk . length - 1 ] == ' ' ) { squareStk . pop ( ) ; squareCount += 1 ; } } else if ( s [ i ] == ' ' ) { roundStk . push ( s [ i ] ) ; } else { if ( roundStk . length != 0 && squareStk [ squareStk . length - 1 ] == ' ' ) { squareStk . pop ( ) ; roundCount += 1 ; } } } document . write ( N - ( 2 * squareCount + 2 * roundCount ) ) ; } var s = " " ; deleteSubseq ( s ) ;
Count minimum substring removals required to reduce string to a single distinct character | Function to find minimum removals required to convert given string to single distinct characters only ; Unordered map to store positions of characters X , Y and Z ; Update indices of X , Y , Z ; ; Stores the count of minimum removals ; Traverse the Map ; Count the number of removals required for current character ; Update the answer ; Print the answer ; Given string ; Size of string ; Function call
function minimumOperations ( s , n ) { var mp = { } ; for ( var i = 0 ; i < n ; i ++ ) { if ( mp . hasOwnProperty ( s [ i ] ) ) { mp [ s [ i ] ] . push ( i ) ; } else { mp [ s [ i ] ] = [ ] ; mp [ s [ i ] ] . push ( i ) ; } } var ans = 2147483647 ; for ( const [ key , value ] of Object . entries ( mp ) ) { var curr = 0 ; var prev = 0 ; var first = true ; for ( const index of value ) { if ( first ) { if ( index > 0 ) { curr ++ ; } prev = index ; first = false ; } else { if ( index !== prev + 1 ) { curr ++ ; } prev = index ; } } if ( prev !== n - 1 ) { curr ++ ; } ans = Math . min ( ans , curr ) ; } document . write ( ans ) ; } var s = " " ; var N = s . length ; minimumOperations ( s , N ) ;
Program to construct DFA accepting odd number of 0 s and odd number of 1 s | Function to check whether the given string is accepted by DFA or not ; Stores final state of DFA ; Stores previous state of DFA ; Iterate through the string ; Checking for all combinations ; Update the previous_state ; If final state is reached ; Otherwise ; Given string ; Function Call
function checkValidDFA ( s ) { var final_state = 0 ; var previous_state = 0 ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( ( s [ i ] === " " && previous_state === 0 ) || ( s [ i ] === " " && previous_state === 3 ) ) { final_state = 1 ; } else if ( ( s [ i ] === " " && previous_state === 3 ) || ( s [ i ] === " " && previous_state === 0 ) ) { final_state = 2 ; } else if ( ( s [ i ] === " " && previous_state === 1 ) || ( s [ i ] === " " && previous_state === 2 ) ) { final_state = 0 ; } else if ( ( s [ i ] === " " && previous_state === 2 ) || ( s [ i ] === " " && previous_state === 1 ) ) { final_state = 3 ; } previous_state = final_state ; } if ( final_state === 3 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var s = " " ; checkValidDFA ( s ) ;
Find root of the tree where children id sum for every node is given | Javascript : Find root of tree where children sum for every node id is given . ; Every node appears once as an id , and every node except for the root appears once in a sum . So if we subtract all the sums from all the ids , we 're left with the root id.  ; Driver code
const findRoot = ( nodeList ) => { let root = 0 ; nodeList . forEach ( element => root += ( Number ( element [ 0 ] ) - Number ( element [ 1 ] ) ) ) ; return root ; } let nodeList = [ [ 1 , 5 ] , [ 2 , 0 ] , [ 3 , 0 ] , [ 4 , 0 ] , [ 5 , 5 ] , [ 6 , 5 ] ] ; let root = findRoot ( nodeList ) ; document . write ( root ) ;
Binary Tree | Set 1 ( Introduction ) | Class containing left and right child of current node and key value
class Node { constructor ( item ) { this . key = item ; this . left = this . right = null ; } }
Maximum difference between node and its ancestor in Binary Tree | A binary tree node has key , pointer to left and right child ; * Class Res created to implement pass by reference of ' res ' variable ; * Recursive function to calculate maximum ancestor - node difference in binary * tree . It updates value at ' res ' to store the result . The returned value of * this function is minimum value in subtree rooted with ' t ' ; * Returning Maximum var value if node is not there ( one child case ) ; If leaf node then just return node 's value  ; * Recursively calling left and right subtree for minimum value ; * Updating res if ( node value - minimum value from subtree ) is bigger than res ; Returning minimum value got so far ; This function mainly calls maxDiffUtil ( ) ; Initialising result with minimum var value ; * Helper function to prvar inorder traversal of binary tree ; Driver program to test the above functions Making above given diagram 's binary tree
class Node { constructor ( key ) { this . key = key ; this . left = this . right = null ; } } class Res { constructor ( ) { this . r = Number . MIN_VALUE ; } } var root ; function maxDiffUtil ( t , res ) { if ( t == null ) return Number . MAX_VALUE ; if ( t . left == null && t . right == null ) return t . key ; var val = Math . min ( maxDiffUtil ( t . left , res ) , maxDiffUtil ( t . right , res ) ) ; res . r = Math . max ( res . r , t . key - val ) ; return Math . min ( val , t . key ) ; } function maxDiff ( root ) { res = new Res ( ) ; maxDiffUtil ( root , res ) ; return res . r ; } function inorder ( root ) { if ( root != null ) { inorder ( root . left ) ; document . write ( root . key + " " ) ; inorder ( root . right ) ; } } root = new Node ( 8 ) ; root . left = new Node ( 3 ) ; root . left . left = new Node ( 1 ) ; root . left . right = new Node ( 6 ) ; root . left . right . left = new Node ( 4 ) ; root . left . right . right = new Node ( 7 ) ; root . right = new Node ( 10 ) ; root . right . right = new Node ( 14 ) ; root . right . right . left = new Node ( 13 ) ; document . write ( " " + " " + maxDiff ( root ) ) ;
Print Postorder traversal from given Inorder and Preorder traversals | Javascript program to print Postorder traversal from given Inorder and Preorder traversals . ; Find index of next item in preorder traversal in inorder . ; Traverse left tree ; Traverse right tree ; Print root node at the end of traversal ; Driver code
let preIndex = 0 ; function printPost ( In , pre , inStrt , inEnd , hm ) { if ( inStrt > inEnd ) return ; let inIndex = hm . get ( pre [ preIndex ++ ] ) ; printPost ( In , pre , inStrt , inIndex - 1 , hm ) ; printPost ( In , pre , inIndex + 1 , inEnd , hm ) ; document . write ( In [ inIndex ] + " " ) ; } function printPostMain ( In , pre ) { let n = pre . length ; let hm = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) hm . set ( In [ i ] , i ) ; printPost ( In , pre , 0 , n - 1 , hm ) ; } let In = [ 4 , 2 , 5 , 1 , 3 , 6 ] ; let pre = [ 1 , 2 , 4 , 5 , 3 , 6 ] ; printPostMain ( In , pre ) ;