codeparrot/xlcost-text-to-code huggingface dataset:
Longest Common Subsequence with at most k changes allowed | Javascript program to find LCS of two arrays with k changes allowed in first array . ; Return LCS with at most k changes allowed . ; If at most changes is less than 0. ; If any of two array is over . ; Making a reference variable to dp [ n ] [ m ] [ k ] ; If value is already calculated , return that value . ; Calculating LCS with no changes made . ; Calculating LCS when array element are same . ; Calculating LCS with changes made . ; Driver Code
let MAX = 10 ; function lcs ( dp , arr1 , n , arr2 , m , k ) { if ( k < 0 ) return - 10000000 ; if ( n < 0 m < 0 ) return 0 ; let ans = dp ; if ( ans != - 1 ) return ans ; try { ans = Math . max ( lcs ( dp , arr1 , n - 1 , arr2 , m , k ) , lcs ( dp , arr1 , n , arr2 , m - 1 , k ) ) ; if ( arr1 [ n - 1 ] == arr2 [ m - 1 ] ) ans = Math . max ( ans , 1 + lcs ( dp , arr1 , n - 1 , arr2 , m - 1 , k ) ) ; ans = Math . max ( ans , 1 + lcs ( dp , arr1 , n - 1 , arr2 , m - 1 , k - 1 ) ) ; } catch ( e ) { } return ans ; } let k = 0 ; let arr1 = [ 1 , 2 , 3 , 4 , 5 ] ; let arr2 = [ 5 , 3 , 1 , 4 , 2 ] ; let n = arr1 . length ; let m = arr2 . length ; let dp = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) for ( let j = 0 ; j < MAX ; j ++ ) for ( let l = 0 ; l < MAX ; l ++ ) dp = - 1 ; document . write ( lcs ( dp , arr1 , n , arr2 , m , k ) ) ;
Count ways to build street under given constraints | Function to count ways of building a street of n rows ; Base case ; Ways of building houses in both the spots of ith row ; Ways of building an office in one of the two spots of ith row ; Total ways for n rows ; Driver code
function countWays ( n ) { let dp = new Array ( 2 ) ; for ( let i = 0 ; i < 2 ; i ++ ) { dp [ i ] = new Array ( n + 1 ) ; } dp [ 0 ] [ 1 ] = 1 ; dp [ 1 ] [ 1 ] = 2 ; for ( let i = 2 ; i <= n ; i ++ ) { dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + dp [ 1 ] [ i - 1 ] ; dp [ 1 ] [ i ] = dp [ 0 ] [ i - 1 ] * 2 + dp [ 1 ] [ i - 1 ] ; } return dp [ 0 ] [ n ] + dp [ 1 ] [ n ] ; } let n = 5 ; document . write ( " " + n + " " + countWays ( n ) ) ;
Count ways to build street under given constraints | Program to count ways ; Iterate from 2 to n ; driver program ; Count Ways
function countways ( n ) { let A = new Array ( n + 1 ) . fill ( 0 ) ; A [ 0 ] = 1 ; A [ 1 ] = 3 ; A [ 2 ] = 7 ; for ( let i = 2 ; i <= n ; i ++ ) { A [ i ] = 2 * A [ i - 1 ] + A [ i - 2 ] ; } return A [ n ] ; } let n = 5 ; document . write ( countways ( 5 ) )
Maximum length subsequence with difference between adjacent elements as either 0 or 1 | function to find maximum length subsequence with difference between adjacent elements as either 0 or 1 ; Initialize mls [ ] values for all indexes ; Compute optimized maximum length subsequence values in bottom up manner ; Store maximum of all ' mls ' values in ' max ' ; required maximum length subsequence ; driver program
function maxLenSub ( arr , n ) { let mls = new Array ( n ) . fill ( 1 ) , max = 0 ; for ( let i = 0 ; i < n ; i ++ ) mls [ i ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) for ( let j = 0 ; j < i ; j ++ ) if ( Math . abs ( arr [ i ] - arr [ j ] ) <= 1 && mls [ i ] < mls [ j ] + 1 ) mls [ i ] = mls [ j ] + 1 ; for ( let i = 0 ; i < n ; i ++ ) if ( max < mls [ i ] ) max = mls [ i ] ; return max ; } let arr = [ 2 , 5 , 6 , 3 , 7 , 6 , 5 , 8 ] ; let n = arr . length ; document . write ( " " + maxLenSub ( arr , n ) ) ;
Coin game winner where every player has three choices | To find winner of game ; To store results ; Initial values ; Computing other values . ; If A losses any of i - 1 or i - x or i - y game then he will definitely win game i ; Else A loses game . ; If dp [ n ] is true then A will game otherwise he losses ; Driver code
function findWinner ( x , y , n ) { var dp = Array ( n + 1 ) . fill ( 0 ) ; dp [ 0 ] = false ; dp [ 1 ] = true ; for ( var i = 2 ; i <= n ; i ++ ) { if ( i - 1 >= 0 && ! dp [ i - 1 ] ) dp [ i ] = true ; else if ( i - x >= 0 && ! dp [ i - x ] ) dp [ i ] = true ; else if ( i - y >= 0 && ! dp [ i - y ] ) dp [ i ] = true ; else dp [ i ] = false ; } return dp [ n ] ; } var x = 3 , y = 4 , n = 5 ; if ( findWinner ( x , y , n ) ) document . write ( ' ' ) ; else document . write ( ' ' ) ;
Maximum games played by winner | method returns maximum games a winner needs to play in N - player tournament ; for 0 games , 1 player is needed for 1 game , 2 players are required ; loop until i - th Fibonacci number is less than or equal to N ; result is ( i - 2 ) because i will be incremented one extra in while loop and we want the last value which is smaller than N , so one more decrement ; driver program
function maxGameByWinner ( N ) { let dp = new Array ( N ) . fill ( 0 ) ; dp [ 0 ] = 1 ; dp [ 1 ] = 2 ; let i = 2 ; do { dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] ; } while ( dp [ i ++ ] <= N ) ; return ( i - 2 ) ; } let N = 10 ; document . write ( maxGameByWinner ( N ) ) ;
Convert to Strictly increasing integer array with minimum changes | To find min elements to remove from array to make it strictly increasing ; Mark all elements of LIS as 1 ; Find LIS of array ; Return min changes for array to strictly increasing ; driver program
function minRemove ( arr , n ) { let LIS = new Array ( n ) . fill ( 0 ) ; let len = 0 ; for ( let i = 0 ; i < n ; i ++ ) LIS [ i ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) { for ( let j = 0 ; j < i ; j ++ ) { if ( arr [ i ] > arr [ j ] && ( i - j ) <= ( arr [ i ] - arr [ j ] ) ) LIS [ i ] = Math . max ( LIS [ i ] , LIS [ j ] + 1 ) ; } len = Math . max ( len , LIS [ i ] ) ; } return n - len ; } let arr = [ 1 , 2 , 6 , 5 , 4 ] ; let n = arr . length ; document . write ( minRemove ( arr , n ) ) ;
Ways of transforming one string to other by removing 0 or more characters | JavaScript program to count the distinct transformation of one string to other . ; If b = " " i . e . , an empty string . There is only one way to transform ( remove all characters ) ; Fil dp [ ] [ ] in bottom up manner Traverse all character of b [ ] ; Traverse all characters of a [ ] for b [ i ] ; Filling the first row of the dp matrix . ; Filling other rows . ; Driver code
function countTransformation ( a , b ) { var n = a . length , m = b . length ; if ( m == 0 ) { return 1 ; } var dp = new Array ( m , n ) ; for ( var i = 0 ; i < m ; i ++ ) { for ( var j = i ; j < n ; j ++ ) { if ( i == 1 ) { if ( j == 1 ) { dp [ i , j ] = ( a [ j ] == b [ i ] ) ? 1 : 0 ; } else if ( a [ j ] == b [ i ] ) { dp [ i , j ] = dp [ i , j - 1 ] + 1 ; } else { dp [ i , j ] = dp [ i , j - 1 ] ; } } else if ( a [ j ] == b [ j ] ) { dp [ i , j ] = dp [ i , j - 1 ] + dp [ i - 1 , j - 1 ] ; } else { dp [ i , j ] = dp [ i , j - 1 ] ; } } } return dp [ m - 1 , n - 1 ] ; } var a = " " , b = " " ; document . write ( countTransformation ( a , b ) ) ;
n | javascript code to find nth number with digits 0 , 1 , 2 , 3 , 4 , 5 ; function to convert num to base 6 ; initialize an array to 0 ; function calling to convert number n to base 6 ; if size is zero then return zero
var max = 100000 ; function baseconversion ( arr , num , base ) { var i = 0 , rem , j ; if ( num == 0 ) { return 0 ; } while ( num > 0 ) { rem = num % base ; arr [ i ++ ] = rem ; num = parseInt ( num / base ) ; } return i ; } var arr = Array ( max ) . fill ( 0 ) ; var n = 10 ; var size = baseconversion ( arr , n - 1 , 6 ) ; if ( size == 0 ) document . write ( size ) ; for ( i = size - 1 ; i >= 0 ; i -- ) { document . write ( arr [ i ] ) ; }
Find length of longest subsequence of one string which is substring of another string | Javascript program to find maximum length of subsequence of a string X such it is substring in another string Y . ; Return the maximum size of substring of X which is substring in Y . ; Initialize the dp [ ] [ ] to 0. ; Calculating value for each element . ; If alphabet of string X and Y are equal make dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; Else copy the previous value in the row i . e dp [ i - 1 ] [ j - 1 ] ; Finding the maximum length . ; Driver Program
var MAX = 1000 ; function maxSubsequenceSubstring ( x , y , n , m ) { var dp = Array . from ( Array ( MAX ) , ( ) => Array ( MAX ) ) ; for ( var i = 0 ; i <= m ; i ++ ) for ( var j = 0 ; j <= n ; j ++ ) dp [ i ] [ j ] = 0 ; for ( var i = 1 ; i <= m ; i ++ ) { for ( var j = 1 ; j <= n ; j ++ ) { if ( x [ j - 1 ] == y [ i - 1 ] ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = dp [ i ] [ j - 1 ] ; } } var ans = 0 ; for ( var i = 1 ; i <= m ; i ++ ) ans = Math . max ( ans , dp [ i ] [ n ] ) ; return ans ; } var x = " " ; var y = " " ; var n = x . length , m = y . length ; document . write ( maxSubsequenceSubstring ( x , y , n , m ) ) ;
Pyramid form ( increasing then decreasing ) consecutive array using reduce operations | Returns minimum cost to form a pyramid ; Store the maximum possible pyramid height ; Maximum height at start is 1 ; For each position calculate maximum height ; Maximum height at end is 1 ; For each position calculate maximum height ; Find minimum possible among calculated values ; Find maximum height of pyramid ; Calculate cost of this pyramid ; Calculate cost of left half ; Calculate cost of right half ; Driver code
function minPyramidCost ( arr , N ) { let left = new Array ( N ) ; let right = new Array ( N ) ; left [ 0 ] = Math . min ( arr [ 0 ] , 1 ) ; for ( let i = 1 ; i < N ; ++ i ) left [ i ] = Math . min ( arr [ i ] , Math . min ( left [ i - 1 ] + 1 , i + 1 ) ) ; right [ N - 1 ] = Math . min ( arr [ N - 1 ] , 1 ) ; for ( let i = N - 2 ; i >= 0 ; -- i ) right [ i ] = Math . min ( arr [ i ] , Math . min ( right [ i + 1 ] + 1 , N - i ) ) ; let tot = new Array ( N ) ; for ( let i = 0 ; i < N ; ++ i ) tot [ i ] = Math . min ( right [ i ] , left [ i ] ) ; let max_ind = 0 ; for ( let i = 0 ; i < N ; ++ i ) if ( tot [ i ] > tot [ max_ind ] ) max_ind = i ; let cost = 0 ; let height = tot [ max_ind ] ; for ( let x = max_ind ; x >= 0 ; -- x ) { cost += arr [ x ] - height ; if ( height > 0 ) -- height ; } height = tot [ max_ind ] - 1 ; for ( let x = max_ind + 1 ; x < N ; ++ x ) { cost += arr [ x ] - height ; if ( height > 0 ) -- height ; } return cost ; } let arr = [ 1 , 2 , 3 , 4 , 2 , 1 ] ; let N = arr . length ; document . write ( minPyramidCost ( arr , N ) ) ;
Maximum sum in a 2 x n grid such that no two elements are adjacent | Function to find max sum without adjacent ; Sum including maximum element of first column ; Not including first column 's  element ; Traverse for further elements ; Update max_sum on including or excluding of previous column ; Include current column . Add maximum element from both row of current column ; If current column doesn 't  to be included ; Return maximum of excl and incl As that will be the maximum sum ; Driver Code
function maxSum ( grid , n ) { let incl = Math . max ( grid [ 0 ] [ 0 ] , grid [ 1 ] [ 0 ] ) ; let excl = 0 , excl_new ; for ( let i = 1 ; i < n ; i ++ ) { excl_new = Math . max ( excl , incl ) ; incl = excl + Math . max ( grid [ 0 ] [ i ] , grid [ 1 ] [ i ] ) ; excl = excl_new ; } return Math . max ( excl , incl ) ; } let grid = [ [ 1 , 2 , 3 , 4 , 5 ] , [ 6 , 7 , 8 , 9 , 10 ] ] ; let n = 5 ; document . write ( maxSum ( grid , n ) ) ;
Minimum insertions to sort an array | method returns min steps of insertion we need to perform to sort array ' arr ' ; lis [ i ] is going to store length of lis that ends with i . ; Initialize lis values for all indexes ; Compute optimized lis values in bottom up manner ; The overall LIS must end with of the array elements . Pick maximum of all lis values ; return size of array minus length of LIS as final result ; Driver code to test above methods
function minInsertionStepToSortArray ( arr , N ) { let lis = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { lis [ i ] = 1 ; } for ( let i = 1 ; i < N ; i ++ ) { for ( let j = 0 ; j < i ; j ++ ) { if ( arr [ i ] >= arr [ j ] && lis [ i ] < lis [ j ] + 1 ) { lis [ i ] = lis [ j ] + 1 ; } } } let max = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( max < lis [ i ] ) { max = lis [ i ] ; } } return ( N - max ) ; } let arr = new Array ( 2 , 3 , 5 , 1 , 4 , 7 , 6 ) ; let N = arr . length ; document . write ( minInsertionStepToSortArray ( arr , N ) ) ;
Print all distinct characters of a string in order ( 3 Methods ) | Javascript program to print distinct characters of a string . ; Print duplicates present in the passed string ; Create an array of size 256 and count of every character in it ; Count array with frequency of characters ; Print characters having count more than 0 ; Driver program
let NO_OF_CHARS = 256 ; function printDistinct ( str ) { let count = new Array ( NO_OF_CHARS ) ; for ( let i = 0 ; i < NO_OF_CHARS ; i ++ ) { count [ i ] = 0 ; } let i ; for ( i = 0 ; i < str . length ; i ++ ) if ( str [ i ] != ' ' ) count [ str [ i ] . charCodeAt ( 0 ) ] ++ ; let n = i ; for ( i = 0 ; i < n ; i ++ ) if ( count [ str [ i ] . charCodeAt ( 0 ) ] == 1 ) document . write ( str [ i ] ) ; } let str = " " ; printDistinct ( str ) ;
Count number of ways to jump to reach end | function to count ways to jump to reach end for each array element ; count_jump [ i ] store number of ways arr [ i ] can reach to the end ; Last element does not require to jump . Count ways to jump for remaining elements ; if the element can directly jump to the end ; add the count of all the elements that can reach to end and arr [ i ] can reach to them ; if element can reach to end then add its count to count_jump [ i ] ; if arr [ i ] cannot reach to the end ; print count_jump for each array element ; driver code
function countWaysToJump ( arr , n ) { let count_jump = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { count_jump [ i ] = 0 ; } for ( let i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] >= n - i - 1 ) count_jump [ i ] ++ ; for ( let j = i + 1 ; j < n - 1 && j <= arr [ i ] + i ; j ++ ) if ( count_jump [ j ] != - 1 ) count_jump [ i ] += count_jump [ j ] ; if ( count_jump [ i ] == 0 ) count_jump [ i ] = - 1 ; } for ( let i = 0 ; i < n ; i ++ ) document . write ( count_jump [ i ] + " " ) ; } let arr = [ 1 , 3 , 5 , 8 , 9 , 1 , 0 , 7 , 6 , 8 , 9 ] ; let n = arr . length ; countWaysToJump ( arr , n ) ;
Minimum steps to delete a string after repeated deletion of palindrome substrings | method returns minimum step for deleting the string , where in one step a palindrome is removed ; declare dp array and initialize it with 0 s ; loop for substring length we are considering ; loop with two variables i and j , denoting starting and ending of substrings ; If substring length is 1 , then 1 step will be needed ; delete the ith char individually and assign result for subproblem ( i + 1 , j ) ; if current and next char are same , choose min from current and subproblem ( i + 2 , j ) ; loop over all right characters and suppose Kth char is same as ith character then choose minimum from current and two substring after ignoring ith and Kth char ; Uncomment below snippet to print actual dp tablex for ( int i = 0 ; i < N ; i ++ ) { System . out . println ( ) ; for ( int j = 0 ; j < N ; j ++ ) System . out . print ( dp [ i ] [ j ] + " ▁ " ) ; } ; Driver code to test above methods
function minStepToDeleteString ( 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 len = 1 ; len <= N ; len ++ ) { for ( let i = 0 , j = len - 1 ; j < N ; i ++ , j ++ ) { if ( len == 1 ) dp [ i ] [ j ] = 1 ; else { dp [ i ] [ j ] = 1 + dp [ i + 1 ] [ j ] ; if ( str [ i ] == str [ i + 1 ] ) dp [ i ] [ j ] = Math . min ( 1 + dp [ i + 2 ] [ j ] , dp [ i ] [ j ] ) ; for ( let K = i + 2 ; K <= j ; K ++ ) if ( str [ i ] == str [ K ] ) dp [ i ] [ j ] = Math . min ( dp [ i + 1 ] [ K - 1 ] + dp [ K + 1 ] [ j ] , dp [ i ] [ j ] ) ; } } } return dp [ 0 ] [ N - 1 ] ; } let str = " " ; document . write ( minStepToDeleteString ( str ) ) ;
Clustering / Partitioning an array such that sum of square differences is minimum | Initialize answer as infinite . ; function to generate all possible answers . and compute minimum of all costs . i -- > is index of previous partition par -- > is current number of partitions a [ ] and n -- > Input array and its size current_ans -- > Cost of partitions made so far . ; If number of partitions is more than k ; If we have mad k partitions and have reached last element ; 1 ) Partition array at different points 2 ) For every point , increase count of partitions , " par " by 1. 3 ) Before recursive call , add cost of the partition to current_ans ; Driver code
let inf = 1000000000 ; let ans = inf ; function solve ( i , par , a , n , k , current_ans ) { if ( par > k ) return ; if ( par == k && i == n - 1 ) { ans = Math . min ( ans , current_ans ) ; return ; } for ( let j = i + 1 ; j < n ; j ++ ) solve ( j , par + 1 , a , n , k , current_ans + ( a [ j ] - a [ i + 1 ] ) * ( a [ j ] - a [ i + 1 ] ) ) ; } let k = 2 ; let a = [ 1 , 5 , 8 , 10 ] ; let n = a . length ; solve ( - 1 , 0 , a , n , k , 0 ) ; document . write ( ans ) ;
Count of arrays in which all adjacent elements are such that one of them divide the another | Javascript program to count number of arrays of size n such that every element is in range [ 1 , m ] and adjacen are divisible ; For storing factors . ; For storing multiples . ; Calculating the factors and multiples of elements [ 1. . . m ] . ; Initialising for size 1 array for each i <= m . ; Calculating the number of array possible of size i and starting with j . ; For all previous possible values . Adding number of factors . ; Adding number of multiple . ; Calculating the total count of array which start from [ 1. . . m ] . ; Driver Code
let MAX = 1000 ; function numofArray ( n , m ) { let dp = new Array ( MAX ) ; let di = new Array ( MAX ) ; let mu = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { dp [ i ] = new Array ( MAX ) ; for ( let j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < MAX ; i ++ ) { di [ i ] = [ ] ; mu [ i ] = [ ] ; } for ( let i = 1 ; i <= m ; i ++ ) { for ( let j = 2 * i ; j <= m ; j += i ) { di [ j ] . push ( i ) ; mu [ i ] . push ( j ) ; } di [ i ] . push ( i ) ; } for ( let i = 1 ; i <= m ; i ++ ) dp [ 1 ] [ i ] = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { for ( let j = 1 ; j <= m ; j ++ ) { dp [ i ] [ j ] = 0 ; for ( let x = 0 ; x < di [ j ] . length ; x ++ ) dp [ i ] [ j ] += dp [ i - 1 ] [ di [ j ] [ x ] ] ; for ( let x = 0 ; x < mu [ j ] . length ; x ++ ) dp [ i ] [ j ] += dp [ i - 1 ] [ mu [ j ] [ x ] ] ; } } let ans = 0 ; for ( let i = 1 ; i <= m ; i ++ ) { ans += dp [ n ] [ i ] ; di [ i ] = [ ] ; mu [ i ] = [ ] ; } return ans ; } let n = 3 , m = 3 ; document . write ( numofArray ( n , m ) ) ;
Smallest length string with repeated replacement of two distinct adjacent | Returns smallest possible length with given operation allowed . ; Counvar occurrences of three different characters ' a ' , ' b ' and ' c ' in str ; If all characters are same . ; If all characters are present even number of times or all are present odd number of times . ; Answer is 1 for all other cases . ; Driver code
function stringReduction ( str ) { var n = str . length ; var count = Array . from ( { length : 3 } , ( _ , i ) => 0 ) ; for ( var i = 0 ; i < n ; ++ i ) { count [ str . charAt ( i ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } if ( count [ 0 ] == n count [ 1 ] == n count [ 2 ] == n ) { return n ; } if ( ( count [ 0 ] % 2 ) == ( count [ 1 ] % 2 ) && ( count [ 1 ] % 2 ) == ( count [ 2 ] % 2 ) ) { return 2 ; } return 1 ; } var str = " " ; document . write ( stringReduction ( str ) ) ;
Largest sum Zigzag sequence in a matrix | Memoization based Javascript program to find the largest sum zigzag sequence ; Returns largest sum of a Zigzag sequence starting from ( i , j ) and ending at a bottom cell . ; If we have reached bottom ; Find the largest sum by considering all possible next elements in sequence . ; Returns largest possible sum of a Zizag sequence starting from top and ending at bottom . ; Consider all cells of top row as starting point ; Driver code
let MAX = 100 ; let dp = new Array ( MAX ) ; function largestZigZagSumRec ( mat , i , j , n ) { if ( dp [ i ] [ j ] != - 1 ) return dp [ i ] [ j ] ; if ( i == n - 1 ) return ( dp [ i ] [ j ] = mat [ i ] [ j ] ) ; let zzs = 0 ; for ( let k = 0 ; k < n ; k ++ ) if ( k != j ) zzs = Math . max ( zzs , largestZigZagSumRec ( mat , i + 1 , k , n ) ) ; return ( dp [ i ] [ j ] = ( zzs + mat [ i ] [ j ] ) ) ; } function largestZigZag ( mat , n ) { for ( let i = 0 ; i < MAX ; i ++ ) { dp [ i ] = new Array ( MAX ) ; for ( let k = 0 ; k < MAX ; k ++ ) dp [ i ] [ k ] = - 1 ; } let res = 0 ; for ( let j = 0 ; j < n ; j ++ ) res = Math . max ( res , largestZigZagSumRec ( mat , 0 , j , n ) ) ; return res ; } let n = 3 ; let mat = [ [ 4 , 2 , 1 ] , [ 3 , 9 , 6 ] , [ 11 , 3 , 15 ] ] ; document . write ( " " + largestZigZag ( mat , n ) ) ;
Number of subsequences of the form a ^ i b ^ j c ^ k | Returns count of subsequences of the form a ^ i b ^ j c ^ k ; Initialize counts of different subsequences caused by different combination of ' a ' ; Initialize counts of different subsequences caused by different combination of ' a ' and different combination of ' b ' ; Initialize counts of different subsequences caused by different combination of ' a ' , ' b ' and ' c ' ; Traverse all characters of given string ; If current character is ' a ' , then there are following possibilities : a ) Current character begins a new subsequence . b ) Current character is part of aCount subsequences c ) Current character is not part of aCount subsequences . ; If current character is ' b ' , then there are following possibilities : a ) Current character begins a new subsequence of b 's with aCount subsequences. b) Current character is part of bCount subsequences. c) Current character is not part of  bCount subsequences. ; If current character is ' c ' , then there are following possibilities : a ) Current character begins a new subsequence of c 's with bCount subsequences. b) Current character is part of cCount subsequences. c) Current character is not part of  cCount subsequences. ; Driver Code
function countSubsequences ( s ) { let aCount = 0 ; let bCount = 0 ; let cCount = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) aCount = ( 1 + 2 * aCount ) ; else if ( s [ i ] == ' ' ) bCount = ( aCount + 2 * bCount ) ; else if ( s [ i ] == ' ' ) cCount = ( bCount + 2 * cCount ) ; } return cCount ; } let s = " " ; document . write ( countSubsequences ( s ) ) ;
Highway Billboard Problem | Javascript program to find maximum revenue by placing billboard on the highway with given constraints . ; Array to store maximum revenue at each miles . ; actual minimum distance between 2 billboards . ; check if all billboards are already placed . ; check if we have billboard for that particular mile . If not , copy the previous maximum revenue . ; we do have billboard for this mile . ; If current position is less than or equal to t , then we can have only one billboard . ; Else we may have to remove previously placed billboard ; Driver Code
function maxRevenue ( m , x , revenue , n , t ) { let maxRev = new Array ( m + 1 ) ; for ( let i = 0 ; i < m + 1 ; i ++ ) maxRev [ i ] = 0 ; let nxtbb = 0 ; for ( let i = 1 ; i <= m ; i ++ ) { if ( nxtbb < n ) { if ( x [ nxtbb ] != i ) maxRev [ i ] = maxRev [ i - 1 ] ; else { if ( i <= t ) maxRev [ i ] = Math . max ( maxRev [ i - 1 ] , revenue [ nxtbb ] ) ; else maxRev [ i ] = Math . max ( maxRev [ i - t - 1 ] + revenue [ nxtbb ] , maxRev [ i - 1 ] ) ; nxtbb ++ ; } } else maxRev [ i ] = maxRev [ i - 1 ] ; } return maxRev [ m ] ; } let m = 20 ; let x = [ 6 , 7 , 12 , 13 , 14 ] ; let revenue = [ 5 , 6 , 5 , 3 , 1 ] ; let n = x . length ; let t = 5 ; document . write ( maxRevenue ( m , x , revenue , n , t ) ) ;