codeparrot/xlcost-text-to-code huggingface dataset:
Largest subarray sum of all connected components in undirected graph | Function to traverse the undirected graph using the Depth first traversal ; Marking the visited vertex as true ; Store the connected chain ; Recursive call to the DFS algorithm ; Function to return maximum subarray sum of each connected component using Kadane 's Algorithm ; Following loop finds maximum subarray sum based on Kadane 's  algorithm ; Global maximum subarray sum ; Returning the sum ; Function to find the maximum subarray sum among all connected components ; Initializing boolean array to mark visited vertices ; maxSum stores the maximum subarray sum ; Following loop invokes DFS algorithm ; Variable to hold temporary length ; Variable to hold temporary maximum subarray sum values ; Container to store each chain ; DFS algorithm ; Variable to hold each chain size ; Container to store values of vertices of individual chains ; Storing the values of each chain ; Function call to find maximum subarray sum of current connection ; Conditional to store current maximum subarray sum ; Printing global maximum subarray sum ; Initializing graph in the form of adjacency list ; Defining the number of edges and vertices ; Assigning the values for each vertex of the undirected graph ; Constructing the undirected graph
function depthFirst ( v , graph , visited , storeChain ) { visited [ v ] = true ; storeChain . push ( v ) ; for ( let i = 0 ; i < graph [ v ] . length ; i ++ ) { if ( visited [ graph [ v ] [ i ] ] == false ) { depthFirst ( graph [ v ] [ i ] , graph , visited , storeChain ) ; } } } function subarraySum ( arr , n ) { let maxSubarraySum = arr [ 0 ] ; let currentMax = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { currentMax = Math . max ( arr [ i ] , arr [ i ] + currentMax ) ; maxSubarraySum = Math . max ( maxSubarraySum , currentMax ) ; } return maxSubarraySum ; } function maxSubarraySum ( graph , vertices , values ) { let visited = new Array ( 1001 ) ; visited . fill ( false ) ; let maxSum = - 1000000 ; for ( let i = 1 ; i <= vertices ; i ++ ) { if ( visited [ i ] == false ) { let sizeChain ; let tempSum ; let storeChain = [ ] ; depthFirst ( i , graph , visited , storeChain ) ; sizeChain = storeChain . length ; let chainValues = new Array ( sizeChain + 1 ) ; for ( let j = 0 ; j < sizeChain ; j ++ ) { let temp = values [ storeChain [ j ] - 1 ] ; chainValues [ j ] = temp ; } tempSum = subarraySum ( chainValues , sizeChain ) ; if ( tempSum > maxSum ) { maxSum = tempSum ; } } } document . write ( " " ) ; document . write ( " " ) ; document . write ( maxSum ) ; } let graph = [ ] ; for ( let i = 0 ; i < 1001 ; i ++ ) graph . push ( [ ] ) ; let V = 7 ; let values = [ ] ; values . push ( 3 ) ; values . push ( 2 ) ; values . push ( 4 ) ; values . push ( - 2 ) ; values . push ( 0 ) ; values . push ( - 1 ) ; values . push ( - 5 ) ; graph [ 1 ] . push ( 2 ) ; graph [ 2 ] . push ( 1 ) ; graph [ 3 ] . push ( 4 ) ; graph [ 4 ] . push ( 3 ) ; graph [ 4 ] . push ( 5 ) ; graph [ 5 ] . push ( 4 ) ; graph [ 6 ] . push ( 7 ) ; graph [ 7 ] . push ( 6 ) ; maxSubarraySum ( graph , V , values ) ;
Count of Binary strings of length N having atmost M consecutive 1 s or 0 s alternatively exactly K times | Array to contain the final result ; Function to get the number of desirable binary strings ; If we reach end of string and groups are exhausted , return 1 ; If length is exhausted but groups are still to be made , return 0 ; If length is not exhausted but groups are exhausted , return 0 ; If both are negative just return 0 ; If already calculated , return it ; Initialise answer for each state ; Loop through every possible m ; Driver code
dp = Array ( 1000 ) ; for ( i = 0 ; i < 1000 ; i ++ ) dp [ i ] = Array ( 1000 ) . fill ( 0 ) ; function solve ( n , k , m ) { if ( n == 0 && k == 0 ) return 1 ; if ( n == 0 && k != 0 ) return 0 ; if ( n != 0 && k == 0 ) { return 0 ; } if ( n < 0 k < 0 ) return 0 ; if ( dp [ n ] [ k ] > 0 ) return dp [ n ] [ k ] ; var ans = 0 ; for ( var j = 1 ; j <= m ; j ++ ) { ans += solve ( n - j , k - 1 , m ) ; } return dp [ n ] [ k ] = ans ; } var N = 7 , K = 4 , M = 3 ; document . write ( solve ( N , K , M ) ) ;
Number of distinct ways to represent a number as sum of K unique primes | Prime vector ; Sieve array of prime ; DP array ; Sieve of Eratosthenes . ; Push all the primes into prime vector ; Function to get the number of distinct ways to get sum as K different primes ; If index went out of prime array size or the sum became larger than n return 0 ; If sum becomes equal to n and j becomes exactly equal to k . Return 1 , else if j is still not equal to k , return 0 ; If sum != n and still j as exceeded , return 0 ; If that state is already calculated , return directly the ans ; Include the current prime ; Exclude the current prime ; Return by memoizing the ans ; Precompute primes by sieve
var prime = [ ] ; var isprime = Array ( 1000 ) . fill ( true ) ; var dp = Array . from ( Array ( 200 ) , ( ) => Array ( 20 ) ) ; for ( var i = 0 ; i < 200 ; i ++ ) for ( var j = 0 ; j < 20 ; j ++ ) dp [ i ] [ j ] = new Array ( 1000 ) . fill ( 0 ) ; function sieve ( ) { for ( var i = 2 ; i * i <= 1000 ; i ++ ) { if ( isprime [ i ] ) { for ( var j = i * i ; j <= 1000 ; j += i ) { isprime [ j ] = false ; } } } for ( var i = 2 ; i <= 1000 ; i ++ ) { if ( isprime [ i ] ) { prime . push ( i ) ; } } } function CountWays ( i , j , sum , n , k ) { if ( i > prime . length sum > n ) { return 0 ; } if ( sum == n ) { if ( j == k ) { return 1 ; } return 0 ; } if ( j == k ) return 0 ; if ( dp [ i ] [ j ] [ sum ] ) return dp [ i ] [ j ] [ sum ] ; var inc = 0 , exc = 0 ; inc = CountWays ( i + 1 , j + 1 , sum + prime [ i ] , n , k ) ; exc = CountWays ( i + 1 , j , sum , n , k ) ; return dp [ i ] [ j ] [ sum ] = inc + exc ; } sieve ( ) ; var N = 100 , K = 5 ; document . write ( CountWays ( 0 , 0 , 0 , N , K ) ) ;
Count minimum factor jumps required to reach the end of an Array | Vector to store factors of each integer ; Initialize the dp array ; Precompute all the factors of every integer ; Function to count the minimum factor jump ; Initialise minimum jumps to reach each cell as INT_MAX ; 0 jumps required to reach the first cell ; Iterate over all cells ; calculating for each jump ; If a cell is in bound ; Return minimum jumps to reach last cell ; Pre - calculating the factors ; Function call
var factors = Array . from ( Array ( 100005 ) , ( ) => Array ( ) ) ; var dp = Array ( 100005 ) ; function precompute ( ) { for ( var i = 1 ; i <= 100000 ; i ++ ) { for ( var j = i ; j <= 100000 ; j += i ) factors [ j ] . push ( i ) ; } } function solve ( arr , n ) { for ( var i = 0 ; i <= 100005 ; i ++ ) { dp [ i ] = 1000000000 ; } dp [ 0 ] = 0 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j of factors [ arr [ i ] ] ) { if ( i + j < n ) dp [ i + j ] = Math . min ( dp [ i + j ] , 1 + dp [ i ] ) ; } } return dp [ n - 1 ] ; } precompute ( ) ; var arr = [ 2 , 8 , 16 , 55 , 99 , 100 ] ; var n = arr . length document . write ( solve ( arr , n ) ) ;
Find the length of the Largest subset such that all elements are Pairwise Coprime | Dynamic programming table ; Function to obtain the mask for any integer ; List of prime numbers till 50 ; Iterate through all prime numbers to obtain the mask ; Set this prime 's bit ON in the mask ; Return the mask value ; Function to count the number of ways ; Check if subproblem has been solved ; Excluding current element in the subset ; Check if there are no common prime factors then only this element can be included ; Calculate the new mask if this element is included ; Store and return the answer ; Function to find the count of subarray with all digits unique ; Initializing dp ; Driver code
var dp = Array . from ( Array ( 5000 ) , ( ) => Array ( ( 1 << 10 ) + 5 ) ) ; function getmask ( val ) { var mask = 0 ; var prime = [ 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 ] ; for ( var i = 0 ; i < 15 ; i ++ ) { if ( val % prime [ i ] == 0 ) { mask = mask | ( 1 << i ) ; } } return mask ; } function calculate ( pos , mask , a , n ) { if ( pos == n || mask == ( 1 << n - 1 ) ) return 0 ; if ( dp [ pos ] [ mask ] != - 1 ) return dp [ pos ] [ mask ] ; var size = 0 ; size = Math . max ( size , calculate ( pos + 1 , mask , a , n ) ) ; if ( ( getmask ( a [ pos ] ) & mask ) == 0 ) { var new_mask = ( mask | ( getmask ( a [ pos ] ) ) ) ; size = Math . max ( size , 1 + calculate ( pos + 1 , new_mask , a , n ) ) ; } return dp [ pos ] [ mask ] = size ; } function largestSubset ( a , n ) { dp = Array . from ( Array ( 5000 ) , ( ) => Array ( ( 1 << 10 ) + 5 ) . fill ( - 1 ) ) ; return calculate ( 0 , 0 , a , n ) ; } var A = [ 2 , 3 , 13 , 5 , 14 , 6 , 7 , 11 ] ; var N = A . length ; document . write ( largestSubset ( A , N ) ) ;
Maximum sum such that exactly half of the elements are selected and no two adjacent | Function return the maximum sum possible under given condition ; Base case ; When i is odd ; When i is even ; Maximum of if we pick last element or not ; Driver code
function MaximumSum ( a , n ) { var dp = Array . from ( Array ( n + 1 ) , ( ) => Array ( 2 ) . fill ( 0 ) ) ; dp [ 2 ] [ 1 ] = a [ 1 ] ; dp [ 2 ] [ 0 ] = a [ 0 ] ; for ( var i = 3 ; i < n + 1 ; i ++ ) { if ( i & 1 ) { var temp = ( [ dp [ i - 3 ] [ 1 ] , dp [ i - 3 ] [ 0 ] , dp [ i - 2 ] [ 1 ] , dp [ i - 2 ] [ 0 ] ] . reduce ( ( a , b ) => Math . max ( a , b ) ) ) ; dp [ i ] [ 1 ] = a [ i - 1 ] + temp ; dp [ i ] [ 0 ] = ( [ a [ i - 2 ] + dp [ i - 2 ] [ 0 ] , a [ i - 2 ] + dp [ i - 3 ] [ 1 ] , a [ i - 2 ] + dp [ i - 3 ] [ 0 ] , a [ i - 3 ] + dp [ i - 3 ] [ 0 ] ] . reduce ( ( a , b ) => Math . max ( a , b ) ) ) ; } else { dp [ i ] [ 1 ] = a [ i - 1 ] + ( [ dp [ i - 2 ] [ 1 ] , dp [ i - 2 ] [ 0 ] , dp [ i - 1 ] [ 0 ] ] . reduce ( ( a , b ) => Math . max ( a , b ) ) ) ; dp [ i ] [ 0 ] = a [ i - 2 ] + dp [ i - 2 ] [ 0 ] ; } } return Math . max ( dp [ n ] [ 1 ] , dp [ n ] [ 0 ] ) ; } var A = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; var N = A . length ; document . write ( MaximumSum ( A , N ) ) ;
Optimal Strategy for the Divisor game using Dynamic Programming | Recursive function to find the winner ; check if N = 1 or N = 3 then player B wins ; check if N = 2 then player A wins ; check if current state already visited then return the previously obtained ans ; check if currently it is player A 's turn  then initialise the ans to 0 ; Traversing across all the divisors of N which are less than N ; check if current value of i is a divisor of N ; check if it is player A 's turn  then we need at least one true ; Else if it is player B 's turn  then we need at least one false ; Return the current ans ; initialise N
function divisorGame ( N , A , dp ) { if ( N == 1 N == 3 ) return false ; if ( N == 2 ) return true ; if ( dp [ N ] [ A ] != - 1 ) return dp [ N ] [ A ] ; let ans = ( A == 1 ) ? 0 : 1 ; for ( let i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { if ( A ) ans |= divisorGame ( N - i , 0 , dp ) ; else ans &= divisorGame ( N - i , 1 , dp ) ; } } return dp [ N ] [ A ] = ans ; } let N = 3 ; let dp = [ ] ; for ( let i = 0 ; i < N + 1 ; i ++ ) { let temp = [ - 1 ] for ( let j = 0 ; j < 2 ; j ++ ) { temp . push ( [ - 1 ] ) } dp . push ( temp ) } if ( divisorGame ( N , 1 , dp ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Find the count of mountains in a given Matrix | Function to count number of mountains in a given matrix of size n ; form another matrix with one extra layer of border elements . Border elements will contain INT_MIN value . ; For border elements , set value as INT_MIN ; For rest elements , just copy it into new matrix ; Check for mountains in the modified matrix ; check for all directions ; Driver code
function countMountains ( a , n ) { var A = new Array ( n + 2 ) . fill ( 0 ) . map ( ( ) => new Array ( n + 2 ) . fill ( 0 ) ) ; var count = 0 ; for ( var i = 0 ; i < n + 2 ; i ++ ) { for ( var j = 0 ; j < n + 2 ; j ++ ) { if ( ( i == 0 ) || ( j == 0 ) || ( i == n + 1 ) || ( j == n + 1 ) ) { A [ i ] [ j ] = Number . MIN_VALUE ; } else { A [ i ] [ j ] = a [ i - 1 ] [ j - 1 ] ; } } } for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = 1 ; j <= n ; j ++ ) { if ( ( A [ i ] [ j ] > A [ i - 1 ] [ j ] ) && ( A [ i ] [ j ] > A [ i + 1 ] [ j ] ) && ( A [ i ] [ j ] > A [ i ] [ j - 1 ] ) && ( A [ i ] [ j ] > A [ i ] [ j + 1 ] ) && ( A [ i ] [ j ] > A [ i - 1 ] [ j - 1 ] ) && ( A [ i ] [ j ] > A [ i + 1 ] [ j + 1 ] ) && ( A [ i ] [ j ] > A [ i - 1 ] [ j + 1 ] ) && ( A [ i ] [ j ] > A [ i + 1 ] [ j - 1 ] ) ) { count ++ ; } } } return count ; } var a = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; var n = 3 ; document . write ( countMountains ( a , n ) ) ;
Minimum length of the reduced Array formed using given operations | Function to find the length of minimized array ; Creating the required dp t0ables Initialising the dp table by - 1 ; Initialising the dp table by - 1 ; base case ; Check if the two subarray can be combined ; Initialising dp1 table with max value ; Check if the subarray can be reduced to a single element ; Minimal partition of [ 1 : j - 1 ] + 1 ; Driver code
function minimalLength ( a , n ) { var i , j , k ; var dp = Array ( n + 1 ) . fill ( Array ( n + 1 ) . fill ( - 1 ) ) ; var dp1 = Array ( n ) . fill ( 0 ) ; for ( var size = 1 ; size <= n ; size ++ ) { for ( i = 0 ; i < n - size + 1 ; i ++ ) { j = i + size - 1 ; if ( i == j ) dp [ i ] [ j ] = a [ i ] ; else { for ( k = i ; k < j ; k ++ ) { if ( dp [ i ] [ k ] != - 1 && dp [ i ] [ k ] == dp [ k + 1 ] [ j ] ) dp [ i ] [ j ] = dp [ i ] [ k ] + 1 ; } } } } for ( i = 0 ; i < n ; i ++ ) dp1 [ i ] = 1000000000 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) { if ( dp [ j ] [ i ] != - 1 ) { if ( j == 0 ) dp1 [ i ] = 2 ; else dp1 [ i ] = Math . min ( dp1 [ i ] , dp1 [ j - 1 ] + 1 ) ; } } } return dp1 [ n - 1 ] ; } var n = 7 ; var a = [ 3 , 3 , 4 , 4 , 4 , 3 , 3 ] ; document . write ( minimalLength ( a , n ) ) ;
Maximum score possible after performing given operations on an Array | Memoizing by the use of a table ; Function to calculate maximum score ; Bse case ; If the same state has already been computed ; Sum of array in range ( l , r ) ; If the operation is even - numbered the score is decremented ; Exploring all paths , and storing maximum value in DP table to avoid further repetitive recursive calls ; Function to find the max score ; Prefix sum array ; Calculating prefix_sum ; Initialising the DP table , - 1 represents the subproblem hasn 't been solved yet ; Driver code
let dp = new Array ( 100 ) ; for ( let i = 0 ; i < 100 ; i ++ ) { dp [ i ] = new Array ( 100 ) ; for ( let j = 0 ; j < 100 ; j ++ ) { dp [ i ] [ j ] = new Array ( 100 ) ; for ( let k = 0 ; k < 100 ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } function MaximumScoreDP ( l , r , prefix_sum , num ) { if ( l > r ) return 0 ; if ( dp [ l ] [ r ] [ num ] != - 1 ) return dp [ l ] [ r ] [ num ] ; let current_sum = prefix_sum [ r ] - ( l - 1 >= 0 ? prefix_sum [ l - 1 ] : 0 ) ; if ( num % 2 == 0 ) current_sum *= - 1 ; dp [ l ] [ r ] [ num ] = current_sum + Math . max ( MaximumScoreDP ( l + 1 , r , prefix_sum , num + 1 ) , MaximumScoreDP ( l , r - 1 , prefix_sum , num + 1 ) ) ; return dp [ l ] [ r ] [ num ] ; } function findMaxScore ( a , n ) { let prefix_sum = new Array ( n ) ; prefix_sum [ 0 ] = a [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { prefix_sum [ i ] = prefix_sum [ i - 1 ] + a [ i ] ; } return MaximumScoreDP ( 0 , n - 1 , prefix_sum , 1 ) ; } let n = 6 ; let A = [ 1 , 2 , 3 , 4 , 2 , 6 ] ; document . write ( findMaxScore ( A , n ) ) ;
Count number of binary strings without consecutive 1 â €™ s : Set 2 | Table to store the solution of every sub problem ; Here , pos : keeps track of current position . f1 : is the flag to check if current number is less than N or not . pr : represents the previous digit ; Base case ; Check if this subproblem has already been solved ; Placing 0 at the current position as it does not violate the condition ; Here flag will be 1 for the next recursive call ; Placing 1 at this position only if the previously inserted number is 0 ; If the number is smaller than N ; If the digit at current position is 1 ; Storing the solution to this subproblem ; Function to find the number of integers less than or equal to N with no consecutive 1 aTMs in binary representation ; Convert N to binary form ; Loop to convert N from Decimal to binary ; Calling the function ; Driver code
var memo = Array . from ( Array ( 32 ) , ( ) => Array ( 2 ) ) ; for ( var i = 0 ; i < 32 ; i ++ ) { for ( var j = 0 ; j < 2 ; j ++ ) { memo [ i ] [ j ] = new Array ( 2 ) . fill ( - 1 ) ; } } function dp ( pos , fl , pr , bin ) { if ( pos == bin . length ) return 1 ; if ( memo [ pos ] [ fl ] [ pr ] != - 1 ) return memo [ pos ] [ fl ] [ pr ] ; var val = 0 ; if ( bin [ pos ] == ' ' ) val = val + dp ( pos + 1 , fl , 0 , bin ) ; else if ( bin [ pos ] == ' ' ) val = val + dp ( pos + 1 , 1 , 0 , bin ) ; if ( pr == 0 ) { if ( fl == 1 ) { val += dp ( pos + 1 , fl , 1 , bin ) ; } else if ( bin [ pos ] == ' ' ) { val += dp ( pos + 1 , fl , 1 , bin ) ; } } return memo [ pos ] [ fl ] [ pr ] = val ; } function findIntegers ( num ) { var bin = " " ; while ( num > 0 ) { if ( num % 2 ) bin += " " ; else bin += " " ; num = parseInt ( num / 2 ) ; } bin = bin . split ( ' ' ) . reverse ( ) . join ( ' ' ) return dp ( 0 , 0 , 0 , bin ) ; } var N = 12 ; document . write ( findIntegers ( N ) ) ;
Count ways to reach end from start stone with at most K jumps at each step | Function which returns total no . of ways to reach nth step from sth steps ; Initialize dp array ; filling all the elements with 0 ; Initialize ( s - 1 ) th index to 1 ; Iterate a loop from s to n ; starting range for counting ranges ; Calculate Maximum moves to Reach ith step ; For nth step return dp [ n - 1 ] ; no of steps ; Atmost steps allowed ; starting range
function TotalWays ( n , s , k ) { let dp = new Array ( n ) ; dp . fill ( 0 ) ; dp [ s - 1 ] = 1 ; for ( let i = s ; i < n ; i ++ ) { let idx = Math . max ( s - 1 , i - k ) ; for ( let j = idx ; j < i ; j ++ ) { dp [ i ] += dp [ j ] ; } } return dp [ n - 1 ] ; } let n = 5 ; let k = 2 ; let s = 2 ; document . write ( " " + TotalWays ( n , s , k ) ) ;
Maximum subsequence sum with adjacent elements having atleast K difference in index | Function to find the maximum sum subsequence such that two adjacent element have atleast difference of K in their indices ; DP Array to store the maximum sum obtained till now ; Either select the first element or Nothing ; Either Select the ( i - 1 ) element or let the previous best answer be the current best answer ; Either select the best sum till previous_index or select the current element + best_sum till index - k ; Driver Code
function max_sum ( arr , n , k ) { var dp = Array . from ( { length : n } , ( _ , i ) => 0 ) ; dp [ 0 ] = Math . max ( 0 , arr [ 0 ] ) ; var i = 1 ; while ( i < k ) { dp [ i ] = Math . max ( dp [ i - 1 ] , arr [ i ] ) ; i ++ ; } i = k ; while ( i < n ) { dp [ i ] = Math . max ( dp [ i - 1 ] , arr [ i ] + dp [ i - k ] ) ; i ++ ; } return dp [ n - 1 ] ; } var arr = [ 1 , 2 , - 2 , 4 , 3 , 1 ] ; var n = arr . length ; var k = 4 ; document . write ( max_sum ( arr , n , k ) ) ;
Number of binary strings such that there is no substring of length â ‰¥ 3 | Javascript implementation of the approach ; Function to return the count of all possible binary strings ; Base cases ; dp [ i ] [ j ] is the number of possible strings such that '1' just appeared consecutively j times upto ith index ; Taking previously calculated value ; Taking all the possible cases that can appear at the Nth position ; Driver code
var MOD = 1000000007 ; function countStr ( N ) { var dp = Array . from ( Array ( N + 1 ) , ( ) => Array ( 3 ) . fill ( 0 ) ) ; dp [ 1 ] [ 0 ] = 1 ; dp [ 1 ] [ 1 ] = 1 ; dp [ 1 ] [ 2 ] = 0 ; for ( var i = 2 ; i <= N ; i ++ ) { dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] + dp [ i - 1 ] [ 2 ] ) % MOD ; dp [ i ] [ 1 ] = dp [ i - 1 ] [ 0 ] % MOD ; dp [ i ] [ 2 ] = dp [ i - 1 ] [ 1 ] % MOD ; } var ans = ( dp [ N ] [ 0 ] + dp [ N ] [ 1 ] + dp [ N ] [ 2 ] ) % MOD ; return ans ; } var N = 8 ; document . write ( countStr ( N ) ) ;
Maximize length of Subarray of 1 's after removal of a pair of consecutive Array elements | Javascript program to find the maximum count of 1 s ; If arr [ i - 2 ] = = 1 then we increment the count of occurences of 1 's ; Else we initialise the count with 0 ; If arr [ i + 2 ] = = 1 then we increment the count of occurences of 1 's ; Else we initialise the count with 0 ; We get the maximum count by skipping the current and the next element . ; Driver code
function maxLengthOf1s ( arr , n ) { let prefix = new Array ( n ) ; prefix . fill ( 0 ) ; for ( let i = 2 ; i < n ; i ++ ) { if ( arr [ i - 2 ] == 1 ) prefix [ i ] = prefix [ i - 1 ] + 1 ; else prefix [ i ] = 0 ; } let suffix = new Array ( n ) ; suffix . fill ( 0 ) ; for ( let i = n - 3 ; i >= 0 ; i -- ) { if ( arr [ i + 2 ] == 1 ) suffix [ i ] = suffix [ i + 1 ] + 1 ; else suffix [ i ] = 0 ; } let ans = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { ans = Math . max ( ans , prefix [ i + 1 ] + suffix [ i ] ) ; } document . write ( ans ) ; } let n = 6 ; let arr = [ 1 , 1 , 1 , 0 , 1 , 1 ] ; maxLengthOf1s ( arr , n ) ;
Count number of ways to get Odd Sum | Count the ways to sum up with odd by choosing one element form each pair ; Initialize two array with 0 ; if element is even ; store count of even number in i 'th pair ; if the element is odd ; store count of odd number in i 'th pair ; Initial state of dp array ; dp [ i ] [ 0 ] = total number of ways to get even sum upto i 'th pair ; dp [ i ] [ 1 ] = total number of ways to odd even sum upto i 'th pair ; dp [ n - 1 ] [ 1 ] = total number of ways to get odd sum upto n 'th pair ; Driver code
function CountOfOddSum ( a , n ) { let dp = new Array ( n ) , cnt = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { dp [ i ] = new Array ( 2 ) . fill ( 0 ) ; cnt [ i ] = new Array ( 2 ) . fill ( 0 ) ; } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < 2 ; j ++ ) { if ( a [ i ] [ j ] % 2 == 0 ) { cnt [ i ] [ 0 ] ++ ; } else { cnt [ i ] [ 1 ] ++ ; } } } dp [ 0 ] [ 0 ] = cnt [ 0 ] [ 0 ] , dp [ 0 ] [ 1 ] = cnt [ 0 ] [ 1 ] ; for ( let i = 1 ; i < n ; i ++ ) { dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] * cnt [ i ] [ 0 ] + dp [ i - 1 ] [ 1 ] * cnt [ i ] [ 1 ] ) ; dp [ i ] [ 1 ] = ( dp [ i - 1 ] [ 0 ] * cnt [ i ] [ 1 ] + dp [ i - 1 ] [ 1 ] * cnt [ i ] [ 0 ] ) ; } return dp [ n - 1 ] [ 1 ] ; } let a = [ [ 1 , 2 ] , [ 3 , 6 ] ] ; let n = a . length ; let ans = CountOfOddSum ( a , n ) ; document . write ( ans ) ;
Longest Consecuetive Subsequence when only one insert operation is allowed | Function to return the length of longest consecuetive subsequence after inserting an element ; Variable to find maximum value of the array ; Calculating maximum value of the array ; Declaring the DP table ; Variable to store the maximum length ; Iterating for every value present in the array ; Recurrence for dp [ val ] [ 0 ] ; No value can be inserted before 1 , hence the element value should be greater than 1 for this recurrance relation ; Recurrence for dp [ val ] [ 1 ] ; Maximum length of consecutive sequence ending at 1 is equal to 1 ; Update the ans variable with the new maximum length possible ; Return the ans ; Input array
function LongestConsSeq ( arr , N ) { let maxval = 1 ; for ( let i = 0 ; i < N ; i += 1 ) { maxval = Math . max ( maxval , arr [ i ] ) ; } let dp = new Array ( maxval + 1 ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; for ( let j = 0 ; j < 2 ; j ++ ) dp [ i ] [ j ] = 0 ; } let ans = 1 ; for ( let i = 0 ; i < N ; i += 1 ) { dp [ arr [ i ] ] [ 0 ] = ( 1 + dp [ arr [ i ] - 1 ] [ 0 ] ) ; if ( arr [ i ] >= 2 ) dp [ arr [ i ] ] [ 1 ] = Math . max ( 1 + dp [ arr [ i ] - 1 ] [ 1 ] , 2 + dp [ arr [ i ] - 2 ] [ 0 ] ) ; else dp [ arr [ i ] ] [ 1 ] = 1 ; ans = Math . max ( ans , dp [ arr [ i ] ] [ 1 ] ) ; } return ans ; } let arr = [ 2 , 1 , 4 , 5 ] ; let N = arr . length ; document . write ( LongestConsSeq ( arr , N ) ) ;