codeparrot/xlcost-text-to-code huggingface dataset:
Maximize product of a strictly increasing or decreasing subarray | Function to find the maximum product of subarray in the array , arr [ ] ; Maximum positive product ending at the i - th index ; Minimum negative product ending at the current index ; Maximum product up to i - th index ; Check if an array element is positive or not ; Traverse the array ; If current element is positive ; Update max_ending_here ; Update min_ending_here ; Update flag ; If current element is 0 , reset the start index of subarray ; Update max_ending_here ; Update min_ending_here ; If current element is negative ; Stores max_ending_here ; Update max_ending_here ; Update min_ending_here ; Update max_so_far , if needed ; If no array elements is positive and max_so_far is 0 ; Function to find the maximum product of either increasing subarray or the decreasing subarray ; Stores start index of either increasing subarray or the decreasing subarray ; Initially assume maxProd to be 1 ; Traverse the array ; Store the longest either increasing subarray or the decreasing subarray whose start index is i ; Check for increasing subarray ; Insert elements of increasing subarray ; Check for decreasing subarray ; Insert elements of decreasing subarray ; Stores maximum subarray product of current increasing or decreasing subarray ; Update maxProd ; Update i ; Finally print maxProd ; Driver Code
function maxSubarrayProduct ( arr , n ) { var max_ending_here = 1 ; var min_ending_here = 1 ; var max_so_far = 0 ; var flag = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) { max_ending_here = max_ending_here * arr [ i ] ; min_ending_here = Math . min ( min_ending_here * arr [ i ] , 1 ) ; flag = 1 ; } else if ( arr [ i ] == 0 ) { max_ending_here = 1 ; min_ending_here = 1 ; } else { var temp = max_ending_here ; max_ending_here = Math . max ( min_ending_here * arr [ i ] , 1 ) ; min_ending_here = temp * arr [ i ] ; } if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; } if ( flag == 0 && max_so_far == 0 ) return 0 ; return max_so_far ; } function findMaxProduct ( a , n ) { var i = 0 ; var maxProd = - 1e9 ; while ( i < n ) { var v = [ ] ; v . push ( a [ i ] ) ; if ( i < n - 1 && a [ i ] < a [ i + 1 ] ) { while ( i < n - 1 && a [ i ] < a [ i + 1 ] ) { v . push ( a [ i + 1 ] ) ; i += 1 ; } } else if ( i < n - 1 && a [ i ] > a [ i + 1 ] ) { while ( i < n - 1 && a [ i ] > a [ i + 1 ] ) { v . push ( a [ i + 1 ] ) ; i += 1 ; } } var prod = maxSubarrayProduct ( v , v . length ) ; maxProd = Math . max ( maxProd , prod ) ; i ++ ; } return maxProd ; } var arr = [ 1 , 2 , 10 , 8 , 1 , 100 , 101 ] ; var N = arr . length ; document . write ( findMaxProduct ( arr , N ) ) ;
Number of connected components of a graph ( using Disjoint Set Union ) | Stores the parent of each vertex ; Function to find the topmost parent of vertex a ; If current vertex is the topmost vertex ; Otherwise , set topmost vertex of its parent as its topmost vertex ; Function to connect the component having vertex a with the component having vertex b ; Connect edges ; Function to find unique top most parents ; Traverse all vertices ; Insert all topmost vertices obtained ; Print count of connected components ; Function to print answer ; Setting parent to itself ; Traverse all edges ; Print answer ; Given N ; Given edges ; Function call
var parent = Array ( 1000000 ) ; function root ( a ) { if ( a == parent [ a ] ) { return a ; } return parent [ a ] = root ( parent [ a ] ) ; } function connect ( a , b ) { a = root ( a ) ; b = root ( b ) ; if ( a != b ) { parent [ b ] = a ; } } function connectedComponents ( n ) { var s = new Set ( ) ; for ( var i = 0 ; i < n ; i ++ ) { s . add ( parent [ i ] ) ; } document . write ( s . size + " " ) ; } function printAnswer ( N , edges ) { for ( var i = 0 ; i <= N ; i ++ ) { parent [ i ] = i ; } for ( var i = 0 ; i < edges . length ; i ++ ) { connect ( edges [ i ] [ 0 ] , edges [ i ] [ 1 ] ) ; } connectedComponents ( N ) ; } var N = 8 ; var edges = [ [ 1 , 0 ] , [ 0 , 2 ] , [ 5 , 3 ] , [ 3 , 4 ] , [ 6 , 7 ] ] ; printAnswer ( N , edges ) ;
Remove all zero | Function to remove the rows or columns from the matrix which contains all 0 s elements ; Stores count of rows ; col [ i ] : Stores count of 0 s in current column ; row [ i ] : Stores count of 0 s in current row ; Traverse the matrix ; Stores count of 0 s in current row ; Update col [ j ] ; Update count ; Update row [ i ] ; Traverse the matrix ; If all elements of current row is 0 ; If all elements of current column is 0 ; Driver Code ; Function Call
function removeZeroRowCol ( arr ) { let n = arr . length ; let col = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; let row = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; ++ i ) { let count = 0 ; for ( let j = 0 ; j < n ; ++ j ) { if ( arr [ i ] [ j ] == 1 ) col [ j ] += 1 ; else col [ j ] += 0 ; if ( arr [ i ] [ j ] == 1 ) count += 1 ; else count += 0 ; } row [ i ] = count ; } for ( let i = 0 ; i < n ; ++ i ) { if ( row [ i ] == 0 ) { continue ; } for ( let j = 0 ; j < n ; ++ j ) { if ( col [ j ] != 0 ) document . write ( arr [ i ] [ j ] ) ; } document . write ( " " ) ; } } let arr = [ [ 1 , 1 , 0 , 1 ] , [ 0 , 0 , 0 , 0 ] , [ 1 , 1 , 0 , 1 ] , [ 0 , 1 , 0 , 1 ] ] ; removeZeroRowCol ( arr ) ;
Find a pair of overlapping intervals from a given Set | Function to find a pair ( i , j ) such that i - th interval lies within the j - th interval ; Store interval and index of the interval in the form of { { l , r } , index } ; Traverse the array , arr [ ] [ ] ; Stores l - value of the interval ; Stores r - value of the interval ; Push current interval and index into tup ; Sort the vector based on l - value of the intervals ; Stores r - value of current interval ; Stores index of current interval ; Traverse the vector , tup [ ] ; Stores l - value of previous interval ; Stores l - value of current interval ; If Q and R equal ; If Y value of immediate previous interval is less than Y value of current interval ; Print the index of interval ; Stores r - value of current interval ; T is less than or equal to curr ; Update curr ; Update currPos ; If such intervals found ; Driver Code Given l - value of segments ; Given r - value of segments ; Given size ; Function Call
function findOverlapSegement ( N , a , b ) { var tup = [ ] ; for ( var i = 0 ; i < N ; i ++ ) { var x , y ; x = a [ i ] ; y = b [ i ] ; tup . push ( [ [ x , y ] , i ] ) ; } tup . sort ( ( a , b ) => { if ( a [ 0 ] [ 0 ] == b [ 0 ] [ 0 ] ) { return a [ 0 ] [ 1 ] - b [ 0 ] [ 1 ] ; } var tmp = ( a [ 0 ] [ 0 ] - b [ 0 ] [ 0 ] ) ; console . log ( tmp ) ; return ( a [ 0 ] [ 0 ] - b [ 0 ] [ 0 ] ) } ) ; var curr = tup [ 0 ] [ 0 ] [ 1 ] ; var currPos = tup [ 0 ] [ 1 ] ; for ( var i = 1 ; i < N ; i ++ ) { var Q = tup [ i - 1 ] [ 0 ] [ 0 ] ; var R = tup [ i ] [ 0 ] [ 0 ] ; if ( Q == R ) { if ( tup [ i - 1 ] [ 0 ] [ 1 ] < tup [ i ] [ 0 ] [ 1 ] ) { document . write ( tup [ i - 1 ] [ 1 ] + " " + tup [ i ] [ 1 ] ) ; return ; } else { document . write ( tup [ i ] [ 1 ] + " " + tup [ i - 1 ] [ 1 ] ) ; return ; } } var T = tup [ i ] [ 0 ] [ 1 ] ; if ( T <= curr ) { document . write ( tup [ i ] [ 1 ] + " " + currPos ) ; return ; } else { curr = T ; currPos = tup [ i ] [ 1 ] ; } } document . write ( " " ) ; } let a = [ 1 , 2 , 3 , 2 , 2 ] ; let b = [ 5 , 10 , 10 , 2 , 15 ] ; let N = a . length ; findOverlapSegement ( N , a , b ) ;
Replace each node of a Binary Tree with the sum of all the nodes present in its diagonal | Structure of a tree node ; Store sum of nodes at each diagonal of the tree ; Function to replace each node with the sum of nodes at the same diagonal ; IF root is NULL ; Replace nodes ; Traverse the left subtree ; Traverse the right subtree ; Function to find the sum of all the nodes at each diagonal of the tree ; If root is not NULL ; If update sum of nodes at current diagonal ; Traverse the left subtree ; Traverse the right subtree ; Function to print the nodes of the tree using level order traversal ; Stores node at each level of the tree ; Stores count of nodes at current level ; Stores front element of the queue ; Insert left subtree ; Insert right subtree ; Update length ; Build tree ; Find sum of nodes at each diagonal of the tree ; Replace nodes with the sum of nodes at the same diagonal ; Print tree
class TreeNode { constructor ( x ) { this . left = null ; this . right = null ; this . val = x ; } } let diagMap = new Map ( ) ; function replaceDiag ( root , d ) { if ( root == null ) return ; if ( diagMap . has ( d ) ) root . val = diagMap . get ( d ) ; replaceDiag ( root . left , d + 1 ) ; replaceDiag ( root . right , d ) ; } function getDiagSum ( root , d ) { if ( root == null ) return ; if ( diagMap . has ( d ) ) diagMap . set ( d , diagMap . get ( d ) + root . val ) ; else diagMap . set ( d , root . val ) ; getDiagSum ( root . left , d + 1 ) ; getDiagSum ( root . right , d ) ; } function levelOrder ( root ) { let q = [ ] ; q . push ( root ) ; while ( true ) { let length = q . length ; if ( ! length ) break ; while ( length ) { let temp = q [ 0 ] ; q . shift ( ) ; document . write ( temp . val + " " ) ; if ( temp . left != null ) q . push ( temp . left ) ; if ( temp . right != null ) q . push ( temp . right ) ; length -= 1 ; } } } let root = new TreeNode ( 5 ) ; root . left = new TreeNode ( 6 ) ; root . right = new TreeNode ( 3 ) ; root . left . left = new TreeNode ( 4 ) ; root . left . right = new TreeNode ( 9 ) ; root . right . right = new TreeNode ( 2 ) ; getDiagSum ( root , 0 ) ; replaceDiag ( root , 0 ) ; levelOrder ( root ) ;
Count maximum concatenation of pairs from given array that are divisible by 3 | Function to count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Stores count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Check if an element present in any pair or not ; Generate all possible pairs ; If the element already present in a pair ; If the element already present in a pair ; If concatenation of elements is divisible by 3 ; Update ans ; Mark i is True ; Mark j is True ; Driver Code ; To display the result
function countDivBy3InArray ( arr ) { let ans = 0 ; let taken = new Array ( arr . length ) ; taken . fill ( false ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( taken [ i ] == true ) { continue ; } for ( let j = i + 1 ; j < arr . length ; j ++ ) { if ( taken [ j ] == true ) { continue ; } if ( parseInt ( ( arr [ i ] ) . toString ( ) + ( arr [ j ] ) . toString ( ) , 10 ) % 3 == 0 || parseInt ( ( arr [ j ] ) . toString ( ) + ( arr [ i ] ) . toString ( ) , 10 ) % 3 == 0 ) { ans += 1 ; taken [ i ] = true ; taken [ j ] = true ; } } } return ans ; } let arr = [ 5 , 3 , 2 , 8 , 7 ] ; document . write ( countDivBy3InArray ( arr ) ) ;
Longest increasing sequence possible by the boundary elements of an Array | Function to find longest strictly increasing sequence using boundary elements ; Maintains rightmost element in the sequence ; Pointer to start of array ; Pointer to end of array ; Stores the required sequence ; Traverse the array ; If arr [ i ] > arr [ j ] ; If arr [ j ] is greater than rightmost element of the sequence ; Push arr [ j ] into the sequence ; Update rightmost element ; Push arr [ i ] into the sequence ; Update rightmost element ; If arr [ i ] < arr [ j ] ; If arr [ i ] > rightmost element ; Push arr [ i ] into the sequence ; Update rightmost element ; If arr [ j ] > rightmost element ; Push arr [ j ] into the sequence ; Update rightmost element ; If arr [ i ] is equal to arr [ j ] ; If i and j are at the same element ; If arr [ i ] > rightmostelement ; Push arr [ j ] into the sequence ; Update rightmost element ; Traverse array from left to right ; Stores the increasing sequence from the left end ; Traverse array from left to right ; Push arr [ k ] to max_left vector ; Traverse the array from right to left ; Stores the increasing sequence from the right end ; Traverse array from right to left ; Push arr [ k ] to max_right vector ; If size of max_left is greater than max_right ; Push max_left elements to the original sequence ; Otherwise ; Push max_right elements to the original sequence ; Print the sequence ; Driver code ; Print the longest increasing sequence using boundary elements
function findMaxLengthSequence ( N , arr ) { let rightmost_element = - 1 ; let i = 0 ; let j = N - 1 ; let sequence = [ ] ; while ( i <= j ) { if ( arr [ i ] > arr [ j ] ) { if ( arr [ j ] > rightmost_element ) { sequence . push ( arr [ j ] ) ; rightmost_element = arr [ j ] ; j -- ; } else if ( arr [ i ] > rightmost_element ) { sequence . push ( arr [ i ] ) ; rightmost_element = arr [ i ] ; i ++ ; } else break ; } else if ( arr [ i ] < arr [ j ] ) { if ( arr [ i ] > rightmost_element ) { sequence . push ( arr [ i ] ) ; rightmost_element = arr [ i ] ; i ++ ; } else if ( arr [ j ] > rightmost_element ) { sequence . push ( arr [ j ] ) ; rightmost_element = arr [ j ] ; j -- ; } else break ; } else if ( arr [ i ] == arr [ j ] ) { if ( i == j ) { if ( arr [ i ] > rightmost_element ) { sequence . push ( arr [ i ] ) ; rightmost_element = arr [ i ] ; i ++ ; } break ; } else { sequence . push ( arr [ i ] ) ; let k = i + 1 ; let max_left = [ ] ; while ( k < j && arr [ k ] > arr [ k - 1 ] ) { max_left . push ( arr [ k ] ) ; k ++ ; } let l = j - 1 ; let max_right = [ ] ; while ( l > i && arr [ l ] > arr [ l + 1 ] ) { max_right . push ( arr [ l ] ) ; l -- ; } if ( max_left . length > max_right . length ) for ( let element = 0 ; element < max_left . length ; element ++ ) sequence . push ( max_left [ element ] ) ; else for ( let element = 0 ; element < max_right . length ; element ++ ) sequence . push ( max_right [ element ] ) ; break ; } } } for ( let element = 0 ; element < sequence . length ; element ++ ) document . write ( sequence [ element ] + " " ) ; } let N = 4 ; let arr = [ 1 , 3 , 2 , 1 ] ; findMaxLengthSequence ( N , arr ) ;
Maximum average of subtree values in a given Binary Tree | Structure of the Tree node ; Stores the result ; Function for finding maximum subtree average ; Checks if current node is not null and doesn 't have any children ; Stores sum of its subtree in index 0 and count number of nodes in index 1 ; Traverse all children of the current node ; Recursively calculate max average of subtrees among its children ; Increment sum by sum of its child 's subtree ; Increment number of nodes by its child 's node ; Increment sum by current node 's value ; Increment number of nodes by one ; Take maximum of ans and current node 's average  ; Finally return pair of { sum , count } ; Given tree ; Function call ; Print answer
class TreeNode { constructor ( v ) { this . val = v ; this . children = [ ] ; } } let ans = 0.0 ; function MaxAverage ( root ) { if ( root . children != null && root . children . length == 0 ) { ans = Math . max ( ans , root . val ) ; return [ root . val , 1 ] ; } let childResult = new Array ( 2 ) ; for ( let i = 0 ; i < childResult . length ; i ++ ) { childResult [ i ] = 0 ; } for ( let child = 0 ; child < root . children . length ; child ++ ) { let childTotal = MaxAverage ( root . children [ child ] ) ; childResult [ 0 ] = childResult [ 0 ] + childTotal [ 0 ] ; childResult [ 1 ] = childResult [ 1 ] + childTotal [ 1 ] ; } let sum = childResult [ 0 ] + root . val ; let count = childResult [ 1 ] + 1 ; ans = Math . max ( ans , sum / count ) ; return [ sum , count ] ; } let root = new TreeNode ( 20 ) ; let left = new TreeNode ( 12 ) ; let right = new TreeNode ( 18 ) ; root . children . push ( left ) ; root . children . push ( right ) ; left . children . push ( new TreeNode ( 11 ) ) ; left . children . push ( new TreeNode ( 3 ) ) ; right . children . push ( new TreeNode ( 15 ) ) ; right . children . push ( new TreeNode ( 8 ) ) ; MaxAverage ( root ) ; document . write ( ans . toFixed ( 1 ) ) ;
Check if a Binary String can be converted to another by reversing substrings consisting of even number of 1 s | Function to check if string A can be transformed to string B by reversing substrings of A having even number of 1 s ; Store the size of string A ; Store the size of string B ; Store the count of 1 s in A and B ; Stores cntA for string A and cntB for string B ; Traverse the string A ; If current character is 1 ; Increment 1 s count ; Otherwise , update odd1A or even1A depending whether count1A is odd or even ; Traverse the string B ; If current character is 1 ; Increment 1 s count ; Otherwise , update odd1B or even1B depending whether count1B is odd or even ; If the condition is satisfied ; If true , print Yes ; Otherwise , print No ; Driver Code ; Function Call
function canTransformStrings ( A , B ) { let n1 = A . length ; let n2 = B . length ; let count1A = 0 , count1B = 0 ; let odd1A = 0 , odd1B = 0 ; let even1A = 0 , even1B = 0 ; for ( let i = 0 ; i < n1 ; i ++ ) { if ( A [ i ] == ' ' ) count1A ++ ; else { if ( ( count1A & 1 ) == 1 ) odd1A ++ ; else even1A ++ ; } } for ( let i = 0 ; i < n2 ; i ++ ) { if ( B [ i ] == ' ' ) count1B ++ ; else { if ( ( count1B & 1 ) == 1 ) odd1B ++ ; else even1B ++ ; } } if ( count1A == count1B && odd1A == odd1B && even1A == even1B ) { document . write ( " " ) ; } else document . write ( " " ) ; } let A = " " , B = " " ; canTransformStrings ( A , B ) ;
Smallest element present in every subarray of all possible lengths | Function to print the common elements for all subarray lengths ; Function to find and store the minimum element present in all subarrays of all lengths from 1 to n ; Skip lengths for which answer [ i ] is - 1 ; Initialize minimum as the first element where answer [ i ] is not - 1 ; Updating the answer array ; If answer [ i ] is - 1 , then minimum can be substituted in that place ; Find minimum answer ; Function to find the minimum number corresponding to every subarray of length K , for every K from 1 to N ; Stores the minimum common elements for all subarray lengths ; Find for every element , the minimum length such that the number is present in every subsequence of that particular length or more ; To store first occurence and gaps between occurences ; To cover the distance between last occurence and the end of the array ; To find the distance between any two occurences ; Update and store the answer ; Print the required answer ; Function to find the smallest element common in all subarrays for every possible subarray lengths ; Initializing indices array ; Store the numbers present in the array ; Push the index in the indices [ A [ i ] ] and also store the numbers in set to get the numbers present in input array ; Function call to calculate length of subarray for which a number is present in every subarray of that length ; Given array ; Size of array ; Function Call
function printAnswer ( answer , N ) { for ( let i = 1 ; i <= N ; i ++ ) { document . write ( answer [ i ] + " " ) ; } } function updateAnswerArray ( answer , N ) { let i = 0 ; while ( answer [ i ] == - 1 ) i ++ ; let minimum = answer [ i ] ; while ( i <= N ) { if ( answer [ i ] == - 1 ) answer [ i ] = minimum ; else answer [ i ] = Math . min ( minimum , answer [ i ] ) ; minimum = Math . min ( minimum , answer [ i ] ) ; i ++ ; } } function lengthOfSubarray ( indices , st , N ) { let answer = new Array ( N + 1 ) . fill ( - 1 ) ; for ( let itr of st ) { let start = - 1 ; let gap = - 1 ; indices [ itr ] . push ( N ) ; for ( let i = 0 ; i < indices [ itr ] . length ; i ++ ) { gap = Math . max ( gap , indices [ itr ] [ i ] - start ) ; start = indices [ itr ] [ i ] ; } if ( answer [ gap ] == - 1 ) answer [ gap ] = itr ; } updateAnswerArray ( answer , N ) ; printAnswer ( answer , N ) ; } function smallestPresentNumber ( arr , N ) { let indices = new Array ( N + 1 ) . fill ( 0 ) . map ( ( ) => [ ] ) ; let elements = new Set ( ) ; for ( let i = 0 ; i < N ; i ++ ) { indices [ arr [ i ] ] . push ( i ) ; elements . add ( arr [ i ] ) ; } lengthOfSubarray ( indices , elements , N ) ; } let arr = [ 2 , 3 , 5 , 3 , 2 , 3 , 1 , 3 , 2 , 7 ] ; let N = arr . length ; smallestPresentNumber ( arr , N ) ;
Minimize remaining array element by removing pairs and replacing them by their absolute difference | javascript program of the above approach ; Function to find the smallest element left in the array by the given operations ; Base Case ; If this subproblem has occurred previously ; Including i - th array element into the first subset ; If i - th array element is not selected ; Update dp [ i ] [ sum ] ; Utility function to find smallest element left in the array by the given operations ; Stores sum of the array elements ; Traverse the array ; Update total ; Stores overlapping subproblems ; Driver Code
let M = 6 ; let N = 7 ; function smallestLeft ( arr , total , sum , i , dp ) { if ( i == 0 ) { return Math . abs ( total - 2 * sum ) ; } if ( dp [ i ] [ sum ] != - 1 ) return dp [ i ] [ sum ] ; let X = smallestLeft ( arr , total , sum + arr [ i - 1 ] , i - 1 , dp ) ; let Y = smallestLeft ( arr , total , sum , i - 1 , dp ) ; return dp [ i ] [ sum ] = Math . min ( X , Y ) ; } function UtilSmallestElement ( arr , N ) { let total = 0 ; for ( let i = 0 ; i < N ; i ++ ) { total += arr [ i ] ; } let dp = new Array ( N + 1 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < dp . length ; i ++ ) { for ( var j = 0 ; j < dp . length ; j ++ ) { dp [ i ] [ j ] = 1 ; } } document . write ( smallestLeft ( arr , total , 0 , N , dp ) ) ; } let arr = [ 2 , 7 , 4 , 1 , 8 , 1 ] ; let n = arr . length ; UtilSmallestElement ( arr , n ) ;
Minimize remaining array element by removing pairs and replacing them by their absolute difference | Function to find minimize the remaining array element by removing pairs and replacing them by their absolute difference ; Stores sum of array elements ; Traverse the array ; Update totalSum ; Stores half of totalSum ; dp [ i ] : True if sum i can be obtained as a subset sum ; Base case ; Stores closest sum that can be obtained as a subset sum ; Traverse the array ; Iterate over all possible value of sum ; Update dp [ j ] ; If sum i can be obtained from array elements ; Update reach ; Driver Code
function SmallestElementLeft ( arr , N ) { var totalSum = 0 ; for ( i = 0 ; i < N ; i ++ ) { totalSum += arr [ i ] ; } var req = totalSum / 2 ; var dp = Array ( req + 1 ) . fill ( false ) ; dp [ 0 ] = true ; var reach = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = req ; j - arr [ i ] >= 0 ; j -- ) { dp [ j ] = dp [ j ] || dp [ j - arr [ i ] ] ; if ( dp [ j ] ) { reach = Math . max ( reach , j ) ; } } } return totalSum - ( 2 * reach ) ; } var arr = [ 2 , 2 , 2 ] ; var N = arr . length ; document . write ( SmallestElementLeft ( arr , N ) ) ;
Replace even | Function to count the minimum number of substrings of str1 such that replacing even - indexed characters of those substrings converts the string str1 to str2 ; Stores length of str1 ; Stores minimum count of operations to convert str1 to str2 ; Traverse both the given string ; If current character in both the strings are equal ; Stores current index of the string ; If current character in both the strings are not equal ; Replace str1 [ ptr ] by str2 [ ptr ] ; Update ptr ; Update cntOp ; Driver Code
function min_Operations ( str1 , str2 ) { var N = str1 . length ; var cntOp = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( str1 . charCodeAt ( i ) == str2 . charCodeAt ( i ) ) { continue ; } var ptr = i ; while ( ptr < N && str1 [ ptr ] != str2 [ ptr ] ) { str1 = str1 . substring ( 0 , ptr ) + str2 [ ptr ] + str1 . substring ( ptr + 1 ) ; ptr += 2 ; } cntOp ++ ; } return cntOp ; } str1 = " " ; str2 = " " ; document . write ( min_Operations ( str1 , str2 ) ) ;