codeparrot/xlcost-text-to-code huggingface dataset:
Maximum sum among all ( a x b ) submatrices for given Q queries | Function to preprcess input mat [ N ] [ M ] . This function mainly fills dp [ N ] [ M ] such that dp [ i ] [ j ] stores sum of elements from ( 0 , 0 ) to ( i , j ) ; Copy first row of mat [ ] [ ] to dp [ ] [ ] ; Do column wise sum ; Do row wise sum ; A O ( 1 ) time function to compute sum of submatrix between ( tli , tlj ) and ( rbi , rbj ) using dp [ ] [ ] which is built by the preprocess function ; Result is now sum of elements between ( 0 , 0 ) and ( rbi , rbj ) ; Remove elements between ( 0 , 0 ) and ( tli - 1 , rbj ) ; Remove elements between ( 0 , 0 ) and ( rbi , tlj - 1 ) ; Add dp [ tli - 1 ] [ tlj - 1 ] as elements between ( 0 , 0 ) and ( tli - 1 , tlj - 1 ) are subtracted twice ; Function to find the maximum sum among all ( a x b ) sub - matrices of the matrix ; Function call ; Run a loop for finding answer for all queries ; Given input ; Function call ; Print answer for all queries
function preProcess ( mat , dp , n , m ) { for ( let i = 0 ; i < m ; i ++ ) { dp [ 0 ] [ i ] = mat [ 0 ] [ i ] ; } for ( let i = 1 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + mat [ i ] [ j ] ; } } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 1 ; j < m ; j ++ ) { dp [ i ] [ j ] += dp [ i ] [ j - 1 ] ; } } } function sumQuery ( dp , tli , tlj , rbi , rbj ) { let res = dp [ rbi ] [ rbj ] ; if ( tli > 0 ) res = res - dp [ tli - 1 ] [ rbj ] ; if ( tlj > 0 ) res = res - dp [ rbi ] [ tlj - 1 ] ; if ( tli > 0 && tlj > 0 ) res = res + dp [ tli - 1 ] [ tlj - 1 ] ; return res ; } function maxSubMatrixSumQueries ( mat , n , m , queries , q ) { let dp = Array ( n ) . fill ( ) . map ( ( ) => Array ( m ) ) ; preProcess ( mat , dp , n , m ) ; let maxSum = new Array ( queries . length ) . fill ( 0 ) ; for ( let qi = 0 ; qi < q ; qi ++ ) { for ( let i = 0 ; i < n - queries [ qi ] [ 0 ] + 1 ; i ++ ) { for ( let j = 0 ; j < m - queries [ qi ] [ 1 ] + 1 ; j ++ ) { maxSum [ qi ] = Math . max ( maxSum [ qi ] , sumQuery ( dp , i , j , i + queries [ qi ] [ 0 ] - 1 , j + queries [ qi ] [ 1 ] - 1 ) ) ; } } } return maxSum ; } let n = 3 , m = 4 ; let mat = [ [ 1 , 2 , 3 , 9 ] , [ 4 , 5 , 6 , 2 ] , [ 8 , 3 , 2 , 6 ] ] ; let Q = 3 ; let Queries = [ [ 1 , 1 ] , [ 2 , 2 ] , [ 3 , 3 ] ] ; let maxSum = maxSubMatrixSumQueries ( mat , n , m , Queries , Q ) ; for ( let i = 0 ; i < Q ; i ++ ) { document . write ( maxSum [ i ] + " " ) ; }
Number of ways such that only K bars are visible from the left | dp array ; Function to calculate the number of permutations of N , where only K bars are visible from the left . ; If subproblem has already been calculated , return ; Only ascending order is possible ; N is placed at the first position The nest N - 1 are arranged in ( N - 1 ) ! ways ; Recursing ; Input ; Function call
let dp = new Array ( 1005 ) . fill ( 0 ) . map ( ( ) => new Array ( 1005 ) . fill ( - 1 ) ) ; function KvisibleFromLeft ( N , K ) { if ( dp [ N ] [ K ] != - 1 ) return dp [ N ] [ K ] ; if ( N == K ) return dp [ N ] [ K ] = 1 ; if ( K == 1 ) { let ans = 1 ; for ( let i = 1 ; i < N ; i ++ ) ans *= i ; return dp [ N ] [ K ] = ans ; } return dp [ N ] [ K ] = KvisibleFromLeft ( N - 1 , K - 1 ) + ( N - 1 ) * KvisibleFromLeft ( N - 1 , K ) ; } let N = 5 , K = 2 ; document . write ( KvisibleFromLeft ( N , K ) + " " )
Number of distinct words of size N with at most K contiguous vowels | Power function to calculate long powers with mod ; Function for finding number of ways to create string with length N and atmost K contiguous vowels ; Array dp to store number of ways ; dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] . . dp [ i - 1 ] [ k ] ) * 21 ; Now setting sum to be dp [ i ] [ 0 ] ; If j > i , no ways are possible to create a string with length i and vowel j ; If j = i all the character should be vowel ; dp [ i ] [ j ] relation with dp [ i - 1 ] [ j - 1 ] ; Adding dp [ i ] [ j ] in the sum ; Input ; Function Call
function power ( x , y , p ) { let res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } function kvowelwords ( N , K ) { let i , j ; let MOD = 1000000007 ; let dp = new Array ( N + 1 ) for ( i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( K + 1 ) ; } let sum = 1 ; for ( i = 1 ; i <= N ; i ++ ) { dp [ i ] [ 0 ] = sum * 21 ; dp [ i ] [ 0 ] %= MOD ; sum = dp [ i ] [ 0 ] ; for ( j = 1 ; j <= K ; j ++ ) { if ( j > i ) dp [ i ] [ j ] = 0 ; else if ( j == i ) { dp [ i ] [ j ] = power ( 5 , i , MOD ) ; } else { dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] * 5 ; } dp [ i ] [ j ] %= MOD ; sum += dp [ i ] [ j ] ; sum %= MOD ; } } return sum ; } let N = 3 ; let K = 3 ; document . write ( kvowelwords ( N , K ) ) ;
Maximize the length of upper boundary formed by placing given N rectangles horizontally or vertically | Function to find maximum length of the upper boundary formed by placing each of the rectangles either horizontally or vertically ; Stores the intermediate transition states ; Place the first rectangle horizontally ; Place the first rectangle vertically ; Place horizontally ; Stores the difference in height of current and previous rectangle ; Take maximum out of two options ; Place Vertically ; Stores the difference in height of current and previous rectangle ; Take maximum out two options ; Print maximum of horizontal or vertical alignment of the last rectangle ; Driver Code
function maxBoundary ( N , V ) { let dp = new Array ( N ) . fill ( 0 ) . map ( ( ) => new Array ( 2 ) . fill ( 0 ) ) ; dp [ 0 ] [ 0 ] = V [ 0 ] [ 0 ] ; dp [ 0 ] [ 1 ] = V [ 0 ] [ 1 ] ; for ( let i = 1 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = V [ i ] [ 0 ] ; let height1 = Math . abs ( V [ i - 1 ] [ 1 ] - V [ i ] [ 1 ] ) ; let height2 = Math . abs ( V [ i - 1 ] [ 0 ] - V [ i ] [ 1 ] ) ; dp [ i ] [ 0 ] += Math . max ( height1 + dp [ i - 1 ] [ 0 ] , height2 + dp [ i - 1 ] [ 1 ] ) ; dp [ i ] [ 1 ] = V [ i ] [ 1 ] ; let vertical1 = Math . abs ( V [ i ] [ 0 ] - V [ i - 1 ] [ 1 ] ) ; let vertical2 = Math . abs ( V [ i ] [ 0 ] - V [ i - 1 ] [ 0 ] ) ; dp [ i ] [ 1 ] += Math . max ( vertical1 + dp [ i - 1 ] [ 0 ] , vertical2 + dp [ i - 1 ] [ 1 ] ) ; } document . write ( Math . max ( dp [ N - 1 ] [ 0 ] , dp [ N - 1 ] [ 1 ] ) ) ; } let N = 5 ; let V = [ [ 2 , 5 ] , [ 3 , 8 ] , [ 1 , 10 ] , [ 7 , 14 ] , [ 2 , 5 ] , ] ; maxBoundary ( N , V ) ;
Ways to sum to N using Natural Numbers up to K with repetitions allowed | Function to find the total number of ways to represent N as the sum of integers over the range [ 1 , K ] ; Initialize a list ; Update dp [ 0 ] to 1 ; Iterate over the range [ 1 , K + 1 ] ; Iterate over the range [ 1 , N + 1 ] ; If col is greater than or equal to row ; Update current dp [ col ] state ; Return the total number of ways ; Given inputs
function NumberOfways ( N , K ) { let dp = Array . from ( { length : N + 1 } , ( _ , i ) => 0 ) ; dp [ 0 ] = 1 ; for ( let row = 1 ; row < K + 1 ; row ++ ) { for ( let col = 1 ; col < N + 1 ; col ++ ) { if ( col >= row ) dp [ col ] = dp [ col ] + dp [ col - row ] ; } } return ( dp [ N ] ) ; } let N = 8 ; let K = 2 ; document . write ( NumberOfways ( N , K ) ) ;
Queries to check if array elements from indices [ L , R ] forms an Arithmetic Progression or not | javascript program for the above approach Function to check if the given range of queries form an AP or not in the given array arr ; Stores length of the longest subarray forming AP for every array element ; Iterate over the range [ 0 , N ] ; Stores the index of the last element of forming AP ; Iterate until the element at index ( j , j + 1 ) forms AP ; Increment j by 1 ; Traverse the current subarray over the range [ i , j - 1 ] ; Update the length of the longest subarray at index k ; Update the value of i ; Traverse the given queries ; Prvar the result ; Otherwise ; Driver Code
function findAPSequence ( arr , N , Q , M ) { var dp = Array ( N + 5 ) . fill ( 0 ) ; for ( i = 0 ; i + 1 < N ; ) { var j = i + 1 ; while ( j + 1 < N && arr [ j + 1 ] - arr [ j ] == arr [ i + 1 ] - arr [ i ] ) j ++ ; for ( k = i ; k < j ; k ++ ) { dp [ k ] = j - k ; } i = j ; } for ( i = 0 ; i < M ; i ++ ) { if ( dp [ Q [ i ] [ 0 ] ] >= Q [ i ] [ 1 ] - Q [ i ] [ 0 ] ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } } var arr = [ 1 , 3 , 5 , 7 , 6 , 5 , 4 , 1 ] ; var Q = [ [ 0 , 3 ] , [ 3 , 4 ] , [ 2 , 4 ] ] ; var N = arr . length ; var M = Q . length ; findAPSequence ( arr , N , Q , M ) ;
Minimize difference between sum of two K | Stores the values at recursive states ; Function to find the minimum difference between sum of two K - length subsets ; Base Case ; If k1 and k2 are 0 , then return the absolute difference between sum1 and sum2 ; Otherwise , return INT_MAX ; If the result is already computed , return the result ; Store the 3 options ; Including the element in first subset ; Including the element in second subset ; Not including the current element in both the subsets ; Store minimum of 3 values obtained ; Return the value for the current states ; Driver Code
let dp = new Array ( 100 ) ; for ( let i = 0 ; i < 100 ; i ++ ) { dp [ i ] = new Array ( 100 ) ; for ( let j = 0 ; j < 100 ; j ++ ) { dp [ i ] [ j ] = new Array ( 100 ) ; for ( let k = 0 ; k < 100 ; k ++ ) dp [ i ] [ j ] [ k ] = - 1 ; } } function minSumDifference ( arr , n , k1 , k2 , sum1 , sum2 ) { if ( n < 0 ) { if ( k1 == 0 && k2 == 0 ) { return Math . abs ( sum1 - sum2 ) ; } else { return Number . MAX_VALUE ; } } if ( dp [ n ] [ sum1 ] [ sum2 ] != - 1 ) { return dp [ n ] [ sum1 ] [ sum2 ] ; } let op1 = Number . MAX_VALUE ; let op2 = Number . MAX_VALUE ; let op3 = Number . MAX_VALUE ; if ( k1 > 0 ) { op1 = minSumDifference ( arr , n - 1 , k1 - 1 , k2 , sum1 + arr [ n ] , sum2 ) ; } if ( k2 > 0 ) { op2 = minSumDifference ( arr , n - 1 , k1 , k2 - 1 , sum1 , sum2 + arr [ n ] ) ; } op3 = minSumDifference ( arr , n - 1 , k1 , k2 , sum1 , sum2 ) ; dp [ n ] [ sum1 ] [ sum2 ] = Math . min ( op1 , Math . min ( op2 , op3 ) ) ; return dp [ n ] [ sum1 ] [ sum2 ] ; } let arr = [ 12 , 3 , 5 , 6 , 7 , 17 ] ; let K = 2 ; let N = arr . length ; document . write ( minSumDifference ( arr , N - 1 , K , K , 0 , 0 ) ) ;
Geek | Function to calculate the N - th Geek - onacci Number ; Stores the geekonacci series ; Store the first three terms of the series ; Iterate over the range [ 3 , N ] ; Update the value of arr [ i ] as the sum of previous 3 terms in the series ; Return the last element of arr [ ] as the N - th term ; Driver code
function find ( A , B , C , N ) { let arr = new Array ( N ) . fill ( 0 ) ; arr [ 0 ] = A ; arr [ 1 ] = B ; arr [ 2 ] = C ; for ( let i = 3 ; i < N ; i ++ ) { arr [ i ] = arr [ i - 1 ] + arr [ i - 2 ] + arr [ i - 3 ] ; } return arr [ N - 1 ] ; } let A = 1 , B = 3 , C = 2 , N = 4 ; document . write ( find ( A , B , C , N ) ) ;
Minimum days required to cure N persons | Function to find minimum count of days required to give a cure such that the high risk person and risk person does not get a dose on same day . ; Stores count of persons whose age is less than or equal to 10 and greater than or equal to 60. ; Stores the count of persons whose age is in the range [ 11 , 59 ] ; Traverse the array arr ; If age less than or equal to 10 or greater than or equal to 60 ; Update risk ; Update normal_risk ; Calculate days to cure risk and normal_risk persons ; Print the days ; Given array ; Size of the array ; Given P
function daysToCure ( arr , N , P ) { var risk = 0 ; var normal_risk = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= 60 arr [ i ] <= 10 ) { risk ++ ; } else { normal_risk ++ ; } } var days = parseInt ( ( risk / P ) + ( normal_risk / P ) ) ; if ( risk % P > 0 ) { days ++ ; } if ( normal_risk % P > 0 ) { days ++ ; } document . write ( days ) ; } var arr = [ 9 , 80 , 27 , 72 , 79 ] ; var N = arr . length ; var P = 2 ; daysToCure ( arr , N , P ) ;
Count numbers from a given range whose product of digits is K | Javascript program to implement the above approach ; Function to count numbers in the range [ 0 , X ] whose product of digit is K ; If count of digits in a number greater than count of digits in X ; If product of digits of a number equal to K ; If overlapping subproblems already occurred ; Stores count of numbers whose product of digits is K ; Check if the numbers exceeds K or not ; Iterate over all possible value of i - th digits ; If number contains leading 0 ; Update res ; Update res ; Return res ; Utility function to count the numbers in the range [ L , R ] whose prod of digits is K ; Stores numbers in the form of String ; Stores overlapping subproblems ; Initialize dp [ ] [ ] [ ] to - 1 ; Stores count of numbers in the range [ 0 , R ] whose product of digits is k ; Update str ; Initialize dp [ ] [ ] [ ] to - 1 ; Stores count of numbers in the range [ 0 , L - 1 ] whose product of digits is k ; Driver Code
let M = 100 ; function cntNum ( X , i , prod , K , st , tight , dp ) { if ( i >= X . length prod > K ) { return prod == K ? 1 : 0 ; } if ( dp [ prod ] [ i ] [ tight ] [ st ] != - 1 ) { return dp [ prod ] [ i ] [ tight ] [ st ] ; } let res = 0 ; let end = tight > 0 ? X [ i ] - ' ' : 9 ; for ( let j = 0 ; j <= end ; j ++ ) { if ( j == 0 && st == 0 ) { res += cntNum ( X , i + 1 , prod , K , 0 , ( tight & ( ( j == end ) ? 1 : 0 ) ) , dp ) ; } else { res += cntNum ( X , i + 1 , prod * j , K , 1 , ( tight & ( ( j == end ) ? 1 : 0 ) ) , dp ) ; } } return dp [ prod ] [ i ] [ tight ] [ st ] = res ; } function UtilCntNumRange ( L , R , K ) { let str = ( R ) . toString ( ) ; let dp = new Array ( M ) ; for ( let i = 0 ; i < M ; i ++ ) { dp [ i ] = new Array ( M ) ; for ( let j = 0 ; j < M ; j ++ ) { dp [ i ] [ j ] = new Array ( 2 ) ; for ( let k = 0 ; k < 2 ; k ++ ) { dp [ i ] [ j ] [ k ] = new Array ( 2 ) ; for ( let l = 0 ; l < 2 ; l ++ ) dp [ i ] [ j ] [ k ] [ l ] = - 1 ; } } } let cntR = cntNum ( str , 0 , 1 , K , 0 , 1 , dp ) ; str = ( L - 1 ) . toString ( ) ; for ( let i = 0 ; i < M ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { for ( let k = 0 ; k < 2 ; k ++ ) for ( let l = 0 ; l < 2 ; l ++ ) dp [ i ] [ j ] [ k ] [ l ] = - 1 ; } } let cntL = cntNum ( str , 0 , 1 , K , 0 , 1 , dp ) ; return ( cntR - cntL ) ; } let L = 20 , R = 10000 , K = 14 ; document . write ( UtilCntNumRange ( L , R , K ) ) ;
Count all possible N | Function to find the number of vowel permutations possible ; To avoid the large output value ; Initialize 2D dp array ; Initialize dp [ 1 ] [ i ] as 1 since string of length 1 will consist of only one vowel in the string ; Directed graph using the adjacency matrix ; Iterate over the range [ 1 , N ] ; Traverse the directed graph ; Traversing the list ; Update dp [ i + 1 ] [ u ] ; Stores total count of permutations ; Return count of permutations ; Driver code
function countVowelPermutation ( n ) { let MOD = ( 1e9 + 7 ) ; let dp = new Array ( n + 1 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < 5 ; i ++ ) { dp [ 1 ] [ i ] = 1 ; } let relation = [ [ 1 ] , [ 0 , 2 ] , [ 0 , 1 , 3 , 4 ] , [ 2 , 4 ] , [ 0 ] ] ; for ( let i = 1 ; i < n ; i ++ ) { for ( let u = 0 ; u < 5 ; u ++ ) { dp [ i + 1 ] [ u ] = 0 ; for ( let v in relation [ u ] ) { dp [ i + 1 ] [ u ] += dp [ i ] [ v ] % MOD ; } } } let ans = 0 ; for ( let i = 0 ; i < 5 ; i ++ ) { ans = ( ans + dp [ n ] [ i ] ) % MOD ; } return ans ; } let N = 2 ; document . write ( countVowelPermutation ( N ) ) ;
Queries to calculate Bitwise OR of each subtree of a given node in an N | Maximum Number of nodes ; Adjacency list ; Stores Bitwise OR of each node ; Function to add edges to the Tree ; Traverse the edges ; Add edges ; Function to perform DFS Traversal on the given tree ; Initialize answer with bitwise OR of current node ; Iterate over each child of the current node ; Skip parent node ; Call DFS for each child ; Taking bitwise OR of the answer of the child to find node 's OR value ; Function to call DFS from the '= root for precomputing answers ; Function to calculate and print the Bitwise OR for Q queries ; Perform preprocessing ; Iterate over each given query ; Utility function to find and print bitwise OR for Q queries ; Function to add edges to graph ; Function call ; Number of nodes ; Function call
const N = 1e5 + 5 ; let adj = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { adj . push ( [ ] ) } let answer = new Array ( N ) ; function addEdgesToGraph ( Edges , N ) { for ( let i = 0 ; i < N - 1 ; i ++ ) { let u = Edges [ i ] [ 0 ] ; let v = Edges [ i ] [ 1 ] ; adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } } function DFS ( node , parent , Val ) { answer [ node ] = Val [ node ] ; for ( let child of adj [ node ] ) { if ( child == parent ) continue ; DFS ( child , node , Val ) ; answer [ node ] = ( answer [ node ] answer [ child ] ) ; } } function preprocess ( Val ) { DFS ( 1 , - 1 , Val ) ; } function findSubtreeOR ( Queries , Q , Val ) { preprocess ( Val ) ; for ( let i = 0 ; i < Q ; i ++ ) { document . write ( answer [ Queries [ i ] ] + ' ' ) ; } } function findSubtreeORUtil ( N , Edges , Val , Queries , Q ) { addEdgesToGraph ( Edges , N ) ; findSubtreeOR ( Queries , Q , Val ) ; } let n = 5 ; let Edges = [ [ 1 , 2 ] , [ 1 , 3 ] , [ 3 , 4 ] , [ 3 , 5 ] ] ; let Val = [ 0 , 2 , 3 , 4 , 8 , 16 ] ; let Queries = [ 2 , 3 , 1 ] ; let Q = Queries . length ; findSubtreeORUtil ( n , Edges , Val , Queries , Q ) ;
Maximize sum of K elements selected from a Matrix such that each selected element must be preceded by selected row elements | Function to return the maximum of two elements ; Function to find the maximum sum of selecting K elements from the given 2D array arr [ ] [ ] ; dp table of size ( K + 1 ) * ( N + 1 ) ; Initialize dp [ 0 ] [ i ] = 0 ; Initialize dp [ i ] [ 0 ] = 0 ; Selecting i elements ; Select i elements till jth row ; sum = 0 , to keep track of cummulative elements sum ; Traverse arr [ j ] [ k ] until number of elements until k > i ; Select arr [ j ] [ k - 1 ] th item ; Store the maxSum in dp [ i ] [ j + 1 ] ; Return the maximum sum ; Driver code ; Function Call
function max ( a , b ) { return a > b ? a : b ; } function maximumsum ( arr , K , N , M ) { let sum = 0 , maxSum ; let i , j , k ; let dp = new Array ( K + 1 ) ; for ( i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( i = 0 ; i <= N ; i ++ ) dp [ 0 ] [ i ] = 0 ; for ( i = 0 ; i <= K ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( i = 1 ; i <= K ; i ++ ) { for ( j = 0 ; j < N ; j ++ ) { sum = 0 ; maxSum = dp [ i ] [ j ] ; for ( k = 1 ; k <= M && k <= i ; k ++ ) { sum += arr [ j ] [ k - 1 ] ; maxSum = Math . max ( maxSum , sum + dp [ i - k ] [ j ] ) ; } dp [ i ] [ j + 1 ] = maxSum ; } } return dp [ K ] [ N ] ; } let arr = [ [ 10 , 10 , 100 , 30 ] , [ 80 , 50 , 10 , 50 ] ] ; let N = 2 , M = 4 ; let K = 5 ; document . write ( maximumsum ( arr , K , N , M ) ) ;
Count lexicographically increasing K | Function to count K - length strings from first N alphabets ; To keep track of column sum in dp ; Auxiliary 2d dp array ; Initialize dp [ 0 ] [ i ] = 1 and update the column_sum ; Iterate for K times ; Iterate for N times ; dp [ i ] [ j ] : Stores the number of ways to form i - length strings consisting of j letters ; Update the column_sum ; Print number of ways to arrange K - length strings with N alphabets ; Given N and K ; Function Call
function waysToArrangeKLengthStrings ( N , K ) { let column_sum = [ ] ; let i , j ; for ( i = 1 ; i < N + 1 ; i ++ ) { column_sum [ i ] = 0 ; } let dp = new Array ( K + 1 ) ; for ( i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( i = 1 ; i < K + 1 ; i ++ ) { for ( j = 1 ; j < N + 1 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( i = 0 ; i <= N ; i ++ ) { dp [ 0 ] [ i ] = 1 ; column_sum [ i ] = 1 ; } for ( i = 1 ; i <= K ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { dp [ i ] [ j ] += column_sum [ j - 1 ] ; column_sum [ j ] += dp [ i ] [ j ] ; } } document . write ( dp [ K ] [ N ] ) ; } let N = 5 , K = 2 ; waysToArrangeKLengthStrings ( N , K ) ;
Count N | Function to count N - length strings consisting of vowels only sorted lexicographically ; Stores count of strings consisting of vowels sorted lexicographically of all possible lengths ; Initialize DP [ 1 ] [ 1 ] ; Traverse the matrix row - wise ; Base Case ; Return the result ; Driver Code ; Function Call
function findNumberOfStrings ( 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 < DP . length ; j ++ ) { DP [ i ] [ j ] = 0 ; } } DP [ 1 ] [ 1 ] = 1 ; for ( let i = 1 ; i < n + 1 ; i ++ ) { for ( let j = 1 ; j < 6 ; j ++ ) { if ( i == 1 ) { DP [ i ] [ j ] = DP [ i ] [ j - 1 ] + 1 ; } else { DP [ i ] [ j ] = DP [ i ] [ j - 1 ] + DP [ i - 1 ] [ j ] ; } } } return DP [ n ] [ 5 ] ; } let N = 2 ; document . write ( findNumberOfStrings ( N ) ) ;
Largest subtree sum for each vertex of given N | Function to perform the DFS Traversal on the given Tree ; To check if v is leaf vertex ; Initialize answer for vertex v ; Traverse adjacency list of v ; Update maximum subtree sum ; If v is leaf ; Function to calculate maximum subtree sum for each vertex ; Stores the adjacency list ; Add Edegs to the list ; Stores largest subtree sum for each vertex ; Calculate answer ; Print the result ; Given nodes ; Give N edges ; Given values ; Function Call
function dfs ( v , p , adj , ans , vals ) { let isLeaf = true ; ans [ v ] = Number . MIN_VALUE ; for ( let u = 0 ; u < adj [ v ] . length ; u ++ ) { if ( adj [ v ] [ u ] == p ) continue ; isLeaf = false ; dfs ( adj [ v ] [ u ] , v , adj , ans , vals ) ; ans [ v ] = Math . max ( ans [ adj [ v ] [ u ] ] + vals [ v ] , Math . max ( ans [ adj [ v ] [ u ] ] , vals [ adj [ v ] [ u ] ] ) ) ; } if ( isLeaf ) { ans [ v ] = vals [ v ] ; } } function printAnswer ( n , edges , values ) { let adj = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) adj [ i ] = [ ] ; for ( let i = 0 ; i < n - 1 ; i ++ ) { let u = edges [ i ] [ 0 ] - 1 ; let v = edges [ i ] [ 1 ] - 1 ; adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } let ans = new Array ( n ) ; dfs ( 0 , - 1 , adj , ans , values ) ; for ( let x = 0 ; x < ans . length ; x ++ ) { document . write ( ans [ x ] + " " ) ; } } let N = 4 ; let edges = [ [ 1 , 2 ] , [ 1 , 3 ] , [ 3 , 4 ] ] ; let values = [ 1 , - 1 , 0 , 1 ] ; printAnswer ( N , edges , values ) ;
Queries to count frequencies of a given character in a given range of indices | Function to print count of char y present in the range [ l , r ] ; Length of the string ; Stores the precomputed results ; Iterate the given string ; Increment dp [ i ] [ y - ' a ' ] by 1 ; Pre - compute ; Number of queries ; Traverse each query ; Print the result for each query ; Given string ; Given Queries ; Function Call
function noOfChars ( s , queries ) { var n = s . length ; var dp = Array ( n + 1 ) . fill ( 0 ) . map ( x => Array ( 26 ) . fill ( 0 ) ) ; for ( var i = 0 ; i < n ; i ++ ) { dp [ i + 1 ] [ s . charAt ( i ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( var j = 0 ; j < 26 ; j ++ ) { dp [ i + 1 ] [ j ] += dp [ i ] [ j ] ; } } var q = queries . length ; for ( var i = 0 ; i < q ; i ++ ) { var l = String . fromCharCode ( queries [ i ] [ 0 ] ) . charCodeAt ( 0 ) ; var r = String . fromCharCode ( queries [ i ] [ 1 ] ) . charCodeAt ( 0 ) ; var c = queries [ i ] [ 2 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; document . write ( dp [ r ] - dp [ l - 1 ] + " " ) ; } } var S = " " ; var queries = [ [ 1 , 2 , ' ' ] , [ 2 , 3 , ' ' ] ] ; noOfChars ( S , queries ) ;