codeparrot/xlcost-text-to-code huggingface dataset:
Minimum swaps required to make a binary string alternating | function to count minimum swaps required to make binary String alternating ; stores total number of ones ; stores total number of zeroes ; checking impossible condition ; odd length String ; number of even positions ; stores number of zeroes and ones at even positions ; even length String ; stores number of ones at odd and even position respectively ; Driver code
function countMinSwaps ( s ) { var N = s . length ; var one = 0 ; var zero = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == ' ' ) one ++ ; else zero ++ ; } if ( one > zero + 1 zero > one + 1 ) return - 1 ; if ( N % 2 == 1 ) { var num = ( N + 1 ) / 2 ; var one_even = 0 , zero_even = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { if ( s . charAt ( i ) == ' ' ) one_even ++ ; else zero_even ++ ; } } if ( one > zero ) return num - one_even ; else return num - zero_even ; } else { var one_odd = 0 , one_even = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == ' ' ) { if ( i % 2 == 1 ) one_odd ++ ; else one_even ++ ; } } return Math . min ( N / 2 - one_odd , N / 2 - one_even ) ; } } var s = " " ; document . write ( countMinSwaps ( s ) ) ;
Check if it is possible to return to the starting position after moving in the given directions | driver code ; let n = 0 ; Count of North let s = 0 ; Count of South let e = 0 ; Count of East let w = 0 ; Count of West
let st = " " ; let len = st . length ; for ( let i = 0 ; i < len ; i ++ ) { if ( st [ i ] == ' ' ) n += 1 ; if ( st [ i ] == ' ' ) s += 1 ; if ( st [ i ] == ' ' ) w += 1 ; if ( st [ i ] == ' ' ) e += 1 ; } if ( n == s && w == e ) document . write ( " " ) ; else document . write ( " " ) ;
Minimum cost to make array size 1 by removing larger of pairs | function to calculate the minimum cost ; find the minimum using for loop ; Minimum cost is n - 1 multiplied with minimum element . ; Driver code
function cost ( a , n ) { let min = a [ 0 ] ; for ( let i = 1 ; i < a . length ; i ++ ) { if ( a [ i ] < min ) min = a [ i ] ; } return ( n - 1 ) * min ; } let a = [ 4 , 3 , 2 ] ; let n = a . length ; document . write ( cost ( a , n ) ) ;
Minimum cost for acquiring all coins with k extra coins allowed with every coin | Function to calculate min cost ; Sort the coins value ; Calculate no . of coins needed ; Calculate sum of all selected coins ; Driver Code
function minCost ( coin , n , k ) { coin . sort ( function ( a , b ) { return a - b } ) var coins_needed = Math . ceil ( n / ( k + 1 ) ) ; var ans = 0 ; for ( var i = 0 ; i <= coins_needed - 1 ; i ++ ) ans += coin [ i ] ; return ans ; } var coin = [ 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 ] var n = coin . length ; var k = 3 ; document . write ( minCost ( coin , n , k ) ) ;
Greedy Algorithm to find Minimum number of Coins | All denominations of Indian Currency ; Initialize result ; Traverse through all denomination ; Find denominations ; Print result ; Driver code
let deno = [ 1 , 2 , 5 , 10 , 20 , 50 , 100 , 500 , 1000 ] ; let n = deno . length ; function findMin ( V ) { let ans = [ ] ; for ( let i = n - 1 ; i >= 0 ; i -- ) { while ( V >= deno [ i ] ) { V -= deno [ i ] ; ans . push ( deno [ i ] ) ; } } for ( let i = 0 ; i < ans . length ; i ++ ) { document . write ( " " + ans [ i ] ) ; } } n = 93 ; document . write ( " " + " " + n + " " ) ; findMin ( n ) ;
Minimize count of array elements to be removed such that at least K elements are equal to their index values | Function to minimize the removals of array elements such that atleast K elements are equal to their indices ; Store the array as 1 - based indexing Copy of first array ; Make a dp - table of ( N * N ) size ; Delete the current element ; Take the current element ; Check for the minimum removals ; Driver Code
function MinimumRemovals ( a , N , K ) { let b = new Array ( N + 1 ) ; for ( let i = 0 ; i < N ; i ++ ) { b [ i + 1 ] = a [ i ] ; } let dp = new Array ( N + 1 ) . fill ( 0 ) . map ( ( ) => new Array ( N + 1 ) . fill ( 0 ) ) ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j <= i ; j ++ ) { dp [ i + 1 ] [ j ] = Math . max ( dp [ i + 1 ] [ j ] , dp [ i ] [ j ] ) ; dp [ i + 1 ] [ j + 1 ] = Math . max ( dp [ i + 1 ] [ j + 1 ] , dp [ i ] [ j ] + ( ( b [ i + 1 ] == j + 1 ) ? 1 : 0 ) ) ; } } for ( let j = N ; j >= 0 ; j -- ) { if ( dp [ N ] [ j ] >= K ) { return ( N - j ) ; } } return - 1 ; } let arr = [ 5 , 1 , 3 , 2 , 3 ] ; let K = 2 ; let N = arr . length document . write ( MinimumRemovals ( arr , N , K ) ) ;
Count of Arrays of size N having absolute difference between adjacent elements at most 1 | Function to find the count of possible arrays such that the absolute difference between any adjacent elements is atmost 1 ; Stores the dp states where dp [ i ] [ j ] represents count of arrays of length i + 1 having their last element as j ; Case where 1 st array element is missing ; All integers in range [ 1 , M ] are reachable ; Only reachable integer is arr [ 0 ] ; Iterate through all values of i ; If arr [ i ] is not missing ; Only valid value of j is arr [ i ] ; If arr [ i ] is missing ; Iterate through all possible values of j in range [ 1 , M ] ; Stores the count of valid arrays ; Calculate the total count of valid arrays ; Return answer ; Driver Code ; Function Call
function countArray ( arr , N , M ) { let dp = new Array ( N ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( M + 2 ) . fill ( 0 ) ; } if ( arr [ 0 ] == - 1 ) { for ( let j = 1 ; j <= M ; j ++ ) { dp [ 0 ] [ j ] = 1 ; } } else { dp [ 0 ] [ arr [ 0 ] ] = 1 ; } for ( let i = 1 ; i < N ; i ++ ) { if ( arr [ i ] != - 1 ) { let j = arr [ i ] ; dp [ i ] [ j ] += dp [ i - 1 ] [ j - 1 ] + dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j + 1 ] ; } if ( arr [ i ] == - 1 ) { for ( let j = 1 ; j <= M ; j ++ ) { dp [ i ] [ j ] += dp [ i - 1 ] [ j - 1 ] + dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j + 1 ] ; } } } let arrCount = 0 ; for ( let j = 1 ; j <= M ; j ++ ) { arrCount += dp [ N - 1 ] [ j ] ; } return arrCount ; } let arr = [ 4 , - 1 , 2 , 1 , - 1 , - 1 ] ; let N = arr . length ; let M = 10 ; document . write ( countArray ( arr , N , M ) ) ;
Maximize sum of product of neighbouring elements of the element removed from Array | Stores the dp state where dp [ i ] [ j ] represents the maximum possible score in the subarray from index i to j ; Function to calculate maximum possible score using the given operations ; Iterate through all possible lengths of the subarray ; Iterate through all the possible starting indices i having length len ; Stores the rightmost index of the current subarray ; Initial dp [ i ] [ j ] will be 0. ; Iterate through all possible values of k in range [ i + 1 , j - 1 ] ; Return the answer ; Driver Code ; Function Call ; Function Call
let dp = Array ( 101 ) . fill ( ) . map ( ( ) => Array ( 101 ) ) ; function maxMergingScore ( A , N ) { for ( let len = 1 ; len < N ; ++ len ) { for ( let i = 0 ; i + len < N ; ++ i ) { let j = i + len ; dp [ i ] [ j ] = 0 ; for ( let k = i + 1 ; k < j ; ++ k ) { dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i ] [ k ] + dp [ k ] [ j ] + A [ i ] * A [ j ] ) ; } } } return dp [ 0 ] [ N - 1 ] ; } let N = 4 ; let A = [ 1 , 2 , 3 , 4 ] ; document . write ( maxMergingScore ( A , N ) + " " ) ; N = 2 ; let B = [ 1 , 55 ] ; document . write ( maxMergingScore ( B , N ) + " " ) ;
Longest subarray with all even or all odd elements | Function to calculate longest substring with odd or even elements ; Initializing dp ; Initializing dp with 1 ; ans will store the final answer ; Traversing the array from index 1 to N - 1 ; Checking both current and previous element is even or odd ; Updating dp with ( previous dp value ) + 1 ; Storing max element so far to ans ; Returning the final answer ; Input ; Function call
function LongestOddEvenSubarray ( A , N ) { let dp ; dp = 1 ; let ans = 1 ; for ( let i = 1 ; i < N ; i ++ ) { if ( ( A [ i ] % 2 == 0 && A [ i - 1 ] % 2 == 0 ) || ( A [ i ] % 2 != 0 && A [ i - 1 ] % 2 != 0 ) ) { dp = dp + 1 ; ans = Math . max ( ans , dp ) ; } else dp = 1 ; } return ans ; } let A = [ 2 , 5 , 7 , 2 , 4 , 6 , 8 , 3 ] ; let N = A . length ; document . write ( LongestOddEvenSubarray ( A , N ) ) ;
Count of N | javascript program for the above approach ; Function to find number of ' N ' digit numbers such that the element is mean of sum of its adjacent digits ; If digit = n + 1 , a valid n - digit number has been formed ; If the state has already been computed ; If current position is 1 , then any digit from [ 1 - 9 ] can be placed . If n = 1 , 0 can be also placed . ; If current position is 2 , then any digit from [ 1 - 9 ] can be placed . ; previous digit selected is the mean . ; mean = ( current + prev2 ) / 2 current = ( 2 * mean ) - prev2 ; Check if current and current + 1 can be valid placements ; return answer ; Given Input ; Function call
var dp = Array ( 100 ) . fill ( ) . map ( ( ) => Array ( 10 ) . fill ( ) . map ( ( ) => Array ( 10 ) . fill ( - 1 ) ) ) ; function countOfNumbers ( digit , prev1 , prev2 , n ) { if ( digit == n + 1 ) { return 1 ; } var val = dp [ digit ] [ prev1 ] [ prev2 ] ; if ( val != - 1 ) { return val ; } val = 0 ; if ( digit == 1 ) { for ( var i = ( n == 1 ? 0 : 1 ) ; i <= 9 ; ++ i ) { val += countOfNumbers ( digit + 1 , i , prev1 , n ) ; } } else if ( digit == 2 ) { for ( var i = 0 ; i <= 9 ; ++ i ) { val += countOfNumbers ( digit + 1 , i , prev1 , n ) ; } } else { var mean = prev1 ; var current = ( 2 * mean ) - prev2 ; if ( current >= 0 && current <= 9 ) val += countOfNumbers ( digit + 1 , current , prev1 , n ) ; if ( ( current + 1 ) >= 0 && ( current + 1 ) <= 9 ) val += countOfNumbers ( digit + 1 , current + 1 , prev1 , n ) ; } return val ; } var n = 2 ; document . write ( countOfNumbers ( 1 , 0 , 0 , n ) ) ;
Count N | JavaScript program for the above approach ; Function to count N digit numbers whose digits are less than or equal to the absolute difference of previous two digits ; If all digits are traversed ; If the state has already been computed ; If the current digit is 1 , any digit from [ 1 - 9 ] can be placed . If N == 1 , 0 can also be placed . ; If the current digit is 2 , any digit from [ 0 - 9 ] can be placed ; For other digits , any digit from 0 to abs ( prev1 - prev2 ) can be placed ; Return the answer ; Input ; Function call
var dp = Array . from ( Array ( 50 ) , ( ) => Array ( 10 ) ) ; for ( var i = 0 ; i < 10 ; i ++ ) for ( var j = 0 ; j < 10 ; j ++ ) dp [ i ] [ j ] = new Array ( 10 ) . fill ( - 1 ) ; function countOfNumbers ( digit , prev1 , prev2 , N ) { if ( digit == N + 1 ) return 1 ; if ( dp [ digit ] [ prev1 ] [ prev2 ] != - 1 ) return dp [ digit ] [ prev1 ] [ prev2 ] ; dp [ digit ] [ prev1 ] [ prev2 ] = 0 ; if ( digit == 1 ) { for ( var j = ( N == 1 ? 0 : 1 ) ; j <= 9 ; ++ j ) { dp [ digit ] [ prev1 ] [ prev2 ] += countOfNumbers ( digit + 1 , j , prev1 , N ) ; } } else if ( digit == 2 ) { for ( var j = 0 ; j <= 9 ; ++ j ) { dp [ digit ] [ prev1 ] [ prev2 ] += countOfNumbers ( digit + 1 , j , prev1 , N ) ; } } else { for ( var j = 0 ; j <= Math . abs ( prev1 - prev2 ) ; ++ j ) { dp [ digit ] [ prev1 ] [ prev2 ] += countOfNumbers ( digit + 1 , j , prev1 , N ) ; } } return dp [ digit ] [ prev1 ] [ prev2 ] ; } var N = 3 ; document . write ( countOfNumbers ( 1 , 0 , 0 , N ) ) ;
Length of longest subset consisting of A 0 s and B 1 s from an array of strings | Function to count number of 0 s present in the string ; Stores the count of 0 s ; Iterate over characters of string ; If current character is '0' ; Recursive Function to find the length of longest subset from given array of strings with at most A 0 s and B 1 s ; If idx is equal to N or A + B is equal to 0 ; If the state is already calculated ; Stores the count of 0 's ; Stores the count of 1 's ; Stores the length of longest by including arr [ idx ] ; If zero is less than A and one is less than B ; Stores the length of longest subset by excluding arr [ idx ] ; Assign ; Return ; Function to find the length of the longest subset of an array of strings with at most A 0 s and B 1 s ; Stores all Dp - states ; Return ; Driver Code
function count0 ( s ) { let count = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) { count ++ ; } } return count ; } function solve ( vec , A , B , idx , dp ) { if ( idx == vec . length A + B == 0 ) { return 0 ; } if ( dp [ A ] [ B ] [ idx ] > 0 ) { return dp [ A ] [ B ] [ idx ] ; } let zero = count0 ( vec [ idx ] ) ; let one = vec [ idx ] . length - zero ; let inc = 0 ; if ( zero <= A && one <= B ) { inc = 1 + solve ( vec , A - zero , B - one , idx + 1 , dp ) ; } let exc = solve ( vec , A , B , idx + 1 , dp ) ; dp [ A ] [ B ] [ idx ] = Math . max ( inc , exc ) ; return dp [ A ] [ B ] [ idx ] ; } function MaxSubsetlength ( arr , A , B ) { let dp = new Array ( A + 1 ) ; for ( let i = 0 ; i < A + 1 ; i ++ ) { dp [ i ] = new Array ( B + 1 ) ; for ( let j = 0 ; j < B + 1 ; j ++ ) { dp [ i ] [ j ] = new Array ( arr . length + 1 ) ; for ( let k = 0 ; k < arr . length + 1 ; k ++ ) { dp [ i ] [ j ] [ k ] = 0 ; } } } return solve ( arr , A , B , 0 , dp ) ; } let arr = [ " " , " " , " " ] ; let A = 1 , B = 1 ; document . write ( MaxSubsetlength ( arr , A , B ) ) ;
Check if a given pattern exists in a given string or not | Function to check if the pattern consisting of ' * ' , ' . ' and lowercase characters matches the text or not ; Base Case ; Stores length of text ; Stores length of pattern ; dp [ i ] [ j ] : Check if { text [ 0 ] , . . text [ i ] } matches { pattern [ 0 ] , ... pattern [ j ] } or not ; Base Case ; Iterate over the characters of the string pattern ; Update dp [ 0 ] [ i + 1 ] ; Iterate over the characters of both the strings ; If current character in the pattern is ' . ' ; Update dp [ i + 1 ] [ j + 1 ] ; If current character in both the strings are equal ; Update dp [ i + 1 ] [ j + 1 ] ; If current character in the pattern is ' * ' ; Update dp [ i + 1 ] [ j + 1 ] ; Update dp [ i + 1 ] [ j + 1 ] ; Return dp [ M ] [ N ] ; Driver Code
function isMatch ( text , pattern ) { if ( text == null pattern == null ) { return false ; } let N = text . length ; let M = pattern . length ; let dp = new Array ( N + 1 ) ; for ( let i = 0 ; i <= N ; i ++ ) { dp [ i ] = new Array ( M + 1 ) ; for ( let j = 0 ; j <= M ; j ++ ) { dp [ i ] [ j ] = false ; } } dp [ 0 ] [ 0 ] = true ; for ( let i = 0 ; i < M ; i ++ ) { if ( pattern [ i ] == ' ' && dp [ 0 ] [ i - 1 ] ) { dp [ 0 ] [ i + 1 ] = true ; } } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( pattern [ j ] == ' ' ) { dp [ i + 1 ] [ j + 1 ] = dp [ i ] [ j ] ; } if ( pattern [ j ] == text [ i ] ) { dp [ i + 1 ] [ j + 1 ] = dp [ i ] [ j ] ; } if ( pattern [ j ] == ' ' ) { if ( pattern [ j - 1 ] != text [ i ] && pattern [ j - 1 ] != ' ' ) { dp [ i + 1 ] [ j + 1 ] = dp [ i + 1 ] [ j - 1 ] ; } else { dp [ i + 1 ] [ j + 1 ] = ( dp [ i + 1 ] [ j ] dp [ i ] [ j + 1 ] dp [ i + 1 ] [ j - 1 ] ) ; } } } } return dp [ N ] [ M ] ; } let text = " " ; let pattern = " " ; if ( isMatch ( text , pattern ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Count number of unique ways to paint a N x 3 grid | Function to count the number of ways to paint N * 3 grid based on given conditions ; Count of ways to pain a row with same colored ends ; Count of ways to pain a row with different colored ends ; Traverse up to ( N - 1 ) th row ; For same colored ends ; For different colored ends ; Print the total number of ways ; Driver code ; Function call
function waysToPaint ( n ) { var same = 6 ; var diff = 6 ; for ( var i = 0 ; i < n - 1 ; i ++ ) { var sameTmp = 3 * same + 2 * diff ; var diffTmp = 2 * same + 2 * diff ; same = sameTmp ; diff = diffTmp ; } document . write ( same + diff ) ; } var N = 2 ; waysToPaint ( N ) ;
Minimize sum of incompatibilities of K equal | Javascript program for the above approach ; Function to find the minimum incompatibility sum ; Stores the count of element ; Traverse the array ; If number i not occurs in Map ; Put the element in the Map ; Increment the count of i in the Hash Map ; If count of i in Map is greater than K then return - 1 ; Stores all total state ; Traverse over all the state ; If number of set bit is equal to N / K ; Stores the minimum value at a state ; Initialize the dp state with - 1 ; Call the recursive function ; Recursive function to find the minimum required value ; Base Case ; Stores the minimum value of the current state ; If dp [ state ] [ index ] is already calculated ; return dp [ state ] [ index ] ; Traverse over all the bits ; If count of set bit is N / K ; Store the new state after choosing N / K elements ; Stores the minimum and maximum of current subset ; Stores if the elements have been already selected or not ; Stores if it is possible to select N / K elements ; Traverse over the array ; If not chosen already for another subset ; If already chosen for another subset or current subset ; Mark the good false ; Mark the current number visited ; Mark the current position in mask newstate ; Update the maximum and minimum ; If good is true then Update the res ; Update the current sp state ; Return the res ; Driver Code ; Function Call
let k , n , goal ; let dp ; let bits = [ ] ; function minimumIncompatibility ( A , K ) { n = A . length ; k = K ; goal = Math . floor ( n / k ) ; let map = new Map ( ) ; for ( let i = 0 ; i < A . length ; i ++ ) { if ( ! map . has ( A [ i ] ) ) map . set ( A [ i ] , 0 ) ; map . set ( A [ i ] , map . get ( A [ i ] ) + 1 ) ; if ( map . get ( A [ i ] ) > k ) return - 1 ; } let state = ( 1 << n ) - 1 ; for ( let i = 0 ; i <= state ; i ++ ) { if ( i . toString ( 2 ) . split ( ' ' ) . join ( ' ' ) . length == goal ) bits . push ( i ) ; } dp = new Array ( 1 << n ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( k ) ; for ( let j = 0 ; j < k ; j ++ ) dp [ i ] [ j ] = - 1 ; } return dfs ( A , state , 0 ) ; } function dfs ( A , state , index ) { if ( index >= k ) { return 0 ; } let res = 1000 ; if ( dp [ state ] [ index ] != - 1 ) { return dp [ state ] [ index ] ; } for ( let bit = 0 ; bit < bits . length ; bit ++ ) { if ( bits [ bit ] . toString ( 2 ) . split ( ' ' ) . join ( ' ' ) . length == goal ) { let newstate = state ; let mn = 100 , mx = - 1 ; let visit = new Array ( n + 1 ) ; for ( let i = 0 ; i <= n ; i ++ ) { visit [ i ] = false ; } let good = true ; for ( let j = 0 ; j < n ; j ++ ) { if ( ( bits [ bit ] & ( 1 << j ) ) != 0 ) { if ( visit [ A [ j ] ] == true || ( state & ( 1 << j ) ) == 0 ) { good = false ; break ; } visit [ A [ j ] ] = true ; newstate = newstate ^ ( 1 << j ) ; mx = Math . max ( mx , A [ j ] ) ; mn = Math . min ( mn , A [ j ] ) ; } } if ( good ) { res = Math . min ( res , mx - mn + dfs ( A , newstate , index + 1 ) ) ; } } } dp [ state ] [ index ] = res ; return res ; } let arr = [ 1 , 2 , 1 , 4 ] ; let K = 2 ; document . write ( minimumIncompatibility ( arr , K ) )
Maximize sum by selecting X different | Function to find maximum sum of at most N with different index array elements such that at most X are from A [ ] , Y are from B [ ] and Z are from C [ ] ; Base Cases ; Selecting i - th element from A [ ] ; Selecting i - th element from B [ ] ; Selecting i - th element from C [ ] ; i - th elements not selected from any of the arrays ; Select the maximum sum from all the possible calls ; Given X , Y and Z ; Given A [ ] ; Given B [ ] ; Given C [ ] ; Given Size ; Function Call
function FindMaxS ( X , Y , Z , n , A , B , C ) { if ( X < 0 Y < 0 Z < 0 ) return - 1000000000 ; if ( n < 0 ) return 0 ; var ch = A [ n ] + FindMaxS ( X - 1 , Y , Z , n - 1 , A , B , C ) ; var ca = B [ n ] + FindMaxS ( X , Y - 1 , Z , n - 1 , A , B , C ) ; var co = C [ n ] + FindMaxS ( X , Y , Z - 1 , n - 1 , A , B , C ) ; var no = FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ; var maximum = Math . max ( ch , Math . max ( ca , Math . max ( co , no ) ) ) ; return maximum ; } var X = 1 ; var Y = 1 ; var Z = 1 ; var A = [ 10 , 0 , 5 ] ; var B = [ 5 , 10 , 0 ] ; var C = [ 0 , 5 , 10 ] ; var n = B . length ; document . write ( FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ) ;
Rearrange array by interchanging positions of even and odd elements in the given array | Function to replace each even element by odd and vice - versa in a given array ; Stores length of array ; Traverse array ; If current element is even then swap it with odd ; Perform Swap ; Change the sign ; If current element is odd then swap it with even ; Perform Swap ; Change the sign ; Marked element positive ; Prlet final array ; Given array arr [ ] ; Function Call
function replace ( arr ) { let n = arr . length ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] >= 0 && arr [ j ] >= 0 && arr [ i ] % 2 == 0 && arr [ j ] % 2 != 0 ) { let tmp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = tmp ; arr [ j ] = - arr [ j ] ; break ; } else if ( arr [ i ] >= 0 && arr [ j ] >= 0 && arr [ i ] % 2 != 0 && arr [ j ] % 2 == 0 ) { let tmp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = tmp ; arr [ j ] = - arr [ j ] ; break ; } } } for ( let i = 0 ; i < n ; i ++ ) arr [ i ] = Math . abs ( arr [ i ] ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 1 , 3 , 2 , 4 ] ; replace ( arr ) ;
Minimize cost to reduce array to a single element by replacing K consecutive elements by their sum | Function to find the minimum cost to reduce given array to a single element by replacing consecutive K array elements ; If ( N - 1 ) is not multiple of ( K - 1 ) ; Store prefix sum of the array ; Iterate over the range [ 1 , N ] ; Update prefixSum [ i ] ; dp [ i ] [ j ] : Store minimum cost to merge array elements interval [ i , j ] ; L : Stores length of interval [ i , j ] ; Iterate over each interval [ i , j ] of length L in in [ 0 , N ] ; Stores index of last element of the interval [ i , j ] ; If L is greater than K ; Update dp [ i ] [ j ] ; If ( L - 1 ) is multiple of ( K - 1 ) ; Update dp [ i ] [ j ] ; Return dp [ 0 ] [ N - 1 ] ; Driver Code ; Function Call
function minimumCostToMergeK ( arr , K , N ) { if ( ( N - 1 ) % ( K - 1 ) != 0 ) { return - 1 ; } var prefixSum = Array ( N + 1 ) . fill ( 0 ) ; for ( i = 1 ; i < ( N + 1 ) ; i ++ ) { prefixSum [ i ] = ( prefixSum [ i - 1 ] + arr [ i - 1 ] ) ; } var dp = Array ( N ) ; for ( i = 0 ; i < N ; i ++ ) dp [ i ] = Array ( N ) . fill ( 0 ) ; for ( L = K ; L < ( N + 1 ) ; L ++ ) { for ( i = 0 ; i < ( N - L + 1 ) ; i ++ ) { var j = i + L - 1 ; if ( L > K ) { var temp = Number . MAX_VALUE ; for ( x = i ; x < j ; x += K - 1 ) { temp = Math . min ( temp , dp [ i ] [ x ] + dp [ x + 1 ] [ j ] ) ; } dp [ i ] [ j ] = temp ; } if ( ( L - 1 ) % ( K - 1 ) == 0 ) { dp [ i ] [ j ] += ( prefixSum [ j + 1 ] - prefixSum [ i ] ) ; } } } return dp [ 0 ] [ N - 1 ] ; } var arr = [ 3 , 5 , 1 , 2 , 6 ] ; var K = 3 ; var N = arr . length ; document . write ( minimumCostToMergeK ( arr , K , N ) ) ;