codeparrot/xlcost-text-to-code huggingface dataset:
Largest subset of rectangles such that no rectangle fit in any other rectangle | JavaScript implementation of the approach ; Recursive function to get the largest subset ; Base case when it exceeds ; If the state has been visited previously ; Initialize ; No elements in subset yet ; First state which includes current index ; Second state which does not include current index ; If the rectangle fits in , then do not include the current index in subset ; First state which includes current index ; Second state which does not include current index ; Function to get the largest subset ; Initialize the DP table with - 1 ; Sort the array ; Get the answer ; ( height , width ) pairs
var N = 10 ; var dp = Array . from ( Array ( N ) , ( ) => Array ( N ) . fill ( - 1 ) ) ; function findLongest ( a , n , present , previous ) { if ( present == n ) { return 0 ; } else if ( previous != - 1 ) { if ( dp [ present ] [ previous ] != - 1 ) return dp [ present ] [ previous ] ; } var ans = 0 ; if ( previous == - 1 ) { ans = 1 + findLongest ( a , n , present + 1 , present ) ; ans = Math . max ( ans , findLongest ( a , n , present + 1 , previous ) ) ; } else { var h1 = a [ previous ] [ 0 ] ; var h2 = a [ present ] [ 0 ] ; var w1 = a [ previous ] [ 1 ] ; var w2 = a [ present ] [ 1 ] ; if ( ( h1 <= h2 && w1 <= w2 ) ) { ans = Math . max ( ans , findLongest ( a , n , present + 1 , previous ) ) ; } else { ans = 1 + findLongest ( a , n , present + 1 , present ) ; ans = Math . max ( ans , findLongest ( a , n , present + 1 , previous ) ) ; } } return dp [ present ] [ previous ] = ans ; } function getLongest ( a , n ) { dp = Array . from ( Array ( N ) , ( ) => Array ( N ) . fill ( - 1 ) ) ; a . sort ( ( a , b ) => a - b ) ; var ans = findLongest ( a , n , 0 , - 1 ) ; return ans ; } var a = [ [ 1 , 5 ] , [ 2 , 4 ] , [ 1 , 1 ] , [ 3 , 3 ] ] ; var n = a . length ; document . write ( getLongest ( a , n ) ) ;
Maximum length subsequence such that adjacent elements in the subsequence have a common factor | JavaScript implementation of the above approach ; Function to compute least prime divisor of i upto MAX element of the input array it will be space efficient if more test cases are there it 's  better to find prime divisor  upto upperbound of input element  it will be cost efficient ; Function that returns the maximum length subsequence such that adjacent elements have a common factor . ; Initialize dp array with 1. ; p has appeared at least once . ; Update latest occurrence of prime p . ; Take maximum value as the answer . ; Driver code
let N , MAX ; let lpd ; function preCompute ( ) { lpd = new Array ( MAX + 1 ) ; for ( let i = 0 ; i < lpd . length ; i ++ ) { lpd [ i ] = 0 ; } lpd [ 0 ] = lpd [ 1 ] = 1 ; for ( let i = 2 ; i * i <= MAX ; i ++ ) { for ( let j = i * 2 ; j <= MAX ; j += i ) { if ( lpd [ j ] == 0 ) { lpd [ j ] = i ; } } } for ( let i = 2 ; i <= MAX ; i ++ ) { if ( lpd [ i ] == 0 ) { lpd [ i ] = i ; } } } function maxLengthSubsequence ( arr , n ) { let dp = new Array ( N ) ; let pos = new Map ( ) ; for ( let i = 0 ; i <= n ; i ++ ) dp [ i ] = 1 ; for ( let i = 0 ; i <= n ; i ++ ) { while ( arr [ i ] > 1 ) { let p = lpd [ arr [ i ] ] ; if ( pos . has ( p ) ) { dp [ i ] = Math . max ( dp [ i ] , 1 + dp [ pos . get ( p ) ] ) ; } pos . set ( p , i ) ; while ( arr [ i ] % p == 0 ) arr [ i ] = Math . floor ( arr [ i ] / p ) ; } } let ans = Math . max ( ... dp ) ; return ans ; } let arr = [ 13 , 2 , 8 , 6 , 3 , 1 , 9 ] ; N = arr . length ; MAX = Math . max ( ... arr ) ; preCompute ( ) ; document . write ( maxLengthSubsequence ( arr , N - 1 ) ) ;
Number of ways to partition a string into two balanced subsequences | For maximum length of input string ; Declaring the DP table ; Function to calculate the number of valid assignments ; Return 1 if both subsequences are balanced ; Increment the count if it an opening bracket ; Decrement the count if it a closing bracket ; Driver Code ; Initial value for c_x and c_y is zero
let MAX = 10 ; let F = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { F [ i ] = new Array ( MAX ) ; for ( let j = 0 ; j < MAX ; j ++ ) { F [ i ] [ j ] = new Array ( MAX ) ; for ( let k = 0 ; k < MAX ; k ++ ) { F [ i ] [ j ] [ k ] = - 1 ; } } } function noOfAssignments ( s , n , i , c_x , c_y ) { if ( F [ i ] [ c_x ] [ c_y ] != - 1 ) return F [ i ] [ c_x ] [ c_y ] ; if ( i == n ) { F [ i ] [ c_x ] [ c_y ] = ( c_x == 0 && c_y == 0 ) ? 1 : 0 ; return F [ i ] [ c_x ] [ c_y ] ; } if ( s . charAt ( i ) == ' ' ) { F [ i ] [ c_x ] [ c_y ] = noOfAssignments ( s , n , i + 1 , c_x + 1 , c_y ) + noOfAssignments ( s , n , i + 1 , c_x , c_y + 1 ) ; return F [ i ] [ c_x ] [ c_y ] ; } F [ i ] [ c_x ] [ c_y ] = 0 ; if ( c_x != 0 ) F [ i ] [ c_x ] [ c_y ] += noOfAssignments ( s , n , i + 1 , c_x - 1 , c_y ) ; if ( c_y != 0 ) F [ i ] [ c_x ] [ c_y ] += noOfAssignments ( s , n , i + 1 , c_x , c_y - 1 ) ; return F [ i ] [ c_x ] [ c_y ] ; } let s = " " ; let n = s . length ; document . write ( noOfAssignments ( s , n , 0 , 0 , 0 ) ) ;
Gould 's Sequence |  ; Initialize count as zero ; Return 2 ^ count ; Function to generate gould 's Sequence ; loop to generate gould 's Sequence up to n ; Get n ; Function call
/ *	Utility function to count odd numbers in ith row of Pascals '
Longest dividing subsequence | lds ( ) returns the length of the longest dividing subsequence in arr [ ] of size n ; Compute optimized lds values in bottom up manner ; Return maximum value in lds [ ] ; Driver program to test above function
function lds ( arr , n ) { let lds = new Array ( n ) ; lds [ 0 ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) { lds [ i ] = 1 ; for ( let j = 0 ; j < i ; j ++ ) if ( lds [ j ] != 0 && arr [ i ] % arr [ j ] == 0 ) lds [ i ] = Math . max ( lds [ i ] , lds [ j ] + 1 ) ; } let max = Math . max ( ... lds ) ; return max ; } let arr = [ 2 , 11 , 16 , 12 , 36 , 60 , 71 , 17 , 29 , 144 , 288 , 129 , 432 , 993 ] ; let n = arr . length ; document . write ( " " + lds ( arr , n ) ) ;
Memoization ( 1D , 2D and 3D ) | Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; base case ; if equal , then check for next combination ; recursive call ; return the maximum of the three other possible states in recursion ; Driver code
function lcs ( X , Y , Z , m , n , o ) { if ( m == 0 n == 0 o == 0 ) return 0 ; if ( X [ m - 1 ] == Y [ n - 1 ] && Y [ n - 1 ] == Z [ o - 1 ] ) { return 1 + lcs ( X , Y , Z , m - 1 , n - 1 , o - 1 ) ; } else { return Math . max ( lcs ( X , Y , Z , m , n - 1 , o ) , Math . max ( lcs ( X , Y , Z , m - 1 , n , o ) , lcs ( X , Y , Z , m , n , o - 1 ) ) ) ; } } let X = " " . split ( " " ) ; let Y = " " . split ( " " ) ; let Z = " " . split ( " " ) ; let m = X . length ; let n = Y . length ; let o = Z . length ; document . write ( " " + lcs ( X , Y , Z , m , n , o ) ) ;
Memoization ( 1D , 2D and 3D ) | A memoize recursive implementation of LCS problem ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] memoization applied in recursive solution ; base case ; if the same state has already been computed ; if equal , then we store the value of the function call ; store it in arr to avoid further repetitive work in future function calls ; store it in arr to avoid further repetitive work in future function calls ; Utility function to get max of 2 integers ; Driver Code
let arr = new Array ( 100 ) ; for ( let i = 0 ; i < 100 ; i ++ ) { arr [ i ] = new Array ( 100 ) ; for ( let j = 0 ; j < 100 ; j ++ ) { arr [ i ] [ j ] = new Array ( 100 ) ; for ( let k = 0 ; k < 100 ; k ++ ) { arr [ i ] [ j ] [ k ] = - 1 ; } } } function lcs ( X , Y , Z , m , n , o ) { if ( m == 0 n == 0 o == 0 ) return 0 ; if ( arr [ m - 1 ] [ n - 1 ] [ o - 1 ] != - 1 ) return arr [ m - 1 ] [ n - 1 ] [ o - 1 ] ; if ( X [ m - 1 ] == Y [ n - 1 ] && Y [ n - 1 ] == Z [ o - 1 ] ) { arr [ m - 1 ] [ n - 1 ] [ o - 1 ] = 1 + lcs ( X , Y , Z , m - 1 , n - 1 , o - 1 ) ; return arr [ m - 1 ] [ n - 1 ] [ o - 1 ] ; } else { arr [ m - 1 ] [ n - 1 ] [ o - 1 ] = max ( lcs ( X , Y , Z , m , n - 1 , o ) , max ( lcs ( X , Y , Z , m - 1 , n , o ) , lcs ( X , Y , Z , m , n , o - 1 ) ) ) ; return arr [ m - 1 ] [ n - 1 ] [ o - 1 ] ; } } function max ( a , b ) { return ( a > b ) ? a : b ; } let X = " " ; let Y = " " ; let Z = " " ; let m = X . length ; let n = Y . length ; let o = Z . length ; document . write ( " " + lcs ( X , Y , Z , m , n , o ) ) ;
Check if it is possible to reach a number by making jumps of two given length | JavaScript implementation of the approach ; Function to perform BFS traversal to find minimum number of step needed to reach x from K ; Calculate GCD of d1 and d2 ; If position is not reachable return - 1 ; Queue for BFS ; Hash Table for marking visited positions ; we need 0 steps to reach K ; Mark starting position as visited ; stp is the number of steps to reach position s ; if position not visited add to queue and mark visited ; Driver Code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function minStepsNeeded ( k , d1 , d2 , x ) { let gcd = __gcd ( d1 , d2 ) ; if ( ( k - x ) % gcd != 0 ) return - 1 ; let q = [ ] ; let visited = new Set ( ) ; q . push ( [ k , 0 ] ) ; visited . add ( k ) ; while ( q . length != 0 ) { let s = q [ 0 ] [ 0 ] ; let stp = q [ 0 ] [ 1 ] ; if ( s == x ) return stp ; q . shift ( ) ; if ( ! visited . has ( s + d1 ) ) { q . push ( [ s + d1 , stp + 1 ] ) ; visited . add ( s + d1 ) ; } if ( ! visited . has ( s + d2 ) ) { q . push ( [ s + d2 , stp + 1 ] ) ; visited . add ( s + d2 ) ; } if ( ! visited . has ( s - d1 ) ) { q . push ( [ s - d1 , stp + 1 ] ) ; visited . add ( s - d1 ) ; } if ( ! visited . has ( s - d2 ) ) { q . push ( [ s - d2 , stp + 1 ] ) ; visited . add ( s - d2 ) ; } } return Number . MIN_VALUE ; } let k = 10 , d1 = 4 , d2 = 6 , x = 8 ; document . write ( minStepsNeeded ( k , d1 , d2 , x ) ) ;
Newman | Recursion to find n - th element ; Driver code
function sequence ( n ) { if ( n == 1 n == 2 ) return 1 ; else return sequence ( sequence ( n - 1 ) ) + sequence ( n - sequence ( n - 1 ) ) ; } let n = 10 ; document . write ( sequence ( n ) ) ;
Counting pairs when a person can form pair with at most one | Number of ways in which participant can take part . ; Base condition ; A participant can choose to consider ( 1 ) Remains single . Number of people reduce to ( x - 1 ) ( 2 ) Pairs with one of the ( x - 1 ) others . For every pairing , number of people reduce to ( x - 2 ) . ; Driver code
function numberOfWays ( x ) { if ( x == 0 x == 1 ) return 1 ; else return numberOfWays ( x - 1 ) + ( x - 1 ) * numberOfWays ( x - 2 ) ; } var x = 3 ; document . write ( numberOfWays ( x ) ) ;
Counting pairs when a person can form pair with at most one | Number of ways in which participant can take part . ; Driver code
function numberOfWays ( x ) { let dp = Array ( x + 1 ) . fill ( 0 ) ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( i = 2 ; i <= x ; i ++ ) dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ; return dp [ x ] ; } let x = 3 ; document . write ( numberOfWays ( x ) ) ;
Longest Repeated Subsequence | Refer https : www . geeksforgeeks . org / longest - repeating - subsequence / for complete code . This function mainly returns LCS ( str , str ) with a condition that same characters at same index are not considered . ; Create and initialize DP table ; Fill dp table ( similar to LCS loops ) ; If characters match and indexes are not same ; If characters do not match
function findLongestRepeatingSubSeq ( str ) { let n = str . length ; let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i <= n ; i ++ ) { dp [ i ] = new Array ( n + 1 ) ; for ( let j = 0 ; j <= n ; j ++ ) dp [ i ] [ j ] = 0 ; } for ( let i = 1 ; i <= n ; i ++ ) { for ( let j = 1 ; j <= n ; j ++ ) { if ( str [ i - 1 ] == str [ j - 1 ] && i != j ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = Math . max ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] ) ; } } return dp [ n ] [ n ] ; }
Number of ways to arrange N items under given constraints | method returns number of ways with which items can be arranged ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; declare dp array to store result up to ith colored item ; variable to keep track of count of items considered till now ; loop over all different colors ; populate next value using current value and stated relation ; return value stored at last index ; driver function
function waysToArrange ( N , K , 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 <= i ; j ++ ) { if ( j == 0 j == i ) { C [ i ] [ j ] = 1 ; } else { C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ) ; } } } let dp = Array . from ( { length : K + 1 } , ( _ , i ) => 0 ) ; let count = 0 ; dp [ 0 ] = 1 ; for ( i = 0 ; i < K ; i ++ ) { dp [ i + 1 ] = ( dp [ i ] * C [ count + k [ i ] - 1 ] [ k [ i ] - 1 ] ) ; count += k [ i ] ; } return dp [ K ] ; } let N = 4 ; let k = [ 2 , 2 ] ; let K = k . length ; document . write ( waysToArrange ( N , K , k ) ) ;
Minimum cells required to reach destination with jumps equal to cell values | Javascript implementation to count minimum cells required to be covered to reach destination ; function to count minimum cells required to be covered to reach destination ; to store min cells required to be covered to reach a particular cell ; initially no cells can be reached ; base case ; building up the dp [ ] [ ] matrix ; dp [ i ] [ j ] != LET_MAX denotes that cell ( i , j ) can be reached from cell ( 0 , 0 ) and the other half of the condition finds the cell on the right that can be reached from ( i , j ) ; the other half of the condition finds the cell right below that can be reached from ( i , j ) ; it true then cell ( m - 1 , n - 1 ) can be reached from cell ( 0 , 0 ) and returns the minimum number of cells covered ; cell ( m - 1 , n - 1 ) cannot be reached from cell ( 0 , 0 ) ; driver function
let SIZE = 100 ; function minCells ( mat , m , n ) { let dp = new Array ( m ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < m ; i ++ ) for ( let j = 0 ; j < n ; j ++ ) dp [ i ] [ j ] = Number . MAX_VALUE ; dp [ 0 ] [ 0 ] = 1 ; for ( let i = 0 ; i < m ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( dp [ i ] [ j ] != Number . MAX_VALUE && ( j + mat [ i ] [ j ] ) < n && ( dp [ i ] [ j ] + 1 ) < dp [ i ] [ j + mat [ i ] [ j ] ] ) dp [ i ] [ j + mat [ i ] [ j ] ] = dp [ i ] [ j ] + 1 ; if ( dp [ i ] [ j ] != Number . MAX_VALUE && ( i + mat [ i ] [ j ] ) < m && ( dp [ i ] [ j ] + 1 ) < dp [ i + mat [ i ] [ j ] ] [ j ] ) dp [ i + mat [ i ] [ j ] ] [ j ] = dp [ i ] [ j ] + 1 ; } } if ( dp [ m - 1 ] [ n - 1 ] != Number . MAX_VALUE ) return dp [ m - 1 ] [ n - 1 ] ; return - 1 ; } let mat = [ [ 2 , 3 , 2 , 1 , 4 ] , [ 3 , 2 , 5 , 8 , 2 ] , [ 1 , 1 , 2 , 2 , 1 ] ] ; let m = 3 , n = 5 ; document . write ( " " + " " + minCells ( mat , m , n ) ) ;
Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays | JavaScript to find largest bitonic sequence such that ; utility Binary search ; function to find LIS in reverse form ; Add boundary case , when array n is zero Depend on smart pointers ; it will always point to empty location ; new smallest value ; arr [ i ] wants to extend largest subsequence ; arr [ i ] wants to be a potential candidate of future subsequence It will replace ceil value in tailIndices ; put LIS into vector ; function for finding longest bitonic seq ; find LIS of array 1 in reverse form ; reverse res to get LIS of first array ; reverse array2 and find its LIS ; print result ; Driver Code
let res = [ ] ; function GetCeilIndex ( arr , T , l , r , key ) { while ( r - l > 1 ) { let m = l + Math . floor ( ( r - l ) / 2 ) ; if ( arr [ T [ m ] ] >= key ) r = m ; else l = m ; } return r ; } function LIS ( arr , n ) { let tailIndices = new Array ( n ) ; let prevIndices = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { tailIndices [ i ] = 0 ; prevIndices [ i ] = - 1 ; } let len = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ tailIndices [ 0 ] ] ) tailIndices [ 0 ] = i ; else if ( arr [ i ] > arr [ tailIndices [ len - 1 ] ] ) { prevIndices [ i ] = tailIndices [ len - 1 ] ; tailIndices [ len ++ ] = i ; } else { let pos = GetCeilIndex ( arr , tailIndices , - 1 , len - 1 , arr [ i ] ) ; prevIndices [ i ] = tailIndices [ pos - 1 ] ; tailIndices [ pos ] = i ; } } for ( let i = tailIndices [ len - 1 ] ; i >= 0 ; i = prevIndices [ i ] ) res . push ( arr [ i ] ) ; } function longestBitonic ( arr1 , n1 , arr2 , n2 ) { LIS ( arr1 , n1 ) ; res . reverse ( ) ; arr2 . reverse ( ) ; LIS ( arr2 , n2 ) ; for ( let i = 0 ; i < res . length ; i ++ ) document . write ( res [ i ] + " " ) ; } let arr1 = [ 1 , 2 , 4 , 3 , 2 ] ; let arr2 = [ 8 , 6 , 4 , 7 , 8 , 9 ] ; let n1 = arr1 . length ; let n2 = arr2 . length ; longestBitonic ( arr1 , n1 , arr2 , n2 ) ;
Maximize the binary matrix by filpping submatrix once | Javascript program to find maximum number of ones after one flipping in Binary Matrix ; Return number of ones in square submatrix of size k x k starting from ( x , y ) ; Return maximum number of 1 s after flipping a submatrix ; Precomputing the number of 1 s ; Finding the maximum number of 1 s after flipping ; driver function
let R = 3 ; let C = 3 ; function cal ( ones , x , y , k ) { return ones [ x + k - 1 ] [ y + k - 1 ] - ones [ x - 1 ] [ y + k - 1 ] - ones [ x + k - 1 ] [ y - 1 ] + ones [ x - 1 ] [ y - 1 ] ; } function sol ( mat ) { let ans = 0 ; let val = 0 ; let ones = new Array ( R + 1 ) ; for ( var i = 0 ; i < ones . length ; i ++ ) { ones [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < ones . length ; i ++ ) { for ( var j = 0 ; j < ones . length ; j ++ ) { ones [ i ] [ j ] = 0 ; } } for ( let i = 1 ; i <= R ; i ++ ) for ( let j = 1 ; j <= C ; j ++ ) { if ( mat [ i - 1 ] [ j - 1 ] == 1 ) val = 1 ; ones [ i ] [ j ] = ones [ i - 1 ] [ j ] + ones [ i ] [ j - 1 ] - ones [ i - 1 ] [ j - 1 ] + ( val ) ; } for ( let k = 1 ; k <= Math . min ( R , C ) ; k ++ ) for ( let i = 1 ; i + k - 1 <= R ; i ++ ) for ( let j = 1 ; j + k - 1 <= C ; j ++ ) ans = Math . max ( ans , ( ones [ R ] [ C ] + k * k - 2 * cal ( ones , i , j , k ) ) ) ; return ans ; } let mat = [ [ 0 , 0 , 1 ] , [ 0 , 0 , 1 ] , [ 1 , 0 , 1 ] ] ; document . write ( sol ( mat ) ) ;
Minimum steps to minimize n as per given condition | A tabulation based solution in Javascript ; Driver code
function getMinSteps ( n ) { let table = new Array ( n + 1 ) ; table . fill ( 0 ) ; table [ 1 ] = 0 ; for ( let i = 2 ; i <= n ; i ++ ) { if ( ! ( i % 2 ) && ( i % 3 ) ) table [ i ] = 1 + Math . min ( table [ i - 1 ] , table [ i / 2 ] ) ; else if ( ! ( i % 3 ) && ( i % 2 ) ) table [ i ] = 1 + Math . min ( table [ i - 1 ] , table [ i / 3 ] ) ; else if ( ! ( i % 2 ) && ! ( i % 3 ) ) table [ i ] = 1 + Math . min ( table [ i - 1 ] , Math . min ( table [ i / 2 ] , table [ i / 3 ] ) ) ; else table [ i ] = 1 + table [ i - 1 ] ; } return table [ n ] + 1 ; } let n = 10 ; document . write ( getMinSteps ( n ) ) ;
How to solve a Dynamic Programming Problem ? | Returns the number of arrangements to form ' n ' ; base case
function solve ( n ) { if ( n < 0 ) return 0 ; if ( n == 0 ) return 1 ; return solve ( n - 1 ) + solve ( n - 3 ) + solve ( n - 5 ) ; }
Maximum points collected by two persons allowed to meet once | Javascript program to find maximum points that can be collected by two persons in a matrix . ; To store points collected by Person P1 when he / she begins journy from start and from end . ; To store points collected by Person P2 when he / she begins journey from start and from end . ; Table for P1 's journey from  start to meeting cell ; Table for P1 's journey from  end to meet cell ; Table for P2 's journey from start to  meeting cell ; Table for P2 's journey from  end to meeting cell ; Now iterate over all meeting positions ( i , j ) ; input the calories burnt matrix
let M = 3 ; let N = 3 ; function findMaxPoints ( A ) { let P1S = new Array ( M + 2 ) ; let P1E = new Array ( M + 2 ) ; let P2S = new Array ( M + 2 ) ; let P2E = new Array ( M + 2 ) ; for ( let i = 0 ; i < M + 2 ; i ++ ) { P1S [ i ] = new Array ( N + 2 ) ; P1E [ i ] = new Array ( N + 2 ) ; P2S [ i ] = new Array ( N + 2 ) ; P2E [ i ] = new Array ( N + 2 ) ; for ( let j = 0 ; j < N + 2 ; j ++ ) { P1S [ i ] [ j ] = 0 ; P1E [ i ] [ j ] = 0 ; P2S [ i ] [ j ] = 0 ; P2E [ i ] [ j ] = 0 ; } } for ( let i = 1 ; i <= N ; i ++ ) for ( let j = 1 ; j <= M ; j ++ ) P1S [ i ] [ j ] = Math . max ( P1S [ i - 1 ] [ j ] , P1S [ i ] [ j - 1 ] ) + A [ i - 1 ] [ j - 1 ] ; for ( let i = N ; i >= 1 ; i -- ) for ( let j = M ; j >= 1 ; j -- ) P1E [ i ] [ j ] = Math . max ( P1E [ i + 1 ] [ j ] , P1E [ i ] [ j + 1 ] ) + A [ i - 1 ] [ j - 1 ] ; for ( let i = N ; i >= 1 ; i -- ) for ( let j = 1 ; j <= M ; j ++ ) P2S [ i ] [ j ] = Math . max ( P2S [ i + 1 ] [ j ] , P2S [ i ] [ j - 1 ] ) + A [ i - 1 ] [ j - 1 ] ; for ( let i = 1 ; i <= N ; i ++ ) for ( let j = M ; j >= 1 ; j -- ) P2E [ i ] [ j ] = Math . max ( P2E [ i - 1 ] [ j ] , P2E [ i ] [ j + 1 ] ) + A [ i - 1 ] [ j - 1 ] ; let ans = 0 ; for ( let i = 2 ; i < N ; i ++ ) { for ( let j = 2 ; j < M ; j ++ ) { let op1 = P1S [ i ] [ j - 1 ] + P1E [ i ] [ j + 1 ] + P2S [ i + 1 ] [ j ] + P2E [ i - 1 ] [ j ] ; let op2 = P1S [ i - 1 ] [ j ] + P1E [ i + 1 ] [ j ] + P2S [ i ] [ j - 1 ] + P2E [ i ] [ j + 1 ] ; ans = Math . max ( ans , Math . max ( op1 , op2 ) ) ; } } return ans ; } let A = [ [ 100 , 100 , 100 ] , [ 100 , 1 , 100 ] , [ 100 , 100 , 100 ] ] ; document . write ( " " + findMaxPoints ( A ) ) ;
Longest subsequence such that difference between adjacents is one | Function to find the length of longest subsequence ; Initialize the dp [ ] array with 1 as a single element will be of 1 length ; Start traversing the given array ; Compare with all the previous elements ; If the element is consecutive then consider this subsequence and update dp [ i ] if required . ; Longest length will be the maximum value of dp array . ; Longest subsequence with one difference is { 1 , 2 , 3 , 4 , 3 , 2 }
function longestSubseqWithDiffOne ( arr , n ) { let dp = [ ] ; for ( let i = 0 ; i < n ; i ++ ) dp [ i ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) { for ( let j = 0 ; j < i ; j ++ ) { if ( ( arr [ i ] == arr [ j ] + 1 ) || ( arr [ i ] == arr [ j ] - 1 ) ) dp [ i ] = Math . max ( dp [ i ] , dp [ j ] + 1 ) ; } } let result = 1 ; for ( let i = 0 ; i < n ; i ++ ) if ( result < dp [ i ] ) result = dp [ i ] ; return result ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 3 , 2 ] ; let n = arr . length ; document . write ( longestSubseqWithDiffOne ( arr , n ) ) ;
Non | A dynamic programming based function to find nth Catalan number ; Table to store results of subproblems Initialize first two values in table ; Initialize first two values in table ; Fill entries in catalan [ ] using recursive formula ; Return last entry ; Returns count of ways to connect n points on a circle such that no two connecting lines cross each other and every point is connected with one other point . ; Throw error if n is odd ; Else return n / 2 'th Catalan number ; Driver Code
function catalanDP ( n ) { let catalan = [ ] catalan [ 0 ] = catalan [ 1 ] = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { catalan [ i ] = 0 ; for ( let j = 0 ; j < i ; j ++ ) catalan [ i ] += catalan [ j ] * catalan [ i - j - 1 ] ; } return catalan [ n ] ; } function countWays ( n ) { if ( n & 1 ) { document . write ( " " ) ; return 0 ; } return catalanDP ( n / 2 ) ; } document . write ( countWays ( 6 ) + " " ) ;