codeparrot/xlcost-text-to-code huggingface dataset:
Maximize sum of subsets from two arrays having no consecutive values | Function to calculate maximum subset sum ; Initialize array to store dp states ; Base Cases ; Pre initializing for dp [ 0 ] & dp [ 1 ] ; Calculating dp [ index ] based on above formula ; Prlet maximum subset sum ; Given arrays ; Length of the array
function maximumSubsetSum ( arr1 , arr2 , length ) { let dp = new Array ( length ) . fill ( 0 ) ; ; if ( length == 1 ) { document . write ( Math . max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) ; return ; } if ( length == 2 ) { document . write ( Math . max ( Math . max ( arr1 [ 1 ] , arr2 [ 1 ] ) , Math . max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) ) ; return ; } else { dp [ 0 ] = Math . max ( arr1 [ 0 ] , arr2 [ 0 ] ) ; dp [ 1 ] = Math . max ( Math . max ( arr1 [ 1 ] , arr2 [ 1 ] ) , Math . max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) ; let index = 2 ; while ( index < length ) { dp [ index ] = Math . max ( Math . max ( arr1 [ index ] , arr2 [ index ] ) , Math . max ( Math . max ( arr1 [ index ] + dp [ index - 2 ] , arr2 [ index ] + dp [ index - 2 ] ) , dp [ index - 1 ] ) ) ; ++ index ; } document . write ( dp [ length - 1 ] ) ; } } let arr1 = [ - 1 , - 2 , 4 , - 4 , 5 ] ; let arr2 = [ - 1 , - 2 , - 3 , 4 , 10 ] ; let length = arr1 . length ; maximumSubsetSum ( arr1 , arr2 , length ) ;
Modify array by replacing every array element with minimum possible value of arr [ j ] + | j | Function to find minimum value of arr [ j ] + | j - i | for every array index ; Stores minimum of a [ j ] + | i - j | upto position i ; Stores minimum of a [ j ] + | i - j | upto position i from the end ; Traversing and storing minimum of a [ j ] + | i - j | upto i ; Traversing and storing minimum of a [ j ] + | i - j | upto i from the end ; Traversing from [ 0 , N ] and storing minimum of a [ j ] + | i - j | from starting and end ; Print the required array ; Given array arr [ ] ; Size of the array ; Function Call
function minAtEachIndex ( n , arr ) { var dp1 = Array ( n ) ; var dp2 = Array ( n ) ; var i ; dp1 [ 0 ] = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) dp1 [ i ] = Math . min ( arr [ i ] , dp1 [ i - 1 ] + 1 ) ; dp2 [ n - 1 ] = arr [ n - 1 ] ; for ( i = n - 2 ; i >= 0 ; i -- ) dp2 [ i ] = Math . min ( arr [ i ] , dp2 [ i + 1 ] + 1 ) ; var v = [ ] ; for ( i = 0 ; i < n ; i ++ ) v . push ( Math . min ( dp1 [ i ] , dp2 [ i ] ) ) ; v . forEach ( x => { document . write ( x + " " ) ; } ) ; } var arr = [ 1 , 4 , 2 , 5 , 3 ] ; var N = arr . length ; minAtEachIndex ( N , arr ) ;
Count all N | Function to print the count of arrays satisfying given condition ; First element of array is set as 1 ; Since the first element of arr [ ] is 1 , the second element can 't be 1 ; Traverse the remaining indices ; If arr [ i ] = 1 ; If arr [ i ] a 1 ; Since last element needs to be 1 ; Driver Code ; Stores the count of arrays where arr [ 0 ] = arr [ N - 1 ] = 1 ; Since arr [ 0 ] and arr [ N - 1 ] can be any number from 1 to M ; Print answer
function totalArrays ( N , M ) { var end_with_one = Array ( N + 1 ) ; var end_not_with_one = Array ( N + 1 ) ; end_with_one [ 0 ] = 1 ; end_not_with_one [ 0 ] = 0 ; end_with_one [ 1 ] = 0 ; end_not_with_one [ 1 ] = M - 1 ; for ( var i = 2 ; i < N ; i ++ ) { end_with_one [ i ] = end_not_with_one [ i - 1 ] ; end_not_with_one [ i ] = end_with_one [ i - 1 ] * ( M - 1 ) + end_not_with_one [ i - 1 ] * ( M - 2 ) ; } return end_with_one [ N - 1 ] ; } var N = 3 , M = 3 ; var temp = totalArrays ( N , M ) ; var ans = M * temp ; document . write ( ans + " " ) ;
Count numbers from a given range whose product of digits is K | Function to find the product of digits of a number ; Stores product of digits of N ; Update res ; Update N ; Function to count numbers in the range [ 0 , X ] whose product of digit is K ; Stores count of numbers in the range [ L , R ] whose product of digit is K ; Iterate over the range [ L , R ] ; If product of digits of i equal to K ; Update cnt ; Driver Code
function prodOfDigit ( N ) { let res = 1 ; while ( N ) { res = res * ( N % 10 ) ; N = Math . floor ( N / 10 ) ; } return res ; } function cntNumRange ( L , R , K ) { let cnt = 0 ; for ( let i = L ; i <= R ; i ++ ) { if ( prodOfDigit ( i ) == K ) { cnt ++ ; } } return cnt ; } let L = 20 , R = 10000 , K = 14 ; document . write ( cntNumRange ( L , R , K ) ) ;
Median of Bitwise XOR of all submatrices starting from the top left corner | Function to find the median of bitwise XOR of all the submatrix whose topmost leftmost corner is ( 0 , 0 ) ; dp [ i ] [ j ] : Stores the bitwise XOR of submatrix having top left corner at ( 0 , 0 ) and bottom right corner at ( i , j ) ; Stores count of submatrix ; Base Case ; Base Case ; Fill dp [ ] [ ] using tabulation ; Fill dp [ i ] [ j ] ; Driver Code
function findMedXOR ( mat , N , M ) { let dp = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) dp [ i ] = new Array ( M ) ; let med = new Array ( N * M ) ; dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] ; med [ 0 ] = dp [ 0 ] [ 0 ] ; let len = 1 ; for ( let i = 1 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] ^ mat [ i ] [ 0 ] ; med [ len ++ ] = dp [ i ] [ 0 ] ; } for ( let i = 1 ; i < M ; i ++ ) { dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] ^ mat [ 0 ] [ i ] ; med [ len ++ ] = dp [ 0 ] [ i ] ; } for ( let i = 1 ; i < N ; i ++ ) { for ( let j = 1 ; j < M ; j ++ ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ^ dp [ i ] [ j - 1 ] ^ dp [ i - 1 ] [ j - 1 ] ^ mat [ i ] [ j ] ; med [ len ++ ] = dp [ i ] [ j ] ; } } med . sort ( ) ; if ( len % 2 == 0 ) { return ( med [ parseInt ( len / 2 ) ] + med [ parseInt ( len / 2 ) - 1 ] ) / 2.0 ; } return med [ parseInt ( len / 2 ) ] ; } let mat = [ [ 1 , 2 ] , [ 2 , 3 ] ] ; let N = mat . length ; let M = 2 ; document . write ( findMedXOR ( mat , N , M ) ) ;
Count ways to tile an N | Function to count the ways to tile N * 1 board using 1 * 1 and 2 * 1 tiles ; dp [ i ] : Stores count of ways to tile i * 1 board using given tiles ; Base Case ; Iterate over the range [ 2 , N ] ; Fill dp [ i ] using the recurrence relation ; Given N ; Function Call
function countWaysToTileBoard ( N ) { let dp = [ ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 2 ; for ( let i = 2 ; i <= N ; i ++ ) { dp [ i ] = ( 2 * dp [ i - 1 ] + dp [ i - 2 ] ) ; } document . write ( dp [ N ] ) ; } let N = 2 ; countWaysToTileBoard ( N ) ;
Subsequences generated by including characters or ASCII value of characters of given string | Function to print subsequences containing ASCII value of the characters or the the characters of the given string ; Base Case ; If length of the subsequence exceeds 0 ; Print the subsequence ; Stores character present at i - th index of str ; If the i - th character is not included in the subsequence ; Including the i - th character in the subsequence ; Include the ASCII value of the ith character in the subsequence ; Driver Code ; Stores length of str
function FindSub ( str , res , i ) { if ( i === str . length ) { if ( res . length > 0 ) { document . write ( res + " " ) ; } return ; } var ch = str [ i ] ; FindSub ( str , res , i + 1 ) ; FindSub ( str , res + ch , i + 1 ) ; FindSub ( str , res + ch . charCodeAt ( 0 ) , i + 1 ) ; } var str = " " ; var res = " " ; var N = str . length ; FindSub ( str , res , 0 ) ;
Minimize given flips required to reduce N to 0 | Function to find the minimum count of operations required to Reduce N to 0 ; Stores count of bits in N ; Recurrence relation ; Driver code
function MinOp ( N ) { if ( N <= 1 ) return N ; let bit = ( Math . log ( N ) / Math . log ( 2 ) ) + 1 ; return ( ( 1 << bit ) - 1 ) - MinOp ( N - ( 1 << ( bit - 1 ) ) ) ; } let N = 4 ; document . write ( MinOp ( N ) ) ;
Maximum non | Function to find the maximum product from the top left and bottom right cell of the given matrix grid [ ] [ ] ; Store dimension of grid ; Stores maximum product path ; Stores minimum product path ; Traverse the grid and update maxPath and minPath array ; Initialize to inf and - inf ; Base Case ; Calculate for row : ; Update the maximum ; Update the minimum ; Calculate for column : ; Update the maximum ; Update the minimum ; Update maxPath and minPath ; If negative product ; Otherwise ; Given matrix mat [ ] [ ] ; Function Call
function maxProductPath ( grid ) { var n = grid . length ; var m = grid [ 0 ] . length ; var maxPath = Array . from ( Array ( n ) , ( ) => Array ( m ) . fill ( 0 ) ) ; var minPath = Array . from ( Array ( n ) , ( ) => Array ( m ) . fill ( 0 ) ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < m ; j ++ ) { var mn = 1000000000 ; var mx = - 1000000000 ; if ( i == 0 && j == 0 ) { mx = grid [ i ] [ j ] ; mn = grid [ i ] [ j ] ; } if ( i > 0 ) { var tempmx = Math . max ( ( maxPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) , ( minPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) ) ; mx = Math . max ( mx , tempmx ) ; var tempmn = Math . min ( ( maxPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) , ( minPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) ) ; mn = Math . min ( mn , tempmn ) ; } if ( j > 0 ) { var tempmx = Math . max ( ( maxPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) , ( minPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) ) ; mx = Math . max ( mx , tempmx ) ; var tempmn = Math . min ( ( maxPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) , ( minPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) ) ; mn = Math . min ( mn , tempmn ) ; } maxPath [ i ] [ j ] = mx ; minPath [ i ] [ j ] = mn ; } } if ( maxPath [ n - 1 ] [ m - 1 ] < 0 ) return - 1 ; else return ( maxPath [ n - 1 ] [ m - 1 ] ) ; } var mat = [ [ 1 , - 2 , 1 ] , [ 1 , - 2 , 1 ] , [ 3 , - 4 , 1 ] ] ; document . write ( maxProductPath ( mat ) ) ;
Smallest submatrix required to be removed such that sum of the remaining matrix is divisible by K | Function to find the length of the smallest subarray to be removed such that sum of elements is equal to S % K ; Remainder when total_sum is divided by K ; Stores curr_remainder and the most recent index at which curr_remainder has occured ; Stores required answer ; Add current element to curr_sum and take mod ; Update current remainder index ; If mod already exists in map the subarray exists ; Update res ; If not possible ; Return the result ; Function to find the smallest submatrix rqured to be deleted to make the sum of the matrix divisible by K ; Stores the sum of element of the matrix ; Traverse the matrix mat [ ] [ ] ; Update S ; Stores smallest area need to be deleted to get sum divisible by K ; Stores leftmost column of each matrix ; Stores rightmost column of each matrix ; Stores number of coulmns deleted of a matrix ; Store area of the deleted matrix ; prefixRowSum [ i ] : Store sum of sub matrix whose topmost left and bottommost right position is ( 0 , left ) ( i , right ) ; Iterate over all possible values of ( left , right ) ; Initialize all possible values of prefixRowSum [ ] to 0 ; Traverse each row from left to right column ; Update row_sum [ i ] ; ; Update width ; If no submatrix of the length ( right - left + 1 ) found to get the required output ; Update area ; If area is less than min_area ; Update min_area ; Driver Code ; Stores number of rows in the matrix ; Stores number of column in the matrix
function removeSmallestSubarray ( arr , S , n , k ) { var target_remainder = S % k ; var map1 = new Map ( ) ; map1 . set ( 0 , - 1 ) ; var curr_remainder = 0 ; var res = 1000000000 ; for ( var i = 0 ; i < n ; i ++ ) { curr_remainder = ( curr_remainder + arr [ i ] + k ) % k ; map1 . set ( curr_remainder , i ) ; var mod = ( curr_remainder - target_remainder + k ) % k ; if ( map1 . has ( mod ) ) { res = Math . min ( res , i - map1 . get ( mod ) ) ; } } if ( res == 1000000000 res == n ) { res = - 1 ; } return res ; } function smstSubmatDeleted ( mat , N , M , K ) { var S = 0 ; for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < M ; j ++ ) S += mat [ i ] [ j ] ; } var min_area = N * M ; var left = 0 ; var right = 0 ; var width ; var area ; var prefixRowSum = Array ( N ) ; for ( left = 0 ; left < M ; left ++ ) { prefixRowSum = Array ( N ) . fill ( 0 ) ; for ( right = left ; right < M ; right ++ ) { for ( var i = 0 ; i < N ; i ++ ) { prefixRowSum [ i ] += mat [ i ] [ right ] ; } width = removeSmallestSubarray ( prefixRowSum , S , N , K ) ; if ( width != - 1 ) { area = ( right - left + 1 ) * ( width ) ; if ( area < min_area ) { min_area = area ; } } } } return min_area ; } var mat = [ [ 6 , 2 , 6 ] , [ 3 , 2 , 8 ] , [ 2 , 5 , 3 ] ] ; var K = 3 ; var N = mat . length ; var M = mat [ 0 ] . length ; document . write ( smstSubmatDeleted ( mat , N , M , K ) ) ;
Count N | to keep the string in lexicographically sorted order use start index to add the vowels starting the from that index ; base case : if string length is 0 add to the count ; if last character in string is ' e ' add vowels starting from ' e ' i . e ' e ' , ' i ' , ' o ' , ' u ' ; decrease the length of string ; char arr [ 5 ] = { ' a ' , ' e ' , ' i ' , ' o ' , ' u ' } ; starting from index 0 add the vowels to strings
function countstrings ( n , start ) { if ( n == 0 ) { return 1 ; } let cnt = 0 ; for ( let i = start ; i < 5 ; i ++ ) { cnt += countstrings ( n - 1 , i ) ; } return cnt ; } function countVowelStrings ( n ) { return countstrings ( n , 0 ) ; } let n = 2 ; document . write ( countVowelStrings ( n ) ) ;
Count N | Function to count N - length strings consisting of vowels only sorted lexicographically ; Driver Code ; Function Call
function findNumberOfStrings ( n ) { return ( n + 1 ) * ( n + 2 ) * ( n + 3 ) * ( n + 4 ) / 24 ; } let N = 2 ; document . write ( findNumberOfStrings ( N ) ) ;
Count unique paths with given sum in an N | Javascript program for the above approach ; Function for counting total no of paths possible with the sum is equal to X ; If the path of the sum from the root to current node is stored in sum ; If already computed ; Count different no of paths using all possible ways ; Return total no of paths ; Driver Code ; Stores the number of ways to obtains sums 0 to X ; Function call
var mod = 1000000007 ; function findTotalPath ( X , n , dp ) { if ( X == 0 ) { return 1 ; } var ans = 0 ; if ( dp [ X ] != - 1 ) { return dp [ X ] ; } for ( var i = 1 ; i <= Math . min ( X , n ) ; ++ i ) { ans += findTotalPath ( X - i , n , dp ) % mod ; ans %= mod ; } return dp [ X ] = ans ; } var n = 3 , X = 2 ; var dp = Array ( X + 1 ) . fill ( - 1 ) ; document . write ( findTotalPath ( X , n , dp ) ) ;
Count sequences of positive integers having product X | Javascript program for the above approach ; Function to print the total number of possible sequences with product X ; Precomputation of binomial coefficients ; Max length of a subsequence ; Ways dp array ; Fill i slots using all the primes ; Subtract ways for all slots that exactly fill less than i slots ; Total possible sequences ; Print the resultant count ; Driver Code ; Function call
var bin = Array . from ( Array ( 3000 ) , ( ) => Array ( 3000 ) . fill ( 0 ) ) ; function countWays ( arr ) { var mod = 1000000007 ; bin [ 0 ] [ 0 ] = 1 ; for ( var i = 1 ; i < 3000 ; i ++ ) { bin [ i ] [ 0 ] = 1 ; for ( var j = 1 ; j <= i ; j ++ ) { bin [ i ] [ j ] = ( bin [ i - 1 ] [ j ] + bin [ i - 1 ] [ j - 1 ] ) % mod ; } } var n = 0 ; for ( var x = 0 ; x < arr . length ; x ++ ) n += arr [ x ] ; var ways = Array ( n + 1 ) . fill ( 0 ) ; for ( var i = 1 ; i <= n ; i ++ ) { ways [ i ] = 1 ; for ( var j = 0 ; j < arr . length ; j ++ ) { ways [ i ] = ( ways [ i ] * bin [ arr [ j ] + i - 1 ] [ i - 1 ] ) % mod ; } for ( var j = 1 ; j < i ; j ++ ) { ways [ i ] = ( ( ways [ i ] - bin [ i ] [ j ] * ways [ j ] ) % mod + mod ) % mod ; } } var ans = 0 ; for ( var i = 1 ; i <= n ; i ++ ) ans = ( ans + ways [ i ] ) % mod ; document . write ( ans ) ; } var arr = [ 1 , 1 ] ; countWays ( arr ) ;
Maximum subsequence sum obtained by concatenating disjoint subarrays whose lengths are prime | Javascript program for the above approach ; Function to return all prime numbers smaller than N ; Create a boolean array " prime [ 0 . . n ] " ; Initialize all its entries as true ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it ; Stores all prime numbers smaller than MAX ; Store all prime numbers ; If p is prime ; Function to build the auxiliary DP array from the start ; Base Case ; Stores all prime numbers < N ; Stores prefix sum ; Update prefix sum ; Iterate over range ; Update each state i . e . . when current element is excluded ; Find start & end index of subarrays when prime [ i ] is taken ; Check if starting point lies in the array ; Include the elements al al + 1 . . . ar ; Check if element lies before start of selected subarray ; Update value of dp [ i ] ; Function to find the maximum sum subsequence with prime length ; Auxiliary DP array ; Build DP array ; Print the result ; Given arr [ ] ; Size of array ; Function Call
let MAX = 100005 function SieveOfEratosthenes ( ) { let seive = new Array ( MAX ) ; seive . fill ( true ) for ( let p = 2 ; p * p < MAX ; p ++ ) { if ( seive [ p ] == true ) { for ( let i = p * p ; i < MAX ; i += p ) { seive [ i ] = false ; } } } let v = new Array ( ) ; for ( let p = 2 ; p < MAX ; p ++ ) { if ( seive [ p ] ) { v . push ( p ) ; } } return v ; } function build ( dp , arr , N ) { dp [ 0 ] = 0 ; dp [ 1 ] = 0 ; let prime = SieveOfEratosthenes ( ) ; let pref = new Array ( N + 1 ) ; pref [ 0 ] = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { pref [ i ] = pref [ i - 1 ] + arr [ i - 1 ] ; } for ( let i = 2 ; i <= N ; i ++ ) { dp [ i ] = dp [ i - 1 ] ; for ( let j = 0 ; j <= prime . length ; j ++ ) { let r = i - 1 ; let l = r - prime [ j ] + 1 ; if ( l < 0 ) break ; let temp = 0 ; temp = pref [ r + 1 ] - pref [ l ] ; if ( l - 2 >= 0 ) temp += dp [ l - 2 + 1 ] ; dp [ i ] = Math . max ( dp [ i ] , temp ) ; } } } function maxSumSubseq ( arr , N ) { let dp = new Array ( N + 1 ) ; build ( dp , arr , N ) ; document . write ( dp [ N ] ) ; } let arr = [ 10 , 10 , 7 , 10 , 10 , 10 ] ; let N = arr . length ; maxSumSubseq ( arr , N ) ;
Maximum possible score that can be obtained by constructing a Binary Tree based on given conditions | Function to find the maximum score for one possible tree having N nodes N - 1 Edges ; Number of nodes ; Initialize dp [ ] [ ] ; Score with 0 vertices is 0 ; Traverse the nodes from 1 to N ; Find maximum scores for each sum ; Iterate over degree of new node ; Update the current state ; Return maximum score for N node tree having 2 ( N - 1 ) sum of degree ; Given array of scores ; Function Call
function maxScore ( arr ) { var N = arr . length ; N ++ ; var dp = Array . from ( Array ( N + 1 ) , ( ) => Array ( 2 * N ) . fill ( - 10000000 ) ) ; dp [ 0 ] [ 0 ] = 0 ; for ( var i = 1 ; i <= N ; i ++ ) { for ( var s = 1 ; s <= 2 * ( N - 1 ) ; s ++ ) { for ( var j = 1 ; j <= N - 1 && j <= s ; j ++ ) { dp [ i ] [ s ] = Math . max ( dp [ i ] [ s ] , arr [ j - 1 ] + dp [ i - 1 ] [ s - j ] ) ; } } } return dp [ N ] [ 2 * ( N - 1 ) ] ; } var arr = [ 1 , 3 , 0 ] ; document . write ( maxScore ( arr ) ) ;
Minimize cost of choosing and skipping array elements to reach end of the given array | Function to find the minimum cost to reach the end of the array from the first element ; Store the results ; Consider first index cost ; Find answer for each position i ; First Element ; Second Element ; For remaining element ; Consider min cost for skipping ; Last index represents the minimum total cost ; Given X ; Given array cost [ ] ; Function Call
function minimumCost ( cost , n , x ) { let dp = [ ] ; dp [ 0 ] = cost [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( i == 1 ) dp [ i ] = cost [ i ] + dp [ i - 1 ] ; if ( i == 2 ) dp [ i ] = cost [ i ] + Math . min ( dp [ i - 1 ] , x + dp [ i - 2 ] ) ; if ( i >= 3 ) dp [ i ] = cost [ i ] + Math . min ( dp [ i - 1 ] , Math . min ( x + dp [ i - 2 ] , 2 * x + dp [ i - 3 ] ) ) ; } document . write ( dp [ n - 1 ] ) ; } let X = 4 ; let cost = [ 6 , 3 , 9 , 2 , 1 , 3 ] ; let N = cost . length ; minimumCost ( cost , N , X ) ;
Maximum possible sum of non | Function to find the maximum sum not exceeding K possible by selecting a subset of non - adjacent elements ; Base Case ; Not selecting current element ; If selecting current element is possible ; Return answer ; Given array arr [ ] ; Given K ; Function Call
function maxSum ( a , n , k ) { if ( n <= 0 ) return 0 ; let option = maxSum ( a , n - 1 , k ) ; if ( k >= a [ n - 1 ] ) option = Math . max ( option , a [ n - 1 ] + maxSum ( a , n - 2 , k - a [ n - 1 ] ) ) ; return option ; } let arr = [ 50 , 10 , 20 , 30 , 40 ] ; let N = arr . length ; let K = 100 ; document . write ( maxSum ( arr , N , K ) ) ;
Count possible binary strings of length N without P consecutive 0 s and Q consecutive 1 s | Function to check if a string satisfy the given condition or not ; Stores the length of string ; Stores the previous character of the string ; Stores the count of consecutive equal characters ; Traverse the string ; If current character is equal to the previous character ; If count of consecutive 1 s is more than Q ; If count of consecutive 0 s is more than P ; Reset value of cnt ; If count of consecutive 1 s is more than Q ; If count of consecutive 0 s is more than P ; Function to count all distinct binary strings that satisfy the given condition ; Stores the length of str ; If length of str is N ; If str satisfy the given condition ; If str does not satisfy the given condition ; Append a character '0' at end of str ; Append a character '1' at end of str ; Return total count of binary strings ; Driver Code
function checkStr ( str , P , Q ) { let N = str . length ; let prev = str [ 0 ] ; let cnt = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( str [ i ] == prev ) { cnt ++ ; } else { if ( prev == ' ' && cnt >= Q ) { return false ; } if ( prev == ' ' && cnt >= P ) { return false ; } cnt = 1 ; } prev = str [ i ] ; } if ( prev == ' ' && cnt >= Q ) { return false ; } if ( prev == ' ' && cnt >= P ) { return false ; } return true ; } function cntBinStr ( str , N , P , Q ) { let len = str . length ; if ( len == N ) { if ( checkStr ( str , P , Q ) ) return 1 ; return 0 ; } let X = cntBinStr ( str + ' ' , N , P , Q ) ; let Y = cntBinStr ( str + ' ' , N , P , Q ) ; return X + Y ; } let N = 5 , P = 2 , Q = 3 ; document . write ( cntBinStr ( " " , N , P , Q ) ) ;
Count possible permutations of given array satisfying the given conditions | Function to get the value of binomial coefficient ; Stores the value of binomial coefficient ; Since C ( N , R ) = C ( N , N - R ) ; Calculate the value of C ( N , R ) ; Function to get the count of permutations of the array that satisfy the condition ; Stores count of permutations of the array that satisfy the given condition ; Stores the value of C ( 2 N , N ) ; Stores the value of catalan number ; Return answer ; Driver Code
function binCoff ( N , R ) { var res = 1 ; if ( R > ( N - R ) ) { R = ( N - R ) ; } for ( i = 0 ; i < R ; i ++ ) { res *= ( N - i ) ; res /= ( i + 1 ) ; } return res ; } function cntPermutation ( N ) { var cntPerm ; var C_2N_N = binCoff ( 2 * N , N ) ; cntPerm = C_2N_N / ( N + 1 ) ; return cntPerm ; } var arr = [ 1 , 2 , 3 , 4 ] ; var N = arr . length ; document . write ( cntPermutation ( N / 2 ) ) ;
Length of Longest Palindrome Substring | Function to obtain the length of the longest palindromic substring ; Length of given string ; Stores the maximum length ; Iterate over the string ; Iterate over the string ; Check for palindrome ; If string [ i , j - i + 1 ] is palindromic ; Return length of LPS ; Given string ; Function Call
function longestPalSubstr ( str ) { var n = str . length ; var maxLength = 1 , start = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) { for ( var j = i ; j < str . length ; j ++ ) { var flag = 1 ; for ( var k = 0 ; k < ( j - i + 1 ) / 2 ; k ++ ) if ( str [ i + k ] != str [ j - k ] ) flag = 0 ; if ( flag && ( j - i + 1 ) > maxLength ) { start = i ; maxLength = j - i + 1 ; } } } return maxLength ; } var str = " " ; document . write ( longestPalSubstr ( str ) ) ;