codeparrot/xlcost-text-to-code huggingface dataset:
Find the maximum sum of Plus shape pattern in a 2 | JavaScript program to find the maximum value of a + shaped pattern in 2 - D array ; Function to return maximum Plus value ; Initializing answer with the minimum value ; Initializing all four arrays ; Initializing left and up array . ; Initializing right and down array . ; calculating value of maximum Plus ( + ) sign ; Driver code ; Function call to find maximum value
let N = 100 ; let n = 3 , m = 4 ; function maxPlus ( arr ) { let ans = 0 ; let left = new Array ( N ) ; let right = new Array ( N ) ; let up = new Array ( N ) ; let down = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { left [ i ] = new Array ( N ) ; right [ i ] = new Array ( N ) ; up [ i ] = new Array ( N ) ; down [ i ] = new Array ( N ) ; for ( let j = 0 ; j < N ; j ++ ) { left [ i ] [ j ] = 0 ; right [ i ] [ j ] = 0 ; up [ i ] [ j ] = 0 ; down [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { left [ i ] [ j ] = Math . max ( 0 , ( ( j != 0 ) ? left [ i ] [ j - 1 ] : 0 ) ) + arr [ i ] [ j ] ; up [ i ] [ j ] = Math . max ( 0 , ( ( i != 0 ) ? up [ i - 1 ] [ j ] : 0 ) ) + arr [ i ] [ j ] ; } } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { right [ i ] [ j ] = Math . max ( 0 , ( j + 1 == m ? 0 : right [ i ] [ j + 1 ] ) ) + arr [ i ] [ j ] ; down [ i ] [ j ] = Math . max ( 0 , ( i + 1 == n ? 0 : down [ i + 1 ] [ j ] ) ) + arr [ i ] [ j ] ; } } for ( let i = 1 ; i < n - 1 ; ++ i ) for ( let j = 1 ; j < m - 1 ; ++ j ) { ans = Math . max ( ans , up [ i - 1 ] [ j ] + down [ i + 1 ] [ j ] + left [ i ] [ j - 1 ] + right [ i ] [ j + 1 ] + arr [ i ] [ j ] ) ; } return ans ; } let arr = [ [ 1 , 1 , 1 , 1 ] , [ - 6 , 1 , 1 , - 4 ] , [ 1 , 1 , 1 , 1 ] ] ; document . write ( maxPlus ( arr ) ) ;
Total number of different staircase that can made from N boxes | Function to find the total number of different staircase that can made from N boxes ; DP table , there are two states . First describes the number of boxes and second describes the step ; Initialize all the elements of the table to zero ; Base case ; When step is equal to 2 ; When step is greater than 2 ; Count the total staircase from all the steps ; Driver Code
function countStaircases ( N ) { let memo = new Array ( N + 5 ) ; for ( let i = 0 ; i < N + 5 ; i ++ ) { memo [ i ] = new Array ( N + 5 ) ; for ( let j = 0 ; j < N + 5 ; j ++ ) { memo [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i <= N ; i ++ ) { for ( let j = 0 ; j <= N ; j ++ ) { memo [ i ] [ j ] = 0 ; } } memo [ 3 ] [ 2 ] = memo [ 4 ] [ 2 ] = 1 ; for ( let i = 5 ; i <= N ; i ++ ) { for ( let j = 2 ; j <= i ; j ++ ) { if ( j == 2 ) { memo [ i ] [ j ] = memo [ i - j ] [ j ] + 1 ; } else { memo [ i ] [ j ] = memo [ i - j ] [ j ] + memo [ i - j ] [ j - 1 ] ; } } } let answer = 0 ; for ( let i = 1 ; i <= N ; i ++ ) answer = answer + memo [ N ] [ i ] ; return answer ; } let N = 7 ; document . write ( countStaircases ( N ) ) ;
Find maximum points which can be obtained by deleting elements from array | function to return maximum cost obtained ; find maximum element of the array . ; initialize count of all elements to zero . ; calculate frequency of all elements of array . ; stores cost of deleted elements . ; selecting minimum range from L and R . ; finds upto which elements are to be deleted when element num is selected . ; get maximum when selecting element num or not . ; Driver Code ; size of array ; function call to find maximum cost
function maxCost ( a , n , l , r ) { var mx = 0 , k ; for ( var i = 0 ; i < n ; ++ i ) mx = Math . max ( mx , a [ i ] ) ; var count = new Array ( mx + 1 ) ; count . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) count [ a [ i ] ] ++ ; var res = new Array ( mx + 1 ) ; res [ 0 ] = 0 ; l = Math . min ( l , r ) ; for ( var num = 1 ; num <= mx ; num ++ ) { k = Math . max ( num - l - 1 , 0 ) ; res [ num ] = Math . max ( res [ num - 1 ] , num * count [ num ] + res [ k ] ) ; } return res [ mx ] ; } var a = [ 2 , 1 , 2 , 3 , 2 , 2 , 1 ] ; var l = 1 , r = 1 ; var n = a . length ; document . write ( maxCost ( a , n , l , r ) ) ;
Count the number of ways to traverse a Matrix | Returns The number of way from top - left to mat [ m - 1 ] [ n - 1 ] ; Return 1 if it is the first row or first column ; Recursively find the no of way to reach the last cell . ; Driver Code
function countPaths ( m , n ) { if ( m == 1 n == 1 ) return 1 ; return countPaths ( m - 1 , n ) + countPaths ( m , n - 1 ) ; } let n = 5 , m = 5 ; document . write ( countPaths ( n , m ) ) ;
Count the number of ways to traverse a Matrix | Returns The number of way from top - left to mat [ m - 1 ] [ n - 1 ] ; Driver code
function countPaths ( m , n ) { let dp = new Array ( m + 1 ) ; for ( let i = 0 ; i <= m ; i ++ ) { dp [ i ] = new Array ( n + 1 ) ; for ( let j = 0 ; j <= n ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let i = 1 ; i <= m ; i ++ ) { for ( let j = 1 ; j <= n ; j ++ ) { if ( i == 1 j == 1 ) dp [ i ] [ j ] = 1 ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] ; } } return dp [ m ] [ n ] ; } let n = 5 ; let m = 5 ; document . write ( countPaths ( n , m ) ) ;
Number of ways a convex polygon of n + 2 sides can split into triangles by connecting vertices | Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver code
function binomialCoeff ( n , k ) { var res = 1 ; if ( k > n - k ) k = n - k ; for ( i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } function catalan ( n ) { var c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } var n = 3 ; document . write ( catalan ( n ) ) ;
Alternate Fibonacci Numbers | Alternate Fibonacci Series using Dynamic Programming ; 0 th and 1 st number of the series are 0 and 1 ; Driver Code
function alternateFib ( n ) { if ( n < 0 ) return ; var f1 = 0 ; var f2 = 1 ; document . write ( f1 + " " ) ; for ( i = 2 ; i <= n ; i ++ ) { var f3 = f2 + f1 ; if ( i % 2 == 0 ) document . write ( f3 + " " ) ; f1 = f2 ; f2 = f3 ; } } var N = 15 ; alternateFib ( N ) ;
Number of ways to form an array with distinct adjacent elements | Returns the total ways to form arrays such that every consecutive element is different and each element except the first and last can take values from 1 to M ; define the dp [ ] [ ] array ; if the first element is 1 ; there is only one way to place a 1 at the first index ; the value at first index needs to be 1 , thus there is no way to place a non - one integer ; if the first element was 1 then at index 1 , only non one integer can be placed thus there are M - 1 ways to place a non one integer at index 2 and 0 ways to place a 1 at the 2 nd index ; Else there is one way to place a one at index 2 and if a non one needs to be placed here , there are ( M - 2 ) options , i . e neither the element at this index should be 1 , neither should it be equal to the previous element ; Build the dp array in bottom up manner ; f ( i , one ) = f ( i - 1 , non - one ) ; f ( i , non - one ) = f ( i - 1 , one ) * ( M - 1 ) + f ( i - 1 , non - one ) * ( M - 2 ) ; last element needs to be one , so return dp [ n - 1 ] [ 0 ] ; Driver Code
function totalWays ( N , M , X ) { let dp = new Array ( N + 1 ) ; for ( let i = 0 ; i < N + 1 ; i ++ ) { dp [ i ] = new Array ( 2 ) ; for ( let j = 0 ; j < 2 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } if ( X == 1 ) { dp [ 0 ] [ 0 ] = 1 ; } else { dp [ 0 ] [ 1 ] = 0 ; } if ( X == 1 ) { dp [ 1 ] [ 0 ] = 0 ; dp [ 1 ] [ 1 ] = M - 1 ; } else { dp [ 1 ] [ 0 ] = 1 ; dp [ 1 ] [ 1 ] = ( M - 2 ) ; } for ( let i = 2 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = dp [ i - 1 ] [ 1 ] ; dp [ i ] [ 1 ] = dp [ i - 1 ] [ 0 ] * ( M - 1 ) + dp [ i - 1 ] [ 1 ] * ( M - 2 ) ; } return dp [ N - 1 ] [ 0 ] ; } let N = 4 , M = 3 , X = 2 ; document . write ( totalWays ( N , M , X ) ) ;
Memoization ( 1D , 2D and 3D ) | Fibonacci Series using Recursion ; Base case ; recursive calls ; Driver Code
function fib ( n ) { if ( n <= 1 ) return n ; return fib ( n - 1 ) + fib ( n - 2 ) ; } let n = 6 ; document . write ( fib ( n ) ) ;
Memoization ( 1D , 2D and 3D ) | Fibonacci Series using memoized Recursion ; base case ; if fib ( n ) has already been computed we do not do further recursive calls and hence reduce the number of repeated work ; store the computed value of fib ( n ) in an array term at index n to so that it does not needs to be precomputed again ; Driver Code
function fib ( n ) { let term = new Array ( 1000 ) ; term . fill ( 0 ) ; if ( n <= 1 ) return n ; if ( term [ n ] != 0 ) return term [ n ] ; else { term [ n ] = fib ( n - 1 ) + fib ( n - 2 ) ; return term [ n ] ; } } let n = 6 ; document . write ( fib ( n ) ) ;
Memoization ( 1D , 2D and 3D ) | Utility function to get max of 2 integers ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Driver Code
function max ( a , b ) { return ( a > b ) ? a : b ; } function lcs ( X , Y , m , n ) { if ( m == 0 n == 0 ) return 0 ; if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; else return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ; } let X = " " ; let Y = " " ; let m = X . length ; let n = Y . length ; document . write ( " " + lcs ( X , Y , m , n ) ) ;
Smallest number with given sum of digits and sum of square of digits | Top down dp to find minimum number of digits with given sum of dits a and sum of square of digits as b ; Invalid condition ; Number of digits satisfied ; Memoization ; Initialize ans as maximum as we have to find the minimum number of digits ; Check for all possible combinations of digits ; recurrence call ; If the combination of digits cannot give sum as a and sum of square of digits as b ; Returns the minimum number of digits ; Function to print the digits that gives sum as a and sum of square of digits as b ; initialize the dp array as - 1 ; base condition ; function call to get the minimum number of digits ; When there does not exists any number ; Printing the digits from the most significant digit ; Trying all combinations ; checking conditions for minimum digits ; Driver Code ; Function call to print the smallest number
function minimumNumberOfDigits ( a , b ) { if ( a > b a < 0 b < 0 a > 900 b > 8100 ) return - 1 ; if ( a == 0 && b == 0 ) return 0 ; if ( dp [ a ] [ b ] != - 1 ) return dp [ a ] [ b ] ; var ans = 101 ; for ( var i = 9 ; i >= 1 ; i -- ) { var k = minimumNumberOfDigits ( a - i , b - ( i * i ) ) ; if ( k != - 1 ) ans = Math . min ( ans , k + 1 ) ; } return dp [ a ] [ b ] = ans ; } function printSmallestNumber ( a , b ) { dp = new Array ( 901 ) . fill ( - 1 ) . map ( ( ) => new Array ( 8101 ) . fill ( - 1 ) ) ; ; dp [ 0 ] [ 0 ] = 0 ; var k = minimumNumberOfDigits ( a , b ) ; if ( k == - 1 k > 100 ) document . write ( " " ) ; else { while ( a > 0 && b > 0 ) { for ( var i = 1 ; i <= 9 ; i ++ ) { if ( a >= i && b >= i * i && 1 + dp [ a - i ] [ b - i * i ] == dp [ a ] [ b ] ) { document . write ( i ) ; a -= i ; b -= i * i ; break ; } } } } } var a = 18 , b = 162 ; printSmallestNumber ( a , b ) ;
Sum of product of consecutive Binomial Coefficients | Javascript Program to find sum of product of consecutive Binomial Coefficient . ; Find the binomial coefficient up to nth term ; memset ( C , 0 , sizeof ( C ) ) ; C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the sum of the product of consecutive binomial coefficient . ; Driver Code
let MAX = 100 ; function binomialCoeff ( n , k ) { let C = new Array ( k + 1 ) ; C . fill ( 0 ) ; for ( let i = 1 ; i <= n ; i ++ ) { for ( let j = Math . min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } function sumOfproduct ( n ) { return binomialCoeff ( 2 * n , n - 1 ) ; } let n = 3 ; document . write ( sumOfproduct ( n ) ) ;
Check if array sum can be made K by three operations on it | Javascript Program to find if Array can have a sum of K by applying three types of possible operations on it ; Check if it is possible to achieve a sum with three operation allowed . ; If sum is negative . ; If going out of bound . ; If sum is achieved . ; If the current state is not evaluated yet . ; Replacing element with negative value of the element . ; Substracting index number from the element . ; Adding index number to the element . ; Wrapper Function ; Driver Code
var MAX = 100 ; function check ( i , sum , n , k , a , dp ) { if ( sum <= 0 ) return false ; if ( i >= n ) { if ( sum == k ) return true ; return false ; } if ( dp [ i ] [ sum ] != - 1 ) return dp [ i ] [ sum ] ; dp [ i ] [ sum ] = check ( i + 1 , sum - 2 * a [ i ] , n , k , a , dp ) || check ( i + 1 , sum , n , k , a , dp ) ; dp [ i ] [ sum ] = check ( i + 1 , sum - ( i + 1 ) , n , k , a , dp ) || dp [ i ] [ sum ] ; dp [ i ] [ sum ] = check ( i + 1 , sum + i + 1 , n , k , a , dp ) || dp [ i ] [ sum ] ; return dp [ i ] [ sum ] ; } function wrapper ( n , k , a ) { var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) sum += a [ i ] ; var dp = Array . from ( Array ( MAX ) , ( ) => Array ( MAX ) . fill ( - 1 ) ) ; return check ( 0 , sum , n , k , a , dp ) ; } var a = [ 1 , 2 , 3 , 4 ] ; var n = 4 , k = 5 ; ( wrapper ( n , k , a ) ? ( document . write ( " " ) ) : ( document . write ( " " ) ) ) ;
Print Fibonacci sequence using 2 variables | Simple Javascript Program to print Fibonacci sequence ; Driver code
function fib ( n ) { let a = 0 , b = 1 , c ; if ( n >= 0 ) document . write ( a + " " ) ; if ( n >= 1 ) document . write ( b + " " ) ; for ( let i = 2 ; i <= n ; i ++ ) { c = a + b ; document . write ( c + " " ) ; a = b ; b = c ; } } fib ( 9 ) ;
Maximum sum increasing subsequence from a prefix and a given element after prefix is must | Javascript program to find maximum sum increasing subsequence tiint i - th index and including k - th index . ; Initializing the first row of the dp [ ] [ ] . ; Creating the dp [ ] [ ] matrix . ; To calculate for i = 4 and k = 6. ; Driver code
function pre_compute ( a , n , index , k ) { let dp = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { dp [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] > a [ 0 ] ) dp [ 0 ] [ i ] = a [ i ] + a [ 0 ] ; else dp [ 0 ] [ i ] = a [ i ] ; } for ( let i = 1 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( a [ j ] > a [ i ] && j > i ) { if ( dp [ i - 1 ] [ i ] + a [ j ] > dp [ i - 1 ] [ j ] ) dp [ i ] [ j ] = dp [ i - 1 ] [ i ] + a [ j ] ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } else dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } } return dp [ index ] [ k ] ; } let a = [ 1 , 101 , 2 , 3 , 100 , 4 , 5 ] ; let n = a . length ; let index = 4 , k = 6 ; document . write ( pre_compute ( a , n , index , k ) ) ;
Moser | Function to generate nth term of Moser - de Bruijn Sequence ; 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 ) { let S = new Array ( n + 1 ) ; S . fill ( 0 ) ; S [ 0 ] = 0 ; if ( n != 0 ) S [ 1 ] = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) S [ i ] = 4 * S [ parseInt ( i / 2 , 10 ) ] ; else S [ i ] = 4 * S [ parseInt ( i / 2 , 10 ) ] + 1 ; } return S [ n ] ; } function moserDeBruijn ( n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( gen ( i ) + " " ) ; } let n = 15 ; document . write ( " " + n + " " + " " + " " ) ; moserDeBruijn ( n ) ;
Longest Common Substring ( Space optimized DP solution ) | Function to find longest common substring . ; Find length of both the strings . ; Variable to store length of longest common substring . ; Matrix to store result of two consecutive rows at a time . ; Variable to represent which row of matrix is current row . ; For a particular value of i and j , len [ currRow ] [ j ] stores length of longest common substring in string X [ 0. . i ] and Y [ 0. . j ] . ; Make current row as previous row and previous row as new current row . ; Driver code
function LCSubStr ( X , Y ) { var m = X . length ; var n = Y . length ; var result = 0 ; var len = Array . from ( Array ( 2 ) , ( ) => Array ( n ) ) ; var currRow = 0 ; for ( var i = 0 ; i <= m ; i ++ ) { for ( var j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) { len [ currRow ] [ j ] = 0 ; } else if ( X [ i - 1 ] == Y [ j - 1 ] ) { len [ currRow ] [ j ] = len [ 1 - currRow ] [ j - 1 ] + 1 ; result = Math . max ( result , len [ currRow ] [ j ] ) ; } else { len [ currRow ] [ j ] = 0 ; } } currRow = 1 - currRow ; } return result ; } var X = " " ; var Y = " " ; document . write ( LCSubStr ( X , Y ) ) ;
Check if any valid sequence is divisible by M | Function to check if any valid sequence is divisible by M ; Declare mod array ; Calculate the mod array ; Check if sum is divisible by M ; Check if sum is not divisible by 2 ; Remove the first element from the ModArray since it is not possible to place minus on the first element ; Decrease the size of array ; Sort the array ; Loop until the pointer cross each other ; Check if sum becomes equal ; Increase and decrease the pointer accordingly ; Driver code ; Function call
function func ( n , m , A ) { let ModArray = [ ] ; for ( let i = 0 ; i < n ; i ++ ) ModArray . push ( 0 ) ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { ModArray [ i ] = A [ i ] % m ; sum += ModArray [ i ] ; } sum = sum % m ; if ( sum % m == 0 ) { document . write ( " " ) ; return ; } if ( sum % 2 != 0 ) { document . write ( " " ) ; } else { ModArray . shift ( ) ; let i = 0 ; let j = ModArray . length - 1 ; ModArray . sort ( function ( a , b ) { return a - b } ) ; sum = parseInt ( sum / 2 , 10 ) ; let i1 , i2 ; while ( i <= j ) { let s = ModArray [ i ] + ModArray [ j ] ; if ( s == sum ) { i1 = i ; i2 = j ; document . write ( " " ) ; break ; } else if ( s > sum ) j -- ; else i ++ ; } } } let m = 2 ; let a = [ 1 , 3 , 9 ] ; let n = a . length ; func ( n , m , a ) ;
Golomb sequence | Print the first n term of Golomb Sequence ; base cases ; Finding and printing first n terms of Golomb Sequence . ; Driver code
function printGolomb ( n ) { let dp = Array ( n + 1 ) . fill ( 0 ) ; dp [ 1 ] = 1 ; document . write ( dp [ 1 ] + " " ) ; for ( i = 2 ; i <= n ; i ++ ) { dp [ i ] = 1 + dp [ i - dp [ dp [ i - 1 ] ] ] ; document . write ( dp [ i ] + " " ) ; } } let n = 9 ; printGolomb ( n ) ;
Balanced expressions such that given positions have opening brackets | Javascript code to find number of ways of arranging bracket with proper expressions ; function to calculate the number of proper bracket sequence ; hash array to mark the positions of opening brackets ; dp 2d array ; mark positions in hash array ; first position marked as 1 ; iterate and formulate the recurrences ; if position has a opening bracket ; return answer ; driver code ; positions where opening braces will be placed
let N = 1000 ; function arrangeBraces ( n , pos , k ) { let h = new Array ( N ) ; h . fill ( false ) ; let dp = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { dp [ i ] = new Array ( N ) ; for ( let j = 0 ; j < N ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < k ; i ++ ) { h [ pos [ i ] ] = true ; } dp [ 0 ] [ 0 ] = 1 ; for ( let i = 1 ; i <= 2 * n ; i ++ ) { for ( let j = 0 ; j <= 2 * n ; j ++ ) { if ( h [ i ] ) { if ( j != 0 ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ; } else { dp [ i ] [ j ] = 0 ; } } else if ( j != 0 ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] + dp [ i - 1 ] [ j + 1 ] ; } else { dp [ i ] [ j ] = dp [ i - 1 ] [ j + 1 ] ; } } } return dp [ 2 * n ] [ 0 ] ; } let n = 3 ; let pos = [ 2 ] ; let k = pos . length ; document . write ( arrangeBraces ( n , pos , k ) ) ;
Maximum difference of zeros and ones in binary string | Set 2 ( O ( n ) time ) | Find the length of substring with maximum difference of zeros and ones in binary string ; traverse a binary string from left to right ; add current value to the current_sum according to the Character if it ' s ▁ ' 0 ' add 1 else -1 ; update maximum sum ; return - 1 if string does not contain any zero that means string contains all ones otherwise max_sum ; Driver Code
function findLength ( str , n ) { let current_sum = 0 ; let max_sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { current_sum += ( str [ i ] == ' ' ? 1 : - 1 ) ; if ( current_sum < 0 ) { current_sum = 0 ; } max_sum = Math . max ( current_sum , max_sum ) ; } return max_sum == 0 ? - 1 : max_sum ; } let str = " " ; let n = str . length ; document . write ( findLength ( str , n ) ) ;
Count ways to divide circle using N non | JavaScript code to count ways to divide circle using N non - intersecting chords . ; n = no of points required ; dp array containing the sum ; returning the required number ; Driver function
function chordCnt ( A ) { var n = 2 * A ; var dpArray = Array ( n + 1 ) . fill ( 0 ) ; dpArray [ 0 ] = 1 ; dpArray [ 2 ] = 1 ; for ( var i = 4 ; i <= n ; i += 2 ) { for ( var j = 0 ; j < i - 1 ; j += 2 ) { dpArray [ i ] += ( dpArray [ j ] * dpArray [ i - 2 - j ] ) ; } } return dpArray [ n ] ; } var N ; N = 2 ; document . write ( chordCnt ( N ) + ' ' ) ; N = 1 ; document . write ( chordCnt ( N ) + ' ' ) ; N = 4 ; document . write ( chordCnt ( N ) + ' ' ) ;
Check for possible path in 2D matrix | JavaScript program to find if there is path from top left to right bottom ; to find the path from top left to bottom right ; set arr [ 0 ] [ 0 ] = 1 ; Mark reachable ( from top left ) nodes in first row and first column . ; Mark reachable nodes in remaining matrix . ; return yes if right bottom index is 1 ; Given array ; path from arr [ 0 ] [ 0 ] to arr [ row ] [ col ]
var arr = [ [ 5 ] , [ 5 ] ] function isPath ( arr ) { arr [ 0 ] [ 0 ] = 1 ; for ( var i = 1 ; i < 5 ; i ++ ) if ( arr [ i ] [ 0 ] != - 1 ) arr [ i ] [ 0 ] = arr [ i - 1 ] [ 0 ] ; for ( var j = 1 ; j < 5 ; j ++ ) if ( arr [ 0 ] [ j ] != - 1 ) arr [ 0 ] [ j ] = arr [ 0 ] [ j - 1 ] ; for ( var i = 1 ; i < 5 ; i ++ ) for ( var j = 1 ; j < 5 ; j ++ ) if ( arr [ i ] [ j ] != - 1 ) arr [ i ] [ j ] = Math . max ( arr [ i ] [ j - 1 ] , arr [ i - 1 ] [ j ] ) ; return ( arr [ 5 - 1 ] [ 5 - 1 ] == 1 ) ; } var arr = [ [ 0 , 0 , 0 , - 1 , 0 ] , [ - 1 , 0 , 0 , - 1 , - 1 ] , [ 0 , 0 , 0 , - 1 , 0 ] , [ - 1 , 0 , - 1 , 0 , - 1 ] , [ 0 , 0 , - 1 , 0 , 0 ] ] ; if ( isPath ( arr ) ) document . write ( " " ) ; else document . write ( " " ) ;
Newmanâ €“ Shanksâ €“ Williams prime | return nth Newman - Shanks - Williams prime ; Base case ; Recursive step ; Driver code
function nswp ( n ) { if ( n == 0 n == 1 ) return 1 ; return 2 * nswp ( n - 1 ) + nswp ( n - 2 ) ; } let n = 3 ; document . write ( nswp ( n ) ) ;
Newman Shanks Williams prime | return nth Newman Shanks Williams prime ; Base case ; Finding nth Newman Shanks Williams prime ; Driver Code
function nswp ( n ) { let dp = new Array ( n + 1 ) ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( let i = 2 ; i <= n ; i ++ ) dp [ i ] = 2 * dp [ i - 1 ] + dp [ i - 2 ] ; return dp [ n ] ; } let n = 3 ; document . write ( nswp ( n ) ) ;