codeparrot/xlcost-text-to-code huggingface dataset:
Print all distinct characters of a string in order ( 3 Methods ) | Javascript program to print distinct characters of a string . ; Function to print distinct characters in given string str [ ] ; count [ x ] is going to store count of character ' x ' in str . If x is not present , then it is going to store 0. ; index [ x ] is going to store index of character ' x ' in str . If x is not present or x is more than once , then it is going to store a value ( for example , length of string ) that cannot be a valid index in str [ ] ; Initialize counts of all characters and indexes of distinct characters . ; index [ i ] = n ; A value more than any index in str [ ] ; Traverse the input string ; Find current character and increment its count ; If this is first occurrence , then set value in index as index of it . ; If character repeats , then remove it from index [ ] ; Since size of index is constant , below operations take constant time . ; Driver code
let MAX_CHAR = 256 ; function printDistinct ( str ) { let n = str . length ; let count = new Array ( MAX_CHAR ) ; let index = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) { count [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { let x = str [ i ] . charCodeAt ( 0 ) ; ++ count [ x ] ; if ( count [ x ] == 1 && x != ' ' ) index [ x ] = i ; if ( count [ x ] == 2 ) index [ x ] = n ; } index . sort ( function ( a , b ) { return a - b } ) ; for ( let i = 0 ; i < MAX_CHAR && index [ i ] != n ; i ++ ) document . write ( str [ index [ i ] ] ) ; } let str = " " ; printDistinct ( str ) ;
Smallest length string with repeated replacement of two distinct adjacent | Javascript program to find smallest possible length of a string of only three characters ; Program to find length of reduced string in a string made of three characters . ; A memoized function find result recursively . a , b and c are counts of ' a ' s , ' b ' s and ' c ' s in str ; If this subproblem is already evaluated ; If this subproblem is already evaluated ; If there is only one type of character ; If only two types of characters are present ; If all types of characters are present . Try combining all pairs . ; Returns smallest possible length with given operation allowed . ; Counting occurrences of three different characters ' a ' , ' b ' and ' c ' in str ; Initialize DP [ ] [ ] entries as - 1 ; Driver code
let MAX_LEN = 110 ; let DP = new Array ( MAX_LEN ) ; for ( let i = 0 ; i < DP . length ; i ++ ) { DP [ i ] = new Array ( MAX_LEN ) ; for ( let j = 0 ; j < DP [ i ] . length ; j ++ ) { DP [ i ] [ j ] = new Array ( MAX_LEN ) ; for ( let k = 0 ; k < MAX_LEN ; k ++ ) { DP [ i ] [ j ] [ k ] = 0 ; } } } function length ( a , b , c ) { if ( a < 0 b < 0 c < 0 ) if ( DP [ a ] [ b ] != - 1 ) return DP [ a ] [ b ] ; if ( a == 0 && b == 0 ) return ( DP [ a ] [ b ] = c ) ; if ( a == 0 && c == 0 ) return ( DP [ a ] [ b ] = b ) ; if ( b == 0 && c == 0 ) return ( DP [ a ] [ b ] = a ) ; if ( a == 0 ) return ( DP [ a ] [ b ] = length ( a + 1 , b - 1 , c - 1 ) ) ; if ( b == 0 ) return ( DP [ a ] [ b ] = length ( a - 1 , b + 1 , c - 1 ) ) ; if ( c == 0 ) return ( DP [ a ] [ b ] = length ( a - 1 , b - 1 , c + 1 ) ) ; DP [ a ] [ b ] = Math . min ( length ( a - 1 , b - 1 , c + 1 ) , Math . min ( length ( a - 1 , b + 1 , c - 1 ) , length ( a + 1 , b - 1 , c - 1 ) ) ) ; return DP [ a ] [ b ] ; } function stringReduction ( str ) { let n = str . length ; let count = new Array ( 3 ) ; for ( let i = 0 ; i < 3 ; i ++ ) { count [ i ] = 0 ; } for ( let i = 0 ; i < n ; ++ i ) count [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( let i = 0 ; i <= count [ 0 ] ; ++ i ) for ( let j = 0 ; j < count [ 1 ] ; ++ j ) for ( let k = 0 ; k < count [ 2 ] ; ++ k ) DP [ i ] [ j ] [ k ] = - 1 ; return length ( count [ 0 ] , count [ 1 ] , count [ 2 ] ) ; } let str = " " ; document . write ( stringReduction ( str ) ) ;
Equilibrium index of an array | function to find the equilibrium index ; Check for indexes one by one until an equilibrium index is found ; get left sum ; get right sum ; if leftsum and rightsum are same , then we are done ; return - 1 if no equilibrium index is found ; Driver code
function equilibrium ( arr , n ) { var i , j ; var leftsum , rightsum ; for ( i = 0 ; i < n ; ++ i ) { leftsum = 0 ; rightsum = 0 ; for ( let j = 0 ; j < i ; j ++ ) leftsum += arr [ j ] ; for ( let j = i + 1 ; j < n ; j ++ ) rightsum += arr [ j ] ; if ( leftsum == rightsum ) return i ; } return - 1 ; } var arr = new Array ( - 7 , 1 , 5 , 2 , - 4 , 3 , 0 ) ; n = arr . length ; document . write ( equilibrium ( arr , n ) ) ;
Find number of endless points | Javascript program to find count of endless points ; Returns count of endless points ; Fills column matrix . For every column , start from every last row and fill every entry as blockage after a 0 is found . ; flag which will be zero once we get a '0' and it will be 1 otherwise ; encountered a '0' , set the isEndless variable to false ; Similarly , fill row matrix ; Calculate total count of endless points ; If there is NO blockage in row or column after this point , increment result . ; Driver code
let MAX = 100 ; function countEndless ( input , n ) { let row = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { row [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) { row [ i ] [ j ] = false ; } } let col = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { col [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) { col [ i ] [ j ] = false ; } } for ( let j = 0 ; j < n ; j ++ ) { let isEndless = true ; for ( let i = n - 1 ; i >= 0 ; i -- ) { if ( input [ i ] [ j ] == false ) isEndless = false ; col [ i ] [ j ] = isEndless ; } } for ( let i = 0 ; i < n ; i ++ ) { let isEndless = true ; for ( let j = n - 1 ; j >= 0 ; j -- ) { if ( input [ i ] [ j ] == false ) isEndless = false ; row [ i ] [ j ] = isEndless ; } } let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = 1 ; j < n ; j ++ ) if ( row [ i ] [ j ] && col [ i ] [ j ] ) ans ++ ; return ans ; } let input = [ [ true , false , true , true ] , [ false , true , true , true ] , [ true , true , true , true ] , [ false , true , true , false ] ] ; let n = 4 ; document . write ( countEndless ( input , n ) ) ;
Equilibrium index of an array | function to find the equilibrium index ; initialize sum of whole array ; initialize leftsum ; Find sum of the whole array ; sum is now right sum for index i ; If no equilibrium index found , then return 0 ; Driver code
function equilibrium ( arr , n ) { sum = 0 ; leftsum = 0 ; for ( let i = 0 ; i < n ; ++ i ) sum += arr [ i ] ; for ( let i = 0 ; i < n ; ++ i ) { sum -= arr [ i ] ; if ( leftsum == sum ) return i ; leftsum += arr [ i ] ; } return - 1 ; } arr = new Array ( - 7 , 1 , 5 , 2 , - 4 , 3 , 0 ) ; n = arr . length ; document . write ( " " + equilibrium ( arr , n ) ) ;
Sum of all substrings of a string representing a number | Set 1 | Returns sum of all substring of num ; allocate memory equal to length of string ; initialize first value with first digit ; loop over all digits of string ; update each sumofdigit from previous value ; add current value to the result ; Driver code
function sumOfSubstrings ( num ) { let n = num . length ; let sumofdigit = new Array ( n ) ; sumofdigit [ 0 ] = num [ 0 ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; let res = sumofdigit [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { let numi = num [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; sumofdigit [ i ] = ( i + 1 ) * numi + 10 * sumofdigit [ i - 1 ] ; res += sumofdigit [ i ] ; } return res ; } let num = " " ; document . write ( sumOfSubstrings ( num ) ) ;
Sum of all substrings of a string representing a number | Set 1 | Utility method to convert character digit to integer digit ; Returns sum of all subString of num ; Storing prev value ; SubStrings sum upto current index loop over all digits of String ; Update each sumofdigit from previous value ; Add current value to the result ; Update previous ; Driver Code
function toDigit ( ch ) { return ( ch - ' ' ) ; } function sumOfSubStrings ( num ) { let n = num . length ; let prev = toDigit ( num [ 0 ] ) ; let res = prev ; let current = 0 ; for ( let i = 1 ; i < n ; i ++ ) { let numi = toDigit ( num [ i ] ) ; current = ( i + 1 ) * numi + 10 * prev ; res += current ; prev = current ; } return res ; } let num = " " ; document . write ( sumOfSubStrings ( num ) + " " ) ;
Equilibrium index of an array | Program to find equilibrium index of an array ; Taking the prefixsum from front end array ; Taking the prefixsum from back end of array ; Checking if forward prefix sum is equal to rev prefix sum ; If You want all the points of equilibrium create vector and push all equilibrium points in it and return the vector ; Driver code
function equilibrium ( a , n ) { if ( n == 1 ) return ( 0 ) ; var forward = new Array ( 0 ) ; var rev = new Array ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( i ) { forward [ i ] = forward [ i - 1 ] + a [ i ] ; } else { forward [ i ] = a [ i ] ; } } for ( let i = n - 1 ; i > 0 ; i -- ) { if ( i <= n - 2 ) { rev [ i ] = rev [ i + 1 ] + a [ i ] ; } else { rev [ i ] = a [ i ] ; } } for ( let i = 0 ; i < n ; i ++ ) { if ( forward [ i ] == rev [ i ] ) { return i ; } } return - 1 ; } arr = new Array ( - 7 , 1 , 5 , 2 , - 4 , 3 , 0 ) ; n = arr . length ; document . write ( " " + equilibrium ( arr , n ) + " " ) ;
Leaders in an array | Javascript Function to print leaders in an array ; the loop didn 't break ; driver code ; Function calling
function printLeaders ( arr , size ) { for ( let i = 0 ; i < size ; i ++ ) { let j ; for ( j = i + 1 ; j < size ; j ++ ) { if ( arr [ i ] <= arr [ j ] ) break ; } if ( j == size ) document . write ( arr [ i ] + " " ) ; } } let arr = [ 16 , 17 , 4 , 3 , 5 , 2 ] ; let n = arr . length ; printLeaders ( arr , n ) ;
Minimize cost to reach end of an N | Function to calculate the minimum cost required to reach the end of Line ; Checks if possible to reach end or not ; Stores the stations and respective rate of fuel ; Stores the station index and cost of fuel and litres of petrol which is being fueled ; Iterate through the entire line ; Check if there is a station at current index ; Remove all the stations where fuel cannot be pumped ; If there is no station left to fill fuel in tank , it is not possible to reach end ; Stores the best station visited so far ; Pump fuel from the best station ; Update the count of litres taken from that station ; Update the bunk in queue ; Print the cost ; Given value of N , K & M ; Given arrays ; Function call to calculate minimum cost to reach end of the line
function minCost ( N , K , M , a , b ) { var flag = true ; var map = new Map ( ) ; for ( var i = 0 ; i < M ; i ++ ) { map . set ( a [ i ] , b [ i ] ) ; if ( i == M - 1 && K < N - a [ i ] ) { flag = false ; break ; } else if ( i < M - 1 && K < a [ i + 1 ] - a [ i ] ) { flag = false ; break ; } } if ( ! flag ) { document . write ( - 1 ) ; return ; } var pq = [ ] ; var cost = 0 ; flag = false ; for ( var i = 0 ; i < N ; i ++ ) { if ( map . has ( i ) ) { var arr = [ i , map . get ( i ) , 0 ] ; pq . push ( arr ) ; } pq . sort ( ) ; while ( pq . length > 0 && pq [ pq . length - 1 ] [ 2 ] == K ) pq . pop ( ) ; if ( pq . length == 0 ) { flag = true ; break ; } var best_bunk = pq [ pq . length - 1 ] ; pq . pop ( ) ; cost += best_bunk [ 1 ] ; best_bunk [ 2 ] ++ ; pq . push ( best_bunk ) ; pq . sort ( ) ; } if ( flag ) { document . write ( - 1 + " " ) ; return ; } document . write ( cost + " " ) ; } var N = 10 , K = 3 , M = 4 ; var a = [ 0 , 1 , 4 , 6 ] ; var b = [ 5 , 2 , 2 , 4 ] ; minCost ( N , K , M , a , b ) ;
Unbounded Knapsack ( Repetition of items allowed ) | Javascript program to find maximum achievable value with a knapsack of weight W and multiple instances allowed . ; Returns the maximum value with knapsack of W capacity ; dp [ i ] is going to store maximum value with knapsack capacity i . ; Fill dp [ ] using above recursive formula ; Driver program
function max ( i , j ) { return ( i > j ) ? i : j ; } function unboundedKnapsack ( W , n , val , wt ) { let dp = new Array ( W + 1 ) ; dp . fill ( 0 ) ; for ( let i = 0 ; i <= W ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( wt [ j ] <= i ) { dp [ i ] = Math . max ( dp [ i ] , dp [ i - wt [ j ] ] + val [ j ] ) ; } } } return dp [ W ] ; } let W = 100 ; let val = [ 10 , 30 , 20 ] ; let wt = [ 5 , 10 , 15 ] ; let n = val . length ; document . write ( unboundedKnapsack ( W , n , val , wt ) ) ;
Ceiling in a sorted array | Function to get index of ceiling of x in arr [ low . . high ] ; If x is smaller than or equal to first element , then return the first element ; Otherwise , linearly search for ceil value ; if x lies between arr [ i ] and arr [ i + 1 ] including arr [ i + 1 ] , then return arr [ i + 1 ] ; If we reach here then x is greater than the last element of the array , return - 1 in this case ; driver code
function ceilSearch ( arr , low , high , x ) { let i ; if ( x <= arr [ low ] ) return low ; for ( i = low ; i < high ; i ++ ) { if ( arr [ i ] == x ) return i ; if ( arr [ i ] < x && arr [ i + 1 ] >= x ) return i + 1 ; } return - 1 ; } let arr = [ 1 , 2 , 8 , 10 , 10 , 12 , 19 ] ; let n = arr . length ; let x = 3 ; let index = ceilSearch ( arr , 0 , n - 1 , x ) ; if ( index == - 1 ) document . write ( " " + x + " " ) ; else document . write ( " " + x + " " + arr [ index ] ) ;
Maximum sum subarray removing at most one element | Method returns maximum sum of all subarray where removing one element is also allowed ; Maximum sum subarrays in forward and backward directions ; Initialize current max and max so far . ; calculating maximum sum subarrays in forward direction ; Storing current maximum till ith , in forward array ; Calculating maximum sum subarrays in backward direction ; Storing current maximum from ith , in backward array ; Initializing final ans by max_so_far so that , case when no element is removed to get max sum subarray is also handled ; Choosing maximum ignoring ith element ; Driver Code
function maxSumSubarrayRemovingOneEle ( arr , n ) { let fw = [ ] ; let bw = [ ] ; let cur_max = arr [ 0 ] , max_so_far = arr [ 0 ] ; fw [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { cur_max = Math . max ( arr [ i ] , cur_max + arr [ i ] ) ; max_so_far = Math . max ( max_so_far , cur_max ) ; fw [ i ] = cur_max ; } cur_max = max_so_far = bw [ n - 1 ] = arr [ n - 1 ] ; for ( let i = n - 2 ; i >= 0 ; i -- ) { cur_max = Math . max ( arr [ i ] , cur_max + arr [ i ] ) ; max_so_far = Math . max ( max_so_far , cur_max ) ; bw [ i ] = cur_max ; } let fans = max_so_far ; for ( let i = 1 ; i < n - 1 ; i ++ ) fans = Math . max ( fans , fw [ i - 1 ] + bw [ i + 1 ] ) ; return fans ; } let arr = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ] ; let n = arr . length ; document . write ( maxSumSubarrayRemovingOneEle ( arr , n ) ) ;
Ceiling in a sorted array | Function to get index of ceiling of x in arr [ low . . high ] ; If x is smaller than or equal to the first element , then return the first element ; If x is greater than the last element , then return - 1 ; get the index of middle element of arr [ low . . high ] ; If x is same as middle element , then return mid ; If x is greater than arr [ mid ] , then either arr [ mid + 1 ] is ceiling of x or ceiling lies in arr [ mid + 1. . . high ] ; If x is smaller than arr [ mid ] , then either arr [ mid ] is ceiling of x or ceiling lies in arr [ low ... . mid - 1 ] ; Driver Code
function ceilSearch ( arr , low , high , x ) { let mid ; if ( x <= arr [ low ] ) return low ; if ( x > arr [ high ] ) return - 1 ; mid = ( low + high ) / 2 ; if ( arr [ mid ] == x ) return mid ; else if ( arr [ mid ] < x ) { if ( mid + 1 <= high && x <= arr [ mid + 1 ] ) return mid + 1 ; else return ceilSearch ( arr , mid + 1 , high , x ) ; } else { if ( mid - 1 >= low && x > arr [ mid - 1 ] ) return mid ; else return ceilSearch ( arr , low , mid - 1 , x ) ; } } let arr = [ 1 , 2 , 8 , 10 , 10 , 12 , 19 ] ; let n = arr . length ; let x = 20 ; let index = ceilSearch ( arr , 0 , n - 1 , x ) ; if ( index == - 1 ) { document . write ( ` ${ x } ` ) ; } else { document . write ( ` ${ x } ${ arr [ index ] } ` ) ; }
Sum of heights of all individual nodes in a binary tree | A binary tree Node has data , pointer to left child and a pointer to right child ; Compute the " maxHeight " of a particular Node ; compute the height of each subtree ; use the larger one ; Function to sum of heights of individual Nodes Uses Inorder traversal ; Driver code
class Node { constructor ( data ) { this . data = data ; this . left = this . right = null ; } } function getHeight ( Node ) { if ( Node == null ) return 0 ; else { let lHeight = getHeight ( Node . left ) ; let rHeight = getHeight ( Node . right ) ; if ( lHeight > rHeight ) return ( lHeight + 1 ) ; else return ( rHeight + 1 ) ; } } function getTotalHeight ( root ) { if ( root == null ) return 0 ; return getTotalHeight ( root . left ) + getHeight ( root ) + getTotalHeight ( root . right ) ; } let root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; document . write ( " " + getTotalHeight ( root ) ) ;
Path with maximum average value | method returns maximum average of all path of cost matrix ; Initialize first column of total cost ( dp ) array ; Initialize first row of dp array ; Construct rest of the dp array ; divide maximum sum by constant path length : ( 2 N - 1 ) for getting average ; Driver Code
function maxAverageOfPath ( cost , N ) { let dp = new Array ( N + 1 ) ; for ( let i = 0 ; i < N + 1 ; i ++ ) { dp [ i ] = new Array ( N + 1 ) ; for ( let j = 0 ; j < N + 1 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } dp [ 0 ] [ 0 ] = cost [ 0 ] [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + cost [ i ] [ 0 ] ; for ( let j = 1 ; j < N ; j ++ ) dp [ 0 ] [ j ] = dp [ 0 ] [ j - 1 ] + cost [ 0 ] [ j ] ; for ( let i = 1 ; i < N ; i ++ ) for ( let j = 1 ; j < N ; j ++ ) dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) + cost [ i ] [ j ] ; return dp [ N - 1 ] [ N - 1 ] / ( 2 * N - 1 ) ; } let cost = [ [ 1 , 2 , 3 ] , [ 6 , 5 , 4 ] , [ 7 , 3 , 9 ] ] ; document . write ( maxAverageOfPath ( cost , 3 ) ) ;
Maximum weight path ending at any element of last row in a matrix | Function which return the maximum weight path sum ; create 2D matrix to store the sum of the path ; Initialize first column of total weight array ( dp [ i to N ] [ 0 ] ) ; Calculate rest path sum of weight matrix ; find the max weight path sum to reach the last row ; return maximum weight path sum ; Driver program
function maxCost ( mat , N ) { let dp = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { dp [ i ] = new Array ( N ) ; for ( let j = 0 ; j < N ; j ++ ) { dp [ i ] [ j ] = 0 ; } } dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) dp [ i ] [ 0 ] = mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) for ( let j = 1 ; j < i + 1 && j < N ; j ++ ) dp [ i ] [ j ] = mat [ i ] [ j ] + Math . max ( dp [ i - 1 ] [ j - 1 ] , dp [ i - 1 ] [ j ] ) ; let result = 0 ; for ( let i = 0 ; i < N ; i ++ ) if ( result < dp [ N - 1 ] [ i ] ) result = dp [ N - 1 ] [ i ] ; return result ; } let mat = [ [ 4 , 1 , 5 , 6 , 1 ] , [ 2 , 9 , 2 , 11 , 10 ] , [ 15 , 1 , 3 , 15 , 2 ] , [ 16 , 92 , 41 , 4 , 3 ] , [ 8 , 142 , 6 , 4 , 8 ] ] ; let N = 5 ; document . write ( " " + maxCost ( mat , N ) ) ;
Number of permutation with K inversions | Limit on N and K ; 2D array memo for stopping solving same problem again ; Method recursively calculates permutation with K inversion ; base cases ; If already solved then return result directly ; Calling recursively all subproblem of permutation size N - 1 ; Call recursively only if total inversion to be made are less than size ; Store result into memo ; Driver code
let M = 100 ; let memo = new Array ( M ) ; for ( let i = 0 ; i < M ; i ++ ) { memo [ i ] = new Array ( M ) ; for ( let j = 0 ; j < M ; j ++ ) { memo [ i ] [ j ] = 0 ; } } function numberOfPermWithKInversion ( N , K ) { if ( N == 0 ) return 0 ; if ( K == 0 ) return 1 ; if ( memo [ N ] [ K ] != 0 ) return memo [ N ] [ K ] ; let sum = 0 ; for ( let i = 0 ; i <= K ; i ++ ) { if ( i <= N - 1 ) sum += numberOfPermWithKInversion ( N - 1 , K - i ) ; } memo [ N ] [ K ] = sum ; return sum ; } let N = 4 ; let K = 2 ; document . write ( numberOfPermWithKInversion ( N , K ) ) ;
A Space Optimized DP solution for 0 | val [ ] is for storing maximum profit for each weight wt [ ] is for storing weights n number of item W maximum capacity of bag mat [ 2 ] [ W + 1 ] to store final result ; matrix to store final result ; iterate through all items ; one by one traverse each element ; traverse all weights j <= W ; if i is odd that mean till now we have odd number of elements so we store result in 1 th indexed row ; check for each value ; include element ; exclude element ; if i is even that means till now we have even number of elements so we store result in 0 th indexed row ; Return mat [ 0 ] [ W ] if n is odd , else mat [ 1 ] [ W ] ; Driver Code
function KnapSack ( val , wt , n , W ) { let mat = new Array ( 2 ) ; for ( let i = 0 ; i < 2 ; i ++ ) { mat [ i ] = new Array ( W + 1 ) ; } for ( let i = 0 ; i < 2 ; i ++ ) { for ( let j = 0 ; j < W + 1 ; j ++ ) { mat [ i ] [ j ] = 0 ; } } let i = 0 ; while ( i < n ) { let j = 0 ; if ( i % 2 != 0 ) { while ( ++ j <= W ) { if ( wt [ i ] <= j ) { mat [ 1 ] [ j ] = Math . max ( val [ i ] + mat [ 0 ] [ j - wt [ i ] ] , mat [ 0 ] [ j ] ) ; } else { mat [ 1 ] [ j ] = mat [ 0 ] [ j ] ; } } } else { while ( ++ j <= W ) { if ( wt [ i ] <= j ) { mat [ 0 ] [ j ] = Math . max ( val [ i ] + mat [ 1 ] [ j - wt [ i ] ] , mat [ 1 ] [ j ] ) ; } else { mat [ 0 ] [ j ] = mat [ 1 ] [ j ] ; } } } i ++ ; } return ( n % 2 != 0 ) ? mat [ 0 ] [ W ] : mat [ 1 ] [ W ] ; } let val = [ 7 , 8 , 4 ] ; let wt = [ 3 , 8 , 6 ] ; let W = 10 , n = 3 ; document . write ( KnapSack ( val , wt , n , W ) ) ;
Maximum profit by buying and selling a share at most k times | Function to find out maximum profit by buying & selling / a share atmost k times given stock price of n days ; table to store results of subproblems profit [ t ] [ i ] stores maximum profit using atmost t transactions up to day i ( including day i ) ; For day 0 , you can 't earn money  irrespective of how many times you trade ; profit is 0 if we don 't do any  transaction (i.e. k =0) ; fill the table in bottom - up fashion ; Driver code
function maxProfit ( price , n , k ) { var profit = Array ( k + 1 ) . fill ( 0 ) . map ( x => Array ( n + 1 ) . fill ( 0 ) ) ; for ( var i = 0 ; i <= k ; i ++ ) profit [ i ] [ 0 ] = 0 ; for ( j = 0 ; j <= n ; j ++ ) profit [ 0 ] [ j ] = 0 ; for ( var i = 1 ; i <= k ; i ++ ) { var prevDiff = - Number . MAX_VALUE ; for ( var j = 1 ; j < n ; j ++ ) { prevDiff = Math . max ( prevDiff , profit [ i - 1 ] [ j - 1 ] - price [ j - 1 ] ) ; profit [ i ] [ j ] = Math . max ( profit [ i ] [ j - 1 ] , price [ j ] + prevDiff ) ; } } return profit [ k ] [ n - 1 ] ; } var k = 3 ; var price = [ 12 , 14 , 17 , 10 , 14 , 13 , 12 , 15 ] ; var n = price . length ; document . write ( " " + maxProfit ( price , n , k ) ) ;
Count even length binary sequences with same sum of first and second half bits | diff is difference between sums first n bits and last n bits respectively ; We can 't cover difference of more  than n with 2n bits ; n == 1 , i . e . , 2 bit long sequences ; First bit is 0 & last bit is 1 ; First and last bits are same ; First bit is 1 & last bit is 0 ; Driver program
function countSeq ( n , diff ) { if ( Math . abs ( diff ) > n ) return 0 ; if ( n == 1 && diff == 0 ) return 2 ; if ( n == 1 && Math . abs ( diff ) == 1 ) return 1 ; let res = countSeq ( n - 1 , diff + 1 ) + 2 * countSeq ( n - 1 , diff ) + countSeq ( n - 1 , diff - 1 ) ; return res ; } let n = 2 ; document . write ( " " + countSeq ( 2 , 0 ) ) ;