codeparrot/xlcost-text-to-code huggingface dataset:
Closest ( or Next ) smaller and greater numbers with same number of set bits | Function to find next smallest number bigger than n ; Compute c0 and c1 ; If there is no bigger number with the same no . of 1 's ; input 1 ; input 2
function getNext ( n ) { let c = n ; let c0 = 0 ; let c1 = 0 ; while ( ( ( c & 1 ) == 0 ) && ( c != 0 ) ) { c0 ++ ; c >>= 1 ; } while ( ( c & 1 ) == 1 ) { c1 ++ ; c >>= 1 ; } if ( c0 + c1 == 31 c0 + c1 == 0 ) return - 1 ; return n + ( 1 << c0 ) + ( 1 << ( c1 - 1 ) ) - 1 ; } let n = 5 ; document . write ( getNext ( n ) + " " ) ; n = 8 ; document . write ( getNext ( n ) ) ;
Count minimum bits to flip such that XOR of A and B equal to C | Javascript code to count the Minimum bits in A and B ; If both A [ i ] and B [ i ] are equal ; If Both A and B are unequal ; N represent total count of Bits
function totalFlips ( A , B , C , N ) { let count = 0 ; for ( let i = 0 ; i < N ; ++ i ) { if ( A [ i ] == B [ i ] && C [ i ] == ' ' ) ++ count ; else if ( A [ i ] != B [ i ] && C [ i ] == ' ' ) ++ count ; } return count ; } let N = 5 ; let a = " " ; let b = " " ; let c = " " ; document . write ( totalFlips ( a , b , c , N ) ) ;
Swap three variables without using temporary variable | javascript program to swap three variables without using temporary variable . ; Assign c ' s ▁ value ▁ to ▁ a , ▁ a ' s value to b and b 's value to c. ; Store XOR of all in a ; After this , b has value of a ; After this , c has value of b ; After this , a has value of c ; Driver Code ; Calling Function
var a , b , c ; function swapThree ( ) { a = a ^ b ^ c ; b = a ^ b ^ c ; c = a ^ b ^ c ; a = a ^ b ^ c ; } a = 10 ; b = 20 ; c = 30 ; document . write ( " " + a + " " + b + " " + c ) ; swapThree ( ) ; document . write ( " " ) document . write ( " " + a + " " + b + " " + c ) ;
Find Two Missing Numbers | Set 2 ( XOR based solution ) | Function to find two missing numbers in range [ 1 , n ] . This function assumes that size of array is n - 2 and all array elements are distinct ; Get the XOR of all elements in arr [ ] and { 1 , 2 . . n } ; Get a set bit of XOR ( We get the rightmost set bit ) ; Now divide elements in two sets by comparing rightmost set bit of XOR with bit at same position in each element . let x = 0 , y = 0 ; Initialize missing numbers ; XOR of first set in arr [ ] ; XOR of second set in arr [ ] ; XOR of first set in arr [ ] and { 1 , 2 , ... n } ; XOR of second set in arr [ ] and { 1 , 2 , ... n } ; Driver program to test above function ; Range of numbers is 2 plus size of array
function findTwoMissingNumbers ( arr , n ) { let XOR = arr [ 0 ] ; for ( let i = 1 ; i < n - 2 ; i ++ ) XOR ^= arr [ i ] ; for ( let i = 1 ; i <= n ; i ++ ) XOR ^= i ; let set_bit_no = XOR & ~ ( XOR - 1 ) ; for ( let i = 0 ; i < n - 2 ; i ++ ) { if ( arr [ i ] & set_bit_no ) x = x ^ arr [ i ] ; else y = y ^ arr [ i ] ; } for ( let i = 1 ; i <= n ; i ++ ) { if ( i & set_bit_no ) x = x ^ i ; else y = y ^ i ; } document . write ( ` ${ x } ${ y } ` ) ; } let arr = [ 1 , 3 , 5 , 6 ] ; n = 2 + arr . length ; findTwoMissingNumbers ( arr , n ) ;
Find profession in a special family | Function to get no of set bits in binary representation of passed binary no . ; Returns ' e ' if profession of node at given level and position is engineer . Else doctor . The function assumes that given position and level have valid values . ; Count set bits in ' pos - 1' ; If set bit count is odd , then doctor , else engineer ; Driver code
function countSetBits ( n ) { let count = 0 ; while ( n != 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } function findProffesion ( level , pos ) { let c = countSetBits ( pos - 1 ) ; return ( c % 2 != 0 ) ? ' ' : ' ' ; } let level = 3 , pos = 4 ; let prof = ( findProffesion ( level , pos ) == ' ' ) ? " " : " " ; document . write ( prof ) ; }
Find XOR of two number without using XOR operator | Returns XOR of x and y ; Driver code
function myXOR ( x , y ) { return ( x & ( ~ y ) ) | ( ( ~ x ) & y ) ; } let x = 3 , y = 5 ; document . write ( " " + myXOR ( x , y ) ) ;
Find maximum xor of k elements in an array | Function to return the maximum xor for a subset of size k from the given array ; Initialize result ; Traverse all subsets of the array ; bitCount ( ) returns the number of sets bits in an integer ; Initialize current xor as 0 ; If jth bit is set in i then include jth element in the current xor ; Update maximum xor so far ; Utility function ; Driver code
function Max_Xor ( arr , n , k ) { let maxXor = Number . MIN_VALUE ; for ( let i = 0 ; i < ( 1 << n ) ; i ++ ) { if ( bitCount ( i ) == k ) { let cur_xor = 0 ; for ( let j = 0 ; j < n ; j ++ ) { if ( i & ( 1 << j ) ) cur_xor = cur_xor ^ arr [ j ] ; } maxXor = Math . max ( maxXor , cur_xor ) ; } } return maxXor ; } function bitCount ( x ) { let setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } let arr = [ 2 , 5 , 4 , 1 , 3 , 7 , 6 , 8 ] ; let n = arr . length ; let k = 3 ; document . write ( Max_Xor ( arr , n , k ) ) ;
Convert a given temperature to another system based on given boiling and freezing points | Function to return temperature in the second thermometer ; Calculate the temperature ; Driver Code
function temp_convert ( F1 , B1 , F2 , B2 , T ) { var t2 ; t2 = F2 + ( B2 - F2 ) / ( B1 - F1 ) * ( T - F1 ) ; return t2 ; } var F1 = 0 , B1 = 100 ; var F2 = 32 , B2 = 212 ; var T = 37 ; var t2 ; document . write ( temp_convert ( F1 , B1 , F2 , B2 , T ) . toFixed ( 2 ) ) ;
Maximum possible elements which are divisible by 2 | Function to find maximum possible elements which divisible by 2 ; To store count of even numbers ; All even numbers and half of odd numbers ; Driver code ; Function call
function Divisible ( arr , n ) { let count_even = 0 ; for ( let i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 0 ) count_even ++ ; return count_even + parseInt ( ( n - count_even ) / 2 ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let n = arr . length ; document . write ( Divisible ( arr , n ) ) ;
Select a Random Node from a tree with equal probability | JavaScript program to Select a Random Node from a tree ; This is used to fill children counts . ; Inserts Children count for each node ; returns number of children for root ; Helper Function to return a random node ; Returns Random node ; Creating Above Tree
class Node { constructor ( data ) { this . data = data ; this . left = this . right = null ; this . children = 0 ; } } function getElements ( root ) { if ( root == null ) return 0 ; return getElements ( root . left ) + getElements ( root . right ) + 1 ; } function insertChildrenCount ( root ) { if ( root == null ) return null ; root . children = getElements ( root ) - 1 ; root . left = insertChildrenCount ( root . left ) ; root . right = insertChildrenCount ( root . right ) ; return root ; } function children ( root ) { if ( root == null ) return 0 ; return root . children + 1 ; } function randomNodeUtil ( root , count ) { if ( root == null ) return 0 ; if ( count == children ( root . left ) ) return root . data ; if ( count < children ( root . left ) ) return randomNodeUtil ( root . left , count ) ; return randomNodeUtil ( root . right , count - children ( root . left ) - 1 ) ; } function randomNode ( root ) { let count = Math . floor ( Math . random ( ) * ( root . children + 1 ) ) ; return randomNodeUtil ( root , count ) ; } let root = new Node ( 10 ) ; root . left = new Node ( 20 ) ; root . right = new Node ( 30 ) ; root . left . right = new Node ( 40 ) ; root . left . right = new Node ( 50 ) ; root . right . left = new Node ( 60 ) ; root . right . right = new Node ( 70 ) ; insertChildrenCount ( root ) ; document . write ( " " + randomNode ( root ) + " " ) ;
Find postorder traversal of BST from preorder traversal | Javascript program for finding postorder traversal of BST from preorder traversal ; Function to find postorder traversal from preorder traversal . ; If entire preorder array is traversed then return as no more element is left to be added to post order array . ; If array element does not lie in range specified , then it is not part of current subtree . ; Store current value , to be printed later , after printing left and right subtrees . Increment preIndex to find left and right subtrees , and pass this updated value to recursive calls . ; All elements with value between minval and val lie in left subtree . ; All elements with value between val and maxval lie in right subtree . ; Function to find postorder traversal . ; To store index of element to be traversed next in preorder array . This is passed by reference to utility function . ; Driver code ; Calling function
class INT { constructor ( d ) { this . data = d ; } } function findPostOrderUtil ( pre , n , minval , maxval , preIndex ) { if ( preIndex . data == n ) return ; if ( pre [ preIndex . data ] < minval pre [ preIndex . data ] > maxval ) { return ; } let val = pre [ preIndex . data ] ; preIndex . data ++ ; findPostOrderUtil ( pre , n , minval , val , preIndex ) ; findPostOrderUtil ( pre , n , val , maxval , preIndex ) ; document . write ( val + " " ) ; } function findPostOrder ( pre , n ) { let preIndex = new INT ( 0 ) ; findPostOrderUtil ( pre , n , Number . MIN_VALUE , Number . MAX_VALUE , preIndex ) ; } let pre = [ 40 , 30 , 35 , 80 , 100 ] ; let n = pre . length ; findPostOrder ( pre , n ) ;
Find Height of Binary Tree represented by Parent array | This function fills depth of i 'th element in parent. The depth is  filled in depth[i]. ; If depth [ i ] is already filled ; If node at index i is root ; If depth of parent is not evaluated before , then evaluate depth of parent first ; Depth of this node is depth of parent plus 1 ; This function returns height of binary tree represented by parent array ; Create an array to store depth of all nodes / and initialize depth of every node as 0 ( an invalid value ) . Depth of root is 1 ; fill depth of all nodes ; The height of binary tree is maximum of all depths . Find the maximum value in depth and assign it to ht . ; var parent = [ 1 , 5 , 5 , 2 , 2 , - 1 , 3 ] ;
function fillDepth ( parent , i , depth ) { if ( depth [ i ] != 0 ) { return ; } if ( parent [ i ] == - 1 ) { depth [ i ] = 1 ; return ; } if ( depth [ parent [ i ] ] == 0 ) { fillDepth ( parent , parent [ i ] , depth ) ; } depth [ i ] = depth [ parent [ i ] ] + 1 ; } function findHeight ( parent , n ) { var depth = Array ( n ) . fill ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { depth [ i ] = 0 ; } for ( i = 0 ; i < n ; i ++ ) { fillDepth ( parent , i , depth ) ; } var ht = depth [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) { if ( ht < depth [ i ] ) { ht = depth [ i ] ; } } return ht ; } var parent = [ - 1 , 0 , 0 , 1 , 1 , 3 , 5 ] ; var n = parent . length ; document . write ( " " + findHeight ( parent , n ) ) ;
Find if possible to visit every nodes in given Graph exactly once based on given conditions | Function to find print path ; If a [ 0 ] is 1 ; Printing path ; Seeking for a [ i ] = 0 and a [ i + 1 ] = 1 ; Printing path ; If a [ N - 1 ] = 0 ; Given Input ; Function Call
function findpath ( N , a ) { if ( a [ 0 ] ) { document . write ( N + 1 ) ; for ( let i = 1 ; i <= N ; i ++ ) document . write ( i ) ; return ; } for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( ! a [ i ] && a [ i + 1 ] ) { for ( let j = 1 ; j <= i ; j ++ ) document . write ( j + " " ) ; document . write ( N + 1 + " " ) ; for ( let j = i + 1 ; j <= N ; j ++ ) document . write ( j + " " ) ; return ; } } for ( let i = 1 ; i <= N ; i ++ ) document . write ( i + " " ) ; document . write ( N + 1 + " " ) ; } let N = 3 , arr = [ 0 , 1 , 0 ] ; findpath ( N , arr ) ;
Modify a numeric string to a balanced parentheses by replacements | Function to check if the given string can be converted to a balanced bracket sequence or not ; Check if the first and last characters are equal ; Initialize two variables to store the count of open and closed brackets ; If the current character is same as the first character ; If the current character is same as the last character ; If count of open brackets becomes less than 0 ; Print the new string ; If the current character is same as the first character ; If bracket sequence is not balanced ; Check for unbalanced bracket sequence ; Print the sequence ; Given Input ; Function Call
function balBracketSequence ( str ) { let n = str . length ; if ( str [ 0 ] == str [ n - 1 ] ) { document . write ( " " ) ; } else { let cntForOpen = 0 , cntForClose = 0 ; let check = 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) cntForOpen ++ ; else if ( str [ i ] == str [ n - 1 ] ) cntForOpen -- ; else cntForOpen ++ ; if ( cntForOpen < 0 ) { check = 0 ; break ; } } if ( check && cntForOpen == 0 ) { document . write ( " " ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ n - 1 ] ) document . write ( ' ' ) ; else document . write ( ' ' ) ; } return ; } else { for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) cntForClose ++ ; else cntForClose -- ; if ( cntForClose < 0 ) { check = 0 ; break ; } } if ( check && cntForClose == 0 ) { document . write ( " " ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) document . write ( ' ' ) ; else document . write ( ' ' ) ; } return ; } } document . write ( " " ) ; } } let str = " " ; balBracketSequence ( str ) ;
Find postorder traversal of BST from preorder traversal | Run loop from 1 to length of pre ; Print from pivot length - 1 to zero ; Print from end to pivot length ; Driver Code
function getPostOrderBST ( pre , N ) { var pivotPoint = 0 ; for ( var i = 1 ; i < N ; i ++ ) { if ( pre [ 0 ] <= pre [ i ] ) { pivotPoint = i ; break ; } } for ( var i = pivotPoint - 1 ; i > 0 ; i -- ) { document . write ( pre [ i ] + " " ) ; } for ( var i = N - 1 ; i >= pivotPoint ; i -- ) { document . write ( pre [ i ] + " " ) ; } document . write ( pre [ 0 ] ) ; }
Count substrings made up of a single distinct character | Function to count the number of substrings made up of a single distinct character ; Stores the required count ; Stores the count of substrings possible by using current character ; Stores the previous character ; Traverse the string ; If current character is same as the previous character ; Increase count of substrings possible with current character ; Reset count of substrings possible with current character ; Update count of substrings ; Update previous character ;
function countSubstrings ( s ) { let ans = 0 ; let subs = 1 ; let pre = ' ' ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( pre == s [ i ] ) { subs += 1 ; } else { subs = 1 ; } ans += subs ; pre = s [ i ] ; } document . write ( ans ) ; } / *	Driver Code / let s = " " ; countSubstrings ( s ) ;
Possible edges of a tree for given diameter , height and vertices | Function to construct the tree ; Special case when d == 2 , only one edge ; Tree is not possible ; Satisfy the height condition by add edges up to h ; Add d - h edges from 1 to satisfy diameter condition ; Remaining edges at vertex 1 or 2 ( d == h ) ; Driver Code
function constructTree ( n , d , h ) { if ( d == 1 ) { if ( n == 2 && h == 1 ) { document . write ( " " , " " ) ; return ; } document . write ( " " , " " ) ; return ; } if ( d > 2 * h ) { document . write ( " " , " " ) ; return ; } for ( var i = 1 ; i <= h ; i ++ ) document . write ( i + " " + ( i + 1 ) , " " ) ; if ( d > h ) { document . write ( " " + " " + ( h + 2 ) , " " ) ; for ( var i = h + 2 ; i <= d ; i ++ ) { document . write ( i + " " + ( i + 1 ) , " " ) ; } } for ( var i = d + 1 ; i < n ; i ++ ) { var k = 1 ; if ( d == h ) k = 2 ; document . write ( k + " " + ( i + 1 ) , " " ) ; } } var n = 5 , d = 3 , h = 2 ; constructTree ( n , d , h ) ;
Query to find length of the longest subarray consisting only of 1 s | Function to calculate the longest subarray consisting of 1 s only ; Stores the maximum length of subarray containing 1 s only ; Traverse the array ; If current element is '1' ; Increment length ; Otherwise ; Reset length ; Update maximum subarray length ; Function to perform given queries ; Stores count of queries ; Traverse each query ; Flip the character ; Size of array ; Given array ; Given queries ; Number of queries
function longestsubarray ( a , N ) { var maxlength = 0 , sum = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( a [ i ] == 1 ) { sum ++ ; } else { sum = 0 ; } maxlength = Math . max ( maxlength , sum ) ; } return maxlength ; } function solveQueries ( arr , n , Q , k ) { var cntQuery = Q . length ; for ( var i = 0 ; i < cntQuery ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { document . write ( longestsubarray ( arr , n ) + " " ) ; } else { arr [ Q [ i ] [ 1 ] - 1 ] ^= 1 ; } } } var N = 10 ; var arr = [ 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 ] ; var Q = [ [ 1 ] , [ 2 , 3 ] , [ 1 ] ] ; var K = 3 ; solveQueries ( arr , N , Q , K ) ;
Smallest element present in every subarray of all possible lengths | Function to add count of numbers in the map for a subarray of length k ; Set to store unique elements ; add elements to the set ; Adding count in map ; Function to check if there is any number which repeats itself in every subarray of length K ; Check all number starting from 1 ; Check if i occurred n - k + 1 times ; Print the smallest number ; Print - 1 , if no such number found ; Function to count frequency of each number in each subarray of length K ; Traverse all subarrays of length K ; Check and print the smallest number present in every subarray and print it ; Function to generate the value of K ; Function call ; Given array ; Size of array ; Function call
function uniqueElements ( arr , start , K , mp ) { let st = new Set ( ) ; for ( let i = 0 ; i < K ; i ++ ) st . add ( arr [ start + i ] ) ; for ( let itr of st ) { if ( mp . has ( itr ) ) { mp . set ( itr , mp . get ( itr ) + 1 ) ; } else { mp . set ( itr , 1 ) ; } } } function checkAnswer ( mp , N , K ) { for ( let i = 1 ; i <= N ; i ++ ) { if ( mp . has ( i ) ) if ( mp . get ( i ) == ( N - K + 1 ) ) { document . write ( i + " " ) ; return ; } } document . write ( - 1 + " " ) ; } function smallestPresentNumber ( arr , N , K ) { let mp = new Map ( ) ; for ( let i = 0 ; i <= N - K ; i ++ ) { uniqueElements ( arr , i , K , mp ) ; } checkAnswer ( mp , N , K ) ; } function generateK ( arr , N ) { for ( let k = 1 ; k <= N ; k ++ ) smallestPresentNumber ( arr , N , k ) ; } let arr = [ 2 , 3 , 5 , 3 , 2 , 3 , 1 , 3 , 2 , 7 ] ; let N = arr . length ; generateK ( arr , N ) ;
Check if two strings can be made equal by reversing a substring of one of the strings | Function to check if the Strings can be made equal or not by reversing a subString of X ; Store the first index from the left which contains unequal characters in both the Strings ; Store the first element from the right which contains unequal characters in both the Strings ; Checks for the first index from left in which characters in both the Strings are unequal ; Store the current index ; Break out of the loop ; Checks for the first index from right in which characters in both the Strings are unequal ; Store the current index ; Break out of the loop ; Reverse the subString X [ L , R ] ; If X and Y are equal ; Otherwise ; Driver Code ; Function Call
function checkString ( X , Y ) { var L = - 1 ; var R = - 1 ; for ( var i = 0 ; i < X . length ; ++ i ) { if ( X [ i ] !== Y [ i ] ) { L = i ; break ; } } for ( var i = X . length - 1 ; i > 0 ; -- i ) { if ( X [ i ] !== Y [ i ] ) { R = i ; break ; } } X = X . substring ( 0 , L ) + reverse ( X . substring ( L , R + 1 ) ) + X . substring ( R + 1 ) ; if ( X === Y ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } function reverse ( input ) { var a = input . split ( " " ) ; var l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { var temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return a . join ( " " ) ; } var X = " " , Y = " " ; checkString ( X , Y ) ;
Maximum length of same indexed subarrays from two given arrays satisfying the given condition | Stores the segment tree node values ; Function to find maximum length of subarray such that sum of maximum element in subarray in brr and sum of subarray in arr * K is at most C ; Base Case ; Let maximum length be 0 ; Perform Binary search ; Find mid value ; Check if the current mid satisfy the given condition ; If yes , then store length ; Otherwise ; Return maximum length stored ; Function to check if it is possible to have such a subarray of length K ; Check for first window of size K ; Calculate the total cost and check if less than equal to c ; If it satisfy the condition ; Find the sum of current subarray and calculate total cost ; Include the new element of current subarray ; Discard the element of last subarray ; Calculate total cost and check <= c ; If possible , then return true ; If it is not possible ; Function that builds segment Tree ; If there is only one element ; Find the value of mid ; Build left and right parts of segment tree recursively ; Update the value at current index ; Function to find maximum element in the given range ; If the query is out of bounds ; If the segment is completely inside the query range ; Calculate the mid ; Return maximum in left & right of the segment tree recursively ; Function that initializes the segment Tree ; Driver Code ; Initialize and Build the Segment Tree ; Function Call
var seg ; function maxLength ( a , b , n , c ) { if ( n == 0 ) return 0 ; var max_length = 0 ; var low = 0 , high = n ; while ( low <= high ) { var mid = low + parseInt ( ( high - low ) / 2 ) ; if ( possible ( a , b , n , c , mid ) ) { max_length = mid ; low = mid + 1 ; } else high = mid - 1 ; } return max_length ; } function possible ( a , b , n , c , k ) { var sum = 0 ; for ( i = 0 ; i < k ; i ++ ) { sum += a [ i ] ; } var total_cost = sum * k + getMax ( b , 0 , n - 1 , 0 , k - 1 , 0 ) ; if ( total_cost <= c ) return true ; for ( i = k ; i < n ; i ++ ) { sum += a [ i ] ; sum -= a [ i - k ] ; total_cost = sum * k + getMax ( b , 0 , n - 1 , i - k + 1 , i , 0 ) ; if ( total_cost <= c ) return true ; } return false ; } function build ( b , index , s , e ) { if ( s == e ) { seg [ index ] = b [ s ] ; return ; } var mid = s + parseInt ( ( e - s ) / 2 ) ; build ( b , 2 * index + 1 , s , mid ) ; build ( b , 2 * index + 2 , mid + 1 , e ) ; seg [ index ] = Math . max ( seg [ 2 * index + 1 ] , seg [ 2 * index + 2 ] ) ; } function getMax ( b , ss , se , qs , qe , index ) { if ( se < qs ss > qe ) return parseInt ( Number . MIN_VALUE / 2 ) ; if ( ss >= qs && se <= qe ) return seg [ index ] ; var mid = ss + ( se - ss ) / 2 ; return Math . max ( getMax ( b , ss , mid , qs , qe , 2 * index + 1 ) , getMax ( b , mid + 1 , se , qs , qe , 2 * index + 2 ) ) ; } function initialiseSegmentTree ( N ) { seg = Array ( 4 * N ) . fill ( 0 ) ; } var A = [ 1 , 2 , 1 , 6 , 5 , 5 , 6 , 1 ] ; var B = [ 14 , 8 , 15 , 15 , 9 , 10 , 7 , 12 ] ; var C = 40 ; var N = A . length ; initialiseSegmentTree ( N ) ; build ( B , 0 , 0 , N - 1 ) ; document . write ( maxLength ( A , B , N , C ) ) ;