codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of flips required such that the last cell of matrix can be reached from any other cell | Function to calculate the minimum number of flips required ; Dimensions of mat [ ] [ ] ; Initialize answer ; Count all ' D ' s in the last row ; Count all ' R ' s in the last column ; Print let answer ; Given matrix ; Function call ; Print let answer
function countChanges ( mat ) { let n = mat . length ; let m = mat [ 0 ] . length ; let ans = 0 ; for ( let j = 0 ; j < m - 1 ; j ++ ) { if ( mat [ n - 1 ] [ j ] != ' ' ) ans ++ ; } for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( mat [ i ] [ m - 1 ] != ' ' ) ans ++ ; } return ans ; } let arr = [ [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] ] ; let cnt = countChanges ( arr ) ; document . write ( cnt ) ;
Smallest subsequence having GCD equal to GCD of given array | Function to calculate gcd of two numbers ; Function to print the smallest subsequence that satisfies the condition ; Stores gcd of the array . ; Traverse the given array ; Update gcdArr ; Traverse the given array . ; If current element equal to gcd of array . ; Generate all possible pairs . ; If gcd of current pair equal to gcdArr ; Print current pair of the array ; Driver Code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function printSmallSub ( arr , N ) { let gcdArr = 0 ; for ( let i = 0 ; i < N ; i ++ ) { gcdArr = gcd ( gcdArr , arr [ i ] ) ; } for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == gcdArr ) { document . write ( arr [ i ] + " " ) ; return ; } } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { if ( gcd ( arr [ i ] , arr [ j ] ) == gcdArr ) { document . write ( arr [ i ] + " " + arr [ j ] ) ; return ; } } } } let arr = [ 4 , 6 , 12 ] ; let N = arr . length ; printSmallSub ( arr , N ) ;
Count pairs in an array whose product is composite number | Js program to implement the above approach ; Function to get all the prime numbers in the range [ 1 , X ] ; Stores the boolean value to check if a number is prime or not ; Mark all non prime numbers as false ; if p is a prime number ; Mark i as a composite number ; Function to get the count of pairs whose product is a composite number ; Stores the boolean value to check if a number is prime or not ; Stores the count of 1 s ; Stores the count of prime numbers ; Traverse the given array . ; Stores count of pairs whose product is not a composite number ; Stores the count of pairs whose product is composite number ; Driver Code
let X = 1000000 ; function getPrimeNum ( ) { let prime = [ ] ; for ( let i = 0 ; i < X ; i ++ ) { prime . push ( true ) ; } prime [ 0 ] = false ; prime [ 1 ] = false ; for ( let p = 2 ; p * p <= prime . length ; p ++ ) { if ( prime [ p ] ) { for ( let i = p * 2 ; i <= prime . length ; i += p ) prime [ i ] = false ; } } return prime ; } function cntPairs ( arr , N ) { let isPrime = getPrimeNum ( ) ; let cntOne = 0 ; let cntPrime = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { cntOne += 1 ; } else if ( isPrime [ i ] ) { cntPrime += 1 ; } } let cntNonComp = 0 ; cntNonComp = cntPrime * cntOne + Math . floor ( cntOne * ( cntOne - 1 ) / 2 ) ; let res = 0 ; res = N * Math . floor ( ( N - 1 ) / 2 ) - cntNonComp ; return res ; } let arr = [ 1 , 1 , 2 , 2 , 8 ] ; let N = arr . length ; document . write ( cntPairs ( arr , N ) ) ;
Find the greater number closest to N having at most one non | Function to calculate X ^ n in log ( n ) ; Stores the value of X ^ n ; If N is odd ; Function to find the closest number > N having at most 1 non - zero digit ; Stores the count of digits in N ; Stores the power of 10 ^ ( n - 1 ) ; Stores the last ( n - 1 ) digits ; Store the answer ; Driver Code ; Function call
function power ( X , n ) { var res = 1 ; while ( n != 0 ) { if ( ( n & 1 ) != 0 ) res = res * X ; X = X * X ; n = n >> 1 ; } return res ; } function closestgtNum ( N ) { var n = parseInt ( Math . log10 ( N ) + 1 ) ; var P = power ( 10 , n - 1 ) ; var Y = N % P ; var res = N + ( P - Y ) ; return res ; } var N = 120 ; document . write ( closestgtNum ( N ) ) ;
Rearrange an array to make similar indexed elements different from that of another array | Function to find the arrangement of array B [ ] such that element at each index of A [ ] and B [ ] are not equal ; Length of array ; Prlet not possible , if arrays only have single equal element ; Reverse array B ; Traverse over arrays to check if there is any index where A [ i ] and B [ i ] are equal ; Swap B [ i ] with B [ i - 1 ] ; Break the loop ; Prlet required arrangement of array B ; Given arrays A [ ] and B [ ] ; Function Call
function RearrangeB ( A , B ) { let n = A . length ; if ( n == 1 && A [ 0 ] == B [ 0 ] ) { document . write ( " " ) ; return ; } for ( let i = 0 ; i < n / 2 ; i ++ ) { let t = B [ i ] ; B [ i ] = B [ n - i - 1 ] ; B [ n - i - 1 ] = t ; } for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( B [ i ] == A [ i ] ) { let t = B [ i + 1 ] ; B [ i + 1 ] = B [ i ] ; B [ i ] = t ; break ; } } for ( let k in B ) document . write ( B [ k ] + " " ) ; } let A = [ 2 , 4 , 5 , 8 ] ; let B = [ 2 , 4 , 5 , 8 ] ; RearrangeB ( A , B ) ;
Swap the elements between any two given quadrants of a Matrix | Function to iterate over the X quadrant and swap its element with Y quadrant ; Iterate over X quadrant ; Swap operations ; Function to swap the elements of the two given quadrants ; For Swapping 1 st and 2 nd Quadrant ; For Swapping 1 st and 3 rd Quadrant ; For Swapping 1 st and 4 th Quadrant ; For Swapping 2 nd and 3 rd Quadrant ; For Swapping 2 nd and 4 th Quadrant ; For Swapping 3 rd and 4 th Quadrant ; Print the resultant matrix ; Function to print the matrix ; Iterate over the rows ; Iterate over the cols ; Given matrix ; Given quadrants ; Function Call
function swap ( N , M , mat , startx_X , starty_X , startx_Y , starty_Y ) { let row = 0 ; let col = 0 ; for ( let i = startx_X ; ; i ++ ) { col = 0 ; for ( let j = startx_X ; ; j ++ ) { let temp = mat [ i ] [ j ] ; mat [ i ] [ j ] = mat [ startx_Y + row ] [ starty_Y + col ] ; mat [ startx_Y + row ] [ starty_Y + col ] = temp ; col ++ ; if ( col >= M / 2 ) break ; } row ++ ; if ( row >= N / 2 ) break ; } } function swapQuadOfMatrix ( N , M , mat , X , Y ) { if ( X == 1 && Y == 2 ) { swap ( N , M , mat , 0 , 0 , 0 , M / 2 ) ; } else if ( X == 1 && Y == 3 ) { swap ( N , M , mat , 0 , 0 , N / 2 , 0 ) ; } else if ( X == 1 && Y == 4 ) { swap ( N , M , mat , 0 , 0 , N / 2 , M / 2 ) ; } else if ( X == 2 && Y == 3 ) { swap ( N , M , mat , 0 , M / 2 , N / 2 , 0 ) ; } else if ( X == 2 && Y == 4 ) { swap ( N , M , mat , 0 , M / 2 , N / 2 , M / 2 ) ; } else if ( X == 3 && Y == 4 ) { swap ( N , M , mat , N / 2 , 0 , N / 2 , M / 2 ) ; } prletMat ( N , M , mat ) ; } function prletMat ( N , M , mat ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { document . write ( mat [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let N = 6 , M = 6 ; let mat = [ [ 1 , 2 , 3 , 4 , 5 , 6 ] , [ 7 , 8 , 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 , 17 , 18 ] , [ 19 , 20 , 21 , 22 , 23 , 24 ] , [ 25 , 26 , 27 , 28 , 29 , 30 ] , [ 31 , 32 , 33 , 34 , 35 , 36 ] ] ; let X = 1 , Y = 4 ; swapQuadOfMatrix ( N , M , mat , X , Y ) ;
Minimum increments by index value required to obtain at least two equal Array elements | Function to calculate the minimum number of steps required ; Stores minimum difference ; Driver Code
function incrementCount ( arr , N ) { let mini = arr [ 0 ] - arr [ 1 ] ; for ( let i = 2 ; i < N ; i ++ ) { mini = Math . min ( mini , arr [ i - 1 ] - arr [ i ] ) ; } document . write ( mini ) ; } let N = 3 ; let arr = [ 12 , 8 , 4 ] ; incrementCount ( arr , N ) ;
Minimum operations required to convert all characters of a String to a given Character | Function to find the minimum number of operations required ; Maximum number of characters that can be changed in one operation ; If length of the String less than maximum number of characters that can be changed in an operation ; Set the last index as the index for the operation ; Otherwise ; If size of the String is equal to the maximum number of characters in an operation ; Find the number of operations required ; Find the starting position ; Print i - th index ; Shift to next index ; Otherwise ; Find the number of operations required ; If n % div exceeds k ; Print i - th index ; Shift to next index ; Driver Code
function countOperations ( n , k ) { var div = 2 * k + 1 ; if ( n / 2 <= k ) { document . write ( 1 + " " ) ; if ( n > k ) document . write ( k + 1 ) ; else document . write ( n ) ; } else { if ( n % div == 0 ) { var oprn = parseInt ( n / div ) ; document . write ( oprn + " " ) ; var pos = k + 1 ; document . write ( pos + " " ) ; for ( i = 1 ; i <= oprn ; i ++ ) { document . write ( pos + " " ) ; pos += div ; } } else { var oprn = parseInt ( n / div + 1 ) ; document . write ( oprn + " " ) ; var pos = n % div ; if ( n % div > k ) pos -= k ; for ( i = 1 ; i <= oprn ; i ++ ) { document . write ( pos + " " ) ; pos += div ; } } } } var str = " " ; var ch = ' ' ; var n = str . length ; var k = 4 ; countOperations ( n , k ) ;
Length of largest subsequence consisting of a pair of alternating digits |  ; Function to find the length of the largest subsequence consisting of a pair of alternating digits ; Variable initialization ; Nested loops for iteration ; Check if i is not equal to j ; Initialize length as 0 ; Iterate from 0 till the size of the string ; Increment length ; Increment length ; Update maxi ; Check if maxi is not equal to 1 the print it otherwise print 0 ; Given string ; Function call
/ *Js program for the above approach  / * function largestSubsequence ( s ) { let maxi = 0 ; let prev1 ; for ( let i = 0 ; i < 10 ; i ++ ) { for ( let j = 0 ; j < 10 ; j ++ ) { if ( i != j ) { let len = 0 ; prev1 = String ( j ) ; for ( let k = 0 ; k < s . length ; k ++ ) { if ( s [ k ] == String ( i ) && prev1 == String ( j ) ) { prev1 = s [ k ] ; len ++ ; } else if ( s [ k ] == String ( j ) && prev1 == String ( i ) ) { prev1 = s [ k ] ; len ++ ; } } maxi = Math . max ( len , maxi ) ; } } } if ( maxi != 1 ) document . write ( maxi , ' ' ) ; else document . write ( 0 , ' ' ) ; } let s = " " ; largestSubsequence ( s ) ;
Find maximum GCD value from root to leaf in a Binary tree | Initialise to update the maximum gcd value from all the path ; Node structure ; Initialize constructor ; Function to find gcd of a and b ; Function to find the gcd of a path ; Function to find the maximum value of gcd from root to leaf in a Binary tree ; Check if root is not null ; Find the maximum gcd of path value and store in global maxm variable ; Traverse left of binary tree ; Traverse right of the binary tree ; Given Tree ; Function call ; Print the maximum AND value
let maxm = 0 ; class Node { constructor ( x ) { this . val = x ; this . left = null ; this . right = null ; } } var root ; function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function find_gcd ( arr ) { if ( arr . length == 1 ) return arr [ 0 ] ; let g = arr [ 0 ] ; for ( let i = 1 ; i < arr . length ; i ++ ) { g = gcd ( g , arr [ 1 ] ) ; } return g ; } function maxm_gcd ( root , ans ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { ans . push ( root . val ) ; maxm = Math . max ( find_gcd ( ans ) , maxm ) ; return ; } ans . push ( root . val ) ; maxm_gcd ( root . left , ans ) ; maxm_gcd ( root . right , ans ) ; } root = new Node ( 15 ) ; root . left = new Node ( 3 ) ; root . right = new Node ( 7 ) ; root . left . left = new Node ( 15 ) ; root . left . right = new Node ( 1 ) ; root . right . left = new Node ( 31 ) ; root . right . right = new Node ( 9 ) ; let arr = [ ] ; maxm_gcd ( root , arr ) ; document . write ( maxm ) ;
Count of pairs with sum N from first N natural numbers | Function to calculate the value of count ; Stores the count of pairs ; Set the two pointers ; Check if the sum of pairs is equal to N ; Increase the count of pairs ; Move to the next pair ; Driver code
function numberOfPairs ( n ) { let count = 0 ; let i = 1 , j = n - 1 ; while ( i < j ) { if ( i + j == n ) { count ++ ; } i ++ ; j -- ; } return count ; } let n = 8 ; document . write ( numberOfPairs ( n ) ) ;
Count of ways to generate a Matrix with product of each row and column as 1 or | Function to return the number of possible ways ; Check if product can be - 1 ; Driver Code
function Solve ( N , M ) { let temp = ( N - 1 ) * ( M - 1 ) ; let ans = ( Math . pow ( 2 , temp ) ) ; if ( ( N + M ) % 2 != 0 ) document . write ( ans ) ; else document . write ( 2 * ans ) ; } let N = 3 ; let M = 3 ; Solve ( N , M ) ;
Make a palindromic string from given string | Function to find winner of the game ; Array to maintain frequency of the characters in S ; Initialise freq array with 0 ; Maintain count of all distinct characters ; Finding frequency of each character ; Count unique duplicate characters ; Loop to count the unique duplicate characters ; Condition for Player - 1 to be winner ; Else Player - 2 is always winner ; Driver Code ; Function call
function palindromeWinner ( S ) { let freq = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { freq [ i ] = 0 ; } let count = 0 ; for ( let i = 0 ; i < S . length ; ++ i ) { if ( freq [ S [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] == 0 ) count ++ ; freq [ S [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } let unique = 0 ; let duplicate = 0 ; for ( let i = 0 ; i < 26 ; ++ i ) { if ( freq [ i ] == 1 ) unique ++ ; else if ( freq [ i ] >= 2 ) duplicate ++ ; } if ( unique == 1 && ( unique + duplicate ) == count ) return 1 ; return 2 ; } let S = " " ; document . write ( " " + palindromeWinner ( S ) ) ;
Number of substrings with length divisible by the number of 1 's in it | Function return count of such substring ; Selection of adequate x value ; Store where 1 's are located ; If there are no ones , then answer is 0 ; For ease of implementation ; Count storage ; Iterate on all k values less than fixed x ; Keeps a count of 1 's occured  during string traversal ; Iterate on string and modify the totCount ; If this character is 1 ; Add to the final sum / count ; Increase totCount at exterior position ; Reduce totCount at index + k * n ; Slightly modified prefix sum storage ; Number of 1 's till i-1 ; Traversing over string considering each position and finding bounds and count using the inequalities ; Calculating bounds for l and r ; If valid then add to answer ; Driver Code
function countOfSubstrings ( s ) { var n = s . length ; var x = parseInt ( Math . sqrt ( n ) ) ; var ones = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) ones . push ( i ) ; } if ( ones . length == 0 ) return 0 ; ones . push ( n ) ; var totCount = Array ( n * x + n ) . fill ( 0 ) ; var sum = 0 ; for ( var k = 0 ; k <= x ; k ++ ) { var now = 0 ; totCount [ k * n ] ++ ; for ( var j = 1 ; j <= n ; j ++ ) { if ( s [ j - 1 ] == ' ' ) now ++ ; var index = j - k * now ; sum += totCount [ index + k * n ] ; totCount [ index + k * n ] ++ ; } now = 0 ; totCount [ k * n ] -- ; for ( var j = 1 ; j <= n ; j ++ ) { if ( s [ j - 1 ] == ' ' ) now ++ ; var index = j - k * now ; totCount [ index + k * n ] -- ; } } var prefix_sum = Array ( n ) . fill ( - 1 ) ; var cnt = 0 ; for ( var i = 0 ; i < n ; i ++ ) { prefix_sum [ i ] = cnt ; if ( s [ i ] == ' ' ) cnt ++ ; } for ( var k = 0 ; k < n ; k ++ ) { for ( var j = 1 ; j <= parseInt ( n / x ) && prefix_sum [ k ] + j <= cnt ; j ++ ) { var l = ones [ prefix_sum [ k ] + j - 1 ] - k + 1 ; var r = ones [ prefix_sum [ k ] + j ] - k ; l = Math . max ( l , j * ( x + 1 ) ) ; if ( l <= r ) { sum += parseInt ( r / j ) - parseInt ( ( l - 1 ) / j ) ; } } } return sum ; } var S = " " ; document . write ( countOfSubstrings ( S ) ) ;
Find the largest number smaller than integer N with maximum number of set bits | Function to get no of set bits in binary representation of positive integer n ; Function to return the largest number less than N ; Iterate through all the numbers ; Find the number of set bits for the current number ; Check if this number has the highest set bits ; Return the result ; Driver code
function countSetBits ( n ) { let count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } function largestNum ( n ) { let num = 0 ; let max_setBits = 0 ; for ( let i = 0 ; i <= n ; i ++ ) { let setBits = countSetBits ( i ) ; if ( setBits >= max_setBits ) { num = i ; max_setBits = setBits ; } } return num ; } let N = 345 ; document . write ( largestNum ( N ) ) ;
Iterative program to count leaf nodes in a Binary Tree | A binary tree Node has data , pointer to left child and a pointer to right child ; Function to get the count of leaf Nodes in a binary tree ; If tree is empty ; Initialize empty queue . ; Do level order traversal starting from root Initialize count of leaves ; 1 / \ 2 3 / \ 4 5 Let us create Binary Tree shown in above example ; get leaf count of the above created tree
class Node { constructor ( data ) { this . data = data ; this . left = this . right = null ; } } function getLeafCount ( node ) { if ( node == null ) { return 0 ; } let q = [ ] ; let count = 0 ; q . push ( node ) ; while ( q . length != 0 ) { let temp = q . shift ( ) ; if ( temp . left != null ) { q . push ( temp . left ) ; } if ( temp . right != null ) { q . push ( temp . right ) ; } if ( temp . left == null && temp . right == null ) { count ++ ; } } return count ; } let root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; document . write ( getLeafCount ( root ) ) ;
Find the Kth smallest element in the sorted generated array | Function to return the Kth element in B [ ] ; Initialize the count Array ; Reduce N repeatedly to half its value ; Add count to start ; Subtract same count after end index ; Store each element of Array [ ] with their count ; Sort the elements wrt value ; If Kth element is in range of element [ i ] return element [ i ] ; If K is out of bound ; Driver code
function solve ( arr , N , K ) { let count_Arr = new Array ( N + 1 ) . fill ( 0 ) ; let factor = 1 ; let size = N ; while ( size ) { let start = 1 ; let end = size ; count_Arr [ 1 ] += factor * N ; count_Arr [ end + 1 ] -= factor * N ; factor ++ ; size = Math . floor ( size / 2 ) ; } for ( let i = 2 ; i <= N ; i ++ ) count_Arr [ i ] += count_Arr [ i - 1 ] ; let element = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { element . push ( [ arr [ i ] , count_Arr [ i + 1 ] ] ) ; } element . sort ( ( a , b ) => a - b ) ; let start = 1 ; for ( let i = 0 ; i < N ; i ++ ) { let end = start + element [ i ] [ 1 ] - 1 ; if ( K >= start && K <= end ) { return element [ i ] [ 0 ] ; } start += element [ i ] [ 1 ] ; } return - 1 ; } let arr = [ 2 , 4 , 5 , 1 ] ; let N = arr . length ; let K = 13 ; document . write ( solve ( arr , N , K ) ) ;
Find the lexicographical next balanced bracket sequence | makes a string containing char d c number of times ; Function to find the lexicographically next balanced bracket expression if possible ; Decrement the depth for every opening bracket ; Increment for the closing brackets ; Last opening bracket ; Generate the required String ; Driver Code
function string ( c , d ) { let s = " " ; for ( let i = 0 ; i < c ; i ++ ) s += d ; return s ; } function next_balanced_sequence ( s ) { let next = " " ; let length = s . length ; let depth = 0 ; for ( let i = length - 1 ; i >= 0 ; -- i ) { if ( s [ i ] == ' ' ) depth -- ; else depth ++ ; if ( s [ i ] == ' ' && depth > 0 ) { depth -- ; let open = ( length - i - 1 - depth ) / 2 ; let close = length - i - 1 - open ; next = s . substr ( 0 , i ) + ' ' + string ( open , ' ' ) + string ( close , ' ' ) ; break ; } } return next ; } let s = " " ; document . write ( next_balanced_sequence ( s ) ) ;
Maximum number that can be display on Seven Segment Display using N segments | Function to print maximum number that can be formed using N segments ; If n is odd ; use 3 three segment to print 7 ; remaining to print 1 ; If n is even ; print n / 2 1 s . ; Driver 's Code
function printMaxNumber ( n ) { if ( n & 1 ) { document . write ( " " ) ; for ( var i = 0 ; i < ( n - 3 ) / 2 ; i ++ ) document . write ( " " ) ; } else { for ( var i = 0 ; i < n / 2 ; i ++ ) document . write ( " " ) ; } } var n = 5 ; printMaxNumber ( n ) ;
Find the sum of digits of a number at even and odd places | Function to find the sum of the odd and even positioned digits in a number ; To store the respective sum ; Converting integer to String ; Traversing the String ; Driver code
function getSum ( n ) { let sumOdd = 0 ; let sumEven = 0 ; let num = ( n ) . toString ( ) ; for ( let i = 0 ; i < num . length ; i ++ ) if ( i % 2 == 0 ) sumOdd = sumOdd + ( num [ i ] - ' ' ) ; else sumEven = sumEven + ( num [ i ] - ' ' ) ; document . write ( " " + sumOdd + " " ) ; document . write ( " " + sumEven + " " ) ; } let n = 457892 ; getSum ( n ) ;
Iterative program to count leaf nodes in a Binary Tree | Node class ; Program to count leaves ; If the node itself is " null " return 0 , as there are no leaves ; It the node is a leaf then both right and left children will be " null " ; Now we count the leaves in the left and right subtrees and return the sum ; Driver Code ; get leaf count of the above created tree
class Node { constructor ( data ) { this . data = data ; this . next = this . right = null ; } } function countLeaves ( node ) { if ( node == null ) return 0 ; if ( node . left == null && node . right == null ) return 1 ; return countLeaves ( node . left ) + countLeaves ( node . right ) ; } let root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; document . write ( countLeaves ( root ) ) ;
Partition an array such into maximum increasing segments | Returns the maximum number of sorted subarrays in a valid partition ; Find minimum value from right for every index ; Finding the shortest prefix such that all the elements in the prefix are less than or equal to the elements in the rest of the array . ; if current max is less than the right prefix min , we increase number of partitions . ; Driver Code ; Find minimum value from right for every index
function sorted_partitions ( arr ) { let n = arr . length ; let right_min = new Array ( n + 1 ) ; right_min . fill ( 0 ) ; right_min [ n ] = Number . MAX_VALUE ; for ( let i = n - 1 ; i >= 0 ; i -- ) { right_min [ i ] = Math . min ( right_min [ i + 1 ] , arr [ i ] ) ; } let partitions = 0 ; for ( let current_max = arr [ 0 ] , i = 0 ; i < n ; i ++ ) { current_max = Math . max ( current_max , arr [ i ] ) ; if ( current_max <= right_min [ i + 1 ] ) partitions ++ ; } return partitions ; } let arr = [ 3 , 1 , 2 , 4 , 100 , 7 , 9 ] ; let ans = sorted_partitions ( arr ) ; document . write ( ans ) ;
Minimize Cost with Replacement with other allowed | This function returns the minimum cost of the array ; Driver code
function getMinCost ( arr , n ) { let min_ele = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { min_ele = Math . min ( min_ele , arr [ i ] ) ; } return min_ele * ( n - 1 ) ; } let arr = [ 4 , 2 , 5 ] ; let n = arr . length ; document . write ( getMinCost ( arr , n ) ) ;