codeparrot/xlcost-text-to-code huggingface dataset:
Sort an array containing two types of elements | JavaScript program to sort an array with two types of values in one traversal . ; Method for segregation 0 and 1 given input array ; Driver Code
function segregate0and1 ( arr , n ) { let type0 = 0 ; let type1 = n - 1 ; while ( type0 < type1 ) { if ( arr [ type0 ] == 1 ) { arr [ type0 ] = arr [ type0 ] + arr [ type1 ] ; arr [ type1 ] = arr [ type0 ] - arr [ type1 ] ; arr [ type0 ] = arr [ type0 ] - arr [ type1 ] ; type1 -- ; } else { type0 ++ ; } } } let arr = [ 1 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 , 1 ] ; segregate0and1 ( arr , arr . length ) ; for ( let i = 0 ; i < arr . length ; i ++ ) document . write ( arr [ i ] + " " ) ;
Pentanacci Numbers | Recursive function to find the Nth Pentanacci number ; Function to print the Nth Pentanacci number ; Driver code
function printpentaRec ( n ) { if ( n == 0 n == 1 n == 2 n == 3 n == 4 ) return 0 ; else if ( n == 5 ) return 1 ; else return ( printpentaRec ( n - 1 ) + printpentaRec ( n - 2 ) + printpentaRec ( n - 3 ) + printpentaRec ( n - 4 ) + printpentaRec ( n - 5 ) ) ; } function printPenta ( n ) { document . write ( printpentaRec ( n ) + " " ) ; } let n = 10 ; printPenta ( n ) ;
Count Inversions in an array | Set 1 ( Using Merge Sort ) | Javascript program to count inversions in an array ; function call
function getInvCount ( arr ) { let inv_count = 0 ; for ( let i = 0 ; i < arr . length - 1 ; i ++ ) { for ( let j = i + 1 ; j < arr . length ; j ++ ) { if ( arr [ i ] > arr [ j ] ) inv_count ++ ; } } return inv_count ; } arr = [ 1 , 20 , 6 , 4 , 5 ] ; document . write ( " " + getInvCount ( arr ) ) ;
Range queries for alternatively addition and subtraction on given Array | Function to find the result of alternatively adding and subtracting elements in the range [ L , R ] ; A boolean variable flag to alternatively add and subtract ; Iterate from [ L , R ] ; if flag is false , then add & toggle the flag ; if flag is true subtract and toggle the flag ; Return the final result ; Function to find the value for each query ; Iterate for each query ; Given array ; Given Queries ; Function Call
function findResultUtil ( arr , L , R ) { var result = 0 ; var flag = false ; for ( var i = L ; i <= R ; i ++ ) { if ( flag == false ) { result = result + arr [ i ] ; flag = true ; } else { result = result - arr [ i ] ; flag = false ; } } return result ; } function findResult ( arr , n , q , m ) { for ( var i = 0 ; i < m ; i ++ ) { document . write ( findResultUtil ( arr , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) + " " ) ; } } var arr = [ 10 , 13 , 15 , 2 , 45 , 31 , 22 , 3 , 27 ] ; var n = arr . length ; var q = [ [ 2 , 5 ] , [ 6 , 8 ] , [ 1 , 7 ] , [ 4 , 8 ] , [ 0 , 5 ] ] ; var m = q . length ; findResult ( arr , n , q , m ) ;
Two elements whose sum is closest to zero | JavaScript code to find Two elements whose sum is closest to zero ; Array should have at least two elements ; Initialization of values ; Driver Code
function minAbsSumPair ( arr , arr_size ) { var inv_count = 0 ; var l , r , min_sum , sum , min_l , min_r ; if ( arr_size < 2 ) { document . write ( " " ) ; return ; } min_l = 0 ; min_r = 1 ; min_sum = arr [ 0 ] + arr [ 1 ] ; for ( l = 0 ; l < arr_size - 1 ; l ++ ) { for ( r = l + 1 ; r < arr_size ; r ++ ) { sum = arr [ l ] + arr [ r ] ; if ( Math . abs ( min_sum ) > Math . abs ( sum ) ) { min_sum = sum ; min_l = l ; min_r = r ; } } } document . write ( " " + arr [ min_l ] + " " + arr [ min_r ] ) ; } arr = new Array ( 1 , 60 , - 10 , 70 , - 80 , 85 ) ; minAbsSumPair ( arr , 6 ) ;
Maximize sum of topmost elements of S stacks by popping at most N elements | Function for computing the maximum sum at the top of the stacks after popping at most N elements from S stack ; Constructing a dp matrix of dimensions ( S + 1 ) x ( N + 1 ) ; Loop over all i stacks ; Store the maximum of popping j elements up to the current stack by popping k elements from current stack and j - k elements from all previous stacks combined ; Store the maximum sum of popping N elements across all stacks ; dp [ S ] [ N ] has the maximum sum ; Number of stacks ; Length of each stack ; Maximum elements that can be popped
function maximumSum ( S , M , N , stacks ) { var dp = Array . from ( Array ( S + 1 ) , ( ) => Array ( N + 1 ) . fill ( 0 ) ) ; for ( var i = 0 ; i < S ; i ++ ) { for ( var j = 0 ; j <= N ; j ++ ) { for ( var k = 0 ; k <= Math . min ( j , M ) ; k ++ ) { dp [ i + 1 ] [ j ] = Math . max ( dp [ i + 1 ] [ j ] , stacks [ i ] [ k ] + dp [ i ] [ j - k ] ) ; } } } var result = - 1000000000 ; for ( var i = 0 ; i <= N ; i ++ ) { result = Math . max ( result , dp [ S ] [ i ] ) ; } return result ; } var S = 2 ; var M = 4 ; var stacks = [ [ 2 , 6 , 4 , 5 ] , [ 1 , 6 , 15 , 10 ] ] ; var N = 3 ; document . write ( maximumSum ( S , M , N , stacks ) ) ;
Abstraction of Binary Search | Function to find X such that it is less than the target value and function is f ( x ) = x ^ 2 ; Initialise start and end ; Loop till start <= end ; Find the mid ; Check for the left half ; Store the result ; Reinitialize the start point ; Check for the right half ; Print the maximum value of x such that x ^ 2 is less than the targetValue ; Given targetValue ; ; Function Call
function findX ( targetValue ) { var start = 0 , end = targetValue ; var mid , result ; while ( start <= end ) { mid = start + parseInt ( ( end - start ) / 2 ) ; if ( mid * mid <= targetValue ) { result = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } document . write ( result ) ; } var targetValue = 81 ; findX ( targetValue ) ;
Two elements whose sum is closest to zero | Modified to sort by abolute values ; Absolute value shows how close it is to zero ; If found an even close value update min and store the index ; Driver code
function findMinSum ( arr , n ) { for ( let i = 1 ; i < n ; i ++ ) { if ( ! ( Math . abs ( arr [ i - 1 ] ) < Math . abs ( arr [ i ] ) ) ) { let temp = arr [ i - 1 ] ; arr [ i - 1 ] = arr [ i ] ; arr [ i ] = temp ; } } let min = Number . MAX_VALUE ; let x = 0 , y = 0 ; for ( let i = 1 ; i < n ; i ++ ) { if ( Math . abs ( arr [ i - 1 ] + arr [ i ] ) <= min ) { min = Math . abs ( arr [ i - 1 ] + arr [ i ] ) ; x = i - 1 ; y = i ; } } document . write ( " " + " " + arr [ x ] + " " + arr [ y ] ) ; } let arr = [ 1 , 60 , - 10 , 70 , - 80 , 85 ] ; let n = arr . length ; findMinSum ( arr , n ) ;
Probability of getting all possible values on throwing N dices | JavaScript program to calculate the probability of all the possible values that can be obtained throwing N dices ; Store the probabilities ; Precompute the probabilities for values possible using 1 dice ; Compute the probabilies for all values from 2 to N ; Prlet the result ; Driver Code
function dicesSum ( n ) { let dp = new Array ( n + 1 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < dp . length ; i ++ ) { for ( var j = 0 ; j < 6 * n + 1 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let i = 1 ; i <= 6 ; i ++ ) dp [ 1 ] [ i ] = 1 / 6.0 ; for ( let i = 2 ; i <= n ; i ++ ) for ( let j = i - 1 ; j <= 6 * ( i - 1 ) ; j ++ ) for ( let k = 1 ; k <= 6 ; k ++ ) { dp [ i ] [ j + k ] += ( dp [ i - 1 ] [ j ] * dp [ 1 ] [ k ] ) ; } for ( let i = n ; i <= 6 * n ; i ++ ) { document . write ( i + " " + Math . round ( dp [ n ] [ i ] * 1000.0 ) / 1000.0 + " " ) ; } } let n = 2 ; dicesSum ( n ) ;
Convert undirected connected graph to strongly connected directed graph | To store the assigned Edges ; Flag variable to check Bridges ; Function to implement DFS Traversal ; Mark the current node as visited ; Update the order of node v ; Update the bridge_detect for node v ; Traverse the adjacency list of Node v ; Ignores if same edge is traversed ; Ignores the edge u -- > v as v -- > u is already processed ; Finds a back Edges , cycle present ; Update the bridge_detect [ v ] ; Else DFS traversal for current node in the adjacency list ; Update the bridge_detect [ v ] ; Store the current directed Edge ; Condition for Bridges ; Return flag ; Function to print the direction of edges to make graph SCCs ; Arrays to store the visited , bridge_detect and order of Nodes ; DFS Traversal from vertex 1 ; If flag is zero , then Bridge is present in the graph ; Else print the direction of Edges assigned ; Function to create graph ; Traverse the Edges ; Push the edges in an adjacency list ; N vertices and M Edges ; To create Adjacency List ; Create an undirected graph ; Function Call
let ans ; let flag = 1 ; function dfs ( adj , order , bridge_detect , mark , v , l ) { mark [ v ] = true ; order [ v ] = order [ l ] + 1 ; bridge_detect [ v ] = order [ v ] ; for ( let i = 0 ; i < adj [ v ] . length ; i ++ ) { let u = adj [ v ] [ i ] ; if ( u == l ) { continue ; } if ( order [ v ] < order [ u ] ) { continue ; } if ( mark [ u ] ) { bridge_detect [ v ] = Math . min ( order [ u ] , bridge_detect [ v ] ) ; } else { dfs ( adj , order , bridge_detect , mark , u , v ) ; } bridge_detect [ v ] = Math . min ( bridge_detect [ u ] , bridge_detect [ v ] ) ; ans . push ( [ v , u ] ) ; } if ( bridge_detect [ v ] == order [ v ] && l != 0 ) { flag = 0 ; } return flag ; } function convert ( adj , n ) { let order = new Array ( n ) ; let bridge_detect = new Array ( n ) ; let mark = new Array ( n ) ; let flag = dfs ( adj , order , bridge_detect , mark , 1 , 0 ) ; if ( flag == 0 ) { document . write ( " " ) ; } else { for ( let it = 0 ; it < ans . length - 1 ; it ++ ) { document . write ( ans [ it ] [ 0 ] + " " + ans [ it ] [ 1 ] + " " ) ; } } } function createGraph ( Edges , adj , M ) { for ( let i = 0 ; i < M ; i ++ ) { let u = Edges [ i ] [ 0 ] ; let v = Edges [ i ] [ 1 ] ; adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } } let N = 5 , M = 6 ; let Edges = [ [ 0 , 1 ] , [ 0 , 2 ] , [ 1 , 2 ] , [ 1 , 4 ] , [ 2 , 3 ] , [ 3 , 4 ] ] ; let adj = [ ] ; ans = [ ] ; for ( let i = 0 ; i < N ; i ++ ) adj . push ( [ ] ) ; createGraph ( Edges , adj , M ) ; convert ( adj , N ) ;
Shortest Un | boolean function to check array elements are in increasing order or not ; boolean function to check array elements are in decreasing order or not ; increasing and decreasing are two functions . if function return true value then prlet 0 otherwise 3. ; Driver Code
function increasing ( a , n ) { for ( let i = 0 ; i < n - 1 ; i ++ ) if ( a [ i ] >= a [ i + 1 ] ) return false ; return true ; } function decreasing ( arr , n ) { for ( let i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] < arr [ i + 1 ] ) return false ; return true ; } function shortestUnsorted ( a , n ) { if ( increasing ( a , n ) == true || decreasing ( a , n ) == true ) return 0 ; else return 3 ; } let ar = [ 7 , 9 , 10 , 8 , 11 ] ; let n = ar . length ; document . write ( shortestUnsorted ( ar , n ) ) ;
Minimum number of swaps required to sort an array | swap function ; indexOf function ; Return the minimum number of swaps required to sort the array ; This is checking whether the current element is at the right place or not ; Swap the current element with the right index so that arr [ 0 ] to arr [ i ] is sorted ; Driver class ; Output will be 5
function swap ( arr , i , j ) { let temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } function indexOf ( arr , ele ) { for ( let i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] == ele ) { return i ; } } return - 1 ; } function minSwaps ( arr , N ) { let ans = 0 ; let temp = [ ... arr ] ; temp . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != temp [ i ] ) { ans ++ ; swap ( arr , i , indexOf ( arr , temp [ i ] ) ) ; } } return ans ; } let a = [ 101 , 758 , 315 , 730 , 472 , 619 , 460 , 479 ] ; let n = a . length ; document . write ( minSwaps ( a , n ) ) ;
Check if N can be converted to the form K power K by the given operation | Javascript implementation to Check whether a given number N can be converted to the form K power K by the given operation ; Function to check if a number is converatable ; Check if n is of the form k ^ k ; Check if the subproblem has been solved before ; Iterate through each digit of n ; Check if it is possible to obtain number of given form ; Reduce the number each time ; Store and return the answer to this subproblem ; Fcuntion to check the above method ; Initialise the dp table ; Check if conversion if possible ; Driver code ; Pre store K power K form of numbers Loop till 8 , because 8 ^ 8 > 10 ^ 7
var kPowKform = new Set ( ) ; var dp = Array ( 100005 ) ; function func ( n ) { if ( n <= 0 ) return 0 ; if ( kPowKform . has ( n ) ) return 1 ; if ( dp [ n ] != - 1 ) return dp [ n ] ; var answer = 0 ; var x = n ; while ( x > 0 ) { var d = x % 10 ; if ( d != 0 ) { if ( func ( n - d * d ) ) { answer = 1 ; break ; } } x /= 10 ; } return dp [ n ] = answer ; } function canBeConverted ( n ) { dp = Array ( 100005 ) . fill ( - 1 ) ; if ( func ( n ) ) document . write ( " " ) ; else document . write ( " " ) ; } var N = 13 ; for ( var i = 1 ; i <= 8 ; i ++ ) { var val = 1 ; for ( var j = 1 ; j <= i ; j ++ ) val *= i ; kPowKform . add ( val ) ; } canBeConverted ( N ) ;
Find the Nth Pure number | Function to check if it is a power of 2 or not ; if a number belongs to 4 series it should lie between 2 ^ blocks - 1 to 2 ^ blocks + 2 ^ ( blocks - 1 ) - 1 ; Method to find pure number ; Iterate from 1 to N ; Check if number is power of two ; Distance to previous block numbers ; Distance to previous block numbers ; Driver Code ; Function call to find the Nth pure number
function isPowerOfTwo ( N ) { let number = Math . log ( N ) / Math . log ( 2 ) ; let checker = Math . floor ( number ) ; return number - checker == 0 ; } function isSeriesFour ( N , digits ) { let upperBound = Math . floor ( Math . pow ( 2 , digits ) + Math . pow ( 2 , digits - 1 ) - 1 ) ; let lowerBound = Math . floor ( Math . pow ( 2 , digits ) - 1 ) ; return ( N >= lowerBound ) && ( N < upperBound ) ; } function getPureNumber ( N ) { let numbers = new Array ( N + 1 ) ; numbers [ 0 ] = " " ; let blocks = 0 ; let displacement = 0 ; for ( let i = 1 ; i < N + 1 ; i ++ ) { if ( isPowerOfTwo ( i + 1 ) ) { blocks = blocks + 1 ; } if ( isSeriesFour ( i , blocks ) ) { displacement = Math . floor ( Math . pow ( 2 , blocks - 1 ) ) ; numbers [ i ] = " " + numbers [ i - displacement ] + " " ; } else { displacement = Math . floor ( Math . pow ( 2 , blocks ) ) ; numbers [ i ] = " " + numbers [ i - displacement ] + " " ; } } return numbers [ N ] ; } let N = 5 ; let pure = getPureNumber ( N ) ; document . write ( pure + " " ) ;
Count numbers less than N containing digits from the given set : Digit DP | JavaScript implementation to find the count of numbers possible less than N , such that every digit is from the given set of digits ; Recursive function to find the count of numbers possible less than N , such that every digit is from the given set of digits ; Base case ; Condition when the subproblem is computed previously ; Condition when the number chosen till now is definietly smaller than the given number N ; Loop to traverse all the digits of the given set ; Loop to traverse all the digits from the given set ; Store the solution for current subproblem ; Function to count the numbers less then N from given set of digits ; Converting the number to string ; Find the solution of all the number equal to the length of the given number N ; Loop to find the number less in in the length of the given number ; Driver Code ; Function Call
var dp = Array . from ( Array ( 15 ) , ( ) => Array ( 2 ) . fill ( - 1 ) ) ; function calculate ( pos , tight , D , sz , num ) { if ( pos == num . length ) return 1 ; if ( dp [ pos ] [ tight ] != - 1 ) return dp [ pos ] [ tight ] ; var val = 0 ; if ( tight == 0 ) { for ( var i = 0 ; i < sz ; i ++ ) { if ( D [ i ] < ( num [ pos ] - ' ' ) ) { val += calculate ( pos + 1 , 1 , D , sz , num ) ; } else if ( D [ i ] == num [ pos ] - ' ' ) val += calculate ( pos + 1 , tight , D , sz , num ) ; } } else { for ( var i = 0 ; i < sz ; i ++ ) { val += calculate ( pos + 1 , tight , D , sz , num ) ; } } return dp [ pos ] [ tight ] = val ; } function countNumbers ( D , N , sz ) { var num = ( N . toString ( ) ) ; var len = num . length ; var ans = calculate ( 0 , 0 , D , sz , num ) ; for ( var i = 1 ; i < len ; i ++ ) ans += calculate ( i , 1 , D , sz , num ) ; return ans ; } var sz = 3 ; var D = [ 1 , 4 , 9 ] ; var N = 10 ; document . write ( countNumbers ( D , N , sz ) ) ;
Maximum sum of elements divisible by K from the given array | JavaScript program to implement the above approach ; Function to return the maximum sum divisible by k from elements of v ; check if sum of elements excluding the current one is divisible by k ; check if sum of elements including the current one is divisible by k ; Store the maximum ; Driver Code
let dp = new Array ( 1000 + 1 ) ; function find_max ( i , sum , v , k ) { if ( i == v . length ) return 0 ; if ( dp [ i ] [ sum ] != - 1 ) return dp [ i ] [ sum ] ; let ans = 0 ; if ( ( sum + find_max ( i + 1 , sum , v , k ) ) % k == 0 ) ans = find_max ( i + 1 , sum , v , k ) ; if ( ( sum + v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) % k == 0 ) ans = Math . max ( ans , v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) ; return dp [ i ] [ sum ] = ans ; } let arr = [ 43 , 1 , 17 , 26 , 15 ] ; let k = 16 ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < dp . length ; i ++ ) { for ( var j = 0 ; j < dp . length ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } document . write ( find_max ( 0 , 0 , arr , k ) ) ;
Find Union and Intersection of two unsorted arrays | Prints union of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Before finding union , make sure arr1 [ 0. . m - 1 ] is smaller ; Now arr1 [ ] is smaller Sort the first array and print its elements ( these two steps can be swapped as order in output is not important ) ; Search every element of bigger array in smaller array and print the element if not found ; Prints intersection of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Before finding intersection , make sure arr1 [ 0. . m - 1 ] is smaller ; Now arr1 [ ] is smaller Sort smaller array arr1 [ 0. . m - 1 ] ; Search every element of bigger array in smaller array and print the element if found ; A recursive binary search function . It returns location of x in given array arr [ l . . r ] is present , otherwise - 1 ; If the element is present at the middle itself ; If element is smaller than mid , then it can only be presen in left subarray ; Else the element can only be present in right subarray ; We reach here when element is not present in array ; Driver program to test above function ; Function call
function printUnion ( arr1 , arr2 , m , n ) { if ( m > n ) { let tempp = arr1 ; arr1 = arr2 ; arr2 = tempp ; let temp = m ; m = n ; n = temp ; } arr1 . sort ( ( a , b ) => a - b ) ; for ( let i = 0 ; i < m ; i ++ ) document . write ( arr1 [ i ] + " " ) ; for ( let i = 0 ; i < n ; i ++ ) if ( binarySearch ( arr1 , 0 , m - 1 , arr2 [ i ] ) == - 1 ) document . write ( arr2 [ i ] + " " ) ; } function printIntersection ( arr1 , arr2 , m , n ) { if ( m > n ) { let tempp = arr1 ; arr1 = arr2 ; arr2 = tempp ; let temp = m ; m = n ; n = temp ; } arr1 . sort ( ( a , b ) => a - b ) ; for ( let i = 0 ; i < n ; i ++ ) if ( binarySearch ( arr1 , 0 , m - 1 , arr2 [ i ] ) != - 1 ) document . write ( arr2 [ i ] + " " ) ; } function binarySearch ( arr , l , r , x ) { if ( r >= l ) { let mid = l + Math . floor ( ( r - l ) / 2 ) ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; } let arr1 = [ 7 , 1 , 5 , 2 , 3 , 6 ] ; let arr2 = [ 3 , 8 , 6 , 20 , 7 ] ; let m = arr1 . length ; let n = arr2 . length ; document . write ( " " ) ; printUnion ( arr1 , arr2 , m , n ) ; document . write ( " " ) ; printIntersection ( arr1 , arr2 , m , n ) ;
Find Union and Intersection of two unsorted arrays | Function to find intersection ; when both are equal ; Driver Code ; sort ; Function call
function intersection ( a , b , n , m ) { let i = 0 , j = 0 ; while ( i < n && j < m ) { if ( a [ i ] > b [ j ] ) { j ++ ; } else if ( b [ j ] > a [ i ] ) { i ++ ; } else { document . write ( a [ i ] + " " ) ; i ++ ; j ++ ; } } } let a = [ 1 , 3 , 2 , 3 , 4 , 5 , 5 , 6 ] ; let b = [ 3 , 3 , 5 ] let n = a . length ; let m = b . length ; a . sort ( ) ; b . sort ( ) ; intersection ( a , b , n , m ) ;
Count of 1 's in any path in a Binary Tree | A binary tree node ; A utility function to allocate a new node ; This function updates overall count of 1 in ' res ' And returns count 1 s going through root . ; Base Case ; l and r store count of 1 s going through left and right child of root respectively ; maxCount represents the count of 1 s when the Node under consideration is the root of the maxCount path and no ancestors of the root are there in maxCount path ; if the value at node is 1 then its count will be considered including the leftCount and the rightCount ; Store the Maximum Result . ; if the value at node is 1 then its count will be considered including the maximum of leftCount or the rightCount ; Returns maximum count of 1 in any path in tree with given root ; Initialize result ; Compute and return result ; Driver program
class Node { constructor ( ) { this . left = null ; this . data = 0 ; this . right = null ; } } ; var res = 0 ; function newNode ( data ) { var newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return ( newNode ) ; } function countUntil ( root ) { if ( root == null ) return 0 ; var l = countUntil ( root . left ) ; var r = countUntil ( root . right ) ; var maxCount ; if ( root . data == 1 ) maxCount = l + r + 1 ; else maxCount = l + r ; res = Math . max ( res , maxCount ) ; if ( root . data == 1 ) return Math . max ( l , r ) + 1 ; else return Math . max ( l , r ) ; } function findMaxCount ( root ) { res = - 1000000000 ; countUntil ( root ) ; return res ; } var root = newNode ( 1 ) ; root . left = newNode ( 0 ) ; root . right = newNode ( 1 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 1 ) ; root . left . right . left = newNode ( 1 ) ; root . left . right . right = newNode ( 0 ) ; document . write ( findMaxCount ( root ) ) ;
Minimum number of coins that can generate all the values in the given range | Function to return the count of minimum coins required ; To store the required sequence ; Creating list of the sum of all previous bit values including that bit value ; Driver Code
function findCount ( N ) { let list = [ ] ; let sum = 0 ; let i ; for ( i = 0 ; i < 20 ; i ++ ) { sum += parseInt ( Math . pow ( 2 , i ) , 10 ) ; list . push ( sum ) ; } for ( i = 0 ; i < 20 ; i ++ ) { if ( list [ i ] >= N ) return ( i + 1 ) ; } return 0 ; } let N = 10 ; document . write ( findCount ( N ) ) ;
Sort an array of 0 s , 1 s and 2 s | Sort the input array , the array is assumed to have values in { 0 , 1 , 2 } ; Utility function to print array arr [ ] ; Driver function to check for above functions
function sort012 ( a , arr_size ) { let lo = 0 ; let hi = arr_size - 1 ; let mid = 0 ; let temp = 0 ; while ( mid <= hi ) { if ( a [ mid ] == 0 ) { temp = a [ lo ] ; a [ lo ] = a [ mid ] ; a [ mid ] = temp ; lo ++ ; mid ++ ; } else if ( a [ mid ] == 1 ) { mid ++ ; } else { temp = a [ mid ] ; a [ mid ] = a [ hi ] ; a [ hi ] = temp ; hi -- ; } } } function printArray ( arr , arr_size ) { let i ; for ( i = 0 ; i < arr_size ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; } let arr = [ 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 ] ; let arr_size = arr . length ; sort012 ( arr , arr_size ) ; document . write ( " " ) printArray ( arr , arr_size ) ;