codeparrot/xlcost-text-to-code huggingface dataset:
Find minimum time to finish all jobs with given constraints | Utility function to get maximum element in job [ 0. . n - 1 ] ; Returns true if it is possible to finish jobs [ ] within given time ' time ' ; cnt is count of current assignees required for jobs ; time assigned to current assignee ; If time assigned to current assignee exceeds max , increment count of assignees . ; Else add time of job to current time and move to next job . ; Returns true if count is smaller than k ; Returns minimum time required to finish given array of jobs k -- > number of assignees T -- > Time required by every assignee to finish 1 unit m -- > Number of jobs ; Set start and end for binary search end provides an upper limit on time ; Initialize answer ; Find the job that takes maximum time ; Do binary search for minimum feasible time ; If it is possible to finish jobs in mid time ; Update answer ; Driver program
function getMax ( arr , n ) { let result = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) if ( arr [ i ] > result ) result = arr [ i ] ; return result ; } function isPossible ( time , K , job , n ) { let cnt = 1 ; let curr_time = 0 ; for ( let i = 0 ; i < n ; ) { if ( curr_time + job [ i ] > time ) { curr_time = 0 ; cnt ++ ; } else { curr_time += job [ i ] ; i ++ ; } } return ( cnt <= K ) ; } function findMinTime ( K , T , job , n ) { let end = 0 , start = 0 ; for ( let i = 0 ; i < n ; ++ i ) end += job [ i ] ; let ans = end ; let job_max = getMax ( job , n ) ; while ( start <= end ) { let mid = parseInt ( ( start + end ) / 2 , 10 ) ; if ( mid >= job_max && isPossible ( mid , K , job , n ) ) { ans = Math . min ( ans , mid ) ; end = mid - 1 ; } else start = mid + 1 ; } return ( ans * T ) ; } let job = [ 10 , 7 , 8 , 12 , 6 , 8 ] ; let n = job . length ; let k = 4 , T = 5 ; document . write ( findMinTime ( k , T , job , n ) ) ;
Longest subarray with all even or all odd elements | Function to calculate longest substring with odd or even elements ; Initializing dp [ ] ; Initializing dp [ 0 ] 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 [ i ] with dp [ i - 1 ] + 1 ; Storing max element to ans ; Returning the final answer ; Input ; Function call
function LongestOddEvenSubarray ( A , N ) { let dp = new Array ( N ) ; dp [ 0 ] = 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 [ i ] = dp [ i - 1 ] + 1 ; } else dp [ i ] = 1 ; } for ( let i = 0 ; i < N ; i ++ ) ans = Math . max ( ans , dp [ i ] ) ; return ans ; } let A = [ 2 , 5 , 7 , 2 , 4 , 6 , 8 , 3 ] ; let N = A . length ; document . write ( LongestOddEvenSubarray ( A , N ) ) ;
Count of subarrays with maximum value as K | Function to count the subarrays with maximum not greater than K ; If arr [ i ] > k then arr [ i ] cannot be a part of any subarray . ; Count the number of elements where arr [ i ] is not greater than k . ; Summation of all possible subarrays in the variable ans . ; Function to count the subarrays with maximum value is equal to K ; Stores count of subarrays with max <= k - 1. ; Stores count of subarrays with max >= k + 1. ; Stores count of subarrays with max = k . ; Given Input ; Function Call
function totalSubarrays ( arr , n , k ) { let ans = 0 , i = 0 ; while ( i < n ) { if ( arr [ i ] > k ) { i ++ ; continue ; } let count = 0 ; while ( i < n && arr [ i ] <= k ) { i ++ ; count ++ ; } ans += ( Math . floor ( ( count * ( count + 1 ) ) / 2 ) ) ; } return ans ; } function countSubarrays ( arr , n , k ) { let count1 = totalSubarrays ( arr , n , k - 1 ) ; let count2 = totalSubarrays ( arr , n , k ) ; let ans = count2 - count1 ; return ans ; } let n = 4 , k = 3 ; let arr = [ 2 , 1 , 3 , 4 ] ; document . write ( countSubarrays ( arr , n , k ) ) ;
Count N | Stores the dp - states ; Function to calculate the count of N - digit numbers that contains all digits from [ 0 - 9 ] atleast once ; If all digits are traversed ; Check if all digits are included in the mask ; 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 ; For other positions , any digit from [ 0 - 9 ] can be placed ; Return the answer ; Given Input ; Function Call
let dp = new Array ( 100 ) ; for ( let i = 0 ; i < 100 ; i ++ ) { dp [ i ] = new Array ( 1 << 10 ) . fill ( - 1 ) ; } function countOfNumbers ( digit , mask , N ) { if ( digit == N + 1 ) { if ( mask == ( 1 << 10 ) - 1 ) return 1 ; return 0 ; } let val = dp [ digit ] [ mask ] ; if ( val != - 1 ) return val ; val = 0 ; if ( digit == 1 ) { for ( let i = ( N == 1 ? 0 : 1 ) ; i <= 9 ; ++ i ) { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , N ) ; } } else { for ( let i = 0 ; i <= 9 ; ++ i ) { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , N ) ; } } return val ; } let N = 10 ; document . write ( countOfNumbers ( 1 , 0 , N ) ) ;
Maximum subsequence sum such that no three are consecutive in O ( 1 ) space | Function to calculate the maximum subsequence sum such that no three elements are consecutive ; when N is 1 , answer would be the only element present ; when N is 2 , answer would be sum of elements ; variable to store sum up to i - 3 ; variable to store sum up to i - 2 ; variable to store sum up to i - 1 ; variable to store the final sum of the subsequence ; find the maximum subsequence sum up to index i ; update first , second and third ; return ans ; ; Input ; Function call
function maxSumWO3Consec ( A , N ) { if ( N == 1 ) return A [ 0 ] ; if ( N == 2 ) return A [ 0 ] + A [ 1 ] ; let third = A [ 0 ] ; let second = third + A [ 1 ] ; let first = Math . max ( second , A [ 1 ] + A [ 2 ] ) ; let sum = Math . max ( Math . max ( third , second ) , first ) ; for ( let i = 3 ; i < N ; i ++ ) { sum = Math . max ( Math . max ( first , second + A [ i ] ) , third + A [ i ] + A [ i - 1 ] ) ; third = second ; second = first ; first = sum ; } return sum ; } let A = [ 3000 , 2000 , 1000 , 3 , 10 ] ; let N = A . length ; document . write ( maxSumWO3Consec ( A , N ) ) ;
Minimum number of given operations required to reduce a number to 2 | Function to find the minimum number of operations required to reduce n to 2 ; Initialize a dp array ; Handle the base case ; Iterate in the range [ 2 , n ] ; Check if i * 5 <= n ; Check if i + 3 <= n ; Return the result ; Given Input ; Function Call ; Print the result
function findMinOperations ( n ) { let i = 0 ; let dp = new Array ( n + 1 ) ; for ( i = 0 ; i < n + 1 ; i ++ ) { dp [ i ] = 999999 ; } dp [ 2 ] = 0 ; for ( i = 2 ; i < n + 1 ; i ++ ) { if ( i * 5 <= n ) { dp [ i * 5 ] = Math . min ( dp [ i * 5 ] , dp [ i ] + 1 ) ; } if ( i + 3 <= n ) { dp [ i + 3 ] = Math . min ( dp [ i + 3 ] , dp [ i ] + 1 ) ; } } return dp [ n ] ; } let n = 28 ; let m = findMinOperations ( n ) ; if ( m != 9999 ) document . write ( m ) ; else document . write ( - 1 ) ;
Number of M | Function to find the number of M - length sorted arrays possible using numbers from the range [ 1 , N ] ; Create an array of size M + 1 ; Base cases ; Fill the dp table ; dp [ j ] will be equal to maximum number of sorted array of size j when elements are taken from 1 to i ; Here dp [ m ] will be equal to the maximum number of sorted arrays when element are taken from 1 to i ; Return the result ; Given Input ; Function Call
function countSortedArrays ( n , m ) { let dp = new Array ( m + 1 ) ; dp . fill ( 0 ) ; dp [ 0 ] = 1 ; for ( let i = 1 ; i <= n ; i ++ ) { for ( let j = 1 ; j <= m ; j ++ ) { dp [ j ] = dp [ j - 1 ] + dp [ j ] ; } } return dp [ m ] ; } let n = 2 , m = 3 ; document . write ( countSortedArrays ( n , m ) ) ;
Split array into subarrays such that sum of difference between their maximums and minimums is maximum | Function to find maximum sum of difference between maximums and minimums in the splitted subarrays ; Base Case ; Traverse the array ; Stores the maximum and minimum elements upto the i - th index ; Traverse the range [ 0 , i ] ; Update the minimum ; Update the maximum ; Update dp [ i ] ; Return the maximum sum of difference ; Driver code
function getValue ( arr , N ) { let dp = Array . from ( { length : N } , ( _ , i ) => 0 ) ; dp [ 0 ] = 0 ; for ( let i = 1 ; i < N ; i ++ ) { let min = arr [ i ] ; let max = arr [ i ] ; for ( let j = i ; j >= 0 ; j -- ) { min = Math . min ( arr [ j ] , min ) ; max = Math . max ( arr [ j ] , max ) ; dp [ i ] = Math . max ( dp [ i ] , max - min + ( ( j >= 1 ) ? dp [ j - 1 ] : 0 ) ) ; } } return dp [ N - 1 ] ; } let arr = [ 8 , 1 , 7 , 9 , 2 ] ; let N = arr . length ; document . write ( getValue ( arr , N ) ) ;
Length of longest subset consisting of A 0 s and B 1 s from an array of strings | Function to count 0 's in a string ; Stores count of 0 s ; Iterate over characters of string ; If current character is '0' ; Recursive function to find the length of longest subset from an 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 ; Stores the count of 0 's in arr[idx] ; Stores the count of 1 's in arr[idx] ; Stores the length of the subset if arr [ i ] is included ; If zero is less than or equal to A and one is less than or equal to B ; Stores the length of the subset if arr [ i ] is excluded ; Returns max of inc and exc ; Function to find the length of the longest subset from an array of strings with at most A 0 ' s ▁ and ▁ B ▁ 1' s ; 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 ) { if ( idx == vec . length A + B == 0 ) { return 0 ; } 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 ) ; } let exc = solve ( vec , A , B , idx + 1 ) ; return Math . max ( inc , exc ) ; } function MaxSubsetlength ( arr , A , B ) { return solve ( arr , A , B , 0 ) ; } let arr = [ " " , " " , " " ] ; let A = 1 , B = 1 ; document . write ( MaxSubsetlength ( arr , A , B ) ) ;
Maximum score possible by removing substrings made up of single distinct character | Function to check if the string s consists of a single distinct character or not ; Function to calculate the maximum score possible by removing substrings ; If string is empty ; If length of string is 1 ; Store the maximum result ; Try to remove all substrings that satisfy the condition and check for resultant string after removal ; Store the substring { s [ i ] , . . , s [ j ] } ; Check if the substring contains only a single distinct character ; Return the maximum score ; Driver Code
function isUnique ( s ) { var set = new Set ( ) ; for ( let i = 0 ; i < s . length ; i ++ ) { set . add ( s [ i ] ) ; } return set . size == 1 ; } function maxScore ( s , a ) { let n = s . length ; if ( n == 0 ) return 0 ; if ( n == 1 ) return a [ 0 ] ; let mx = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i ; j < n ; j ++ ) { let sub = s . substring ( i , j + 1 ) ; if ( isUnique ( sub ) ) mx = Math . max ( mx , a [ sub . length - 1 ] + maxScore ( s . substring ( 0 , i ) + s . substring ( j + 1 ) , a ) ) ; } } return mx ; } let s = " " ; let a = [ 1 , 3 , 1 ] ; document . write ( maxScore ( s , a ) ) ;
Maximum score possible by removing substrings made up of single distinct character | Initialize a dictionary to store the precomputed results ; Function to calculate the maximum score possible by removing substrings ; If s is present in dp [ ] array ; Base Cases : ; If length of string is 0 ; If length of string is 1 ; Put head pointer at start ; Initialize the max variable ; Generate the substrings using two pointers ; If s [ head ] and s [ tail ] are different ; Move head to tail and break ; Store the substring ; Update the maximum ; Move the tail ; Store the score ; Driver code
let dp = new Map ( ) ; function maxScore ( s , a ) { if ( dp . has ( s ) ) return dp . get ( s ) ; let n = s . length ; if ( n == 0 ) return 0 ; if ( n == 1 ) return a [ 0 ] ; let head = 0 ; let mx = - 1 ; while ( head < n ) { let tail = head ; while ( tail < n ) { if ( s [ tail ] != s [ head ] ) { head = tail ; break ; } let sub = s . substring ( head , head + tail + 1 ) ; mx = Math . max ( mx , a [ sub . length - 1 ] + maxScore ( s . substring ( 0 , head ) + s . substring ( tail + 1 , tail + 1 + s . length ) , a ) ) ; tail += 1 ; } if ( tail == n ) break ; } dp . set ( s , mx ) ; return mx ; } let s = " " ; let a = [ 1 , 3 , 1 ] ; document . write ( ( maxScore ( s , a ) ) - 1 ) ;
Count all unique outcomes possible by performing S flips on N coins | Dimensions of the DP table ; Stores the dp states ; Function to recursively count the number of unique outcomes possible by performing S flips on N coins ; Base Case ; If the count for the current state is not calculated , then calculate it recursively ; Otherwise return the already calculated value ; Driver Code
let size = 100 ; let ans = new Array ( size ) ; function initialize ( ) { for ( let i = 0 ; i < size ; i ++ ) { ans [ i ] = new Array ( size ) ; for ( let j = 0 ; j < size ; j ++ ) { ans [ i ] [ j ] = 0 ; } } } function numberOfUniqueOutcomes ( n , s ) { if ( s < n ) ans [ n ] [ s ] = 0 ; else if ( n == 1 n == s ) ans [ n ] [ s ] = 1 ; else if ( ans [ n ] [ s ] == 0 ) { ans [ n ] [ s ] = numberOfUniqueOutcomes ( n - 1 , s - 1 ) + numberOfUniqueOutcomes ( n - 1 , s - 2 ) ; } return ans [ n ] [ s ] ; } initialize ( ) ; let N = 5 , S = 8 ; document . write ( numberOfUniqueOutcomes ( N , S ) ) ;
Minimize removals to remove another string as a subsequence of a given string | Function to print the minimum number of character removals required to remove X as a subsequence from the string str ; Length of the string str ; Length of the string X ; Stores the dp states ; Fill first row of dp [ ] [ ] ; If X [ j ] matches with str [ 0 ] ; If str [ i ] is equal to X [ j ] ; Update state after removing str [ i [ ; Update state after keeping str [ i ] ; If str [ i ] is not equal to X [ j ] ; Print the minimum number of characters removals ; Input ; Function call to get minimum number of character removals
function printMinimumRemovals ( str , X ) { let N = str . length ; let M = X . length ; let dp = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { dp [ i ] = new Array ( M ) ; for ( let j = 0 ; j < M ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let j = 0 ; j < M ; j ++ ) { if ( str [ 0 ] == X [ j ] ) { dp [ 0 ] [ j ] = 1 ; } } for ( let i = 1 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( str [ i ] == X [ j ] ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + 1 ; if ( j != 0 ) dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - 1 ] ) ; } else { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } } } document . write ( dp [ N - 1 ] [ M - 1 ] ) ; } let str = " " ; let X = " " ; printMinimumRemovals ( str , X ) ;
Maximum sum not exceeding K possible for any rectangle of a Matrix | Function to find the maximum possible sum of arectangle which is less than K ; Stores the values ( cum_sum - K ) ; Insert 0 into the set sumSet ; Traverse over the rows ; Get cumulative sum from [ 0 to i ] ; Search for upperbound of ( cSum - K ) in the hashmap ; If upper_bound of ( cSum - K ) exists , then update max sum ; Insert cummulative value in the hashmap ; Return the maximum sum which is less than K ; Function to find the maximum sum of rectangle such that its sum is no larger than K ; Stores the number of rows and columns ; Store the required result ; Set the left column ; Set the right column for the left column set by outer loop ; Calculate sum between the current left and right for every row ; Stores the sum of rectangle ; Update the overall maximum sum ; Print the result ; Driver Code ; Function Call
function maxSubarraySum ( sum , k , row ) { let curSum = 0 , curMax = Number . MIN_VALUE ; let sumSet = new Set ( ) ; sumSet . add ( 0 ) ; for ( let r = 0 ; r < row ; ++ r ) { curSum += sum [ r ] ; let list = [ ] ; list = Array . from ( sumSet ) ; let it = list . lastIndexOf ( curSum - k ) ; if ( it > - 1 ) { curMax = Math . max ( curMax , curSum - it ) ; } sumSet . add ( curSum ) ; } return curMax ; } function maxSumSubmatrix ( matrix , k ) { let row = matrix . length ; let col = matrix [ 0 ] . length ; let ret = Number . MIN_VALUE ; for ( let i = 0 ; i < col ; ++ i ) { let sum = new Array ( row ) ; for ( let j = 0 ; j < row ; j ++ ) sum [ j ] = 0 ; for ( let j = i ; j < col ; ++ j ) { for ( let r = 0 ; r < row ; ++ r ) { sum [ r ] += matrix [ r ] [ j ] ; } let curMax = maxSubarraySum ( sum , k , row ) ; ret = Math . max ( ret , curMax ) ; } } document . write ( ret ) ; } let matrix = [ [ 1 , 0 , 1 ] , [ 0 , - 2 , 3 ] ] ; let K = 2 ; maxSumSubmatrix ( matrix , K )
Count N | Function to count binary strings of length N having substring "11" ; Initialize dp of size N + 1 ; Base Cases ; Stores the first N powers of 2 ; Generate ; Iterate over the range [ 2 , N ] ; Print total count of substrings ; Driver Code
function binaryStrings ( N ) { var dp = Array ( N + 1 ) . fill ( 0 ) ; dp [ 0 ] = 0 ; dp [ 1 ] = 0 ; var power = Array ( N + 1 ) . fill ( 0 ) ; power [ 0 ] = 1 ; for ( i = 1 ; i <= N ; i ++ ) { power [ i ] = 2 * power [ i - 1 ] ; } for ( i = 2 ; i <= N ; i ++ ) { dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + power [ i - 2 ] ; } document . write ( dp [ N ] ) ; } var N = 12 ; binaryStrings ( N ) ;
Maximum sum submatrix | Function to find maximum continuous maximum sum in the array ; Stores current and maximum sum ; Traverse the array v ; Add the value of the current element ; Update the maximum sum ; Return the maximum sum ; Function to find the maximum submatrix sum ; Store the rows and columns of the matrix ; Create an auxiliary matrix ; Traverse the matrix , prefix and initialize it will all 0 s ; Calculate prefix sum of all rows of matrix A [ ] [ ] and store in matrix prefix [ ] ; Update the prefix [ ] [ ] ; Store the maximum submatrix sum ; Iterate for starting column ; Iterate for last column ; To store current array elements ; Traverse every row ; Store the sum of the kth row ; Update the prefix sum ; Push it in a vector ; Update the maximum overall sum ; Print the answer ; Driver Code ; Function Call
function kadane ( v ) { let currSum = 0 ; let maxSum = Number . MIN_VALUE ; for ( let i = 0 ; i < v . length ; i ++ ) { currSum += v [ i ] ; if ( currSum > maxSum ) { maxSum = currSum ; } if ( currSum < 0 ) { currSum = 0 ; } } return maxSum ; } function maxSubmatrixSum ( A ) { let r = A . length ; let c = A [ 0 ] . length ; let prefix = new Array ( r ) ; for ( let i = 0 ; i < r ; i ++ ) { prefix [ i ] = new Array ( c ) ; for ( let j = 0 ; j < c ; j ++ ) { prefix [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < r ; i ++ ) { for ( let j = 0 ; j < c ; j ++ ) { if ( j == 0 ) prefix [ i ] [ j ] = A [ i ] [ j ] ; else prefix [ i ] [ j ] = A [ i ] [ j ] + prefix [ i ] [ j - 1 ] ; } } let maxSum = Number . MIN_VALUE ; for ( let i = 0 ; i < c ; i ++ ) { for ( let j = i ; j < c ; j ++ ) { let v = [ ] ; for ( let k = 0 ; k < r ; k ++ ) { let el = 0 ; if ( i == 0 ) el = prefix [ k ] [ j ] ; else el = prefix [ k ] [ j ] - prefix [ k ] [ i - 1 ] ; v . push ( el ) ; } maxSum = Math . max ( maxSum , kadane ( v ) ) ; } } document . write ( maxSum + " " ) ; } let matrix = [ [ 0 , - 2 , - 7 , 0 ] , [ 9 , 2 , - 6 , 2 ] , [ - 4 , 1 , - 4 , 1 ] , [ - 1 , 8 , 0 , - 2 ] ] ; maxSubmatrixSum ( matrix ) ;
Minimize cost of painting N houses such that adjacent houses have different colors | Function to find the minimum cost of coloring the houses such that no two adjacent houses has the same color ; Corner Case ; Auxiliary 2D dp array ; Base Case ; If current house is colored with red the take min cost of previous houses colored with ( blue and green ) ; If current house is colored with blue the take min cost of previous houses colored with ( red and green ) ; If current house is colored with green the take min cost of previous houses colored with ( red and blue ) ; Print the min cost of the last painted house ; Driver Code ; Function Call
function minCost ( costs , N ) { if ( N == 0 ) return 0 ; let dp = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { dp [ i ] = new Array ( 3 ) ; for ( let j = 0 ; j < 3 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } dp [ 0 ] [ 0 ] = costs [ 0 ] [ 0 ] ; dp [ 0 ] [ 1 ] = costs [ 0 ] [ 1 ] ; dp [ 0 ] [ 2 ] = costs [ 0 ] [ 2 ] ; for ( let i = 1 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = Math . min ( dp [ i - 1 ] [ 1 ] , dp [ i - 1 ] [ 2 ] ) + costs [ i ] [ 0 ] ; dp [ i ] [ 1 ] = Math . min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 2 ] ) + costs [ i ] [ 1 ] ; dp [ i ] [ 2 ] = Math . min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) + costs [ i ] [ 2 ] ; } document . write ( Math . min ( dp [ N - 1 ] [ 0 ] , Math . min ( dp [ N - 1 ] [ 1 ] , dp [ N - 1 ] [ 2 ] ) ) ) ; } let costs = [ [ 14 , 2 , 11 ] , [ 11 , 14 , 5 ] , [ 14 , 3 , 10 ] ] ; let N = costs . length ; minCost ( costs , N ) ;
Minimize count of flips required to make sum of the given array equal to 0 | Initialize dp [ ] [ ] ; Function to find the minimum number of operations to make sum of A [ ] 0 ; Initialize answer ; Base case ; Otherwise , return 0 ; Pre - computed subproblem ; Recurrence relation for finding the minimum of the sum of subsets ; Return the result ; Function to find the minimum number of elements required to be flipped to amke sum the array equal to 0 ; Find the sum of array ; Initialise [ , ] dp with - 1 ; No solution exists ; Otherwise ; If sum is odd , no subset is possible ; Driver Code ; Function Call
let dp = [ ] ; for ( var i = 0 ; i < 2001 ; i ++ ) { dp [ i ] = [ ] ; for ( var j = 0 ; j < 2001 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } function solve ( A , i , sum , N ) { let res = 2001 ; if ( sum < 0 || ( i == N && sum != 0 ) ) { return 2001 ; } if ( sum == 0 i >= N ) { dp [ i ] [ sum ] = 0 ; return dp [ i ] [ sum ] ; } if ( dp [ i ] [ sum ] != - 1 ) { return dp [ i ] [ sum ] ; } res = Math . min ( solve ( A , i + 1 , sum - A [ i ] , N ) + 1 , solve ( A , i + 1 , sum , N ) ) ; dp [ i ] [ sum ] = res ; return dp [ i ] [ sum ] ; } function minOp ( A , N ) { let sum = 0 ; for ( let i = 0 ; i < A . length ; i ++ ) { sum += A [ i ] ; } if ( sum % 2 == 0 ) { let dp = [ ] ; for ( var i = 0 ; i < 2001 ; i ++ ) { dp [ i ] = [ ] ; for ( var j = 0 ; j < 2001 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } let ans = solve ( A , 0 , Math . floor ( sum / 2 ) , N ) ; if ( ans < 0 ans > N ) { document . write ( " " ) ; } else { document . write ( ans , " " ) ; } } else { document . write ( " " ) ; } } let A = [ 2 , 3 , 1 , 4 ] ; let N = A . length ; minOp ( A , N ) ;
Count subsequences having average of its elements equal to K | Stores the dp states ; Function to find the count of subsequences having average K ; Base condition ; Three loops for three states ; Recurrence relation ; Stores the sum of dp [ n ] [ j ] [ K * j ] all possible values of j with average K and sum K * j ; Iterate over the range [ 1 , N ] ; Return the final count ; Driver Code
var dp = Array . from ( Array ( 101 ) , ( ) => Array ( 101 ) ) ; for ( var i = 0 ; i < 101 ; i ++ ) for ( var j = 0 ; j < 101 ; j ++ ) dp [ i ] [ j ] = new Array ( 1001 ) . fill ( 0 ) ; function countAverage ( n , K , arr ) { dp [ 0 ] [ 0 ] [ 0 ] = 1 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var k = 0 ; k < n ; k ++ ) { for ( var s = 0 ; s <= 1000 ; s ++ ) { dp [ i + 1 ] [ k + 1 ] [ s + arr [ i ] ] += dp [ i ] [ k ] [ s ] ; dp [ i + 1 ] [ k ] [ s ] += dp [ i ] [ k ] [ s ] ; } } } var cnt = 0 ; for ( var j = 1 ; j <= n ; j ++ ) { cnt += dp [ n ] [ j ] [ K * j ] ; } return cnt ; } var arr = [ 9 , 7 , 8 , 9 ] ; var K = 8 ; var N = arr . length document . write ( countAverage ( N , K , arr ) ) ;