codeparrot/xlcost-text-to-code huggingface dataset:
Maximize array elements upto given number | Function to find maximum possible value of number that can be obtained using array elements . ; Variable to represent current index . ; Variable to show value between 0 and maxLimit . ; Table to store whether a value can be obtained or not upto a certain index . 1. dp [ i ] [ j ] = 1 if value j can be obtained upto index i . 2. dp [ i ] [ j ] = 0 if value j cannot be obtained upto index i . ; Check for index 0 if given value val can be obtained by either adding to or subtracting arr [ 0 ] from num . ; 1. If arr [ ind ] is added to obtain given val then val - arr [ ind ] should be obtainable from index ind - 1. 2. If arr [ ind ] is subtracted to obtain given val then val + arr [ ind ] should be obtainable from index ind - 1. Check for both the conditions . ; If either of one condition is true , then val is obtainable at index ind . ; Find maximum value that is obtained at index n - 1. ; If no solution exists return - 1. ; Driver Code
function findMaxVal ( arr , n , num , maxLimit ) { var ind ; var val ; var dp = Array . from ( Array ( n ) , ( ) => Array ( maxLimit + 1 ) ) ; for ( ind = 0 ; ind < n ; ind ++ ) { for ( val = 0 ; val <= maxLimit ; val ++ ) { if ( ind == 0 ) { if ( num - arr [ ind ] == val num + arr [ ind ] == val ) { dp [ ind ] [ val ] = 1 ; } else { dp [ ind ] [ val ] = 0 ; } } else { if ( val - arr [ ind ] >= 0 && val + arr [ ind ] <= maxLimit ) { dp [ ind ] [ val ] = dp [ ind - 1 ] [ val - arr [ ind ] ] || dp [ ind - 1 ] [ val + arr [ ind ] ] ; } else if ( val - arr [ ind ] >= 0 ) { dp [ ind ] [ val ] = dp [ ind - 1 ] [ val - arr [ ind ] ] ; } else if ( val + arr [ ind ] <= maxLimit ) { dp [ ind ] [ val ] = dp [ ind - 1 ] [ val + arr [ ind ] ] ; } else { dp [ ind ] [ val ] = 0 ; } } } } for ( val = maxLimit ; val >= 0 ; val -- ) { if ( dp [ n - 1 ] [ val ] ) { return val ; } } return - 1 ; } var num = 1 ; var arr = [ 3 , 10 , 6 , 4 , 5 ] ; var n = arr . length ; var maxLimit = 15 ; document . write ( findMaxVal ( arr , n , num , maxLimit ) ) ;
Moser | Function to generate nth term of Moser - de Bruijn Sequence ; S ( 0 ) = 0 ; S ( 1 ) = 1 ; S ( 2 * n ) = 4 * S ( n ) ; S ( 2 * n + 1 ) = 4 * S ( n ) + 1 ; Generating the first ' n ' terms of Moser - de Bruijn Sequence ; Driver code
function gen ( n ) { if ( n == 0 ) return 0 ; else if ( n == 1 ) return 1 ; else if ( n % 2 == 0 ) return 4 * gen ( parseInt ( n / 2 , 10 ) ) ; else if ( n % 2 == 1 ) return 4 * gen ( parseInt ( n / 2 , 10 ) ) + 1 ; return 0 ; } function moserDeBruijn ( n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( gen ( i ) + " " ) ; document . write ( " " ) ; } let n = 15 ; document . write ( " " + n + " " + " " + " " ) ; moserDeBruijn ( n ) ;
Minimum Sum Path in a Triangle | JavaScript program for Dynamic Programming implementation of Min Sum Path in a Triangle ; Util function to find minimum sum for a path ; For storing the result in a 1 - D array , and simultaneously updating the result . ; For the bottom row ; Calculation of the remaining rows , in bottom up manner . ; Return the top element ; Driver code
let A = [ [ 2 ] , [ 3 , 9 ] , [ 1 , 6 , 7 ] ] ; function minSumPath ( ) { let memo = [ ] ; let n = A . length - 1 ; for ( let i = 0 ; i < A [ n ] . length ; i ++ ) memo [ i ] = A [ n ] [ i ] ; for ( let i = A . length - 2 ; i >= 0 ; i -- ) for ( let j = 0 ; j < A [ i ] . length ; j ++ ) memo [ j ] = A [ i ] [ j ] + Math . min ( memo [ j ] , memo [ j + 1 ] ) ; return memo [ 0 ] ; } document . write ( minSumPath ( ) ) ;
Check if any valid sequence is divisible by M |  ; Base case ; Check if sum is divisible by M ; 1. Try placing ' + ' ; 2. Try placing ' - '
function isPossible ( index , sum ) { if ( index == n ) { if ( ( sum % M ) == 0 ) return true ; return false ; } let placeAdd = isPossible ( index + 1 , sum + arr [ index ] ) ; let placeMinus = isPossible ( index + 1 , sum - arr [ index ] ) ; if ( placeAdd placeMinus ) return true ; return false ; }
Minimum removals from array to make max | JavaScript program to find minimum removals to make max - min <= K ; function to check all possible combinations of removal and return the minimum one ; base case when all elements are removed ; if condition is satisfied , no more removals are required ; if the state has already been visited ; when Amax - Amin > d ; minimum is taken of the removal of minimum element or removal of the maximum element ; To sort the array and return the answer ; sort the array ; fill all stated with - 1 when only one element ; Driver Code
let MAX = 100 ; let dp = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { dp [ i ] = new Array ( MAX ) ; } function countRemovals ( a , i , j , k ) { if ( i >= j ) return 0 ; else if ( ( a [ j ] - a [ i ] ) <= k ) return 0 ; else if ( dp [ i ] [ j ] != - 1 ) return dp [ i ] [ j ] ; else if ( ( a [ j ] - a [ i ] ) > k ) { dp [ i ] [ j ] = 1 + Math . min ( countRemovals ( a , i + 1 , j , k ) , countRemovals ( a , i , j - 1 , k ) ) ; } return dp [ i ] [ j ] ; } function removals ( a , n , k ) { a . sort ( function ( a , b ) { return a - b } ) ; for ( let i = 0 ; i < MAX ; i ++ ) { for ( let j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } if ( n == 1 ) return 0 ; else return countRemovals ( a , 0 , n - 1 , k ) ; } let a = [ 1 , 3 , 4 , 9 , 10 , 11 , 12 , 17 , 20 ] ; let n = a . length ; let k = 4 ; document . write ( removals ( a , n , k ) ) ;
Minimum removals from array to make max | Function to find rightmost index which satisfies the condition arr [ j ] - arr [ i ] <= k ; Initialising start to i + 1 ; Initialising end to n - 1 ; Binary search implementation to find the rightmost element that satisfy the condition ; Check if the condition satisfies ; Store the position ; Make start = mid + 1 ; Make end = mid ; Return the rightmost position ; Function to calculate minimum number of elements to be removed ; Sort the given array ; Iterate from 0 to n - 1 ; Find j such that arr [ j ] - arr [ i ] <= k ; If there exist such j that satisfies the condition ; Number of elements to be removed for this particular case is ( j - i + 1 ) ; Return answer ; Driver Code
function findInd ( key , i , n , k , arr ) { var start , end , mid , ind = - 1 ; start = i + 1 ; end = n - 1 ; while ( start < end ) { mid = start + ( end - start ) / 2 ; if ( arr [ mid ] - key <= k ) { ind = mid ; start = mid + 1 ; } else { end = mid ; } } return ind ; } function removals ( arr , n , k ) { var i , j , ans = n - 1 ; arr . sort ( ( a , b ) => a - b ) ; for ( i = 0 ; i < n ; i ++ ) { j = findInd ( arr [ i ] , i , n , k , arr ) ; if ( j != - 1 ) { ans = Math . min ( ans , n - ( j - i + 1 ) ) ; } } return ans ; } var a = [ 1 , 3 , 4 , 9 , 10 , 11 , 12 , 17 , 20 ] ; var n = a . length ; var k = 4 ; document . write ( removals ( a , n , k ) ) ;
Minimum sum of multiplications of n numbers | Used in recursive memoized solution ; function to calculate the cumulative sum from a [ i ] to a [ j ] ; base case ; memoization , if the partition has been called before then return the stored value ; store a max value ; we break them into k partitions ; store the min of the formula thus obtained ; return the minimum ; Driver code
var dp = Array . from ( Array ( 1000 ) , ( ) => Array ( 1000 ) ) ; function sum ( a , i , j ) { var ans = 0 ; for ( var m = i ; m <= j ; m ++ ) ans = ( ans + a [ m ] ) % 100 ; return ans ; } function solve ( a , i , j ) { if ( i == j ) return 0 ; if ( dp [ i ] [ j ] != - 1 ) return dp [ i ] [ j ] ; dp [ i ] [ j ] = 1000000000 ; for ( var k = i ; k < j ; k ++ ) { dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , ( solve ( a , i , k ) + solve ( a , k + 1 , j ) + ( sum ( a , i , k ) * sum ( a , k + 1 , j ) ) ) ) ; } return dp [ i ] [ j ] ; } function initialize ( n ) { for ( var i = 0 ; i <= n ; i ++ ) for ( var j = 0 ; j <= n ; j ++ ) dp [ i ] [ j ] = - 1 ; } var a = [ 40 , 60 , 20 ] ; var n = a . length ; initialize ( n ) ; document . write ( solve ( a , 0 , n - 1 ) ) ;
Print Fibonacci Series in reverse order | JavaScript program to print Fibonacci series in reverse order ; Assigning first and second elements ; Storing sum in the preceding location ; Printing array in reverse order ; Driver Code
function reverseFibonacci ( n ) { let a = [ ] ; a [ 0 ] = 0 ; a [ 1 ] = 1 ; for ( let i = 2 ; i < n ; i ++ ) { a [ i ] = a [ i - 2 ] + a [ i - 1 ] ; } for ( let i = n - 1 ; i >= 0 ; i -- ) { document . write ( a [ i ] + " " ) ; } } let n = 5 ; reverseFibonacci ( n ) ;
Check if it is possible to transform one string to another | function to check if a string can be converted to another string by performing following operations ; calculates length ; mark 1 st position as true ; traverse for all DPi , j ; if possible for to convert i characters of s1 to j characters of s2 ; if upper_case ( s1 [ i ] ) == s2 [ j ] is same ; if not upper then deletion is possible ; driver code
function check ( s1 , s2 ) { let n = s1 . length ; let m = s2 . length ; let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i <= n ; i ++ ) { dp [ i ] = new Array ( m + 1 ) ; for ( let j = 0 ; j <= m ; j ++ ) { dp [ i ] [ j ] = false ; } } dp [ 0 ] [ 0 ] = true ; for ( let i = 0 ; i < s1 . length ; i ++ ) { for ( let j = 0 ; j <= s2 . length ; j ++ ) { if ( dp [ i ] [ j ] ) { if ( j < s2 . length && ( s1 [ i ] . toUpperCase ( ) == s2 [ j ] ) ) dp [ i + 1 ] [ j + 1 ] = true ; if ( ! ( s1 [ i ] == s1 [ i ] . toUpperCase ( ) ) ) dp [ i + 1 ] [ j ] = true ; } } } return ( dp [ n ] [ m ] ) ; } let s1 = " " ; let s2 = " " ; if ( check ( s1 , s2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Probability of reaching a point with 2 or 3 steps at a time | Returns probability to reach N ; Driver Code
function find_prob ( N , P ) { let dp = [ ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 0 ; dp [ 2 ] = P ; dp [ 3 ] = 1 - P ; for ( let i = 4 ; i <= N ; ++ i ) dp [ i ] = ( P ) * dp [ i - 2 ] + ( 1 - P ) * dp [ i - 3 ] ; return ( dp [ N ] ) ; } let n = 5 ; let p = 0.2 ; document . write ( find_prob ( n , p ) ) ;
Minimum number of deletions to make a string palindrome | Set 2 | Javascript program to find minimum deletions to make palindrome . ; Find reverse of input string ; Create a DP table for storing edit distance of string and reverse . ; Find edit distance between input and revInput considering only delete operation . ; Go from bottom left to top right and find the minimum ; Driver Code
function getLevenstein ( input ) { let revInput = ( input ) . split ( " " ) ; revInput = revInput . reverse ( ) ; let n = input . length ; let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) { dp [ i ] = new Array ( n + 1 ) ; for ( let j = 0 ; j < n + 1 ; j ++ ) dp [ i ] [ j ] = 0 ; } for ( let i = 0 ; i <= n ; ++ i ) { dp [ 0 ] [ i ] = i ; dp [ i ] [ 0 ] = i ; } for ( let i = 1 ; i <= n ; ++ i ) { for ( let j = 1 ; j <= n ; ++ j ) { if ( input [ i - 1 ] == revInput [ j - 1 ] ) dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = 1 + Math . min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } let res = Number . MAX_VALUE ; for ( let i = n , j = 0 ; i >= 0 ; i -- , j ++ ) { res = Math . min ( res , dp [ i ] [ j ] ) ; if ( i < n ) res = Math . min ( res , dp [ i + 1 ] [ j ] ) ; if ( i > 0 ) res = Math . min ( res , dp [ i - 1 ] [ j ] ) ; } return res ; } let input = ( " " ) ; document . write ( getLevenstein ( input ) ) ;
Number of ways to form a heap with n distinct integers | dp [ i ] = number of max heaps for i distinct integers ; nck [ i ] [ j ] = number of ways to choose j elements form i elements , no order ; log2 [ i ] = floor of logarithm of base 2 of i ; to calculate nCk ; calculate l for give value of n ; number of elements that are actually present in last level ( hth level ) ( 2 ^ h - 1 ) ; if more than half - filled ; return ( 1 << h ) - 1 ; ( 2 ^ h ) - 1 ; find maximum number of heaps for n ; function to initialize arrays ; for each power of two find logarithm ; Driver code
let dp = new Array ( MAXN ) ; let nck = new Array ( MAXN ) ; for ( let i = 0 ; i < MAXN ; i ++ ) { nck [ i ] = new Array ( MAXN ) ; for ( let j = 0 ; j < MAXN ; j ++ ) nck [ i ] [ j ] = 0 ; } let log2 = new Array ( MAXN ) ; function choose ( n , k ) { if ( k > n ) { return 0 ; } if ( n <= 1 ) { return 1 ; } if ( k == 0 ) { return 1 ; } if ( nck [ n ] [ k ] != - 1 ) { return nck [ n ] [ k ] ; } let answer = choose ( n - 1 , k - 1 ) + choose ( n - 1 , k ) ; nck [ n ] [ k ] = answer ; return answer ; } function getLeft ( n ) { if ( n == 1 ) { return 0 ; } let h = log2 [ n ] ; let last = n - ( ( 1 << h ) - 1 ) ; if ( last >= ( numh / 2 ) ) { } else { return ( 1 << h ) - 1 - ( ( numh / 2 ) - last ) ; } } function numberOfHeaps ( n ) { if ( n <= 1 ) { return 1 ; } if ( dp [ n ] != - 1 ) { return dp [ n ] ; } let left = getLeft ( n ) ; let ans = ( choose ( n - 1 , left ) * numberOfHeaps ( left ) ) * ( numberOfHeaps ( n - 1 - left ) ) ; dp [ n ] = ans ; return ans ; } function solve ( n ) { for ( let i = 0 ; i <= n ; i ++ ) { dp [ i ] = - 1 ; } for ( let i = 0 ; i <= n ; i ++ ) { for ( let j = 0 ; j <= n ; j ++ ) { nck [ i ] [ j ] = - 1 ; } } let currLog2 = - 1 ; let currPower2 = 1 ; for ( let i = 1 ; i <= n ; i ++ ) { if ( currPower2 == i ) { currLog2 ++ ; currPower2 *= 2 ; } log2 [ i ] = currLog2 ; } return numberOfHeaps ( n ) ; } let n = 10 ; document . write ( solve ( n ) ) ;
Hosoya 's Triangle | Javascript Program to print Hosoya 's triangle of height n. ; Print the Hosoya triangle of height n . ; base case . ; For each row . ; for each column ; ; recursive steps . ; printing the solution ; Driven Program
var N = 5 function printHosoya ( n ) { var dp = Array . from ( Array ( N ) , ( ) => Array ( N ) . fill ( 0 ) ) ; dp [ 0 ] [ 0 ] = dp [ 1 ] [ 0 ] = dp [ 1 ] [ 1 ] = 1 ; for ( var i = 2 ; i < n ; i ++ ) { for ( var j = 0 ; j < n ; j ++ ) { if ( i > j ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 2 ] [ j ] ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] + dp [ i - 2 ] [ j - 2 ] ; } } for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j <= i ; j ++ ) document . write ( dp [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } var n = 5 ; printHosoya ( n ) ;
Different ways to sum n using numbers greater than or equal to m | Return number of ways to which numbers that are greater than given number can be added to get sum . ; Filling the table . k is for numbers greater than or equal that are allowed . ; i is for sum ; initializing dp [ i ] [ k ] to number ways to get sum using numbers greater than or equal k + 1 ; if i > k ; Driver Program
function numberofways ( n , m ) { let dp = new Array ( n + 2 ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( n + 2 ) ; for ( let j = 0 ; j < dp [ i ] . length ; j ++ ) { dp [ i ] [ j ] = 0 ; } } dp [ 0 ] [ n + 1 ] = 1 ; for ( let k = n ; k >= m ; k -- ) { for ( let i = 0 ; i <= n ; i ++ ) { dp [ i ] [ k ] = dp [ i ] [ k + 1 ] ; if ( i - k >= 0 ) dp [ i ] [ k ] = ( dp [ i ] [ k ] + dp [ i - k ] [ k ] ) ; } } return dp [ n ] [ m ] ; } let n = 3 , m = 1 ; document . write ( numberofways ( n , m ) ) ;
Entringer Number | Return Entringer Number E ( n , k ) ; Base Case ; Base Case ; Recursive step ; Driven Program
function zigzag ( n , k ) { if ( n == 0 && k == 0 ) return 1 ; if ( k == 0 ) return 0 ; return zigzag ( n , k - 1 ) + zigzag ( n - 1 , n - k ) ; } n = 4 ; k = 3 ; document . write ( zigzag ( n , k ) ) ;
Eulerian Number | Return euleriannumber A ( n , m ) ; For each row from 1 to n ; For each column from 0 to m ; If i is greater than j ; If j is 0 , then make that state as 1. ; basic recurrence relation . ; Driven Program
function eulerian ( n , m ) { var dp = Array . from ( Array ( n + 1 ) , ( ) => Array ( m + 1 ) . fill ( 0 ) ) ; for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = 0 ; j <= m ; j ++ ) { if ( i > j ) { if ( j == 0 ) dp [ i ] [ j ] = 1 ; else dp [ i ] [ j ] = ( ( i - j ) * dp [ i - 1 ] [ j - 1 ] ) + ( ( j + 1 ) * dp [ i - 1 ] [ j ] ) ; } } } return dp [ n ] [ m ] ; } var n = 3 , m = 1 ; document . write ( eulerian ( n , m ) ) ;
Delannoy Number | Return the nth Delannoy Number . ; Base case ; Recursive step . ; Driver code
function dealnnoy ( n , m ) { if ( m == 0 n == 0 ) return 1 ; return dealnnoy ( m - 1 , n ) + dealnnoy ( m - 1 , n - 1 ) + dealnnoy ( m , n - 1 ) ; } let n = 3 , m = 4 ; document . write ( dealnnoy ( n , m ) ) ;
Delannoy Number | Return the nth Delannoy Number . ; Base cases ; Driven Program
function dealnnoy ( n , m ) { var dp = Array . from ( Array ( m + 1 ) , ( ) => Array ( n + 1 ) ) ; for ( var i = 0 ; i <= m ; i ++ ) dp [ i ] [ 0 ] = 1 ; for ( var i = 0 ; i <= m ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( var i = 1 ; i <= m ; i ++ ) for ( var j = 1 ; j <= n ; j ++ ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j - 1 ] + dp [ i ] [ j - 1 ] ; return dp [ m ] [ n ] ; } var n = 3 , m = 4 ; document . write ( dealnnoy ( n , m ) ) ;
Longest alternating ( positive and negative ) subarray starting at every index | JavaScript program to find longest alternating subarray starting from every index . ; Fill count [ ] from end . ; Prlet result ; Driver code
function longestAlternating ( arr , n ) { let count = new Array ( n ) ; count [ n - 1 ] = 1 ; for ( let i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] * arr [ i + 1 ] < 0 ) count [ i ] = count [ i + 1 ] + 1 ; else count [ i ] = 1 ; } for ( let i = 0 ; i < n ; i ++ ) document . write ( count [ i ] + " " ) ; } let a = [ - 5 , - 1 , - 1 , 2 , - 2 , - 3 ] ; let n = a . length ; longestAlternating ( a , n ) ;
Maximum value with the choice of either dividing or considering as it is | function for calculating max possible result ; Compute remaining values in bottom up manner . ; Driver code
function maxDP ( n ) { let res = [ ] ; res [ 0 ] = 0 ; res [ 1 ] = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { res [ i ] = Math . max ( i , ( res [ Math . floor ( i / 2 ) ] + res [ Math . floor ( i / 3 ) ] + res [ Math . floor ( i / 4 ) ] + res [ Math . floor ( i / 5 ) ] ) ) ; } return res [ n ] ; } let n = 60 ; document . write ( " " + maxDP ( n ) ) ;
Maximum difference of zeros and ones in binary string | Javascript Program to find the length of substring with maximum difference of zeroes and ones in binary string . ; Return true if there all 1 s ; Checking each index is 0 or not . ; Find the length of substring with maximum difference of zeroes and ones in binary string ; If string is over . ; If the state is already calculated . ; Returns length of substring which is having maximum difference of number of 0 s and number of 1 s ; If all 1 s return - 1. ; Else find the length . ; Driven Program
var MAX = 100 ; function allones ( s , n ) { var co = 0 ; for ( var i = 0 ; i < s . length ; i ++ ) co += ( s [ i ] == ' ' ) ; return ( co == n ) ; } function findlength ( arr , s , n , ind , st , dp ) { if ( ind >= n ) return 0 ; if ( dp [ ind ] [ st ] != - 1 ) return dp [ ind ] [ st ] ; if ( st == 0 ) return dp [ ind ] [ st ] = Math . max ( arr [ ind ] + findlength ( arr , s , n , ind + 1 , 1 , dp ) , findlength ( arr , s , n , ind + 1 , 0 , dp ) ) ; else return dp [ ind ] [ st ] = Math . max ( arr [ ind ] + findlength ( arr , s , n , ind + 1 , 1 , dp ) , 0 ) ; } function maxLen ( s , n ) { if ( allones ( s , n ) ) return - 1 ; var arr = Array ( MAX ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) arr [ i ] = ( s [ i ] == ' ' ? 1 : - 1 ) ; var dp = Array . from ( Array ( MAX ) , ( ) => Array ( 3 ) . fill ( - 1 ) ) ; return findlength ( arr , s , n , 0 , 0 , dp ) ; } var s = " " ; var n = 11 ; document . write ( maxLen ( s , n ) ) ;
Minimum jumps to reach last building in a matrix | Recursive Javascript program to find minimum jumps to reach last building from first . ; Returns minimum jump path from ( 0 , 0 ) to ( m , n ) in height [ R ] [ C ] ; base case ; Find minimum jumps if we go through diagonal ; Find minimum jumps if we go through down ; Find minimum jumps if we go through right ; return minimum jumps ; Driver program to test above functions
R = 4 C = 3 function isSafe ( x , y ) { return ( x < R && y < C ) ; } function minJump ( height , x , y ) { if ( x == R - 1 && y == C - 1 ) return 0 ; var diag = 1000000000 ; if ( isSafe ( x + 1 , y + 1 ) ) diag = minJump ( height , x + 1 , y + 1 ) + Math . abs ( height [ x ] [ y ] - height [ x + 1 ] [ y + 1 ] ) ; var down = 1000000000 ; if ( isSafe ( x + 1 , y ) ) down = minJump ( height , x + 1 , y ) + Math . abs ( height [ x ] [ y ] - height [ x + 1 ] [ y ] ) ; var right = 1000000000 ; if ( isSafe ( x , y + 1 ) ) right = minJump ( height , x , y + 1 ) + Math . abs ( height [ x ] [ y ] - height [ x ] [ y + 1 ] ) ; return Math . min ( down , Math . min ( right , diag ) ) ; } var height = [ [ 5 , 4 , 2 ] , [ 9 , 2 , 1 ] , [ 2 , 5 , 9 ] , [ 1 , 3 , 11 ] ] ; document . write ( minJump ( height , 0 , 0 ) ) ;
Length of Longest Balanced Subsequence | Javascript program to find the length of the longest balanced subsequence . ; Variable to count all the open brace that does not have the corresponding closing brace . ; To count all the close brace that does not have the corresponding open brace . ; Iterating over the String ; Number of open braces that hasn 't been closed yet.vvvvvv ; Number of close braces that cannot be mapped to any open brace . ; Mapping the ith close brace to one of the open brace . ; driver program
function maxLength ( s , n ) { let invalidOpenBraces = 0 ; let invalidCloseBraces = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) { invalidOpenBraces ++ ; } else { if ( invalidOpenBraces == 0 ) { invalidCloseBraces ++ ; } else { invalidOpenBraces -- ; } } } return ( n - ( invalidOpenBraces + invalidCloseBraces ) ) ; } let s = " " ; let n = s . length ; document . write ( maxLength ( s , n ) ) ;
Longest alternating sub | Function to calculate alternating sub - array for each index of array elements ; Initialize the base state of len [ ] ; Calculating value for each element ; If both elements are different then add 1 to next len [ i + 1 ] ; else initialize to 1 ; Prlet lengths of binary subarrays . ; Driver program
function alternateSubarray ( arr , n ) { let len = new Array ( n ) ; len [ n - 1 ] = 1 ; for ( let i = n - 2 ; i >= 0 ; -- i ) { if ( arr [ i ] ^ arr [ i + 1 ] == 1 ) len [ i ] = len [ i + 1 ] + 1 ; else len [ i ] = 1 ; } for ( let i = 0 ; i < n ; ++ i ) document . write ( len [ i ] + " " ) ; } let arr = [ 1 , 0 , 1 , 0 , 0 , 1 ] ; let n = arr . length ; alternateSubarray ( arr , n ) ;
Longest alternating sub | Function to calculate alternating sub - array for each index of array elements ; Initialize count variable for storing length of sub - array ; Initialize ' prev ' variable which indicates the previous element while traversing for index ' i ' ; If both elements are same print elements because alternate element is not found for current index ; Print count and decrement it . ; Increment count for next element ; Re - initialize previous variable ; If elements are still available after traversing whole array , print it ; Driver code
function alternateSubarray ( arr , n ) { let count = 1 ; let prev = arr [ 0 ] ; for ( let i = 1 ; i < n ; ++ i ) { if ( ( arr [ i ] ^ prev ) == false ) { while ( count > 0 ) { document . write ( count -- + " " ) ; } } ++ count ; prev = arr [ i ] ; } while ( count != 0 ) { document . write ( count -- + " " ) ; } } let arr = [ true , false , true , false , false , true ] ; let n = arr . length ; alternateSubarray ( arr , n ) ;