codeparrot/xlcost-text-to-code huggingface dataset:
Check if end of a sorted Array can be reached by repeated jumps of one more , one less or same number of indices as previous jump | JavaScript program for the above approach ; Utility function to check if it is possible to move from index 1 to N - 1 ; Successfully reached end index ; memo [ i ] [ j ] is already calculated ; Check if there is any index having value of A [ i ] + j - 1 , A [ i ] + j or A [ i ] + j + 1 ; If A [ k ] > A [ i ] + j + 1 , can 't make a move further ; It 's possible to move A[k] ; Check is it possible to move from index k having previously taken A [ k ] - A [ i ] steps ; If yes then break the loop ; Store value of flag in memo ; Return memo [ i ] [ j ] ; Function to check if it is possible to move from index 1 to N - 1 ; Stores the memoized state ; Initialize all values as - 1 ; Initially , starting index = 1 ; Function call ; Driver code ; Function Call
let N = 8 ; function check ( memo , i , j , A ) { if ( i == N - 1 ) return 1 ; if ( memo [ i ] [ j ] != - 1 ) return memo [ i ] [ j ] ; let flag = 0 , k ; for ( k = i + 1 ; k < N ; k ++ ) { if ( A [ k ] - A [ i ] > j + 1 ) break ; if ( A [ k ] - A [ i ] >= j - 1 && A [ k ] - A [ i ] <= j + 1 ) flag = check ( memo , k , A [ k ] - A [ i ] , A ) ; if ( flag != 0 ) break ; } memo [ i ] [ j ] = flag ; return memo [ i ] [ j ] ; } function checkEndReach ( A , K ) { let memo = new Array ( N ) ; for ( var i = 0 ; i < memo . length ; i ++ ) { memo [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) { memo [ i ] [ j ] = - 1 ; } } let startIndex = 1 ; if ( check ( memo , startIndex , K , A ) != 0 ) document . write ( " " ) ; else document . write ( " " ) ; } let A = [ 0 , 1 , 3 , 5 , 6 , 8 , 12 , 17 ] ; let K = 1 ; checkEndReach ( A , K ) ;
Maximum subsequence sum such that no K elements are consecutive | Function to find the maximum sum of a subsequence consisting of no K consecutive array elements ; Stores states of dp ; Stores the prefix sum ; Update the prefix sum ; Base case for i < K ; For indices less than k take all the elements ; For i >= K case ; Skip each element from i to ( i - K + 1 ) to ensure that no K elements are consecutive ; Update the current dp state ; dp [ N ] stores the maximum sum ; Given array arr [ ] ; Function Call
function Max_Sum ( arr , K , N ) { var dp = Array ( N + 1 ) . fill ( 0 ) ; var prefix = Array ( N + 1 ) ; prefix [ 0 ] = 0 ; for ( var i = 1 ; i <= N ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + arr [ i - 1 ] ; } dp [ 0 ] = 0 ; for ( var i = 1 ; i < K ; i ++ ) { dp [ i ] = prefix [ i ] ; } for ( var i = K ; i <= N ; ++ i ) { for ( var j = i ; j >= ( i - K + 1 ) ; j -- ) { dp [ i ] = Math . max ( dp [ i ] , dp [ j - 1 ] + prefix [ i ] - prefix [ j ] ) ; } } return dp [ N ] ; } var arr = [ 4 , 12 , 22 , 18 , 34 , 12 , 25 ] ; var N = arr . length ; var K = 5 ; document . write ( Max_Sum ( arr , K , N ) ) ;
Count possible splits of sum N into K integers such that the minimum is at least P | Function that finds the value of the Binomial Coefficient C ( n , k ) ; Stores the value of Binomial Coefficient in bottom up manner ; Base Case ; Find the value using previously stored values ; Return the value of C ( N , K ) ; Function that count the number of ways to divide N leto K letegers >= P such that their sum is N ; Update the value of N ; Find the binomial coefficient recursively ; Given K , N , and P
function binomialCoeff ( n , k ) { let C = new Array ( n + 1 ) ; for ( let i = 0 ; i < C . length ; i ++ ) { C [ i ] = new Array ( 2 ) ; } let i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } function waysToSplitN ( k , n , P ) { let new_N = n - k * P ; return binomialCoeff ( new_N + k - 1 , new_N ) ; } let K = 3 , N = 8 , P = 2 ; document . write ( waysToSplitN ( K , N , P ) ) ;
Minimum pair merge operations required to make Array non | Function to find the minimum operations to make the array Non - increasing ; Size of the array ; Dp table initialization ; dp [ i ] : Stores minimum number of operations required to make subarray { A [ i ] , ... , A [ N ] } non - increasing ; Increment the value of j ; Add current value to sum ; Update the dp tables ; Return the answer ; Driver Code
function solve ( a ) { let n = a . length ; let dp = new Array ( n + 1 ) . fill ( 0 ) ; let val = new Array ( n + 1 ) . fill ( 0 ) ; for ( let i = n - 1 ; i >= 0 ; i -- ) { let sum = a [ i ] ; let j = i ; while ( j + 1 < n && sum < val [ j + 1 ] ) { j ++ ; sum += a [ j ] ; } dp [ i ] = ( j - i ) + dp [ j + 1 ] ; val [ i ] = sum ; } return dp [ 0 ] ; } let arr = [ 1 , 5 , 3 , 9 , 1 ] ; document . write ( solve ( arr ) ) ;
Count of Distinct Substrings occurring consecutively in a given String | Function to count the distinct substrings placed consecutively in the given string ; Length of the string ; If length of the string does not exceed 1 ; Initialize a DP - table ; Stores the distinct substring ; Iterate from end of the string ; Iterate backward until dp table is all computed ; If character at i - th index is same as character at j - th index ; Update dp [ i ] [ j ] based on previously computed value ; Otherwise ; Condition for consecutively placed similar substring ; Return the count ; Driver Code
function distinctSimilarSubstrings ( str ) { var n = str . length ; if ( n <= 1 ) { return 0 ; } var dp = Array . from ( Array ( n + 1 ) , ( ) => Array ( n + 1 ) . fill ( 0 ) ) ; var substrings = new Set ( ) ; for ( var j = n - 1 ; j >= 0 ; j -- ) { for ( var i = j - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == str [ j ] ) { dp [ i ] [ j ] = dp [ i + 1 ] [ j + 1 ] + 1 ; } else { dp [ i ] [ j ] = 0 ; } if ( dp [ i ] [ j ] >= j - i ) { substrings . add ( str . substring ( i , j ) ) ; } } } return substrings . size ; } var str = " " ; document . write ( distinctSimilarSubstrings ( str ) ) ;
Finding shortest path between any two nodes using Floyd Warshall Algorithm | Javascript program to find the shortest path between any two nodes using Floyd Warshall Algorithm . ; Infinite value for array ; Initializing the distance and Next array ; No edge between node i and j ; Function construct the shortest path between u and v ; If there 's no path between  node u and v, simply return  an empty array ; Storing the path in a vector ; Standard Floyd Warshall Algorithm with little modification Now if we find that dis [ i ] [ j ] > dis [ i ] [ k ] + dis [ k ] [ j ] then we modify next [ i ] [ j ] = next [ i ] [ k ] ; We cannot travel through edge that doesn 't exist ; Print the shortest path ; Driver code ; Function to initialise the distance and Next array ; Calling Floyd Warshall Algorithm , this will update the shortest distance as well as Next array ; Path from node 1 to 3 ; Path from node 0 to 2 ; Path from node 3 to 2
let MAXN = 100 ; let INF = 1e7 ; let dis = new Array ( MAXN ) ; let Next = new Array ( MAXN ) ; for ( let i = 0 ; i < MAXN ; i ++ ) { dis [ i ] = new Array ( MAXN ) ; Next [ i ] = new Array ( MAXN ) ; } function initialise ( V , graph ) { for ( let i = 0 ; i < V ; i ++ ) { for ( let j = 0 ; j < V ; j ++ ) { dis [ i ] [ j ] = graph [ i ] [ j ] ; if ( graph [ i ] [ j ] == INF ) Next [ i ] [ j ] = - 1 ; else Next [ i ] [ j ] = j ; } } } function constructPath ( u , v ) { if ( Next [ u ] [ v ] == - 1 ) return null ; let path = [ ] ; path . push ( u ) ; while ( u != v ) { u = Next [ u ] [ v ] ; path . push ( u ) ; } return path ; } function floydWarshall ( V ) { for ( let k = 0 ; k < V ; k ++ ) { for ( let i = 0 ; i < V ; i ++ ) { for ( let j = 0 ; j < V ; j ++ ) { if ( dis [ i ] [ k ] == INF dis [ k ] [ j ] == INF ) continue ; if ( dis [ i ] [ j ] > dis [ i ] [ k ] + dis [ k ] [ j ] ) { dis [ i ] [ j ] = dis [ i ] [ k ] + dis [ k ] [ j ] ; Next [ i ] [ j ] = Next [ i ] [ k ] ; } } } } } function printPath ( path ) { let n = path . length ; for ( let i = 0 ; i < n - 1 ; i ++ ) document . write ( path [ i ] + " " ) ; document . write ( path [ n - 1 ] + " " ) ; } let V = 4 ; let graph = [ [ 0 , 3 , INF , 7 ] , [ 8 , 0 , 2 , INF ] , [ 5 , INF , 0 , 1 ] , [ 2 , INF , INF , 0 ] ] ; initialise ( V , graph ) ; floydWarshall ( V ) ; let path ; document . write ( " " ) ; path = constructPath ( 1 , 3 ) ; printPath ( path ) ; document . write ( " " ) ; path = constructPath ( 0 , 2 ) ; printPath ( path ) ; document . write ( " " ) ; path = constructPath ( 3 , 2 ) ; printPath ( path ) ;
Count sequences of length K having each term divisible by its preceding term | Stores the factors of i - th element in v [ i ] ; Function to find all the factors of N ; Iterate upto Math . sqrt ( N ) ; Function to return the count of sequences of length K having alet terms divisible by its preceding term ; Calculate factors of i ; Initialize dp [ 0 ] [ i ] = 0 : No subsequence of length 0 ending with i - th element exists ; Initialize dp [ 0 ] [ i ] = 1 : Only 1 subsequence of length 1 ending with i - th element exists ; Iterate [ 2 , K ] to obtain sequences of each length ; Calculate sum of alet dp [ i - 1 ] [ vp [ j ] [ k ] ] ; vp [ j ] [ k ] stores alet factors of j ; Store the sum in A [ i ] [ j ] ; Sum of alet dp [ K ] [ j ] obtain all K length sequences ending with j ; Driver Code
let vp = new Array ( 2009 ) ; function finding_factors ( n ) { let i , a ; for ( i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i == n ) { vp [ n ] . push ( i ) ; } else { vp [ n ] . push ( i ) ; vp [ n ] . push ( n / i ) ; } } } } function countSeq ( N , K ) { let i , j , k ; let dp = new Array ( 109 ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( i = 1 ; i <= N ; i ++ ) { finding_factors ( i ) ; dp [ 0 ] [ i ] = 0 ; dp [ 1 ] [ i ] = 1 ; } for ( i = 2 ; i <= K ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { let sum = 0 ; for ( k = 0 ; k < vp [ j ] . length ; k ++ ) { sum = ( sum + dp [ i - 1 ] [ vp [ j ] [ k ] ] ) ; } dp [ i ] [ j ] = sum ; } } let ans = 0 ; for ( j = 1 ; j <= N ; j ++ ) { ans = ( ans + dp [ K ] [ j ] ) ; } return ans ; } let N , K ; N = 3 ; K = 2 ; for ( let i = 0 ; i < vp . length ; i ++ ) vp [ i ] = [ ] ; document . write ( countSeq ( N , K ) + " " ) ;
Largest possible square submatrix with maximum AND value | Function to calculate and return the length of square submatrix with maximum AND value ; Extract dimensions ; Auxiliary array ; c : Stores the maximum value in the matrix p : Stores the number of elements in the submatrix having maximum AND value ; Iterate over the matrix to fill the auxiliary matrix ; Find the max element in the matrix side by side ; Fill first row and column with 1 's ; For every cell , check if the elements at the left , top and top left cells from the current cell are equal or not ; Store the minimum possible submatrix size these elements are part of ; Store 1 otherwise ; Checking maximum value ; If the maximum AND value occurs more than once ; Update the maximum size of submatrix ; Final output ; Driver code
function MAX_value ( arr ) { var row = arr . length ; var col = arr [ 0 ] . length ; var dp = Array ( row ) ; for ( var i = 0 ; i < row ; i ++ ) dp [ i ] = Array ( col ) . fill ( 0 ) ; var i = 0 , j = 0 ; var c = arr [ 0 ] [ 0 ] , p = 0 ; var d = row ; for ( i = 0 ; i < d ; i ++ ) { for ( j = 0 ; j < d ; j ++ ) { if ( c < arr [ i ] [ j ] ) { c = arr [ i ] [ j ] ; } if ( i == 0 j == 0 ) { dp [ i ] [ j ] = 1 ; } else { if ( arr [ i - 1 ] [ j - 1 ] == arr [ i ] [ j ] && arr [ i - 1 ] [ j ] == arr [ i ] [ j ] && arr [ i ] [ j - 1 ] == arr [ i ] [ j ] ) { dp [ i ] [ j ] = Math . min ( dp [ i - 1 ] [ j - 1 ] , Math . min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ) + 1 ; } else { dp [ i ] [ j ] = 1 ; } } } } for ( i = 0 ; i < d ; i ++ ) { for ( j = 0 ; j < d ; j ++ ) { if ( arr [ i ] [ j ] == c ) { if ( p < dp [ i ] [ j ] ) { p = dp [ i ] [ j ] ; } } } } return p * p ; } var arr = [ [ 9 , 9 , 3 , 3 , 4 , 4 ] , [ 9 , 9 , 7 , 7 , 7 , 4 ] , [ 1 , 2 , 7 , 7 , 7 , 4 ] , [ 4 , 4 , 7 , 7 , 7 , 4 ] , [ 5 , 5 , 1 , 1 , 2 , 7 ] , [ 2 , 7 , 1 , 1 , 4 , 4 ] ] ; document . write ( MAX_value ( arr ) + " " ) ;
Smallest power of 2 consisting of N digits | Function to return smallest power of 2 with N digits ; Iterate through all powers of 2 ; Driver Code
function smallestNum ( n ) { res = 1 ; for ( var i = 2 ; ; i *= 2 ) { var length = parseInt ( Math . log ( i ) / Math . log ( 10 ) ) + 1 ; if ( length == n ) return parseInt ( Math . log ( i ) / Math . log ( 2 ) ) ; } } n = 4 ; document . write ( smallestNum ( n ) ) ;
Count Possible Decodings of a given Digit Sequence in O ( N ) time and Constant Auxiliary space | A Dynamic Programming based function to count decodings in digit sequence ; For base condition "01123" should return 0 ; Using last two calculated values , calculate for ith index ; Change bool to int ; Return the required answer ; Driver Code ; Function call
function countDecodingDP ( digits , n ) { if ( digits [ 0 ] == ' ' ) return 0 ; var count0 = 1 , count1 = 1 , count2 ; for ( var i = 2 ; i <= n ; i ++ ) { count2 = ( ( digits [ i - 1 ] != ' ' ) * count1 ) + ( ( digits [ i - 2 ] == ' ' ) || ( digits [ i - 2 ] == ' ' && digits [ i - 1 ] < ' ' ) ) * count0 ; count0 = count1 ; count1 = count2 ; } return count1 ; } var digits = " " ; var n = digits . length ; document . write ( countDecodingDP ( digits , n ) ) ;
Minimize total cost without repeating same task in two consecutive iterations | Function to return the minimum cost for N iterations ; Construct the dp table ; 1 st row of dp table will be equal to the 1 st of cost matrix ; Iterate through all the rows ; To iterate through the columns of current row ; Initialize val as infinity ; To iterate through the columns of previous row ; Fill the dp matrix ; Returning the minimum value ; Number of iterations ; Number of tasks ; Cost matrix
function findCost ( cost_mat , N , M ) { var dp = Array ( N ) ; for ( i = 0 ; i < N ; i ++ ) dp [ i ] = Array ( M ) . fill ( 0 ) ; for ( i = 0 ; i < M ; i ++ ) dp [ 0 ] [ i ] = cost_mat [ 0 ] [ i ] ; for ( row = 1 ; row < N ; row ++ ) { for ( curr_col = 0 ; curr_col < M ; curr_col ++ ) { var val = 999999999 ; for ( prev_col = 0 ; prev_col < M ; prev_col ++ ) { if ( curr_col != prev_col ) val = Math . min ( val , dp [ row - 1 ] [ prev_col ] ) ; } dp [ row ] [ curr_col ] = val + cost_mat [ row ] [ curr_col ] ; } } var ans = Number . MAX_VALUE ; for ( i = 0 ; i < M ; i ++ ) ans = Math . min ( ans , dp [ N - 1 ] [ i ] ) ; return ans ; } var N = 4 ; var M = 4 ; var cost_mat = [ [ 4 , 5 , 3 , 2 ] , [ 6 , 2 , 8 , 1 ] , [ 6 , 2 , 2 , 1 ] , [ 0 , 5 , 5 , 1 ] ] ; document . write ( findCost ( cost_mat , N , M ) ) ;
Split the given string into Primes : Digit DP | Function to precompute all the primes upto 1000000 and store it in a set using Sieve of Eratosthenes ; Here to_String ( ) is used for converting int to String ; A function to find the minimum number of segments the given String can be divided such that every segment is a prime ; Declare a splitdp [ ] array and initialize to - 1 ; Call sieve function to store primes in primes array ; Build the DP table in a bottom - up manner ; If the prefix is prime then the prefix will be found in the prime set ; If the Given Prefix can be split into Primes then for the remaining String from i to j Check if Prime . If yes calculate the minimum split till j ; To check if the subString from i to j is a prime number or not ; If it is a prime , then update the dp array ; Return the minimum number of splits for the entire String ; Driver Code
function getPrimesFromSeive ( primes ) { let prime = new Array ( 1000001 ) ; for ( let i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = prime [ 1 ] = false ; for ( let i = 2 ; i * i <= 1000000 ; i ++ ) { if ( prime [ i ] == true ) { for ( let j = i * i ; j <= 1000000 ; j += i ) prime [ j ] = false ; } } for ( let i = 2 ; i <= 1000000 ; i ++ ) { if ( prime [ i ] == true ) primes . add ( ( i ) . toString ( ) ) ; } } function splitIntoPrimes ( number ) { let numLen = number . length ; let splitDP = new Array ( numLen + 1 ) ; for ( let i = 0 ; i < splitDP . length ; i ++ ) { splitDP [ i ] = - 1 ; } let primes = new Set ( ) ; getPrimesFromSeive ( primes ) ; for ( let i = 1 ; i <= numLen ; i ++ ) { if ( i <= 6 && ( primes . has ( number . substring ( 0 , i ) ) ) ) splitDP [ i ] = 1 ; if ( splitDP [ i ] != - 1 ) { for ( let j = 1 ; j <= 6 && i + j <= numLen ; j ++ ) { if ( primes . has ( number . substring ( i , i + j ) ) ) { if ( splitDP [ i + j ] == - 1 ) splitDP [ i + j ] = 1 + splitDP [ i ] ; else splitDP [ i + j ] = Math . min ( splitDP [ i + j ] , 1 + splitDP [ i ] ) ; } } } } return splitDP [ numLen ] ; } document . write ( splitIntoPrimes ( " " ) + " " ) ; document . write ( splitIntoPrimes ( " " ) + " " ) ;
Number of ways to convert a character X to a string Y | JavaScript implementation of the approach ; Function to find the modular - inverse ; While power > 1 ; Updating s and a ; Updating power ; Return the final answer ; Function to return the count of ways ; To store the final answer ; To store pre - computed factorials ; Computing factorials ; Loop to find the occurrences of x and update the ans ; Multiplying the answer by ( n - 1 ) ! ; Return the final answer ; Driver Code
let MOD = 1000000007 ; function modInv ( a ) { let p = MOD - 2 ; let s = 1 ; while ( p != 1 ) { if ( p % 2 == 1 ) s = ( s * a ) % MOD ; a = ( a * a ) % MOD ; p = parseInt ( p / 2 , 10 ) ; } return ( a * s ) % MOD ; } function findCnt ( x , y ) { let ans = 0 ; let fact = new Array ( y . length + 1 ) ; fact . fill ( 0 ) ; for ( let i = 0 ; i < y . length + 1 ; i ++ ) fact [ i ] = 1 ; for ( let i = 1 ; i <= y . length ; i ++ ) fact [ i ] = ( fact [ i - 1 ] * i ) % MOD ; for ( let i = 0 ; i < y . length ; i ++ ) { if ( y [ i ] == x ) { ans += ( modInv ( fact [ i ] ) * modInv ( fact [ y . length - i - 1 ] ) ) % MOD ; ans %= MOD ; } } ans *= fact [ ( y . length - 1 ) ] * 0 ; ans = ( ans + 6 ) % MOD ; return ans ; } let x = ' ' ; let y = " " ; document . write ( findCnt ( x , y ) ) ;
Find the Largest divisor Subset in the Array | Function to find the required subsequence ; Sort the array ; Keep a count of the length of the subsequence and the previous element ; Set the initial values ; Maximum length of the subsequence and the last element ; Run a loop for every element ; Check for all the divisors ; If the element is a divisor and the length of subsequence will increase by adding j as previous element of i ; Increase the count ; Update the max count ; Get the last index of the subsequence ; Print the element ; Move the index to the previous element ; Driver Code
function findSubSeq ( arr , n ) { arr . sort ( ) ; var count = new Array ( n ) ; var prev = new Array ( n ) ; count . fill ( 1 ) ; prev . fill ( - 1 ) ; var max = 0 ; var maxprev = - 1 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] % arr [ j ] == 0 && count [ j ] + 1 > count [ i ] ) { count [ i ] = count [ j ] + 1 ; prev [ i ] = j ; } } if ( max < count [ i ] ) { max = count [ i ] ; maxprev = i ; } } var i = maxprev ; while ( i >= 0 ) { if ( arr [ i ] != - 1 ) document . write ( arr [ i ] + " " ) ; i = prev [ i ] ; } } var arr = [ 1 , 2 , 3 , 4 , 5 ] ; var n = arr . length ; findSubSeq ( arr , n ) ;
Optimally accommodate 0 s and 1 s from a Binary String into K buckets | Function to find the minimum required sum using dynamic programming ; dp [ i ] [ j ] = minimum val of accommodation till j 'th index of the string  using i+1 number of buckets.  Final ans will be in dp[n-1][K-1] ; Corner cases ; Filling first row , if only 1 bucket then simple count number of zeros and ones and do the multiplication ; If k = 0 then this arrangement is not possible ; If no arrangement is possible then our answer will remain INT_MAX so return - 1 ; Driver code ; K buckets
function solve ( str , K ) { let n = str . length ; let dp = new Array ( K ) ; for ( let i = 0 ; i < K ; i ++ ) { dp [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] = 0 ; } } if ( n < K ) return - 1 ; else if ( n == K ) return 0 ; let zeroes = 0 , ones = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) zeroes ++ ; else ones ++ ; dp [ 0 ] [ i ] = ones * zeroes ; } for ( let s = 1 ; s < K ; s ++ ) { for ( let i = 0 ; i < n ; i ++ ) { dp [ s ] [ i ] = Number . MAX_VALUE ; ones = 0 ; zeroes = 0 ; for ( let k = i ; k >= 0 ; k -- ) { if ( str [ k ] == ' ' ) zeroes ++ ; else ones ++ ; dp [ s ] [ i ] = Math . min ( dp [ s ] [ i ] , + ( ( k - 1 >= 0 ) ? ones * zeroes + dp [ s - 1 ] [ k - 1 ] : Number . MAX_VALUE ) ) ; } } } return ( dp [ K - 1 ] [ n - 1 ] == Number . MAX_VALUE ) ? - 1 : dp [ K - 1 ] [ n - 1 ] ; } let S = " " ; let K = 2 ; document . write ( solve ( S , K ) ) ;
Queries for bitwise AND in the index range [ L , R ] of the given array | Javascript implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix count ; Function to answer query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; Condition for ith bit of answer to be set ; Driver code
let MAX = 100000 ; let bitscount = 32 ; let prefix_count = new Array ( bitscount ) ; for ( let i = 0 ; i < bitscount ; i ++ ) { prefix_count [ i ] = new Array ( MAX ) ; for ( let j = 0 ; j < MAX ; j ++ ) { prefix_count [ i ] [ j ] = 0 ; } } function findPrefixCount ( arr , n ) { for ( let i = 0 ; i < bitscount ; i ++ ) { prefix_count [ i ] [ 0 ] = ( ( arr [ 0 ] >> i ) & 1 ) ; for ( let j = 1 ; j < n ; j ++ ) { prefix_count [ i ] [ j ] = ( ( arr [ j ] >> i ) & 1 ) ; prefix_count [ i ] [ j ] += prefix_count [ i ] [ j - 1 ] ; } } } function rangeAnd ( l , r ) { let ans = 0 ; for ( let i = 0 ; i < bitscount ; i ++ ) { let x ; if ( l == 0 ) x = prefix_count [ i ] [ r ] ; else x = prefix_count [ i ] [ r ] - prefix_count [ i ] [ l - 1 ] ; if ( x == r - l + 1 ) ans = ( ans | ( 1 << i ) ) ; } return ans ; } let arr = [ 7 , 5 , 3 , 5 , 2 , 3 ] ; let n = arr . length ; findPrefixCount ( arr , n ) ; let queries = [ [ 1 , 3 ] , [ 4 , 5 ] ] ; let q = queries . length ; for ( let i = 0 ; i < q ; i ++ ) document . write ( rangeAnd ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) + " " ) ;
Longest path in a directed Acyclic graph | Dynamic Programming | Function to traverse the DAG and apply Dynamic Programming to find the longest path ; Mark as visited ; Traverse for all its children ; If not visited ; Store the max of the paths ; Function to add an edge ; Function that returns the longest path ; Dp array ; Visited array to know if the node has been visited previously or not ; Call DFS for every unvisited vertex ; Traverse and find the maximum of all dp [ i ] ; Driver Code ; Example - 1
function dfs ( node , adj , dp , vis ) { vis [ node ] = true ; for ( var i = 0 ; i < adj [ node ] . length ; i ++ ) { if ( ! vis [ adj [ node ] [ i ] ] ) dfs ( adj [ node ] [ i ] , adj , dp , vis ) ; dp [ node ] = Math . max ( dp [ node ] , 1 + dp [ adj [ node ] [ i ] ] ) ; } } function addEdge ( adj , u , v ) { adj [ u ] . push ( v ) ; } function findLongestPath ( adj , n ) { var dp = Array ( n + 1 ) . fill ( 0 ) ; var vis = Array ( n + 1 ) . fill ( false ) ; for ( var i = 1 ; i <= n ; i ++ ) { if ( ! vis [ i ] ) dfs ( i , adj , dp , vis ) ; } var ans = 0 ; for ( var i = 1 ; i <= n ; i ++ ) { ans = Math . max ( ans , dp [ i ] ) ; } return ans ; } var n = 5 ; var adj = Array . from ( Array ( n + 1 ) , ( ) => Array ( ) ) ; addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 1 , 3 ) ; addEdge ( adj , 3 , 2 ) ; addEdge ( adj , 2 , 4 ) ; addEdge ( adj , 3 , 4 ) ; document . write ( findLongestPath ( adj , n ) ) ;