codeparrot/xlcost-text-to-code huggingface dataset:
Longest subarray having maximum sum | function to find the maximum sum that exists in a subarray ; function to find the length of longest subarray having sum k ; unordered_map ' um ' implemented as hash table ; traverse the given array ; accumulate sum ; when subarray starts from index '0' ; make an entry for ' sum ' if it is not present in ' um ' ; check if ' sum - k ' is present in ' um ' or not ; update maxLength ; required maximum length ; function to find the length of the longest subarray having maximum sum ; Driver program to test above
function maxSubArraySum ( arr , size ) { var max_so_far = arr [ 0 ] ; var curr_max = arr [ 0 ] ; for ( var i = 1 ; i < size ; i ++ ) { curr_max = Math . max ( arr [ i ] , curr_max + arr [ i ] ) ; max_so_far = Math . max ( max_so_far , curr_max ) ; } return max_so_far ; } function lenOfLongSubarrWithGivenSum ( arr , n , k ) { var um = new Map ( ) ; var sum = 0 , maxLen = 0 ; for ( var i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == k ) maxLen = i + 1 ; if ( ! um . has ( sum ) ) um . set ( sum , i ) ; if ( um . has ( sum - k ) ) { if ( maxLen < ( i - um . get ( sum - k ) ) ) maxLen = i - um . get ( sum - k ) } } return maxLen ; } function lenLongSubarrWithMaxSum ( arr , n ) { var maxSum = maxSubArraySum ( arr , n ) ; return lenOfLongSubarrWithGivenSum ( arr , n , maxSum ) ; } var arr = [ 5 , - 2 , - 1 , 3 , - 4 ] ; var n = arr . length ; document . write ( " " + lenLongSubarrWithMaxSum ( arr , n ) ) ;
Number of Co | Javascript program to count the pairs whose sum of digits is co - prime ; Function to find the elements after doing the sum of digits ; Traverse from a to b ; Find the sum of the digits of the elements in the given range one by one ; Function to count the co - prime pairs ; Function to make the pairs by doing the sum of digits ; Count pairs that are co - primes ; Driver code ; Function to count the pairs
function GCD ( a , b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } function makePairs ( pairs , a , b ) { for ( i = a ; i <= b ; i ++ ) { var sumOfDigits = 0 , k = i ; while ( k > 0 ) { sumOfDigits += k % 10 ; k = parseInt ( k / 10 ) ; } if ( sumOfDigits <= 162 ) pairs . push ( sumOfDigits ) ; } } function countCoPrime ( a , b ) { var pairs = [ ] ; makePairs ( pairs , a , b ) ; var count = 0 ; for ( i = 0 ; i < pairs . length ; i ++ ) for ( j = i + 1 ; j < pairs . length ; j ++ ) if ( GCD ( pairs [ i ] , pairs [ j ] ) == 1 ) count ++ ; return count ; } var a = 12 , b = 15 ; document . write ( countCoPrime ( a , b ) ) ;
Number of Co | Javascript program to count the pairs whose sum of digits is co - prime ; Recursive function to return the frequency of numbers having their sum of digits i ; Returns 1 or 0 ; Returns value of the dp if already stored ; Loop from digit 0 to 9 ; To change the tight to 1 ; Calling the recursive function to find the frequency ; Function to find out frequency of numbers from 1 to N having their sum of digits from 1 to 162 and store them in array ; Number to string conversion ; Calling the recursive function and pushing it into array ; Function to find the pairs ; Calling the formArray function of a - 1 numbers ; Calling the formArray function of b numbers ; Subtracting the frequency of higher number array with lower number array and thus finding the range of numbers from a to b having sum from 1 to 162 ; To find out total number of pairs which are co - prime ; Driver code ; Function to count the pairs
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function recursive ( idx , sum , tight , st , dp , num ) { if ( idx == num ) { return sum == 0 ? 1 : 0 ; } if ( dp [ idx ] [ tight ] [ sum ] != - 1 ) return dp [ idx ] [ tight ] [ sum ] ; let newTight ; let ans = 0 ; let d ; for ( d = 0 ; d < 10 ; ++ d ) { newTight = 0 ; if ( tight == 1 && st [ idx ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) < d ) continue ; if ( tight == 1 && st [ idx ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) == d ) newTight = 1 ; if ( sum >= d ) ans += recursive ( idx + 1 , sum - d , newTight , st , dp , num ) ; } return dp [ idx ] [ tight ] [ sum ] = ans ; } function formArray ( N ) { let dp = new Array ( 20 ) ; for ( let i = 0 ; i < 20 ; i ++ ) { dp [ i ] = new Array ( 2 ) ; for ( let j = 0 ; j < 2 ; j ++ ) { dp [ i ] [ j ] = new Array ( 166 ) ; for ( let k = 0 ; k < 166 ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } let st = ( N ) . toString ( ) ; let num = st . length ; let arr = [ ] ; for ( let i = 1 ; i <= 162 ; ++ i ) { arr . push ( recursive ( 0 , i , 1 , st , dp , num ) ) ; } return arr ; } function findPair ( a , b ) { let arr_smaller = formArray ( a - 1 ) ; let arr_greater = formArray ( b ) ; for ( let i = 0 ; i < arr_greater . length ; ++ i ) { arr_greater [ i ] -= arr_smaller [ i ] ; } let ans = 0 ; for ( let i = 1 ; i <= 162 ; ++ i ) { for ( let j = i + 1 ; j <= 162 ; ++ j ) { if ( gcd ( i , j ) == 1 ) { ans = ( ans + arr_greater [ i - 1 ] * arr_greater [ j - 1 ] ) ; } } } return ans ; } let a = 12 , b = 15 ; document . write ( findPair ( a , b ) ) ;
Semiperfect Number | Code to find all the factors of the number excluding the number itself ; vector to store the factors ; note that this loop runs till Math . sqrt ( n ) ; if the value of i is a factor ; condition to check the divisor is not the number itself ; return the vector ; Function to check if the number is semi - perfect or not ; find the divisors ; sorting the vector ; subset to check if no is semiperfect ; initialising 1 st column to true ; initializing 1 st row except zero position to 0 ; loop to find whether the number is semiperfect ; calculation to check if the number can be made by summation of divisors ; If not possible to make the number by any combination of divisors ; Driver code to check if possible
function factors ( n ) { let v = [ ] ; v . push ( 1 ) ; for ( let i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v . push ( i ) ; if ( Math . floor ( n / i ) != i ) { v . push ( Math . floor ( n / i ) ) ; } } } return v ; } function check ( n ) { let v = [ ] ; v = factors ( n ) ; v . sort ( function ( a , b ) { return a - b ; } ) ; let r = v . length ; let subset = new Array ( r + 1 ) ; for ( let i = 0 ; i < r + 1 ; i ++ ) { subset [ i ] = new Array ( n + 1 ) ; for ( let j = 0 ; j < n + 1 ; j ++ ) { subset [ i ] [ j ] = false ; } } for ( let i = 0 ; i <= r ; i ++ ) subset [ i ] [ 0 ] = true ; for ( let i = 1 ; i <= n ; i ++ ) subset [ 0 ] [ i ] = false ; for ( let i = 1 ; i <= r ; i ++ ) { for ( let j = 1 ; j <= n ; j ++ ) { if ( j < v [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; else { subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - v [ i - 1 ] ] ; } } } if ( ( subset [ r ] [ n ] ) == false ) return false ; else return true ; } let n = 40 ; if ( check ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Number of ways to represent a number as sum of k fibonacci numbers | to store fibonacci numbers 42 second number in fibonacci series largest possible integer ; Function to generate fibonacci series ; Recursive function to return the number of ways ; base condition ; for recursive function call ; Driver code
let fib = new Array ( 43 ) ; function fibonacci ( ) { fib [ 0 ] = 1 ; fib [ 1 ] = 2 ; for ( let i = 2 ; i < 43 ; i ++ ) fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } function rec ( x , y , last ) { if ( y == 0 ) { if ( x == 0 ) return 1 ; return 0 ; } let sum = 0 ; for ( let i = last ; i >= 0 && fib [ i ] * y >= x ; i -- ) { if ( fib [ i ] > x ) continue ; sum += rec ( x - fib [ i ] , y - 1 , i ) ; } return sum ; } fibonacci ( ) ; let n = 13 , k = 3 ; document . write ( " " + rec ( n , k , 42 ) ) ;
Minimum cost to reach the top of the floor by climbing stairs | function to find the minimum cost to reach N - th floor ; declare an array ; base case ; initially to climb till 0 - th or 1 th stair ; iterate for finding the cost ; return the minimum ; Driver Code
function minimumCost ( cost , n ) { let dp = new Array ( n ) ; if ( n == 1 ) return cost [ 0 ] ; dp [ 0 ] = cost [ 0 ] ; dp [ 1 ] = cost [ 1 ] ; for ( let i = 2 ; i < n ; i ++ ) { dp [ i ] = Math . min ( dp [ i - 1 ] , dp [ i - 2 ] ) + cost [ i ] ; } return Math . min ( dp [ n - 2 ] , dp [ n - 1 ] ) ; } let a = [ 16 , 19 , 10 , 12 , 18 ] ; let n = a . length ; document . write ( minimumCost ( a , n ) ) ;
Minimum cost to reach the top of the floor by climbing stairs | function to find the minimum cost to reach N - th floor ; traverse till N - th stair ; update the last two stairs value ; Driver Code
function minimumCost ( cost , n ) { let dp1 = 0 , dp2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let dp0 = cost [ i ] + Math . min ( dp1 , dp2 ) ; dp2 = dp1 ; dp1 = dp0 ; } return Math . min ( dp1 , dp2 ) ; } let a = [ 2 , 5 , 3 , 1 , 7 , 3 , 4 ] ; let n = a . length ; document . write ( minimumCost ( a , n ) ) ;
Sudo Placement [ 1.5 ] | Wolfish | Javascript program for SP - Wolfish ; Function to find the maxCost of path from ( n - 1 , n - 1 ) to ( 0 , 0 ) ; base condition ; reaches the point ; if the state has been visited previously ; i + j ; check if it is a power of 2 , then only move diagonally ; if not a power of 2 then move side - wise ; Function to return the maximum cost ; calling dp function to get the answer ; Driver Code ; Function calling to get the answer
let size = 1000 ; function maxCost ( a , m , n , dp ) { if ( n < 0 m < 0 ) return - 1e9 ; else if ( m == 0 && n == 0 ) return 0 ; else if ( dp [ m ] [ n ] != - 1 ) return dp [ m ] [ n ] ; else { let num = m + n ; if ( ( num & ( num - 1 ) ) == 0 ) return dp [ m ] [ n ] = a [ m ] [ n ] + maxCost ( a , m - 1 , n - 1 , dp ) ; else return dp [ m ] [ n ] = ( a [ m ] [ n ] + Math . max ( maxCost ( a , m - 1 , n , dp ) , maxCost ( a , m , n - 1 , dp ) ) ) ; } } function answer ( a , n ) { let dp = new Array ( size ) ; for ( let i = 0 ; i < size ; i ++ ) { dp [ i ] = new Array ( size ) ; for ( let j = 0 ; j < size ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } return maxCost ( a , n - 1 , n - 1 , dp ) ; } let a = [ [ 1 , 2 , 3 , 1 ] , [ 4 , 5 , 6 , 1 ] , [ 7 , 8 , 9 , 1 ] , [ 1 , 1 , 1 , 1 ] ] ; let n = 4 ; document . write ( answer ( a , n ) ) ;
Edit distance and LCS ( Longest Common Subsequence ) | Javascript program to find Edit Distance ( when only twooperations are allowed , insert and delete ) using LCS . ; Find LCS ; Edit distance is delete operations + insert operations . ; Driver program to test above function
function editDistanceWith2Ops ( X , Y ) { let m = X . length , n = Y . length ; let L = new Array ( m + 1 ) ; for ( let i = 0 ; i < L . length ; i ++ ) { L [ i ] = new Array ( n + 1 ) ; for ( let j = 0 ; j < L [ i ] . length ; j ++ ) { L [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i <= m ; i ++ ) { for ( let j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) { L [ i ] [ j ] = 0 ; } else if ( X [ i - 1 ] == Y [ j - 1 ] ) { L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; } else { L [ i ] [ j ] = Math . max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } } let lcs = L [ m ] [ n ] ; return ( m - lcs ) + ( n - lcs ) ; } let X = " " , Y = " " ; document . write ( editDistanceWith2Ops ( X , Y ) ) ;
Longest Common Subsequence | DP using Memoization | Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Driver Code ; Find the length of String
function lcs ( X , Y , m , n ) { if ( m == 0 n == 0 ) { return 0 ; } if ( X [ m - 1 ] == Y [ n - 1 ] ) { return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; } else { return Math . max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ; } } let X = " " ; let Y = " " ; let m = X . length ; let n = Y . length ; document . write ( " " + lcs ( X , Y , m , n ) ) ;
Number of different cyclic paths of length N in a tetrahedron | Function to count the number of steps in a tetrahedron ; Initially coming to B is B -> B ; Cannot reach A , D or C ; Iterate for all steps ; recurrence relation ; Memoize previous values ; Returns steps ; Driver code
function countPaths ( n ) { let zB = 1 ; let zADC = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { let nzB = zADC * 3 ; let nzADC = ( zADC * 2 + zB ) ; zB = nzB ; zADC = nzADC ; } return zB ; } let n = 3 ; document . write ( countPaths ( n ) ) ;
Number of subsets with sum divisible by m | Use Dynamic Programming to find sum of subsequences . ; Find sum pf array elements ; dp [ i ] [ j ] would be > 0 if arr [ 0. . i - 1 ] has a subsequence with sum equal to j . ; There is always sum equals zero ; Fill up the dp table ; Initialize the counter ; Check if the sum exists ; check sum is divisible by m ; Driver Code
function sumSubSequence ( arr , len , m ) { let sum = 0 ; for ( let x = 0 ; x < arr . length ; x ++ ) { sum += arr [ x ] ; } let dp = new Array ( len + 1 ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( sum + 1 ) ; for ( let j = 0 ; j < dp [ i ] . length ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i <= len ; i ++ ) dp [ i ] [ 0 ] ++ ; for ( let i = 1 ; i <= len ; i ++ ) { dp [ i ] [ arr [ i - 1 ] ] ++ ; for ( let j = 1 ; j <= sum ; j ++ ) { if ( dp [ i - 1 ] [ j ] > 0 ) { dp [ i ] [ j ] ++ ; dp [ i ] [ j + arr [ i - 1 ] ] ++ ; } } } let count = 0 ; for ( let j = 1 ; j <= sum ; j ++ ) if ( dp [ len ] [ j ] > 0 ) if ( j % m == 0 ) count += dp [ len ] [ j ] ; return count ; } let arr = [ 1 , 2 , 3 ] ; let m = 3 ; let len = arr . length ; document . write ( sumSubSequence ( arr , len , m ) + " " ) ;
Longest Decreasing Subsequence | Function that returns the length of the longest decreasing subsequence ; Initialize LDS with 1 for all index . The minimum LDS starting with any element is always 1 ; Compute LDS from every index in bottom up manner ; Select the maximum of all the LDS values ; returns the length of the LDS ; Driver Code
function lds ( arr , n ) { let lds = new Array ( n ) ; let i , j , max = 0 ; for ( i = 0 ; i < n ; i ++ ) lds [ i ] = 1 ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] < arr [ j ] && lds [ i ] < lds [ j ] + 1 ) lds [ i ] = lds [ j ] + 1 ; for ( i = 0 ; i < n ; i ++ ) if ( max < lds [ i ] ) max = lds [ i ] ; return max ; } let arr = [ 15 , 27 , 14 , 38 , 63 , 55 , 46 , 65 , 85 ] ; let n = arr . length ; document . write ( " " + lds ( arr , n ) ) ;
Total number of decreasing paths in a matrix | Javascript program to count number of decreasing path in a matrix ; Function that returns the number of decreasing paths from a cell ( i , j ) ; checking if already calculated ; all possible paths ; counts the total number of paths ; In all four allowed direction . ; new co - ordinates ; Checking if not going out of matrix and next cell value is less than current cell value . ; function that returns the answer ; Function that counts the total decreasing path in the matrix ; Initialising dp [ ] [ ] to - 1. ; Calculating number of decreasing path from each cell . ; Driver Code ; function call that returns the count of decreasing paths in a matrix
var MAX = 100 function CountDecreasingPathsCell ( mat , dp , n , x , y ) { if ( dp [ x ] [ y ] != - 1 ) return dp [ x ] [ y ] ; var delta = [ [ 0 , 1 ] , [ 1 , 0 ] , [ - 1 , 0 ] , [ 0 , - 1 ] ] ; var newx , newy ; var ans = 1 ; for ( var i = 0 ; i < 4 ; i ++ ) { newx = x + delta [ i ] [ 0 ] ; newy = y + delta [ i ] [ 1 ] ; if ( newx >= 0 && newx < n && newy >= 0 && newy < n && mat [ newx ] [ newy ] < mat [ x ] [ y ] ) { ans += CountDecreasingPathsCell ( mat , dp , n , newx , newy ) ; } } dp [ x ] [ y ] = ans ; return ans ; } function countDecreasingPathsMatrix ( n , mat ) { var dp = Array . from ( Array ( MAX ) , ( ) => Array ( MAX ) ) ; for ( var i = 0 ; i < n ; i ++ ) for ( var j = 0 ; j < n ; j ++ ) dp [ i ] [ j ] = - 1 ; var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) for ( var j = 0 ; j < n ; j ++ ) sum += CountDecreasingPathsCell ( mat , dp , n , i , j ) ; return sum ; } var n = 2 ; var mat = [ [ 1 , 2 ] , [ 1 , 3 ] ] ; document . write ( countDecreasingPathsMatrix ( n , mat ) ) ;
Sum of product of consecutive Binomial Coefficients | Javascript Program to find sum of product of consecutive Binomial Coefficient . ; Find the binomial coefficient upto nth term ; C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the sum of the product of consecutive binomial coefficient . ; finding the sum of product of consecutive coefficient . ; Driven Program
var MAX = 100 ; function binomialCoeff ( C , n ) { for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = Math . min ( i , n ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } } function sumOfproduct ( n ) { var sum = 0 ; var C = Array ( MAX ) . fill ( 0 ) ; binomialCoeff ( C , n ) ; for ( var i = 0 ; i <= n ; i ++ ) sum += C [ i ] * C [ i + 1 ] ; return sum ; } var n = 3 ; document . write ( sumOfproduct ( n ) ) ;
Sum of product of r and rth Binomial Coefficient ( r * nCr ) | Javascript Program to find sum of product of r and rth Binomial Coefficient i . e summation r * nCr ; Return the first n term of binomial coefficient . ; C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return summation of r * nCr ; finding the first n term of binomial coefficient ; Iterate a loop to find the sum . ; Driven Program
MAX = 100 function binomialCoeff ( n , C ) { for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = Math . min ( i , n ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } } function summation ( n ) { C = Array ( MAX ) . fill ( 0 ) ; binomialCoeff ( n , C ) ; var sum = 0 ; for ( var i = 0 ; i <= n ; i ++ ) sum += ( i * C [ i ] ) ; return sum ; } var n = 2 ; document . write ( summation ( n ) ) ;
Number of arrays of size N whose elements are positive integers and sum is K | Return nCr ; C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the number of array that can be formed of size n and sum equals to k . ; Driver Code
function binomialCoeff ( n , k ) { var C = Array ( k + 1 ) . fill ( 0 ) ; for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = Math . min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } function countArray ( N , K ) { return binomialCoeff ( K - 1 , N - 1 ) ; } var N = 2 , K = 3 ; document . write ( countArray ( N , K ) ) ;
Partitioning into two contiguous element subarrays with equal sums | Structure to store the minimum element and its position ; initialize prefix and suffix sum arrays with 0 ; add current element to Sum ; add current element to Sum ; initialize the minimum element to be a large value ; check for the minimum absolute difference between current prefix sum and the next suffix sum element ; if prefixsum has a greater value then position is the next element , else it 's the same element. ; return the data in struct . ; Driver Code
class data { constructor ( element , position ) { this . element = element ; this . position = position ; } } ; function findMinElement ( arr , n ) { let result = new data ( ) ; let prefixSum = new Array ( n ) ; let suffixSum = new Array ( n ) ; prefixSum [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] ; } suffixSum [ n - 1 ] = arr [ n - 1 ] ; for ( let i = n - 2 ; i >= 0 ; i -- ) { suffixSum [ i ] = suffixSum [ i + 1 ] + arr [ i ] ; } let min = suffixSum [ 0 ] ; let pos = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( Math . abs ( suffixSum [ i + 1 ] - prefixSum [ i ] ) < min ) { min = Math . abs ( suffixSum [ i + 1 ] - prefixSum [ i ] ) ; if ( suffixSum [ i + 1 ] < prefixSum [ i ] ) pos = i + 1 ; else pos = i ; } } result . element = min ; result . position = pos ; return result ; } let arr = [ 10 , 1 , 2 , 3 , 4 ] ; let n = arr . length ; let values ; values = findMinElement ( arr , n ) ; document . write ( " " + values . element + " " + values . position ) ;
Number of circular tours that visit all petrol pumps | Javascript Program to find the number of circular tour that visits all petrol pump ; Return the number of pumps from where we can start the journey . ; Making Circular Array . ; for each of the petrol pump . ; If tank is less than 0. ; If starting pump is greater than n , return ans as 0. ; For each of the petrol pump ; Finding the need array ; If need is 0 , increment the count . ; Drivers code
var N = 100 ; function count ( n , c , a , b ) { var need = Array ( N ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { a [ i + n ] = a [ i ] ; b [ i + n ] = b [ i ] ; } var s = 0 ; var tank = 0 ; for ( var i = 0 ; i < 2 * n ; i ++ ) { tank += a [ i ] ; tank = Math . min ( tank , c ) ; tank -= b [ i ] ; if ( tank < 0 ) { tank = 0 ; s = i + 1 ; } } if ( s >= n ) return 0 ; var ans = 1 ; need [ s + n ] = 0 ; for ( var i = 1 ; i < n ; i ++ ) { var id = s + n - i ; need [ id ] = Math . max ( 0 , need [ id + 1 ] + b [ id ] - Math . min ( a [ id ] , c ) ) ; if ( need [ id ] == 0 ) ans ++ ; } return ans ; } var n = 3 ; var c = 3 ; var a = [ 3 , 1 , 2 ] ; var b = [ 2 , 2 , 2 ] ; document . write ( count ( n , c , a , b ) ) ;
Print equal sum sets of array ( Partition Problem ) | Set 2 | Function to print equal sum sets of array . ; Finding sum of array elements ; Check sum is even or odd . If odd then array cannot be partitioned . Print - 1 and return . ; Divide sum by 2 to find sum of two possible subsets . ; Boolean DP table to store result of states . dp [ i ] [ j ] = true if there is a subset of elements in first i elements of array that has sum equal to j . ; If number of elements are zero , then no sum can be obtained . ; Sum 0 can be obtained by not selecting any element . ; Fill the DP table in bottom up manner . ; Excluding current element . ; Including current element ; Required sets set1 and set2 . ; If partition is not possible print - 1 and return . ; Start from last element in dp table . ; If current element does not contribute to k , then it belongs to set 2. ; If current element contribute to k then it belongs to set 1. ; Print elements of both the sets . ; Driver program .
function printEqualSumSets ( arr , n ) { var i , currSum ; var sum = 0 ; for ( var i = 0 ; i < arr . length ; i ++ ) { sum += arr [ i ] ; } if ( sum & 1 ) { document . write ( " " ) ; return ; } var k = sum >> 1 ; var dp = Array . from ( Array ( n + 1 ) , ( ) => Array ( k + 1 ) ) ; for ( i = 1 ; i <= k ; i ++ ) dp [ 0 ] [ i ] = false ; for ( i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = true ; for ( i = 1 ; i <= n ; i ++ ) { for ( currSum = 1 ; currSum <= k ; currSum ++ ) { dp [ i ] [ currSum ] = dp [ i - 1 ] [ currSum ] ; if ( arr [ i - 1 ] <= currSum ) dp [ i ] [ currSum ] = dp [ i ] [ currSum ] | dp [ i - 1 ] [ currSum - arr [ i - 1 ] ] ; } } var set1 = [ ] , set2 = [ ] ; if ( ! dp [ n ] [ k ] ) { document . write ( " " ) ; return ; } i = n ; currSum = k ; while ( i > 0 && currSum >= 0 ) { if ( dp [ i - 1 ] [ currSum ] ) { i -- ; set2 . push ( arr [ i ] ) ; } else if ( dp [ i - 1 ] [ currSum - arr [ i - 1 ] ] ) { i -- ; currSum -= arr [ i ] ; set1 . push ( arr [ i ] ) ; } } document . write ( " " ) ; for ( i = 0 ; i < set1 . length ; i ++ ) document . write ( set1 [ i ] + " " ) ; document . write ( " " ) ; for ( i = 0 ; i < set2 . length ; i ++ ) document . write ( set2 [ i ] + " " ) ; } var arr = [ 5 , 5 , 1 , 11 ] ; var n = arr . length ; printEqualSumSets ( arr , n ) ;
Maximum average sum partition of an array | javascript program for maximum average sum partition ; storing prefix sums ; for each i to n storing averages ; Driver code ; var K = 3 ; atmost partitioning size
function largestSumOfAverages ( A , K ) { var n = A . length ; var pre_sum = Array ( n + 1 ) . fill ( - 1 ) ; pre_sum [ 0 ] = 0 ; for ( var i = 0 ; i < n ; i ++ ) pre_sum [ i + 1 ] = pre_sum [ i ] + A [ i ] ; var dp = Array ( n ) . fill ( - 1 ) ; var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) dp [ i ] = ( pre_sum [ n ] - pre_sum [ i ] ) / ( n - i ) ; for ( k = 0 ; k < K - 1 ; k ++ ) for ( i = 0 ; i < n ; i ++ ) for ( j = i + 1 ; j < n ; j ++ ) dp [ i ] = Math . max ( dp [ i ] , ( pre_sum [ j ] - pre_sum [ i ] ) / ( j - i ) + dp [ j ] ) ; return dp [ 0 ] ; } var A = [ 9 , 1 , 2 , 3 , 9 ] ; document . write ( largestSumOfAverages ( A , K ) ) ;