codeparrot/xlcost-text-to-code huggingface dataset:
Maximum count of values of S modulo M lying in a range [ L , R ] after performing given operations on the array | Lookup table ; Function to count the value of S after adding arr [ i ] or arr [ i - 1 ] to the sum S at each time ; Base Case ; Store the mod value ; If the mod value lies in the range then return 1 ; Else return 0 ; Store the current state ; If already computed , return the computed value ; Recursively adding the elements to the sum adding ai value ; Adding arr [ i ] - 1 value ; Return the maximum count to check for root value as well ; Avoid counting idx = 0 as possible solution we are using idx != 0 ; Return the value of current state ; Driver Code
let dp = new Map ( ) ; function countMagicNumbers ( idx , sum , a , n , m , l , r ) { if ( idx == n ) { let temp = sum % m ; if ( temp == l || temp == r || ( temp > l && temp < r ) ) return dp [ [ idx , sum ] ] = 1 ; else return dp [ [ idx , sum ] ] = 0 ; } let curr = [ idx , sum ] ; if ( dp . has ( curr ) ) return dp [ curr ] ; let ls = countMagicNumbers ( idx + 1 , sum + a [ idx ] , a , n , m , l , r ) ; let rs = countMagicNumbers ( idx + 1 , sum + ( a [ idx ] - 1 ) , a , n , m , l , r ) ; let temp1 = Math . max ( ls , rs ) ; let temp = sum % m ; if ( ( temp == l || temp == r || ( temp > l && temp < r ) ) && idx != 0 ) { temp1 += 1 ; } dp [ [ idx , sum ] ] = temp1 ; return dp [ [ idx , sum ] ] ; } let N = 5 , M = 22 , L = 14 , R = 16 ; let arr = [ 17 , 11 , 10 , 8 , 15 ] ; document . write ( countMagicNumbers ( 0 , 0 , arr , N , M , L , R ) ) ;
Check if a path exists for a cell valued 1 to reach the bottom right corner of a Matrix before any cell valued 2 | Function to check if cell with value 1 doesn 't reaches the bottom right cell or not ; Number of rows and columns ; Initialise the deque ; Traverse the matrix ; Push 1 to front of queue ; Push 2 to back of queue ; Store all the possible direction of the current cell ; Run multi - source BFS ; Get the front element ; Pop the front element ; If 1 reached corner first ; Insert new point in queue ; If 1 can 't reach the bottom  right then return false ; Given matrix ; Function Call
function reachesBottom ( a ) { var n = a . length ; var m = a [ 0 ] . length ; var q = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < m ; j ++ ) { if ( a [ i ] [ j ] == 1 ) { q . slice ( 0 , 0 , [ i , j , 1 ] ) ; } else if ( a [ i ] [ j ] == 2 ) { q . push ( [ i , j , 2 ] ) ; } a [ i ] [ j ] = 0 ; } } var dx = [ - 1 , 0 , 1 , 0 ] ; var dy = [ 0 , 1 , 0 , - 1 ] ; while ( ! q . length ) { var front = q [ 0 ] ; q . shift ( ) ; var i = front [ 0 ] , j = front [ 1 ] ; var t = front [ 2 ] ; if ( a [ i ] [ j ] ) continue ; a [ i ] [ j ] = 1 ; if ( t == 1 && ( i == n - 1 && j == m - 1 ) ) { return true ; } for ( var d = 0 ; d < 4 ; d ++ ) { var ni = i + dx [ d ] ; var nj = j + dy [ d ] ; if ( ni >= 0 && ni < n && nj >= 0 && nj < m ) { q . push ( [ ni , nj , t ] ) ; } } } return false ; } var matrix = [ [ 0 , 2 , 0 ] , [ 0 , 1 , 0 ] , [ 0 , 2 , 0 ] ] ; if ( reachesBottom ( matrix ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Smallest element from all square submatrices of size K from a given Matrix | Function to returns a smallest elements of all KxK submatrices of a given NxM matrix ; Stores the dimensions of the given matrix ; Stores the required smallest elements ; Update the smallest elements row - wise ; Update the minimum column - wise ; Store the final submatrix with required minimum values ; Return the resultant matrix ; Function Call ; Print resultant matrix with the minimum values of KxK sub - matrix ; Given matrix ; Given K
function matrixMinimum ( nums , K ) { let N = nums . length ; let M = nums [ 0 ] . length ; let res = new Array ( N - K + 1 ) ; for ( var i = 0 ; i < res . length ; i ++ ) { res [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M - K + 1 ; j ++ ) { let min = Number . MAX_VALUE ; for ( let k = j ; k < j + K ; k ++ ) { min = Math . min ( min , nums [ i ] [ k ] ) ; } nums [ i ] [ j ] = min ; } } for ( let j = 0 ; j < M ; j ++ ) { for ( let i = 0 ; i < N - K + 1 ; i ++ ) { let min = Number . MAX_VALUE ; for ( let k = i ; k < i + K ; k ++ ) { min = Math . min ( min , nums [ k ] [ j ] ) ; } nums [ i ] [ j ] = min ; } } for ( let i = 0 ; i < N - K + 1 ; i ++ ) for ( let j = 0 ; j < M - K + 1 ; j ++ ) res [ i ] [ j ] = nums [ i ] [ j ] ; return res ; } function smallestinKsubmatrices ( arr , K ) { let res = matrixMinimum ( arr , K ) ; for ( let i = 0 ; i < res . length ; i ++ ) { for ( let j = 0 ; j < res [ 0 ] . length ; j ++ ) { document . write ( res [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let arr = [ [ - 1 , 5 , 4 , 1 , - 3 ] , [ 4 , 3 , 1 , 1 , 6 ] , [ 2 , - 2 , 5 , 3 , 1 ] , [ 8 , 5 , 1 , 9 , - 4 ] , [ 12 , 3 , 5 , 8 , 1 ] ] ; let K = 3 ; smallestinKsubmatrices ( arr , K ) ;
Queries to find Kth greatest character in a range [ L , R ] from a string with updates | Function to find the kth greatest character from the strijng ; Sorting the String in non - increasing Order ; Function to print the K - th character from the subString S [ l ] . . S [ r ] ; 0 - based indexing ; SubString of str from the indices l to r . ; Extract kth Largest character ; Function to replace character at pos of str by the character s ; Index of S to be updated . ; Character to be replaced at index in S ; Given String ; Count of queries ; Queries
function find_kth_largest ( str , k ) { str . sort ( ) ; reverse ( str ) ; return str [ k - 1 ] ; } function reverse ( a ) { let i , n = a . length ; let t ; for ( i = 0 ; i < Math . floor ( n / 2 ) ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } function printCharacter ( str , l , r , k ) { l = l - 1 ; r = r - 1 ; let temp = str . substring ( l , r - l + 1 ) ; let ans = find_kth_largest ( temp . split ( " " ) , k ) ; return ans ; } function updateString ( str , pos , s ) { let index = pos - 1 ; let c = s ; str [ index ] = c ; } let str = " " ; let Q = 3 ; document . write ( printCharacter ( str , 1 , 2 , 2 ) + " " ) ; updateString ( str . split ( " " ) , 4 , ' ' ) ; document . write ( printCharacter ( str , 1 , 5 , 4 ) + " " ) ;
Split array into two subarrays such that difference of their sum is minimum | Function to return minimum difference between sum of two subarrays ; To store total sum of array ; Calculate the total sum of the array ; Stores the prefix sum ; Stores the minimum difference ; Traverse the given array ; To store minimum difference ; Update minDiff ; Return minDiff ; Given array ; Length of the array
function minDiffSubArray ( arr , n ) { var total_sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) total_sum += arr [ i ] ; var prefix_sum = 0 ; var minDiff = 1000000000 ; for ( var i = 0 ; i < n - 1 ; i ++ ) { prefix_sum += arr [ i ] ; var diff = Math . abs ( ( total_sum - prefix_sum ) - prefix_sum ) ; if ( diff < minDiff ) minDiff = diff ; } return minDiff ; } var arr = [ 7 , 9 , 5 , 10 ] ; var n = arr . length ; document . write ( minDiffSubArray ( arr , n ) ) ;
Maximize count of non | Function to count the maximum number of subarrays with sum K ; Stores all the distinct prefixSums obtained ; Stores the prefix sum of the current subarray ; Stores the count of subarrays with sum K ; If a subarray with sum K is already found ; Increase count ; Reset prefix sum ; Clear the set ; Insert the prefix sum ; Driver Code
function CtSubarr ( arr , N , K ) { var st = new Set ( ) ; var prefixSum = 0 ; st . add ( prefixSum ) ; var res = 0 ; for ( var i = 0 ; i < N ; i ++ ) { prefixSum += arr [ i ] ; if ( st . has ( prefixSum - K ) ) { res += 1 ; prefixSum = 0 ; st = new Set ( ) ; st . add ( 0 ) ; } st . add ( prefixSum ) ; } return res ; } var arr = [ - 2 , 6 , 6 , 3 , 5 , 4 , 1 , 2 , 8 ] ; var N = arr . length ; var K = 10 ; document . write ( CtSubarr ( arr , N , K ) ) ;
Count of subarrays consisting of only prime numbers | Function to check if a number is prime or not . ; If n has any factor other than 1 , then n is non - prime . ; Function to return the count of subarrays made up of prime numbers only ; Stores the answer ; Stores the count of continuous prime numbers in an array ; If the current array element is prime ; Increase the count ; Update count of subarrays ; If the array ended with a continuous prime sequence ; Driver Code
function is_prime ( n ) { if ( n <= 1 ) return 0 ; for ( var i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return 0 ; } return 1 ; } function count_prime_subarrays ( ar , n ) { var ans = 0 ; var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( is_prime ( ar [ i ] ) ) count ++ ; else { if ( count ) { ans += ( count * ( count + 1 ) ) / 2 ; count = 0 ; } } } if ( count ) ans += ( count * ( count + 1 ) ) / 2 ; return ans ; } var N = 10 ; var ar = [ 2 , 3 , 5 , 6 , 7 , 11 , 3 , 5 , 9 , 3 ] ; document . write ( count_prime_subarrays ( ar , N ) ) ;
Minimum Subarray flips required to convert all elements of a Binary Array to K | Function to count the minimum number of subarray flips required ; Iterate the array ; If arr [ i ] and flag are equal ; Return the answer ; Driver Code
function minSteps ( arr , n , k ) { let i , cnt = 0 ; let flag ; if ( k == 1 ) flag = 0 ; else flag = 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == flag ) { cnt ++ ; flag = ( flag + 1 ) % 2 ; } } return cnt ; } let arr = [ 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 ] ; let n = arr . length ; let k = 1 ; document . write ( minSteps ( arr , n , k ) ) ;
Minimize Sum of an Array by at most K reductions | Function to obtain the minimum possible sum from the array by K reductions ; Implements the MaxHeap ; Insert elements into the MaxHeap ; Remove the maximum ; Insert maximum / 2 ; Stores the sum of remaining elements ; Driver Code
function minSum ( a , n , k ) { let maxheap = [ ] ; for ( let i = 0 ; i < n ; i ++ ) maxheap . push ( a [ i ] ) ; maxheap . sort ( function ( a , b ) { return a - b ; } ) ; while ( maxheap . length > 0 && k > 0 ) { let max_ele = maxheap . pop ( ) ; maxheap . push ( Math . floor ( max_ele / 2 ) ) ; k -= 1 ; maxheap . sort ( function ( a , b ) { return a - b ; } ) ; } let sum = 0 ; while ( maxheap . length > 0 ) sum += maxheap . shift ( ) ; return sum ; } let n = 4 ; let k = 3 ; let a = [ 20 , 7 , 5 , 4 ] ; document . write ( minSum ( a , n , k ) ) ;
Sum of indices of Characters removed to obtain an Empty String based on given conditions | Function to add index of the deleted character ; If index is beyond the range ; Insert the index of the deleted characeter ; Search over the subtrees to find the desired index ; Function to return count of deleted indices which are to the left of the current index ; Function to generate the sum of indices ; Stores the original index of the characters in sorted order of key ; Traverse the map ; Extract smallest index of smallest character ; Delete the character from the map if it has no remaining occurrence ; Stores the original index ; Count of elements removed to the left of current character ; Current index of the current character ; For 1 - based indexing ; Insert the deleted index in the segment tree ; Final answer ; Driver Code
function add_seg ( seg , start , end , current , index ) { if ( index > end index < start ) return ; if ( start == end ) { seg [ current ] = 1 ; return ; } let mid = parseInt ( ( start + end ) / 2 , 10 ) ; add_seg ( seg , start , mid , 2 * current + 1 , index ) ; add_seg ( seg , mid + 1 , end , 2 * current + 2 , index ) ; seg [ current ] = seg [ 2 * current + 1 ] + seg [ 2 * current + 2 ] ; } function deleted ( seg , l , r , start , end , current ) { if ( end < l start > r ) return 0 ; if ( start >= l && end <= r ) return seg [ current ] ; let mid = parseInt ( ( start + end ) / 2 , 10 ) ; return deleted ( seg , l , r , start , mid , 2 * current + 1 ) + deleted ( seg , l , r , mid + 1 , end , 2 * current + 2 ) ; } function sumOfIndices ( s ) { let N = s . length ; let x = ( Math . ceil ( Math . log ( N ) / Math . log ( 2 ) ) ) ; let seg_size = 2 * Math . pow ( 2 , x ) - 1 ; let segment = new Array ( seg_size ) ; segment . fill ( 0 ) ; let count = 41 ; let fre = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { let key = s [ i ] . charCodeAt ( ) ; let que = [ ] ; if ( fre . has ( key ) ) { que = fre [ key ] ; } que . push ( i ) ; fre [ key ] = que ; } let array = Array . from ( fre . keys ( ) ) ; while ( array . length > 0 ) { let a = Array . from ( fre . keys ( ) ) ; let it = a [ 0 ] ; if ( fre [ it ] . length == 0 ) fre . delete ( it ) ; else { let que = fre [ it ] ; let original_index = que [ 0 ] ; let curr_index = deleted ( segment , 0 , original_index - 1 , 0 , N - 1 , 0 ) ; let new_index = original_index - curr_index ; count += new_index + 1 ; add_seg ( segment , 0 , N - 1 , 0 , original_index ) ; que . shift ( ) ; fre [ it ] = que ; } } document . write ( count ) ; } let s = " " ; sumOfIndices ( s ) ;
Maximum Length of Sequence of Sums of prime factors generated by the given operations | Smallest prime factor array ; Stores if a number is prime or not ; Function to compute all primes using Sieve of Eratosthenes ; Function for finding smallest prime factors for every integer ; Function to find the sum of prime factors of number ; Add smallest prime factor to the sum ; Reduce N ; Return the answer ; Function to return the length of sequence of for the given number ; If the number is prime ; If a previously computed subproblem occurred ; Calculate the sum of prime factors ; Function to return the maximum length of sequence for the given range ; Pre - calculate primes ; Precalculate smallest prime factors ; Iterate over the range ; Update maximum length ; Driver Code
let spf = Array . from ( { length : 100005 } , ( _ , i ) => 0 ) ; let prime = Array . from ( { length : 100005 } , ( _ , i ) => 0 ) ; let dp = Array . from ( { length : 100005 } , ( _ , i ) => 0 ) ; function sieve ( ) { prime [ 0 ] = prime [ 1 ] = false ; for ( let i = 2 ; i < 100005 ; i ++ ) prime [ i ] = true ; for ( let i = 2 ; i * i < 100005 ; i ++ ) { if ( prime [ i ] ) { for ( let j = i * i ; j < 100005 ; j += i ) { prime [ j ] = false ; } } } } function smallestPrimeFactors ( ) { for ( let i = 0 ; i < 100005 ; i ++ ) spf [ i ] = - 1 ; for ( let i = 2 ; i * i < 100005 ; i ++ ) { for ( let j = i ; j < 100005 ; j += i ) { if ( spf [ j ] == - 1 ) { spf [ j ] = i ; } } } } function sumOfPrimeFactors ( n ) { let ans = 0 ; while ( n > 1 ) { ans += spf [ n ] ; n /= spf [ n ] ; } return ans ; } function findLength ( n ) { if ( prime [ n ] ) { return 1 ; } if ( dp [ n ] != 0 ) { return dp [ n ] ; } let sum = sumOfPrimeFactors ( n ) ; return dp [ n ] = 1 + findLength ( sum ) ; } function maxLength ( n , m ) { sieve ( ) ; smallestPrimeFactors ( ) ; let ans = Number . MIN_VALUE ; for ( let i = n ; i <= m ; i ++ ) { if ( i == 4 ) { continue ; } ans = Math . max ( ans , findLength ( i ) ) ; } return ans ; } let n = 2 , m = 14 ; document . write ( maxLength ( n , m ) ) ;
Longest Subarray consisting of unique elements from an Array | Function to find largest subarray with no duplicates ; Stores index of array elements ; Update j based on previous occurrence of a [ i ] ; Update ans to store maximum length of subarray ; Store the index of current occurrence of a [ i ] ; Return final ans ; Driver code
function largest_subarray ( a , n ) { let index = new Map ( ) ; let ans = 0 ; for ( let i = 0 , j = 0 ; i < n ; i ++ ) { j = Math . max ( index . has ( a [ i ] ) ? index . get ( a [ i ] ) : 0 , j ) ; ans = Math . max ( ans , i - j + 1 ) ; index . set ( a [ i ] , i + 1 ) ; } return ans ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 1 , 2 , 3 ] ; let n = arr . length ; document . write ( largest_subarray ( arr , n ) ) ;
Count of Ways to obtain given Sum from the given Array elements | Function to call dfs ( ) to calculate the number of ways ; Function to count the number of ways ; Base Case : Reached the end of the array ; Sum is equal to the required sum ; Recursively check if required sum can be obtained by adding current element or by subtracting the current index element ; Driver Code
function findWays ( nums , S ) { return dfs ( nums , S , 0 , 0 ) ; } function dfs ( nums , S , curr_sum , index ) { if ( index == nums . length ) { if ( S == curr_sum ) return 1 ; else return 0 ; } return dfs ( nums , S , curr_sum + nums [ index ] , index + 1 ) + dfs ( nums , S , curr_sum - nums [ index ] , index + 1 ) ; } let S = 3 ; let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let answer = findWays ( arr , S ) ; document . write ( answer ) ;
Check if all the Nodes in a Binary Tree having common values are at least D distance apart | Structure of a Tree node ; Function to create a new node ; Function to count the frequency of node value present in the tree ; Function that returns the max distance between the nodes that have the same key ; If right and left subtree did not have node whose key is value ; Check if the current node is equal to value ; If the left subtree has no node whose key is equal to value ; If the right subtree has no node whose key is equal to value ; Function that finds if the distance between any same nodes is at most K ; Create the map to look for same value of nodes ; Counting the frequency of nodes ; If the returned value of distance is exceeds dist ; Print the result ; Driver Code
class Node { constructor ( key ) { this . key = key ; this . left = this . right = null ; } } function newNode ( key ) { let temp = new Node ( key ) ; return ( temp ) ; } function frequencyCounts ( map , root ) { if ( root == null ) return ; if ( map . has ( root . key ) ) map . set ( root . key , map . get ( root . key ) + 1 ) ; else map . set ( root . key , 1 ) ; frequencyCounts ( map , root . left ) ; frequencyCounts ( map , root . right ) ; } function computeDistance ( root , value ) { if ( root == null ) { return - 1 ; } let left = computeDistance ( root . left , value ) ; let right = computeDistance ( root . right , value ) ; if ( left == - 1 && right == - 1 ) { if ( root . key == value ) { return 1 ; } else return - 1 ; } if ( left == - 1 ) { return right + 1 ; } if ( right == - 1 ) { return left + 1 ; } else { return 1 + Math . max ( left , right ) ; } } function solve ( root , dist ) { let map = new Map ( ) ; frequencyCounts ( map , root ) ; let flag = 0 ; map . forEach ( ( values , keys ) => { if ( values > 1 ) { let result = computeDistance ( root , keys ) ; if ( result > dist result == - 1 ) { flag = 1 ; map . clear ( ) ; } } } ) if ( flag == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } let root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . right = newNode ( 4 ) ; root . right . left = newNode ( 4 ) ; let dist = 7 ; solve ( root , dist ) ;
Minimum Sum of a pair at least K distance apart from an Array | Function to find the minimum sum of two elements that are atleast K distance apart ; Length of the array ; Iterate over the array ; Initialize the min value ; Iterate from i + k to N ; Find the minimum ; Update the minimum sum ; Print the answer ; Driver Code
function findMinSum ( A , K , n ) { let minimum_sum = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { let mini = Number . MAX_VALUE ; for ( let j = i + K ; j < n ; j ++ ) mini = Math . min ( mini , A [ j ] ) ; if ( mini == Number . MAX_VALUE ) continue ; minimum_sum = Math . min ( minimum_sum , A [ i ] + mini ) ; } document . write ( minimum_sum ) ; } let A = [ 4 , 2 , 5 , 4 , 3 , 2 , 5 ] ; let K = 3 ; let n = A . length ; findMinSum ( A , K , n ) ;
Count of all possible pairs having sum of LCM and GCD equal to N | Recursive function to return gcd of a and b ; Function to calculate and return LCM of two numbers ; Function to count pairs whose sum of GCD and LCM is equal to N ; Driver Code
function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } function lcm ( a , b ) { return ( a * b ) / __gcd ( a , b ) ; } function countPair ( N ) { var count = 0 ; for ( i = 1 ; i <= N ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { if ( __gcd ( i , j ) + lcm ( i , j ) == N ) { count ++ ; } } } return count ; } var N = 14 ; document . write ( countPair ( N ) ) ;
Minimize cost to color all the vertices of an Undirected Graph using given operation | JavaScript Program to find the minimum cost to color all vertices of an Undirected Graph ; Function to add edge in the given graph ; Function to perform DFS traversal and find the node with minimum cost ; Update the minimum cost ; Recur for all connected nodes ; Function to calculate and return the minimum cost of coloring all vertices of the Undirected Graph ; Marks if a vertex is visited or not ; Perform DFS traversal ; If vertex is not visited ; Update minimum cost ; Return the final cost ; Driver Code
var MAX = 10 var adj = Array . from ( Array ( MAX ) , ( ) => Array ( ) ) ; function addEdge ( u , v ) { adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } function dfs ( v , cost , vis , min_cost_node ) { vis [ v ] = true ; min_cost_node = Math . min ( min_cost_node , cost [ v - 1 ] ) ; for ( var i = 0 ; i < adj [ v ] . length ; i ++ ) { if ( vis [ adj [ v ] [ i ] ] == false ) { min_cost_node = dfs ( adj [ v ] [ i ] , cost , vis , min_cost_node ) ; } } return min_cost_node ; } function minimumCost ( V , cost ) { var vis = Array ( V + 1 ) . fill ( false ) ; var min_cost = 0 ; for ( var i = 1 ; i <= V ; i ++ ) { if ( ! vis [ i ] ) { var min_cost_node = 1000000000 ; min_cost_node = dfs ( i , cost , vis , min_cost_node ) ; min_cost += min_cost_node ; } } return min_cost ; } var V = 6 , E = 5 ; var cost = [ 12 , 25 , 8 , 11 , 10 , 7 ] ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 3 , 2 ) ; addEdge ( 2 , 5 ) ; addEdge ( 4 , 6 ) ; var min_cost = minimumCost ( V , cost ) ; document . write ( min_cost ) ;
Maximize count of set bits in a root to leaf path in a binary tree | JavaScript Program to implement the above approach ; Node structure ; Initialize constructor ; Function to find the maximum count of setbits in a root to leaf ; Check if root is not null ; Update the maximum count of setbits ; Traverse left of binary tree ; Traverse right of the binary tree ; Driver Code
let maxm = 0 ; class Node { constructor ( x ) { this . val = x ; this . left = null ; this . right = null ; } } var root ; function maxm_setbits ( root , ans ) { if ( ! root ) return ; if ( root . left == null && root . right == null ) { ans += ( root . val ) . toString ( 2 ) . split ( ' ' ) . filter ( y => y == ' ' ) . length ; maxm = Math . max ( ans , maxm ) ; return ; } maxm_setbits ( root . left , ans + ( root . val ) . toString ( 2 ) . split ( ' ' ) . filter ( y => y == ' ' ) . length ) ; maxm_setbits ( root . right , ans + ( root . val ) . toString ( 2 ) . split ( ' ' ) . filter ( y => y == ' ' ) . length ) ; } root = new Node ( 15 ) ; root . left = new Node ( 3 ) ; root . right = new Node ( 7 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 1 ) ; root . right . left = new Node ( 31 ) ; root . right . right = new Node ( 9 ) ; maxm_setbits ( root , 0 ) ; document . write ( maxm ) ;
Longest subarray with odd product | Function to return length of longest subarray with odd product ; If even element is encountered ; Update maximum ; Driver Code
function Maxlen ( arr , n ) { let ans = 0 ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) count = 0 ; else count ++ ; ans = Math . max ( ans , count ) ; } return ans ; } let arr = [ 1 , 7 , 2 ] ; let n = arr . length ; document . write ( Maxlen ( arr , n ) ) ;