codeparrot/xlcost-text-to-code huggingface dataset:
Sort an array using Bubble Sort without using loops | Function to implement bubble sort without using loops ; Base Case : If array contains a single element ; Base Case : If array contains two elements ; Store the first two elements of the list in variables a and b ; Store remaining elements in the list bs ; Store the list after each recursive call ; If a < b ; Otherwise , if b >= a ; Recursively call for the list less than the last element and and return the newly formed list ; Driver Code ; Print the array
function bubble_sort ( ar ) { if ( ar . length <= 1 ) return ar ; if ( ar . length == 2 ) { if ( ar [ 0 ] < ar [ 1 ] ) return ar ; else return [ ar [ 1 ] , ar [ 0 ] ] ; } let a = ar [ 0 ] ; let b = ar [ 1 ] ; let bs = [ ] ; for ( let i = 2 ; i < ar . length ; i ++ ) bs . push ( ar [ i ] ) ; let res = [ ] ; if ( a < b ) { let temp1 = [ ] ; temp1 . push ( b ) ; for ( let i = 0 ; i < bs . length ; i ++ ) temp1 . push ( bs [ i ] ) ; let v = bubble_sort ( temp1 ) ; v . unshift ( a ) ; res = v ; } else { let temp1 = [ ] ; temp1 . push ( a ) ; for ( let i = 0 ; i < bs . length ; i ++ ) temp1 . push ( bs [ i ] ) ; let v = bubble_sort ( temp1 ) ; v . unshift ( b ) ; res = v ; } let pass = [ ] ; for ( let i = 0 ; i < res . length - 1 ; i ++ ) pass . push ( res [ i ] ) ; let ans = bubble_sort ( pass ) ; ans . push ( res [ res . length - 1 ] ) ; return ans ; } let arr = [ 1 , 3 , 4 , 5 , 6 , 2 ] ; let res = bubble_sort ( arr ) ; document . write ( res . join ( " " ) ) ;
Modify a given matrix by placing sorted boundary elements in clockwise manner | Function to print the elements of the matrix in row - wise manner ; Function to sort boundary elements of a matrix starting from the outermost to the innermost boundary and place them in a clockwise manner ; k - starting row index m - ending row index l - starting column index n - ending column index i - iterator ; Stores the current boundary elements ; Push the first row ; Push the last column ; Push the last row ; Push the first column ; Sort the boundary elements ; Update the first row ; Update the last column ; Update the last row ; Update the first column ; Print the resultant matrix ; Given matrix
function printMatrix ( a ) { for ( let i = 0 ; i < a . length ; i ++ ) { for ( let j = 0 ; j < a [ i ] . length ; j ++ ) { document . write ( a [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } function sortBoundaryWise ( a ) { let i , k = 0 , l = 0 ; let m = a . length , n = a [ 0 ] . length ; let n_i , n_k = 0 , n_l = 0 , n_m = m , n_n = n ; while ( k < m && l < n ) { let boundary = [ ] ; for ( i = l ; i < n ; ++ i ) { boundary . push ( a [ k ] [ i ] ) ; } k ++ ; for ( i = k ; i < m ; ++ i ) { boundary . push ( a [ i ] [ n - 1 ] ) ; } n -- ; if ( k < m ) { for ( i = n - 1 ; i >= l ; -- i ) { boundary . push ( a [ m - 1 ] [ i ] ) ; } m -- ; } if ( l < n ) { for ( i = m - 1 ; i >= k ; -- i ) { boundary . push ( a [ i ] [ l ] ) ; } l ++ ; } boundary . sort ( function ( a , b ) { return a - b ; } ) ; let ind = 0 ; for ( i = n_l ; i < n_n ; ++ i ) { a [ n_k ] [ i ] = boundary [ ind ] ; ind ++ ; } n_k += 1 ; for ( i = n_k ; i < n_m ; ++ i ) { a [ i ] [ n_n - 1 ] = boundary [ ind ] ; ind ++ ; } n_n -- ; if ( n_k < n_m ) { for ( i = n_n - 1 ; i >= n_l ; -- i ) { a [ n_m - 1 ] [ i ] = boundary [ ind ] ; ind ++ ; } n_m -- ; } if ( n_l < n_n ) { for ( i = n_m - 1 ; i >= n_k ; -- i ) { a [ i ] [ n_l ] = boundary [ ind ] ; ind ++ ; } n_l ++ ; } } printMatrix ( a ) ; } let matrix = [ ] ; let list1 = [ 9 , 7 , 4 , 5 ] ; let list2 = [ 1 , 6 , 2 , - 6 ] ; let list3 = [ 12 , 20 , 2 , 0 ] ; let list4 = [ - 5 , - 6 , 7 , - 2 ] ; matrix . push ( list1 ) ; matrix . push ( list2 ) ; matrix . push ( list3 ) ; matrix . push ( list4 ) ; sortBoundaryWise ( matrix ) ;
Minimum sum of absolute differences between pairs of a triplet from an array | Function to find minimum sum of absolute differences of pairs of a triplet ; Sort the array ; Stores the minimum sum ; Traverse the array ; Update the minimum sum ; Print the minimum sum ; Input ; Function call to find minimum sum of absolute differences of pairs in a triplet
function minimum_sum ( A , N ) { A . sort ( ) ; var sum = 1000000000 ; for ( var i = 0 ; i <= N - 3 ; i ++ ) { sum = Math . min ( sum , Math . abs ( A [ i ] - A [ i + 1 ] ) + Math . abs ( A [ i + 1 ] - A [ i + 2 ] ) ) ; } document . write ( sum ) ; } var A = [ 1 , 1 , 2 , 3 ] ; var N = A . length ; minimum_sum ( A , N ) ;
Make all characters of a string same by minimum number of increments or decrements of ASCII values of characters | Function to check if all characters of the string can be made the same ; Sort the string ; Calculate ASCII value of the median character ; Stores the minimum number of operations required to make all characters equal ; Traverse the string ; Calculate absolute value of current character and median character ; Print the minimum number of operations required ; Driver Code
function sameChar ( S , N ) { var arr = S . split ( " " ) ; var sorted = arr . sort ( ) ; S = sorted . join ( " " ) ; var mid = S [ parseInt ( N / 2 ) ] . charCodeAt ( 0 ) ; var total_operations = 0 ; for ( var i = 0 ; i < N ; i ++ ) { total_operations += Math . abs ( S [ i ] . charCodeAt ( 0 ) - mid ) ; } document . write ( total_operations ) ; } var S = " " ; var N = S . length ; sameChar ( S , N ) ;
Maximum score possible from an array with jumps of at most length K | Function to count the maximum score of an index ; Base Case ; If the value for the current index is pre - calculated ; Calculate maximum score for all the steps in the range from i + 1 to i + k ; Score for index ( i + j ) ; Update dp [ i ] and return the maximum value ; Function to get maximum score possible from the array A [ ] ; Initialize dp [ ] with - 1 ; Driver Code
function maxScore ( i , A , K , N , dp ) { if ( i >= N - 1 ) return A [ N - 1 ] ; if ( dp [ i ] != - 1 ) return dp [ i ] ; let score = - 1000 ; for ( let j = 1 ; j <= K ; j ++ ) { score = Math . max ( score , maxScore ( i + j , A , K , N , dp ) ) ; } return dp [ i ] = score + A [ i ] ; } function getScore ( A , N , K ) { let dp = new Array ( N ) . fill ( - 1 ) ; document . write ( maxScore ( 0 , A , K , N , dp ) ) ; } let A = [ 100 , - 30 , - 50 , - 15 , - 20 , - 30 ] ; let K = 3 ; let N = A . length ; getScore ( A , N , K ) ;
Maximum score possible from an array with jumps of at most length K | Function to calculate maximum score possible from the array A [ ] ; Stores the score of previous k indices ; Stores the maximum score for current index ; Maximum score at first index ; Traverse the array to calculate maximum score for all indices ; Remove maximum scores for indices less than i - K ; Calculate maximum score for current index ; Push maximum score of current index along with index in maxheap ; Return the maximum score ; Driver Code ; Function call to calculate maximum score from the array A [ ]
function maxScore ( A , K , N ) { var maxheap = [ ] ; var maxScore = 0 ; maxheap . push ( [ A [ 0 ] , 0 ] ) ; for ( var i = 1 ; i < N ; i ++ ) { while ( maxheap [ maxheap . length - 1 ] [ 1 ] < ( i - K ) ) { maxheap . pop ( ) ; } maxScore = A [ i ] + maxheap [ maxheap . length - 1 ] [ 0 ] ; maxheap . push ( [ maxScore , i ] ) ; maxheap . sort ( ) ; } return maxScore ; } var A = [ - 44 , - 17 , - 54 , 79 ] ; var K = 2 ; var N = A . length ; document . write ( maxScore ( A , K , N ) ) ;
Count triplets from an array which can form quadratic equations with real roots | Function to find count of triplets ( a , b , c ) such that the equations ax ^ 2 + bx + c = 0 has real roots ; Stores count of triplets ( a , b , c ) such that ax ^ 2 + bx + c = 0 has real roots ; Base case ; Generate all possible triplets ( a , b , c ) ; If the coefficient of X ^ 2 and X are equal ; If coefficient of X ^ 2 or x are equal to the constant ; Condition for having real roots ; Driver Code ; Function Call
function getCount ( arr , N ) { var count = 0 ; if ( N < 3 ) return 0 ; for ( var b = 0 ; b < N ; b ++ ) { for ( var a = 0 ; a < N ; a ++ ) { if ( a == b ) continue ; for ( var c = 0 ; c < N ; c ++ ) { if ( c == a c == b ) continue ; var d = arr [ b ] * arr [ b ] / 4 ; if ( arr [ a ] * arr <= d ) count ++ ; } } } return count ; } var arr = [ 1 , 2 , 3 , 4 , 5 ] ; var N = arr . length ; document . write ( getCount ( arr , N ) ) ;
Check if all array elements can be reduced to less than X | Function to check if all array elements can be reduced to less than X or not ; Checks if all array elements are already a X or not ; Traverse every possible pair ; Calculate GCD of two array elements ; If gcd is a 1 ; If gcd is a X , then a pair is present to reduce all array elements to a X ; If no pair is present with gcd is a X ; Function to check if all array elements area X ; Function to calculate gcd of two numbers ; Driver Code
function findAns ( A , N , X ) { if ( check ( A , X ) ) { return true ; } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { let gcdd = gcd ( A [ i ] , A [ j ] ) ; if ( gcdd != 1 ) { if ( gcdd <= X ) { return true ; } } } } return false ; } function check ( A , X ) { for ( let i = 0 ; i < A . length ; i ++ ) { if ( A [ i ] > X ) { return false ; } } return true ; } function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } let X = 4 ; let A = [ 2 , 1 , 5 , 3 , 6 ] ; let N = 5 ; document . write ( findAns ( A , N , X ) ) ;
Check if X and Y elements can be selected from two arrays respectively such that the maximum in X is less than the minimum in Y | Function to check if it is possible to choose X and Y elements from a and b such that maximum element among X element is less than minimum element among Y elements ; Check if there are atleast X elements in arr1 and atleast Y elements in arr2 ; Sort arrays in ascending order ; Check if ( X - 1 ) - th element in arr1 is less than from M - Yth element in arr2 ; Return false ; Driver Code ; Function Call
function check ( a , b , Na , Nb , k , m ) { if ( Na < k Nb < m ) return " " ; a . sort ( ) ; b . sort ( ) ; if ( a [ k - 1 ] < b [ Nb - m ] ) { return " " ; } return " " ; } var arr1 = [ 1 , 2 , 3 ] ; var arr2 = [ 3 , 4 , 5 ] ; var N = arr1 . length ; var M = arr2 . length ; var X = 2 , Y = 1 ; document . write ( check ( arr1 , arr2 , N , M , X , Y ) ) ;
Partition array into two subsets with minimum Bitwise XOR between their maximum and minimum | Function to split the array into two subset such that the Bitwise XOR between the maximum of one subset and minimum of other is minimum ; Sort the array in increasing order ; Calculating the min Bitwise XOR between consecutive elements ; Return the final minimum Bitwise XOR ; Given array arr [ ] ; Size of array ; Function Call
function splitArray ( arr , N ) { arr . sort ( ) ; let result = Number . MAX_VALUE ; for ( let i = 1 ; i < N ; i ++ ) { result = Math . min ( result , arr [ i ] - arr [ i - 1 ] ) ; } return result ; } let arr = [ 3 , 1 , 2 , 6 , 4 ] ; let N = arr . length ; document . write ( splitArray ( arr , N ) ) ;
Sort an array by left shifting digits of array elements | Function to check if an array is sorted in increasing order or not ; Traverse the array ; Function to sort the array by left shifting digits of array elements ; Stores previous array element ; Traverse the array arr [ ] ; Stores current element ; Stores current element in string format ; Left - shift digits of current element in all possible ways ; Left - shift digits of current element by idx ; If temp greater than or equal to prev and temp less than optEle ; Update arr [ i ] ; Update prev ; If arr is in increasing order ; Otherwise ; Driver Code
function isIncreasing ( arr ) { for ( let i = 0 ; i < arr . length - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) return false ; } return true ; } function sortArr ( arr ) { let prev = - 1 ; for ( let i = 0 ; i < arr . length ; i ++ ) { let optEle = arr [ i ] ; let strEle = arr [ i ] . toString ( ) ; for ( let idx = 0 ; idx < strEle . length ; idx ++ ) { let strEle2 = strEle . substr ( idx ) + strEle . substr ( 0 , idx ) ; let temp = parseInt ( strEle2 ) ; if ( temp >= prev && temp < optEle ) optEle = temp ; } arr [ i ] = optEle ; prev = arr [ i ] ; } if ( isIncreasing ( arr ) ) return arr ; else { arr = [ - 1 ] ; return arr ; } } let arr = [ 511 , 321 , 323 , 432 , 433 ] ; let res = sortArr ( arr ) ; for ( let i = 0 ; i < res . length ; i ++ ) document . write ( res [ i ] + " " ) ;
Maximize maximum possible subarray sum of an array by swapping with elements from another array | Function to find the maximum subarray sum possible by swapping elements from array arr [ ] with that from array brr [ ] ; Stores elements from the arrays arr [ ] and brr [ ] ; Store elements of array arr [ ] and brr [ ] in the vector crr ; Sort the vector crr in descending order ; Stores maximum sum ; Calculate the sum till the last index in crr [ ] which is less than N which contains a positive element ; Print the sum ; Given arrays and respective lengths ; Calculate maximum subarray sum
function maxSum ( arr , brr , N , K ) { let crr = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { crr . push ( arr [ i ] ) ; } for ( let i = 0 ; i < K ; i ++ ) { crr . push ( brr [ i ] ) ; } crr . sort ( function ( a , b ) { return a - b } ) ; crr . reverse ( ) ; let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( crr [ i ] > 0 ) { sum += crr [ i ] ; } else { break ; } } document . write ( sum ) ; } let arr = [ 7 , 2 , - 1 , 4 , 5 ] ; let N = arr . length ; let brr = [ 1 , 2 , 3 , 2 ] ; let K = brr . length ; maxSum ( arr , brr , N , K ) ;
Count pairs with Bitwise XOR greater than both the elements of the pair | Function that counts the pairs whose Bitwise XOR is greater than both the elements of pair ; Stores the count of pairs ; Generate all possible pairs ; Find the Bitwise XOR ; Find the maximum of two ; If xo < mx , increment count ; Prvar the value of count ; Driver Code ; Function Call
function countPairs ( A , N ) { var count = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { var xo = ( A [ i ] ^ A [ j ] ) ; var mx = Math . max ( A [ i ] , A [ j ] ) ; if ( xo > mx ) { count ++ ; } } } document . write ( count ) ; } var arr = [ 2 , 4 , 3 ] ; var N = arr . length ; countPairs ( arr , N ) ;
Possible arrangement of persons waiting to sit in a hall | Function to find the arrangement of seating ; Stores the row in which the ith person sits ; Stores the width of seats along with their index or row number ; Sort the array ; Store the seats and row for boy 's seat ; Stores the index of row upto which boys have taken seat ; Iterate the string ; Push the row number at index in vector and heap ; Increment the index to let the next boy in the next minimum width vacant row ; Otherwise ; If girl then take top of element of the max heap ; Pop from queue ; Print the values ; Given N ; Given arr [ ] ; Given string ; Function Call
function findTheOrder ( arr , s , N ) { let ans = [ ] ; let A = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { let p = [ arr [ i ] , i + 1 ] ; A . push ( p ) ; } A . sort ( function ( p1 , p2 ) { return p1 [ 0 ] - p2 [ 0 ] ; } ) ; let q = [ ] ; let index = 0 ; for ( let i = 0 ; i < 2 * N ; i ++ ) { if ( s [ i ] == ' ' ) { ans . push ( A [ index ] [ 1 ] ) ; q . push ( A [ index ] ) ; index ++ ; } else { ans . push ( q [ 0 ] [ 1 ] ) ; q . shift ( ) ; } q . sort ( function ( a , b ) { return b [ 0 ] - a [ 0 ] ; } ) ; } for ( let i = 0 ; i < ans . length ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } let N = 3 ; let arr = [ 2 , 1 , 3 ] ; let s = " " ; findTheOrder ( arr , s , N ) ;
Rearrange array to make product of prefix sum array non zero | Function to print array elements ; Function to rearrange array that satisfies the given condition ; Stores sum of elements of the given array ; Calculate totalSum ; If the totalSum is equal to 0 ; No possible way to rearrange array ; If totalSum exceeds 0 ; Rearrange the array in descending order ; Otherwise ; Rearrange the array in ascending order ; Driver Code
function printArr ( arr , N ) { for ( var i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } function rearrangeArr ( arr , N ) { var totalSum = 0 ; for ( var i = 0 ; i < N ; i ++ ) { totalSum += arr [ i ] ; } if ( totalSum == 0 ) { document . write ( " " + " " ) ; } else if ( totalSum > 0 ) { arr . sort ( compare ) ; arr = reverse ( arr ) ; printArr ( arr , N ) ; } else { arr . sort ( compare ) ; printArr ( arr , N ) ; } } function compare ( a , b ) { if ( a < b ) { return - 1 ; } else if ( a > b ) { return 1 ; } else { return 0 ; } } function reverse ( a ) { var i , n = a . length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } var arr = [ 1 , - 1 , - 2 , 3 ] ; var N = arr . length ; rearrangeArr ( arr , N ) ;
Count ways to distribute exactly one coin to each worker | Javascript program for the above approach ; Function to find number of way to distribute coins giving exactly one coin to each person ; Sort the given arrays ; Start from bigger salary ; Increment the amount ; Reduce amount of valid coins by one each time ; Return the result ; Given two arrays ; Function Call
var MOD = 1000000007 ; function solve ( values , salary ) { var ret = 1 ; var amt = 0 ; values . sort ( ( a , b ) => a - b ) ; salary . sort ( ( a , b ) => a - b ) ; while ( salary . length ) { while ( values . length && values [ values . length - 1 ] >= salary [ salary . length - 1 ] ) { amt ++ ; values . pop ( ) ; } if ( amt == 0 ) return 0 ; ret *= amt -- ; ret %= MOD ; salary . pop ( ) ; } return ret ; } var values = [ 1 , 2 ] , salary = [ 2 ] ; document . write ( solve ( values , salary ) ) ;
Count of index pairs with equal elements in an array | Set 2 | Function that counts the pair in the array arr [ ] ; Sort the array ; Initialize two pointers ; Add all valid pairs to answer ; Return the answer ; Given array [ ] arr ; Function call
function countPairs ( arr , n ) { let ans = 0 ; arr . sort ( function ( a , b ) { return a - b } ) ; let left = 0 , right = 1 ; while ( right < n ) { if ( arr [ left ] == arr [ right ] ) ans += right - left ; else left = right ; right ++ ; } return ans ; } let arr = [ 2 , 2 , 3 , 2 , 3 ] ; let N = arr . length ; document . write ( countPairs ( arr , N ) ) ;
Minimize Cost to sort a String in Increasing Order of Frequencies of Characters | Javascript program to implement above approach ; For a single character ; Stores count of repetitions of a character ; If repeating character ; Otherwise ; Store frequency ; Reset count ; Insert the last character block ; Sort the frequencies ; Stores the minimum cost of all operations ; Store the absolute difference of i - th frequencies of ordered and unordered sequences ; Return the minimum cost ; Driver Code
function sortString ( S ) { let sorted = [ ] ; let original = [ ] ; let insert = false ; if ( S . length == 1 ) { document . write ( 0 + " " ) ; } let curr = 1 ; for ( let i = 0 ; i < ( S . length - 1 ) ; i ++ ) { if ( S [ i ] == S [ i + 1 ] ) { curr += 1 ; insert = false ; } else { sorted . push ( curr ) ; original . push ( curr ) ; curr = 1 ; insert = true ; } } if ( ( S [ S . length - 1 ] != S [ S . length - 2 ] ) insert == false ) { sorted . push ( curr ) ; original . push ( curr ) ; } sorted . sort ( function ( a , b ) { return a - b } ) ; let t_cost = 0 ; for ( let i = 0 ; i < sorted . length ; i ++ ) { t_cost += Math . abs ( sorted [ i ] - original [ i ] ) ; } return parseInt ( t_cost / 2 , 10 ) ; } let S = " " ; document . write ( sortString ( S ) ) ;
K most occurring strings | Function that returns list of K most frequent strings ; Hash map to store the frequency of each string ; Set the default frequency of each string 0 ; Using a priority queue to store the strings in accordance of the frequency and alphabetical order ( if frequency is equal ) Lambda expression is used set the priority , if frequencies are not equal than the string with greater frequency is returned else the string which is lexically smaller is returned ; Traverse the HashMap ; Remove top ( N - K ) elements ; Order queue has K most frequent strings in a reverse order ; Reverse the ArrayList so as to get in the desired order ; Driver Code ; Given array ; Function Call ; Print the K most occurring strings
function frequentWords ( arr , K ) { var Freq = new Map ( ) ; for ( var word of arr ) { Freq . set ( word , Freq . has ( word ) ? Freq . get ( word ) : 0 ) ; } var Order = [ ] ; for ( var word of [ ... Freq . keys ( ) ] ) { Order . push ( word ) ; if ( Order . length > K ) { Order . pop ( ) ; } } var ans = [ ] ; Order . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ; while ( Order . length != 0 ) { ans . push ( Order [ Order . length - 1 ] ) Order . pop ( ) ; } ans . reverse ( ) ; return ans ; } var N = 3 , K = 2 ; var arr = [ ] ; arr . push ( " " ) ; arr . push ( " " ) ; arr . push ( " " ) ; var ans = frequentWords ( arr , K ) ; for ( var word of ans ) { document . write ( word + " " ) ; }
Sort numbers based on count of letters required to represent them in words | letters [ i ] stores the count of letters required to represent the digit i ; Function to return the sum of letters required to represent N ; Function to sort the array according to the sum of letters to represent n ; Vector to store the digit sum with respective elements ; Inserting digit sum with elements in the vector pair ; Making the vector pair ; Sort the vector , this will sort the pair according to the sum of letters to represent n ; Prlet the sorted vector content ; Driver Code
let letters = [ 4 , 3 , 3 , 3 , 4 , 4 , 3 , 5 , 5 , 4 ] ; function sumOfLetters ( n ) { let sum = 0 ; while ( n > 0 ) { sum += letters [ n % 10 ] ; n = Math . floor ( n / 10 ) ; } return sum ; } function sortArr ( arr , n ) { let vp = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { vp . push ( [ sumOfLetters ( arr [ i ] ) , arr [ i ] ] ) ; } vp . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ; for ( let i = 0 ; i < vp . length ; i ++ ) document . write ( vp [ i ] [ 1 ] + " " ) ; } let arr = [ 12 , 10 , 31 , 18 ] ; let n = arr . length ; sortArr ( arr , n ) ;
Check if the Left View of the given tree is sorted or not | Binary Tree Node ; Utility function to create a new node ; Function to find left view and check if it is sorted ; Queue to hold values ; Variable to check whether level order is sorted or not ; Iterate until the queue is empty ; Traverse every level in tree ; Variable for initial level ; Checking values are sorted or not ; Push left value if it is not null ; Push right value if it is not null ; Pop out the values from queue ; Check if the value are not sorted then break the loop ; Driver code
class node { constructor ( key ) { this . left = null ; this . right = null ; this . val = key ; } } function newnode ( key ) { let temp = new node ( key ) ; return temp ; } function func ( root ) { let q = [ ] ; let t = true ; q . push ( root ) ; let i = - 1 , j = - 1 ; while ( q . length > 0 ) { let h = q . length ; while ( h > 0 ) { root = q [ 0 ] ; if ( i == - 1 ) { j = root . val ; } if ( i == - 2 ) { if ( j <= root . val ) { j = root . val ; i = - 3 ; } else { t = false ; break ; } } if ( root . left != null ) { q . push ( root . left ) ; } if ( root . right != null ) { q . push ( root . right ) ; } h = h - 1 ; q . shift ( ) ; } i = - 2 ; if ( t == false ) { break ; } } if ( t ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } let root = newnode ( 10 ) ; root . right = newnode ( 50 ) ; root . right . right = newnode ( 15 ) ; root . left = newnode ( 20 ) ; root . left . left = newnode ( 50 ) ; root . left . right = newnode ( 23 ) ; root . right . left = newnode ( 10 ) ; func ( root ) ;