codeparrot/xlcost-text-to-code huggingface dataset:
Count even length binary sequences with same sum of first and second half bits | A lookup table to store the results of subproblems ; dif is difference between sums of first n bits and last n bits i . e . , dif = ( Sum of first n bits ) - ( Sum of last n bits ) ; We can 't cover difference of  more than n with 2n bits ; n == 1 , i . e . , 2 bit long sequences ; Check if this subproblem is already solved n is added to dif to make sure index becomes positive ; let res = First bit is 0 & last bit is 1 ; First and last bits are same ; First bit is 1 & last bit is 0 ; Store result in lookup table and return the result ; A Wrapper over countSeqUtil ( ) . It mainly initializes lookup table , then calls countSeqUtil ( ) ; Initialize all entries of lookup table as not filled memset ( lookup , - 1 , sizeof ( lookup ) ) ; ; call countSeqUtil ( ) ; Driver program
let lookup = new Array ( 1000 ) ; for ( let i = 0 ; i < 1000 ; i ++ ) { lookup [ i ] = new Array ( 1000 ) ; } function countSeqUtil ( n , dif ) { if ( Math . abs ( dif ) > n ) return 0 ; if ( n == 1 && dif == 0 ) return 2 ; if ( n == 1 && Math . abs ( dif ) == 1 ) return 1 ; if ( lookup [ n ] [ n + dif ] != - 1 ) return lookup [ n ] [ n + dif ] ; countSeqUtil ( n - 1 , dif + 1 ) + 2 * countSeqUtil ( n - 1 , dif ) + countSeqUtil ( n - 1 , dif - 1 ) ; return lookup [ n ] [ n + dif ] = res ; } function countSeq ( n ) { for ( let k = 0 ; k < lookup . length ; k ++ ) { for ( let j = 0 ; j < lookup . length ; j ++ ) { lookup [ k ] [ j ] = - 1 ; } } return countSeqUtil ( n , 0 ) ; } let n = 2 ; document . write ( " " + countSeq ( 2 ) ) ;
Boolean Parenthesization Problem | DP |  ; Count number of True in left Partition ; Count number of False in left Partition ; Count number of True in right Partition ; Count number of False in right Partition ; Evaluate AND operation ; Evaluate OR operation ; Evaluate XOR operation ; Driver code ; We obtain the string T | T & F ^ T ; There are 4 ways ( ( T T ) & ( F ^ T ) ) , ( T | ( T & ( F ^ T ) ) ) , ( ( ( T T ) & F ) ^ T ) and ( T | ( ( T & F ) ^ T ) )
function countWays ( N , S ) { 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 ] = new Array ( 2 ) ; for ( let k = 0 ; k < 2 ; k ++ ) dp [ i ] [ j ] [ k ] = - 1 ; } } return parenthesis_count ( S , 0 , N - 1 , 1 , dp ) ; } function parenthesis_count ( str , i , j , isTrue , dp ) { if ( i > j ) return 0 ; if ( i == j ) { if ( isTrue == 1 ) { return ( str [ i ] == ' ' ) ? 1 : 0 ; } else { return ( str [ i ] == ' ' ) ? 1 : 0 ; } } if ( dp [ i ] [ j ] [ isTrue ] != - 1 ) return dp [ i ] [ j ] [ isTrue ] ; let temp_ans = 0 ; let leftTrue , rightTrue , leftFalse , rightFalse ; for ( let k = i + 1 ; k <= j - 1 ; k = k + 2 ) { if ( dp [ i ] [ k - 1 ] [ 1 ] != - 1 ) leftTrue = dp [ i ] [ k - 1 ] [ 1 ] ; else { leftTrue = parenthesis_count ( str , i , k - 1 , 1 , dp ) ; } if ( dp [ i ] [ k - 1 ] [ 0 ] != - 1 ) leftFalse = dp [ i ] [ k - 1 ] [ 0 ] ; else { leftFalse = parenthesis_count ( str , i , k - 1 , 0 , dp ) ; } if ( dp [ k + 1 ] [ j ] [ 1 ] != - 1 ) rightTrue = dp [ k + 1 ] [ j ] [ 1 ] ; else { rightTrue = parenthesis_count ( str , k + 1 , j , 1 , dp ) ; } if ( dp [ k + 1 ] [ j ] [ 0 ] != - 1 ) rightFalse = dp [ k + 1 ] [ j ] [ 0 ] ; else { rightFalse = parenthesis_count ( str , k + 1 , j , 0 , dp ) ; } if ( str [ k ] == ' ' ) { if ( isTrue == 1 ) { temp_ans = temp_ans + leftTrue * rightTrue ; } else { temp_ans = temp_ans + leftTrue * rightFalse + leftFalse * rightTrue + leftFalse * rightFalse ; } } else if ( str [ k ] == ' ' ) { if ( isTrue == 1 ) { temp_ans = temp_ans + leftTrue * rightTrue + leftTrue * rightFalse + leftFalse * rightTrue ; } else { temp_ans = temp_ans + leftFalse * rightFalse ; } } else if ( str [ k ] == ' ' ) { if ( isTrue == 1 ) { temp_ans = temp_ans + leftTrue * rightFalse + leftFalse * rightTrue ; } else { temp_ans = temp_ans + leftTrue * rightTrue + leftFalse * rightFalse ; } } dp [ i ] [ j ] [ isTrue ] = temp_ans ; } return temp_ans ; } let symbols = " " ; let operators = " " ; let S = [ ] ; let j = 0 ; for ( let i = 0 ; i < symbols . length ; i ++ ) { S . push ( symbols [ i ] ) ; if ( j < operators . length ) S . push ( operators [ j ++ ] ) ; } let N = S . length ; document . write ( countWays ( N , S . join ( " " ) ) ) ;
Remove minimum elements from either side such that 2 * min becomes more than max | A utility function to find minimum in arr [ l . . h ] ; A utility function to find maximum in arr [ l . . h ] ; Returns the minimum number of removals from either end in arr [ l . . h ] so that 2 * min becomes greater than max . ; If there is 1 or less elements , return 0 For a single element , 2 * min > max ( Assumption : All elements are positive in arr [ ] ) ; 1 ) Find minimum and maximum in arr [ l . . h ] ; If the property is followed , no removals needed ; Otherwise remove a character from left end and recur , then remove a character from right end and recur , take the minimum of two is returned ; Driver Code
function min ( arr , l , h ) { let mn = arr [ l ] ; for ( let i = l + 1 ; i <= h ; i ++ ) { if ( mn > arr [ i ] ) mn = arr [ i ] ; } return mn ; } function max ( arr , l , h ) { let mx = arr [ l ] ; for ( let i = l + 1 ; i <= h ; i ++ ) { if ( mx < arr [ i ] ) mx = arr [ i ] ; } return mx ; } function minRemovals ( arr , l , h ) { if ( l >= h ) return 0 ; let mn = min ( arr , l , h ) ; let mx = max ( arr , l , h ) ; if ( 2 * mn > mx ) return 0 ; return Math . min ( minRemovals ( arr , l + 1 , h ) , minRemovals ( arr , l , h - 1 ) ) + 1 ; } let arr = [ 4 , 5 , 100 , 9 , 10 , 11 , 12 , 15 , 200 ] ; let n = arr . length ; document . write ( minRemovals ( arr , 0 , n - 1 ) ) ;
Two Pointers Technique | Two pointer technique based solution to find if there is a pair in A [ 0. . N - 1 ] with a given sum . ; represents first pointer ; represents second pointer ; If we find a pair ; If sum of elements at current pointers is less , we move towards higher values by doing i ++ ; If sum of elements at current pointers is more , we move towards lower values by doing j -- ; array declaration ; value to search ; size of the array ; Function call
function isPairSum ( A , N , X ) { var i = 0 ; var j = N - 1 ; while ( i < j ) { if ( A [ i ] + A [ j ] == X ) return true ; else if ( A [ i ] + A [ j ] < X ) i ++ ; else j -- ; } return false ; } var arr = [ 3 , 5 , 9 , 2 , 8 , 10 , 11 ] ; var val = 17 ; var arrSize = 7 ; document . write ( isPairSum ( arr , arrSize , val ) ) ;
Sum of heights of all individual nodes in a binary tree | A binary tree Node has data , pointer to left child and a pointer to right child ; Function to sum of heights of individual Nodes Uses Inorder traversal ; Driver code
class Node { constructor ( data ) { this . data = data ; this . left = this . right = null ; } } let sum ; function getTotalHeightUtil ( root ) { if ( root == null ) { return 0 ; } let lh = getTotalHeightUtil ( root . left ) ; let rh = getTotalHeightUtil ( root . right ) ; let h = Math . max ( lh , rh ) + 1 ; sum = sum + h ; return h ; } function getTotalHeight ( root ) { sum = 0 ; getTotalHeightUtil ( root ) ; return sum ; } let root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; document . write ( " " , getTotalHeight ( root ) ) ;
Count all possible paths from top left to bottom right of a mXn matrix | Returns count of possible paths to reach cell at row number m and column number n from the topmost leftmost cell ( cell at 1 , 1 ) ; Create a 2D table to store results of subproblems ; Count of paths to reach any cell in first column is 1 ; Count of paths to reach any cell in first column is 1 ; Calculate count of paths for other cells in bottom - up manner using the recursive solution ; By uncommenting the last part the code calculates the total possible paths if the diagonal Movements are allowed ; Driver program to test above function
function numberOfPaths ( m , n ) { var count = Array ( m ) . fill ( 0 ) . map ( x => Array ( n ) . fill ( 0 ) ) ; for ( i = 0 ; i < m ; i ++ ) count [ i ] [ 0 ] = 1 ; for ( j = 0 ; j < n ; j ++ ) count [ 0 ] [ j ] = 1 ; for ( i = 1 ; i < m ; i ++ ) { for ( j = 1 ; j < n ; j ++ ) count [ i ] [ j ] = count [ i - 1 ] [ j ] + count [ i ] [ j - 1 ] + count [ i - 1 ] [ j - 1 ] ; ; } return count [ m - 1 ] [ n - 1 ] ; } document . write ( numberOfPaths ( 3 , 3 ) ) ;
Maximum Product Cutting | DP | The main function that returns maximum product obtainable from a rope of length n ; Base cases ; Make a cut at different places and take the maximum of all ; Return the maximum of all values ; Driver program to test above functions
function maxProd ( n ) { if ( n == 0 n == 1 ) return 0 ; let max_val = 0 ; for ( let i = 1 ; i < n ; i ++ ) { max_val = Math . max ( max_val , Math . max ( i * ( n - i ) , maxProd ( n - i ) * i ) ) ; } return max_val ; } document . write ( " " + maxProd ( 10 ) ) ;
Assembly Line Scheduling | DP | A JavaScript program to find minimum possible time by the car chassis to complete ; Utility function to find a minimum of two numbers ; time taken to leave first station in line 1 ; time taken to leave first station in line 2 ; Fill tables T1 [ ] and T2 [ ] using the above given recursive relations ; Consider exit times and retutn minimum ; Driver Code
const NUM_LINE = 2 ; const NUM_STATION = 4 ; function min ( a , b ) { return a < b ? a : b ; } function carAssembly ( a , t , e , x ) { let T1 = new Array ( NUM_STATION ) ; let T2 = new Array ( NUM_STATION ) ; let i ; T1 [ 0 ] = e [ 0 ] + a [ 0 ] [ 0 ] ; T2 [ 0 ] = e [ 1 ] + a [ 1 ] [ 0 ] ; for ( i = 1 ; i < NUM_STATION ; ++ i ) { T1 [ i ] = min ( T1 [ i - 1 ] + a [ 0 ] [ i ] , T2 [ i - 1 ] + t [ 1 ] [ i ] + a [ 0 ] [ i ] ) ; T2 [ i ] = min ( T2 [ i - 1 ] + a [ 1 ] [ i ] , T1 [ i - 1 ] + t [ 0 ] [ i ] + a [ 1 ] [ i ] ) ; } return min ( T1 [ NUM_STATION - 1 ] + x [ 0 ] , T2 [ NUM_STATION - 1 ] + x [ 1 ] ) ; } let a = [ [ 4 , 5 , 3 , 2 ] , [ 2 , 10 , 1 , 4 ] ] ; let t = [ [ 0 , 7 , 4 , 5 ] , [ 0 , 9 , 2 , 8 ] ] ; let e = [ 10 , 12 ] , x = [ 18 , 7 ] ; document . write ( carAssembly ( a , t , e , x ) ) ;
Longest Common Substring | DP | Returns length of longest common substring of X [ 0. . m - 1 ] and Y [ 0. . n - 1 ] ; Create a table to store lengths of longest common suffixes of substrings . Note that LCSuff [ i ] [ j ] contains length of longest common suffix of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] . The first row and first column entries have no logical meaning , they are used only for simplicity of program ; To store length of the longest common substring ; Following steps build LCSuff [ m + 1 ] [ n + 1 ] in bottom up fashion ; Driver Code
function LCSubStr ( X , Y , m , n ) { var LCStuff = Array ( m + 1 ) . fill ( ) . map ( ( ) => Array ( n + 1 ) . fill ( 0 ) ) ; var result = 0 ; for ( i = 0 ; i <= m ; i ++ ) { for ( j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) LCStuff [ i ] [ j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) { LCStuff [ i ] [ j ] = LCStuff [ i - 1 ] [ j - 1 ] + 1 ; result = Math . max ( result , LCStuff [ i ] [ j ] ) ; } else LCStuff [ i ] [ j ] = 0 ; } } return result ; } var X = " " ; var Y = " " ; var m = X . length ; var n = Y . length ; document . write ( " " + LCSubStr ( X , Y , m , n ) ) ;
Minimum insertions to form a palindrome | DP | A DP function to find minimum number of insertions ; Create a table of size n * n . table [ i ] [ j ] will store minimum number of insertions needed to convert str [ i . . j ] to a palindrome . ; Fill the table ; Return minimum number of insertions for str [ 0. . n - 1 ] ; Driver program to test above function .
function findMinInsertionsDP ( str , n ) { let table = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { table [ i ] = new Array ( n ) ; } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { table [ i ] [ j ] = 0 ; } } let l = 0 , h = 0 , gap = 0 ; for ( gap = 1 ; gap < n ; gap ++ ) { for ( l = 0 , h = gap ; h < n ; l ++ , h ++ ) { table [ l ] [ h ] = ( str [ l ] == str [ h ] ) ? table [ l + 1 ] [ h - 1 ] : ( Math . min ( table [ l ] [ h - 1 ] , table [ l + 1 ] [ h ] ) + 1 ) ; } } return table [ 0 ] [ n - 1 ] ; } let str = " " ; document . write ( findMinInsertionsDP ( str , str . length ) ) ;
Maximum Subarray Sum using Divide and Conquer algorithm | A utility function to find maximum of two integers ; A utility function to find maximum of three integers ; Find the maximum possible sum in arr [ ] auch that arr [ m ] is part of it ; Include elements on left of mid . ; Include elements on right of mid ; Return sum of elements on left and right of mid returning only left_sum + right_sum will fail for [ - 2 , 1 ] ; Returns sum of maximum sum subarray in aa [ l . . h ] ; Base Case : Only one element ; Find middle point ; Return maximum of following three possible cases a ) Maximum subarray sum in left half b ) Maximum subarray sum in right half c ) Maximum subarray sum such that the subarray crosses the midpoint ; Driver Code
function max ( a , b ) { return ( a > b ) ? a : b ; } function max ( a , b , c ) { return Math . max ( Math . max ( a , b ) , c ) ; } function maxCrossingSum ( arr , l , m , h ) { let sum = 0 ; let left_sum = Number . MIN_VALUE ; for ( let i = m ; i >= l ; i -- ) { sum = sum + arr [ i ] ; if ( sum > left_sum ) left_sum = sum ; } sum = 0 ; let right_sum = Number . MIN_VALUE ; for ( let i = m + 1 ; i <= h ; i ++ ) { sum = sum + arr [ i ] ; if ( sum > right_sum ) right_sum = sum ; } return max ( left_sum + right_sum , left_sum , right_sum ) ; } function maxSubArraySum ( arr , l , h ) { if ( l == h ) return arr [ l ] ; let m = parseInt ( ( l + h ) / 2 , 10 ) ; return max ( maxSubArraySum ( arr , l , m ) , maxSubArraySum ( arr , m + 1 , h ) , maxCrossingSum ( arr , l , m , h ) ) ; } let arr = [ 2 , 3 , 4 , 5 , 7 ] ; let n = arr . length ; let max_sum = maxSubArraySum ( arr , 0 , n - 1 ) ; document . write ( " " + max_sum ) ;
Largest Independent Set Problem | DP | A utility function to find max of two integers ; A binary tree node has data , pointer to left child and a pointer to right child ; The function returns size of the largest independent set in a given binary tree ; Calculate size excluding the current node ; Calculate size including the current node ; Return the maximum of two sizes ; Let us construct the tree given in the above diagram
function max ( x , y ) { return ( x > y ) ? x : y ; } class Node { constructor ( data ) { this . data = data ; this . left = this . right = null ; } } function LISS ( root ) { if ( root == null ) return 0 ; let size_excl = LISS ( root . left ) + LISS ( root . right ) ; let size_incl = 1 ; if ( root . left != null ) size_incl += LISS ( root . left . left ) + LISS ( root . left . right ) ; if ( root . right != null ) size_incl += LISS ( root . right . left ) + LISS ( root . right . right ) ; return max ( size_incl , size_excl ) ; } let root = new Node ( 20 ) ; root . left = new Node ( 8 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 12 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 14 ) ; root . right = new Node ( 22 ) ; root . right . right = new Node ( 25 ) ; document . write ( " " + " " + LISS ( root ) ) ;
Program to find amount of water in a given glass | Returns the amount of water in jth glass of ith row ; A row number i has maximum i columns . So input column number must be less than i ; There will be i * ( i + 1 ) / 2 glasses till ith row ( including ith row ) ; Put all water in first glass ; Now let the water flow to the downward glasses till the row number is less than or / equal to i ( given row ) correction : X can be zero for side glasses as they have lower rate to fill ; Fill glasses in a given row . Number of columns in a row is equal to row number ; Get the water from current glass ; Keep the amount less than or equal to capacity in current glass ; Get the remaining amount ; Distribute the remaining amount to the down two glasses ; The index of jth glass in ith row will be i * ( i - 1 ) / 2 + j - 1 ; Driver Code ; Total amount of water
function findWater ( i , j , X ) { if ( j > i ) { document . write ( " " ) ; } var ll = Math . round ( ( i * ( i + 1 ) ) ) ; glass = Array . from ( { length : ll + 2 } , ( _ , i ) => 0.0 ) ; var index = 0 ; glass [ index ] = X ; for ( row = 1 ; row <= i ; ++ row ) { for ( col = 1 ; col <= row ; ++ col , ++ index ) { X = glass [ index ] ; glass [ index ] = ( X >= 1.0 ) ? 1.0 : X ; X = ( X >= 1.0 ) ? ( X - 1 ) : 0.0 ; glass [ index + row ] += X / 2 ; glass [ index + row + 1 ] += X / 2 ; } } return glass [ parseInt ( ( i * ( i - 1 ) / 2 + j - 1 ) ) ] ; } var i = 2 , j = 2 ; var X = 2.0 ; document . write ( " " + " " + findWater ( i , j , X ) ) ;
Program to find amount of water in a given glass | class Triplet which stores curr row curr col and curr rem - water of every glass ; Returns the amount of water in jth glass of ith row ; stores how much amount of water present in every glass ; store Triplet i . e curr - row , curr - col , rem - water ; we take the center of the first row for the location of the first glass ; this while loop runs unless the queue is not empty ; First we remove the Triplet from the queue ; as we know we have to calculate the amount of water in jth glass of ith row . so first we check if we find solutions for the the every glass of i 'th row.  so, if we have calculated the result  then we break the loop  and return our answer ; As we know maximum capacity of every glass is 1 unit . so first we check the capacity of curr glass is full or not . ; calculate the remaining capacity of water for curr glass ; if the remaining capacity of water for curr glass is greater than then the remaining capacity of tatal water then we put all remaining water into curr glass ; if remaining capacity of tatal water is not equal to zero then we add left and right glass of the next row and gives half capacity of tatal water to both the glass ; return the result for jth glass of ith row ; Driver Code ; Total amount of water
class Triplet { constructor ( row , col , rem_water ) { this . row = row ; this . col = col ; this . rem_water = rem_water ; } } function findWater ( i , j , totalWater ) { let dp = new Array ( i + 1 ) ; for ( let k = 0 ; k < dp . length ; k ++ ) { dp [ k ] = new Array ( ( 2 * i ) + 1 ) ; for ( let l = 0 ; l < dp [ k ] . length ; l ++ ) { dp [ k ] [ l ] = 0 ; } } let queue = [ ] ; queue . push ( new Triplet ( 0 , i , totalWater ) ) ; while ( queue . length != 0 ) { let curr = queue . shift ( ) ; if ( curr . row == i ) break ; if ( dp [ curr . row ] [ curr . col ] != 1.0 ) { let rem_water = 1 - dp [ curr . row ] [ curr . col ] ; if ( rem_water > curr . rem_water ) { dp [ curr . row ] [ curr . col ] += curr . rem_water ; curr . rem_water = 0 ; } else { dp [ curr . row ] [ curr . col ] += rem_water ; curr . rem_water -= rem_water ; } } if ( curr . rem_water != 0 ) { queue . push ( new Triplet ( curr . row + 1 , curr . col - 1 , ( curr . rem_water / 2 ) ) ) ; queue . push ( new Triplet ( curr . row + 1 , curr . col + 1 , ( curr . rem_water / 2 ) ) ) ; } } return dp [ i - 1 ] [ 2 * j - 1 ] ; } let i = 2 , j = 2 ; let totalWater = 2 ; document . write ( " " ) ; document . write ( findWater ( i , j , totalWater ) . toFixed ( 6 ) ) ;
Maximum Length Chain of Pairs | DP | Javascript program for above approach ; This function assumes that arr [ ] is sorted in increasing order according the first ( or smaller ) values in pairs . ; Initialize MCL ( max chain length ) values for all indexes ; Compute optimized chain length values in bottom up manner ; Pick maximum of all MCL values ; Driver program to test above function
class Pair { constructor ( a , b ) { this . a = a ; this . b = b ; } } function maxChainLength ( arr , n ) { let i , j , max = 0 ; let mcl = new Array ( n ) ; for ( i = 0 ; i < n ; i ++ ) mcl [ i ] = 1 ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] . a > arr [ j ] . b && mcl [ i ] < mcl [ j ] + 1 ) mcl [ i ] = mcl [ j ] + 1 ; for ( i = 0 ; i < n ; i ++ ) if ( max < mcl [ i ] ) max = mcl [ i ] ; return max ; } let arr = [ new Pair ( 5 , 24 ) , new Pair ( 15 , 25 ) , new Pair ( 27 , 40 ) , new Pair ( 50 , 60 ) ] ; document . write ( " " + maxChainLength ( arr , arr . length ) ) ;
Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity | Function to find the minimum flips required in binary array ; Boolean value to indicate odd or even value of 1 's ; Loop to iterate over the subarrays of size K ; curr_index is used to iterate over all the subarrays ; Loop to iterate over the array at the jump of K to consider every parity ; Condition to check if the subarray is at even position ; The value needs to be same as the first subarray ; The value needs to be opposite of the first subarray ; Update the minimum difference ; Condition to check if the 1 s in the subarray is odd ; Driver Code
function count_flips ( a , n , k ) { var set = false ; var ans = 0 , min_diff = Number . MAX_VALUE ; for ( i = 0 ; i < k ; i ++ ) { var curr_index = i , segment = 0 , count_zero = 0 , count_one = 0 ; while ( curr_index < n ) { if ( segment % 2 == 0 ) { if ( a [ curr_index ] == 1 ) count_zero ++ ; else count_one ++ ; } else { if ( a [ curr_index ] == 0 ) count_zero ++ ; else count_one ++ ; } curr_index = curr_index + k ; segment ++ ; } ans += Math . min ( count_one , count_zero ) ; if ( count_one < count_zero ) set = ! set ; min_diff = Math . min ( min_diff , Math . abs ( count_zero - count_one ) ) ; } if ( set ) return ans ; else return ans + min_diff ; } var n = 6 , k = 3 ; var a = [ 0 , 0 , 1 , 1 , 0 , 0 ] ; document . write ( count_flips ( a , n , k ) ) ;
Program to build a DFA that checks if a string ends with "01" or "10" | End position is checked using the string length value . q0 is the starting state . q1 and q2 are intermediate states . q3 and q4 are final states . ; state transitions 0 takes to q1 , 1 takes to q3 ; state transitions 0 takes to q4 , 1 takes to q2 ; state transitions 0 takes to q4 , 1 takes to q2 ; state transitions 0 takes to q1 , 1 takes to q3 ; state transitions 0 takes to q1 , 1 takes to q2 ; Driver Code ; all state transitions are printed . if string is accpetable , YES is printed . else NO is printed
function q1 ( s , i ) { document . write ( " " ) ; if ( i === s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] === " " ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } function q2 ( s , i ) { document . write ( " " ) ; if ( i === s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] === " " ) q4 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } function q3 ( s , i ) { document . write ( " " ) ; if ( i === s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] === " " ) q4 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } function q4 ( s , i ) { document . write ( " " ) ; if ( i === s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] === " " ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } function q0 ( s , i ) { document . write ( " " ) ; if ( i === s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] === " " ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } var s = " " ; document . write ( " " ) ; q0 ( s , 0 ) ;
Check whether two strings contain same characters in same order |  ; If length of the b = 0 then we return true ; If length of a = 0 that means b is not present in a so we return false ; Driver code
function checkSequence ( a , b ) { if ( b . length == 0 ) return true ; if ( a . length == 0 ) return false ; if ( a [ 0 ] == b [ 0 ] ) return checkSequence ( a . substring ( 1 ) , b . substring ( 1 ) ) ; else return checkSequence ( a . substring ( 1 ) , b ) ; } let s1 = " " , s2 = " " ; if ( checkSequence ( s1 , s2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
String matching with * ( that matches with any ) in any of the two strings | Function to check if the two strings can be matched or not ; if the string don 't have *  then character at that position  must be same. ; Driver code
function doMatch ( A , B ) { for ( i = 0 ; i < A . length ; i ++ ) { if ( A . charAt ( i ) != ' ' && B . charAt ( i ) != ' ' ) { if ( A . charAt ( i ) != B . charAt ( i ) ) return 0 ; } } return 1 ; } var A = " " ; var B = " " ; document . write ( doMatch ( A , B ) ) ;