codeparrot/xlcost-text-to-code huggingface dataset:
Print Longest Palindromic Subsequence | Returns LCS X and Y ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; Following code is used to print LCS ; Create a String length index + 1 and fill it with \ 0 ; Start from the right - most - bottom - most corner and one by one store characters in lcs [ ] ; If current character in X [ ] and Y are same , then current character is part of LCS ; Put current character in result ; reduce values of i , j and index ; } If not same , then find the larger of two and go in the direction of larger value ; Returns longest palindromic subsequence of str ; Find reverse of str ; Return LCS of str and its reverse ; convert String to character array by using toCharArray ; Driver program to test above function
function lcs ( a , b ) { let m = a . length ; let n = b . length ; let X = a . split ( ' ' ) ; let Y = b . split ( ' ' ) ; let L = new Array ( m + 1 ) ; for ( let i = 0 ; i <= m ; i ++ ) { L [ i ] = new Array ( n + 1 ) ; for ( let j = 0 ; j <= n ; j ++ ) { L [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i <= m ; i ++ ) { for ( let j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) { L [ i ] [ j ] = 0 ; } else if ( X [ i - 1 ] == Y [ j - 1 ] ) { L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; } else { L [ i ] [ j ] = Math . max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } } let index = L [ m ] [ n ] ; let lcs = new Array ( index + 1 ) ; lcs . fill ( ' ' ) ; let i = m , j = n ; while ( i > 0 && j > 0 ) { if ( X [ i - 1 ] == Y [ j - 1 ] ) { lcs [ index - 1 ] = X [ i - 1 ] ; i -- ; j -- ; index -- ; else if ( L [ i - 1 ] [ j ] > L [ i ] [ j - 1 ] ) { i -- ; } else { j -- ; } } let ans = " " ; for ( let x = 0 ; x < lcs . length ; x ++ ) { ans += lcs [ x ] ; } return ans ; } function longestPalSubseq ( str ) { let rev = str ; rev = reverse ( rev ) ; return lcs ( str , rev ) ; } function reverse ( str ) { let ans = " " ; let try1 = str . split ( ' ' ) ; for ( let i = try1 . length - 1 ; i >= 0 ; i -- ) { ans += try1 [ i ] ; } return ans ; } let str = " " ; document . write ( longestPalSubseq ( str ) ) ;
Find the Surface area of a 3D figure | Declaring the size of the matrix ; Absolute Difference between the height of two consecutive blocks ; Function To calculate the Total surfaceArea . ; Traversing the matrix . ; If we are traveling the topmost row in the matrix , we declare the wall above it as 0 as there is no wall above it . ; If we are traveling the leftmost column in the matrix , we declare the wall left to it as 0 as there is no wall left it . ; If its not the topmost row ; If its not the leftmost column ; Summing up the contribution of by the current block ; If its the rightmost block of the matrix it will contribute area equal to its height as a wall on the right of the figure ; If its the lowest block of the matrix it will contribute area equal to its height as a wall on the bottom of the figure ; Adding the contribution by the base and top of the figure ; Driver code
let M = 3 ; let N = 3 ; function contribution_height ( current , previous ) { return Math . abs ( current - previous ) ; } function surfaceArea ( A ) { let ans = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { let up = 0 ; let left = 0 ; if ( i > 0 ) up = A [ i - 1 ] [ j ] ; if ( j > 0 ) left = A [ i ] [ j - 1 ] ; ans += contribution_height ( A [ i ] [ j ] , up ) + contribution_height ( A [ i ] [ j ] , left ) ; if ( i == N - 1 ) ans += A [ i ] [ j ] ; if ( j == M - 1 ) ans += A [ i ] [ j ] ; } } ans += N * M * 2 ; return ans ; } let A = [ [ 1 , 3 , 4 ] , [ 2 , 2 , 3 ] , [ 1 , 2 , 4 ] ] ; document . write ( surfaceArea ( A ) ) ;
Program to calculate area and volume of a Tetrahedron | Utility Function ; Driver Code
function area_of_tetrahedron ( side ) { return ( Math . sqrt ( 3 ) * ( side * side ) ) ; } let side = 3 ; document . write ( " " + area_of_tetrahedron ( side ) . toFixed ( 4 ) ) ; < / script
Program to calculate area and volume of a Tetrahedron | Function to calculate volume ; Driver Code
function vol_tetra ( side ) { let volume = ( Math . pow ( side , 3 ) / ( 6 * Math . sqrt ( 2 ) ) ) ; return volume ; } let side = 3 ; let vol = vol_tetra ( side ) ; vol = Math . round ( vol * 100 ) / 100 ; document . write ( vol ) ;
Program to find slope of a line | function to find the slope of a straight line ; driver code to check the above function
function slope ( x1 , y1 , x2 , y2 ) { if ( x1 == x2 ) return Number . MAX_VALUE ; return ( y2 - y1 ) / ( x2 - x1 ) ; } var x1 = 4 , y1 = 2 ; var x2 = 2 , y2 = 5 ; document . write ( " " + slope ( x1 , y1 , x2 , y2 ) ) ;
Maximum integral co | Making set of coordinates such that any two points are non - integral distance apart ; used to avoid duplicates in result ; Driver function
function printSet ( x , y ) { arr = [ ] ; for ( let i = 0 ; i <= Math . min ( x , y ) ; i ++ ) { let pq = [ i , Math . min ( x , y ) - i ] ; arr . push ( pq ) ; } for ( let [ key , value ] of arr . entries ( ) ) { document . write ( value [ 0 ] + " " + value [ 1 ] + " " ) } } let x = 4 ; let y = 4 ; printSet ( x , y )
Program for Volume and Surface Area of Cuboid | Utility function ; Driver function
function areaCuboid ( l , h , w ) { return ( l * h * w ) ; } function surfaceAreaCuboid ( l , h , w ) { return ( 2 * l * w + 2 * w * h + 2 * l * h ) ; } let l = 1 ; let h = 5 ; let w = 7 ; document . write ( " " + areaCuboid ( l , h , w ) + " " ) ; document . write ( " " + surfaceAreaCuboid ( l , h , w ) + " " ) ;
Program to find Circumference of a Circle | utility function ; driver function
function circumference ( r ) { let cir = 2 * 3.1415 * r ; return cir ; } let r = 5 ; document . write ( " " + circumference ( r ) ) ;
Program to check if three points are collinear | * function to check if point collinear or not ; Driver Code
function cool_line ( x1 , y1 , x2 , y2 , x3 , y3 ) { if ( ( y3 - y2 ) * ( x2 - x1 ) == ( y2 - y1 ) * ( x3 - x2 ) ) document . write ( " " ) ; else document . write ( " " ) ; } var a1 = 1 , a2 = 1 , a3 = 0 , b1 = 1 , b2 = 6 , b3 = 9 ; cool_line ( a1 , b1 , a2 , b2 , a3 , b3 ) ;
Number of rectangles in N * M grid | Javascript Code to count number of rectangles in N * M grid ; Driver code
function rectCount ( n , m ) { return parseInt ( ( m * n * ( n + 1 ) * ( m + 1 ) ) / 4 , 10 ) ; } let n = 5 , m = 4 ; document . write ( rectCount ( n , m ) ) ;
Number of unique rectangles formed using N unit squares | height >= length is maintained ; Driver code
function countRect ( n ) { let ans = 0 ; for ( let length = 1 ; length <= parseInt ( Math . sqrt ( n ) , 10 ) ; ++ length ) for ( let height = length ; height * length <= n ; ++ height ) ans ++ ; return ans ; } let n = 5 ; document . write ( countRect ( n ) ) ;
Find the Missing Point of Parallelogram | coordinates of A ; coordinates of B ; coordinates of C
let ax = 5 , ay = 0 ; let bx = 1 , by = 1 ; let cx = 2 , cy = 5 ; document . write ( ( ax + ( cx - bx ) ) + " " + ( ay + ( cy - by ) ) ) ;
Represent a given set of points by the best possible straight line | function to calculate m and c that best fit points represented by x [ ] and y [ ] ; Driver code
function bestApproximate ( x , y , n ) { let m , c , sum_x = 0 , sum_y = 0 , sum_xy = 0 , sum_x2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum_x += x [ i ] ; sum_y += y [ i ] ; sum_xy += x [ i ] * y [ i ] ; sum_x2 += Math . pow ( x [ i ] , 2 ) ; } m = ( n * sum_xy - sum_x * sum_y ) / ( n * sum_x2 - Math . pow ( sum_x , 2 ) ) ; c = ( sum_y - m * sum_x ) / n ; document . write ( " " + m ) ; document . write ( " " + c ) ; } let x = [ 1 , 2 , 3 , 4 , 5 ] ; let y = [ 14 , 27 , 40 , 55 , 68 ] ; let n = x . length ; bestApproximate ( x , y , n ) ;
Check for star graph | define the size of incidence matrix ; function to find star graph ; initialize number of vertex with deg 1 and n - 1 ; check for S1 ; check for S2 ; check for Sn ( n > 2 ) ; driver code
var size = 4 ; function checkStar ( mat ) { var vertexD1 = 0 , vertexDn_1 = 0 ; if ( size == 1 ) return ( mat [ 0 ] [ 0 ] == 0 ) ; if ( size == 2 ) return ( mat [ 0 ] [ 0 ] == 0 && mat [ 0 ] [ 1 ] == 1 && mat [ 1 ] [ 0 ] == 1 && mat [ 1 ] [ 1 ] == 0 ) ; for ( var i = 0 ; i < size ; i ++ ) { var degreeI = 0 ; for ( var j = 0 ; j < size ; j ++ ) if ( mat [ i ] [ j ] ) degreeI ++ ; if ( degreeI == 1 ) vertexD1 ++ ; else if ( degreeI == size - 1 ) vertexDn_1 ++ ; } return ( vertexD1 == ( size - 1 ) && vertexDn_1 == 1 ) ; } var mat = [ [ 0 , 1 , 1 , 1 ] , [ 1 , 0 , 0 , 0 ] , [ 1 , 0 , 0 , 0 ] , [ 1 , 0 , 0 , 0 ] ] ; checkStar ( mat ) ? document . write ( " " ) : document . write ( " " ) ;
Dynamic Convex hull | Adding Points to an Existing Convex Hull | checks whether the point crosses the convex hull or not ; Returns the square of distance between two input points ; Checks whether the point is inside the convex hull or not ; Initialize the centroid of the convex hull ; Multiplying with n to avoid floating point ; if the mid and the given point lies always on the same side w . r . t every edge of the convex hull , then the point lies inside the convex hull ; Adds a point p to given convex hull a [ ] ; If point is inside p ; point having minimum distance from the point p ; Find the upper tangent ; Find the lower tangent ; Initialize result ; making the final hull by traversing points from up to low of given convex hull . ; Modify the original vector ; the set of points in the convex hull ; Print the modified Convex Hull
function orientation ( a , b , c ) { let res = ( b [ 1 ] - a [ 1 ] ) * ( c [ 0 ] - b [ 0 ] ) - ( c [ 1 ] - b [ 1 ] ) * ( b [ 0 ] - a [ 0 ] ) ; if ( res == 0 ) return 0 ; if ( res > 0 ) return 1 ; return - 1 ; } function sqDist ( p1 , p2 ) { return ( p1 [ 0 ] - p2 [ 0 ] ) * ( p1 [ 0 ] - p2 [ 0 ] ) + ( p1 [ 1 ] - p2 [ 1 ] ) * ( p1 [ 1 ] - p2 [ 1 ] ) ; } function inside ( A , p ) { let mid = [ 0 , 0 ] ; let n = A . length ; for ( let i = 0 ; i < n ; i ++ ) { mid [ 0 ] += A [ i ] [ 0 ] ; mid [ 1 ] += A [ i ] [ 1 ] ; } for ( let i = 0 , j ; i < n ; i ++ ) { j = ( i + 1 ) % n ; let x1 = A [ i ] [ 0 ] * n , x2 = A [ j ] [ 0 ] * n ; let y1 = A [ i ] [ 1 ] * n , y2 = A [ j ] [ 1 ] * n ; let a1 = y1 - y2 ; let b1 = x2 - x1 ; let c1 = x1 * y2 - y1 * x2 ; let for_mid = a1 * mid [ 0 ] + b1 * mid [ 1 ] + c1 ; let for_p = a1 * p [ 0 ] * n + b1 * p [ 1 ] * n + c1 ; if ( for_mid * for_p < 0 ) return false ; } return true ; } function addPoint ( a , p ) { if ( inside ( a , p ) ) return ; let ind = 0 ; let n = a . length ; for ( let i = 1 ; i < n ; i ++ ) { if ( sqDist ( p , a [ i ] ) < sqDist ( p , a [ ind ] ) ) { ind = i ; } } let up = ind ; while ( orientation ( p , a [ up ] , a [ ( up + 1 ) % n ] ) >= 0 ) up = ( up + 1 ) % n ; let low = ind ; while ( orientation ( p , a [ low ] , a [ ( n + low - 1 ) % n ] ) <= 0 ) low = ( n + low - 1 ) % n ; let ret = [ ] ; let curr = up ; ret . push ( a [ curr ] ) ; while ( curr != low ) { curr = ( curr + 1 ) % n ; ret . push ( a [ curr ] ) ; } ret . push ( p ) ; a = [ ] ; for ( let i = 0 ; i < ret . length ; i ++ ) { a . push ( ret [ i ] ) ; } return a ; } let a = [ ] a . push ( [ 0 , 0 ] ) ; a . push ( [ 3 , - 1 ] ) ; a . push ( [ 4 , 5 ] ) ; a . push ( [ - 1 , 4 ] ) ; let n = a . length ; let p = [ 100 , 100 ] ; a = addPoint ( a , p ) ; for ( let e = 0 ; e < a . length ; e ++ ) { document . write ( " " + a [ e ] [ 0 ] + " " + a [ e ] [ 1 ] + " " ) ; }
Triangle with no point inside | method to get square of distance between ( x1 , y1 ) and ( x2 , y2 ) ; Method prints points which make triangle with no point inside ; any point can be chosen as first point of triangle ; choose nearest point as second point of triangle ; Get distance from first point and choose nearest one ; Pick third point by finding the second closest point with different slope . ; if already chosen point then skip them ; get distance from first point ; here cross multiplication is compared instead of division comparison ; Driver code
function getDistance ( x1 , y1 , x2 , y2 ) { return ( x2 - x1 ) * ( x2 - x1 ) + ( y2 - y1 ) * ( y2 - y1 ) ; } function triangleWithNoPointInside ( points , N ) { var first = 0 ; var second = 0 ; var third = 0 ; var minD = Number . MAX_VALUE ; for ( i = 0 ; i < N ; i ++ ) { if ( i == first ) continue ; var d = getDistance ( points [ i ] [ 0 ] , points [ i ] [ 1 ] , points [ first ] [ 0 ] , points [ first ] [ 1 ] ) ; if ( minD > d ) { minD = d ; second = i ; } } minD = Number . MAX_VALUE ; for ( i = 0 ; i < N ; i ++ ) { if ( i == first i == second ) continue ; var d = getDistance ( points [ i ] [ 0 ] , points [ i ] [ 1 ] , points [ first ] [ 0 ] , points [ first ] [ 1 ] ) ; if ( ( points [ i ] [ 0 ] - points [ first ] [ 0 ] ) * ( points [ second ] [ 1 ] - points [ first ] [ 1 ] ) != ( points [ second ] [ 0 ] - points [ first ] [ 0 ] ) * ( points [ i ] [ 1 ] - points [ first ] [ 1 ] ) && minD > d ) { minD = d ; third = i ; } } document . write ( points [ first ] [ 0 ] + " " + points [ first ] [ 1 ] + " " ) ; document . write ( points [ second ] [ 0 ] + " " + points [ second ] [ 1 ] + " " ) ; document . write ( points [ third ] [ 0 ] + " " + points [ third ] [ 1 ] + " " ) ; } var points = [ [ 0 , 0 ] , [ 0 , 2 ] , [ 2 , 0 ] , [ 2 , 2 ] , [ 1 , 1 ] ] ; var N = points . length ; triangleWithNoPointInside ( points , N ) ;
Minimum steps to minimize n as per given condition | A tabulation based solution in Javascript ;
function getMinSteps ( n ) { let table = new Array ( n + 1 ) ; table . fill ( 0 ) ; table [ 1 ] = 0 ; for ( let i = 2 ; i <= n ; i ++ ) { if ( ! ( i % 2 ) && ( i % 3 ) ) table [ i ] = 1 + Math . min ( table [ i - 1 ] , table [ i / 2 ] ) ; else if ( ! ( i % 3 ) && ( i % 2 ) ) table [ i ] = 1 + Math . min ( table [ i - 1 ] , table [ i / 3 ] ) ; else if ( ! ( i % 2 ) && ! ( i % 3 ) ) table [ i ] = 1 + Math . min ( table [ i - 1 ] , Math . min ( table [ i / 2 ] , table [ i / 3 ] ) ) ; else table [ i ] = 1 + table [ i - 1 ] ; } return table [ n ] + 1 ; } ' ' ' ' ' ' let n = 10 ; document . write ( getMinSteps ( n ) ) ;
Minimize swaps required to make all prime | Function to pre - calculate the prime [ ] prime [ i ] denotes whether i is prime or not ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to count minimum number of swaps required ; To count the minimum number of swaps required to convert the array into perfectly prime ; To count total number of prime indexes in the array ; To count the total number of prime numbers in the array ; Check whether index is prime or not ; Element is not prime ; If the total number of prime numbers is greater than or equal to the total number of prime indices , then it is possible to convert the array into perfectly prime ; Pre - calculate prime [ ]
const mxn = 1e4 + 1 ; let prime = new Array ( mxn + 1 ) ; function SieveOfEratosthenes ( ) { prime . fill ( true ) ; for ( let p = 2 ; p * p <= mxn ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * p ; i <= mxn ; i += p ) prime [ i ] = false ; } } } function countMin ( arr , n ) { let cMinSwaps = 0 ; let cPrimeIndices = 0 ; let cPrimeNos = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( prime [ i + 1 ] ) { cPrimeIndices ++ ; if ( ! prime [ arr [ i ] ] ) cMinSwaps ++ ; else cPrimeNos ++ ; } else if ( prime [ arr [ i ] ] ) { cPrimeNos ++ ; } } if ( cPrimeNos >= cPrimeIndices ) return cMinSwaps ; else return - 1 ; } SieveOfEratosthenes ( ) ; let n = 5 ; let arr = [ 2 , 7 , 8 , 5 , 13 ] ; document . write ( countMin ( arr , n ) ) ;
Find the size of Largest Subset with positive Bitwise AND | Function to find the largest possible subset having Bitwise AND positive ; Stores the number of set bits at each bit position ; Traverse the given array arr [ ] ; Current bit position ; Loop till array element becomes zero ; If the last bit is set ; Increment frequency ; Divide array element by 2 ; Decrease the bit position ; Size of the largest possible subset ; Driver Code
function largestSubset ( a , N ) { let bit = new Array ( 32 ) . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { let x = 31 ; while ( a [ i ] > 0 ) { if ( a [ i ] & 1 == 1 ) { bit [ x ] ++ ; } a [ i ] = a [ i ] >> 1 ; x -- ; } } let max = Number . MIN_VALUE ; for ( let i = 0 ; i < 32 ; i ++ ) { max = Math . max ( max , bit [ i ] ) ; } document . write ( max ) ; } let arr = [ 7 , 13 , 8 , 2 , 3 ] ; let N = arr . length ; largestSubset ( arr , N ) ;
Maximum element in connected component of given node for Q queries | Function to perform the find operation to find the parent of a disjoint set ; FUnction to perform union operation of disjoint set union ; If the rank are the same ; Update the maximum value ; Function to find the maximum element of the set which belongs to the element queries [ i ] ; Stores the parent elements of the sets ; Stores the rank of the sets ; Stores the maxValue of the sets ; Update parent [ i ] and maxValue [ i ] to i ; Add arr [ i ] [ 0 ] and arr [ i ] [ 1 ] elements to the same set ; Find the parent element of the element queries [ i ] ; Print the maximum value of the set which belongs to the element P ; Driver Code
function Find ( parent , a ) { return ( parent [ a ] = parent [ a ] == a ? a : Find ( parent , parent [ a ] ) ) ; } function Union ( parent , rank , maxValue , a , b ) { a = Find ( parent , a ) ; b = Find ( parent , b ) ; if ( a == b ) return ; if ( rank [ a ] == rank [ b ] ) { rank [ a ] ++ ; } if ( rank [ a ] < rank [ b ] ) { let temp = a ; a = b ; b = temp ; } parent [ b ] = a ; maxValue [ a ] = Math . max ( maxValue [ a ] , maxValue [ b ] ) ; } function findMaxValueOfSet ( arr , queries , R , N , M ) { let parent = new Array ( R + 1 ) ; let rank = new Array ( R + 1 ) . fill ( 0 ) ; let maxValue = new Array ( R + 1 ) ; for ( let i = 1 ; i < R + 1 ; i ++ ) { parent [ i ] = maxValue [ i ] = i ; } for ( let i = 0 ; i < N ; i ++ ) { Union ( parent , rank , maxValue , arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } for ( let i = 0 ; i < M ; i ++ ) { let P = Find ( parent , queries [ i ] ) ; document . write ( maxValue [ P ] + " " ) ; } } let R = 5 ; let arr = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 4 , 5 ] , ] ; let queries = [ 2 , 4 , 1 , 3 ] ; let N = arr . length ; let M = queries . length ; findMaxValueOfSet ( arr , queries , R , N , M ) ;
Maximum sum of segments among all segments formed in array after Q queries | Stores the maximum integer of the sets for each query ; Function to perform the find operation of disjoint set union ; Function to perform the Union operation of disjoint set union ; Find the parent of a and b ; Update the parent ; Update the sum of set a ; Function to find the maximum element from the sets after each operation ; Stores the parent elements of the sets ; Stores the rank of the sets ; Stores the sum of the sets ; Stores the maximum element for each query ; Initially set is empty ; Update the sum as the current element ; After the last query set will be empty and sum will be 0 ; Check if the current element is not in any set then make parent as current element of the queries ; Check left side of the queries [ i ] is not added in any set ; Add the queries [ i ] and the queries [ i ] - 1 in one set ; Check right side of the queries [ i ] is not added in any set ; Add queries [ i ] and the queries [ i ] + 1 in one set ; Update the maxAns ; Push maxAns to the currMax ; Print currMax values in the reverse order ; Driver Code
let maxAns = - 2147483648 ; const Find = ( parent , a ) => { return parent [ a ] = ( parent [ a ] == a ) ? a : ( Find ( parent , parent [ a ] ) ) ; } const Union = ( parent , rank , setSum , a , b ) => { a = Find ( parent , a ) ; b = Find ( parent , b ) ; if ( a == b ) return ; if ( rank [ a ] > rank [ b ] ) rank [ a ] ++ ; if ( rank [ b ] > rank [ a ] ) swap ( a , b ) ; parent [ b ] = a ; setSum [ a ] += setSum [ b ] ; } const maxValues = ( arr , queries , N ) => { let parent = new Array ( N + 1 ) . fill ( 0 ) ; let rank = new Array ( N + 1 ) . fill ( 0 ) ; let setSum = new Array ( N + 1 ) . fill ( 0 ) ; let currMax = [ ] ; for ( let i = 1 ; i < N + 1 ; i ++ ) { parent [ i ] = - 1 ; setSum [ i ] = arr [ i - 1 ] ; } currMax . push ( 0 ) ; for ( let i = N - 1 ; i > 0 ; i -- ) { if ( parent [ queries [ i ] ] == - 1 ) { parent [ queries [ i ] ] = queries [ i ] ; } if ( queries [ i ] - 1 >= 0 && parent [ queries [ i ] - 1 ] != - 1 ) { Union ( parent , rank , setSum , queries [ i ] , queries [ i ] - 1 ) ; } if ( queries [ i ] + 1 <= N && parent [ queries [ i ] + 1 ] != - 1 ) { Union ( parent , rank , setSum , queries [ i ] , queries [ i ] + 1 ) ; } maxAns = Math . max ( setSum [ queries [ i ] ] , maxAns ) ; currMax . push ( maxAns ) ; } for ( let i = currMax . length - 1 ; i >= 0 ; i -- ) { document . write ( ` ${ currMax [ i ] } ` ) ; } } let arr = [ 1 , 3 , 2 , 5 ] ; let queries = [ 3 , 4 , 1 , 2 ] ; let N = arr . length ; maxValues ( arr , queries , N ) ;
ZigZag Level Order Traversal of an N | Structure of a tree node ; Function to create a new node ; Function to perform zig zag traversal of the given tree ; Stores the vectors containing nodes in each level of tree respectively ; Create a queue for BFS ; Enqueue Root of the tree ; Standard Level Order Traversal code using queue ; Stores the element in the current level ; Iterate over all nodes of the current level ; Insert all children of the current node into the queue ; Insert curLevel into result ; Loop to Print the ZigZag Level order Traversal of the given tree ; If i + 1 is even reverse the order of nodes in the current level ; Print the node of ith level ; Function Call
class Node { constructor ( key ) { this . child = [ ] ; this . val = key ; } } function newNode ( key ) { let temp = new Node ( key ) ; return temp ; } function zigzagLevelOrder ( root ) { if ( root == null ) return ; let result = [ ] ; let q = [ ] ; q . push ( root ) ; while ( q . length > 0 ) { let size = q . length ; let curLevel = [ ] ; for ( let i = 0 ; i < size ; i ++ ) { let node = q [ 0 ] ; q . shift ( ) ; curLevel . push ( node . val ) ; for ( let j = 0 ; j < ( node . child ) . length ; j ++ ) q . push ( node . child [ j ] ) ; } result . push ( curLevel ) ; } for ( let i = 0 ; i < result . length ; i ++ ) { if ( ( i + 1 ) % 2 == 0 ) { result [ i ] . reverse ( ) ; } for ( let j = 0 ; j < result [ i ] . length ; j ++ ) document . write ( result [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } let root = newNode ( 1 ) ; ( root . child ) . push ( newNode ( 2 ) ) ; ( root . child ) . push ( newNode ( 3 ) ) ; ( root . child [ 0 ] . child ) . push ( newNode ( 4 ) ) ; ( root . child [ 0 ] . child ) . push ( newNode ( 5 ) ) ; ( root . child [ 1 ] . child ) . push ( newNode ( 6 ) ) ; ( root . child [ 1 ] ) . child . push ( newNode ( 7 ) ) ; ( root . child [ 1 ] . child ) . push ( newNode ( 8 ) ) ; zigzagLevelOrder ( root ) ;