codeparrot/xlcost-text-to-code huggingface dataset:
Partition of a set into K subsets with equal sum using BitMask and DP | Function to check whether K required partitions are possible or not ; Return true as the entire array is the answer ; If total number of partitions exceeds size of the array ; If the array sum is not divisible by K ; No such partitions are possible ; Required sum of each subset ; Initialize dp array with - 1 ; Sum of empty subset is zero ; Iterate over all subsets / masks ; if current mask is invalid , continue ; Iterate over all array elements ; Check if the current element can be added to the current subset / mask ; Transition ; Driver Code
function isKPartitionPossible ( arr , N , K ) { if ( K == 1 ) return true ; if ( N < K ) return false ; let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum % K != 0 ) return false ; let target = sum / K ; let dp = Array . from ( { length : ( 1 << 15 ) } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < ( 1 << N ) ; i ++ ) dp [ i ] = - 1 ; dp [ 0 ] = 0 ; for ( let mask = 0 ; mask < ( 1 << N ) ; mask ++ ) { if ( dp [ mask ] == - 1 ) continue ; for ( let i = 0 ; i < N ; i ++ ) { if ( ( ( mask & ( 1 << i ) ) == 0 ) && dp [ mask ] + arr [ i ] <= target ) { dp [ mask ( 1 << i ) ] = ( dp [ mask ] + arr [ i ] ) % target ; } } } if ( dp [ ( 1 << N ) - 1 ] == 0 ) return true ; else return false ; } let arr = [ 2 , 1 , 4 , 5 , 3 , 3 ] ; let N = arr . length ; let K = 3 ; if ( isKPartitionPossible ( arr , N , K ) ) { document . write ( " " ) ; document . write ( " " ) ; } else { document . write ( " " ) ; document . write ( " " ) ; }
Minimum cost path in a Matrix by moving only on value difference of X | Javascript implementation to find the minimum number of operations required to move from ( 1 , 1 ) to ( N , M ) ; Function to find the minimum operations required to move to bottom - right cell of matrix ; Condition to check if the current cell is the bottom - right cell of the matrix ; Condition to check if the current cell is out of matrix ; Condition to check if the current indices is already computed ; Condition to check that the movement with the current value is not possible ; Recursive call to compute the number of operation required to compute the value ; Function to find the minimum number of operations required to reach the bottom - right cell ; Loop to iterate over every possible cell of the matrix ; Driver Code ; Function call
let MAX = 1e18 ; let n , m ; let v = new Array ( 151 ) ; for ( let i = 0 ; i < 151 ; i ++ ) { v [ i ] = [ 0 , 0 ] ; } let dp = new Array ( 151 ) ; for ( let i = 0 ; i < 151 ; i ++ ) { dp [ i ] = new Array ( 151 ) ; } function min_operation ( i , j , val , x ) { if ( i == n - 1 && j == m - 1 ) { if ( val > v [ i ] [ j ] ) { return dp [ i ] [ j ] = MAX ; } else { return dp [ i ] [ j ] = v [ i ] [ j ] - val ; } } if ( i == n j == m ) { return dp [ i ] [ j ] = MAX ; } if ( dp [ i ] [ j ] != - 1 ) { return dp [ i ] [ j ] ; } if ( val > v [ i ] [ j ] ) { return dp [ i ] [ j ] = MAX ; } let tmp = v [ i ] [ j ] - val ; tmp += Math . min ( min_operation ( i + 1 , j , val + x , x ) , min_operation ( i , j + 1 , val + x , x ) ) ; return dp [ i ] [ j ] = tmp ; } function solve ( x ) { let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { let val = v [ i ] [ j ] - x * ( i + j ) ; for ( let k = 0 ; k < dp . length ; k ++ ) for ( let l = 0 ; l < dp [ k ] . length ; l ++ ) dp [ k ] [ l ] = - 1 ; val = min_operation ( 0 , 0 , val , x ) ; ans = Math . min ( ans , val ) ; } } return ans ; } let x = 3 ; n = 2 ; m = 2 ; v [ 0 ] = [ 15 , 153 ] ; v [ 1 ] = [ 135 , 17 ] ; document . write ( solve ( x ) + " " ) ;
Minimum count of numbers required from given array to represent S | Function to find the minimum elements required to get the sum of given value S ; Condition if the sequence is found ; Condition when no such sequence found ; Calling for without choosing the current index value ; Calling for after choosing the current index value ; Function for every array ; Driver Code
function prletAllSubsetsRec ( arr , n , v , sum ) { if ( sum == 0 ) { return v . length ; } if ( sum < 0 ) return Number . MAX_VALUE ; if ( n == 0 ) return Number . MAX_VALUE ; let x = prletAllSubsetsRec ( arr , n - 1 , v , sum ) ; v . push ( arr [ n - 1 ] ) ; let y = prletAllSubsetsRec ( arr , n , v , sum - arr [ n - 1 ] ) ; v . pop ( v . length - 1 ) ; return Math . min ( x , y ) ; } function prletAllSubsets ( arr , n , sum ) { let v = [ ] ; return prletAllSubsetsRec ( arr , n , v , sum ) ; } let arr = [ 2 , 1 , 4 , 3 , 5 , 6 ] ; let sum = 6 ; let n = arr . length ; document . write ( prletAllSubsets ( arr , n , sum ) ) ;
Minimum count of numbers required from given array to represent S | Function to find the count of minimum length of the sequence ; Loop to initialize the array as infinite in the row 0 ; Loop to find the solution by pre - computation for the sequence ; Minimum possible for the previous minimum value of the sequence ; Driver Code
function Count ( S , m , n ) { let table = [ ] ; for ( let i = 0 ; i < m + 1 ; i ++ ) { table [ i ] = [ ] ; for ( let j = 0 ; j < n + 1 ; j ++ ) { table [ i ] [ j ] = 0 ; } } for ( let i = 1 ; i <= n ; i ++ ) { table [ 0 ] [ i ] = Number . MAX_VALUE - 1 ; } for ( let i = 1 ; i <= m ; i ++ ) { for ( let j = 1 ; j <= n ; j ++ ) { if ( S [ i - 1 ] > j ) { table [ i ] [ j ] = table [ i - 1 ] [ j ] ; } else { table [ i ] [ j ] = Math . min ( table [ i - 1 ] [ j ] , table [ i ] [ j - S [ i - 1 ] ] + 1 ) ; } } } return table [ m ] [ n ] ; } let arr = [ 9 , 6 , 5 , 1 ] ; let m = arr . length ; document . write ( Count ( arr , m , 11 ) ) ;
Maximize product of same | Javascript implementation to maximize product of same - indexed elements of same size subsequences ; Utility function to find the maximum ; Utility function to find the maximum scalar product from the equal length sub - sequences taken from the two given array ; Return a very small number if index is invalid ; If the sub - problem is already evaluated , then return it ; Take the maximum of all the recursive cases ; Function to find maximum scalar product from same size sub - sequences taken from the two given array ; Initialize a 2 - D array for memoization ; Driver Code
let INF = 10000000 ; function maximum ( A , B , C , D ) { return Math . max ( Math . max ( A , B ) , Math . max ( C , D ) ) ; } function maxProductUtil ( X , Y , A , B , dp ) { if ( X < 0 Y < 0 ) return - INF ; if ( dp [ X ] [ Y ] != - 1 ) return dp [ X ] [ Y ] ; dp [ X ] [ Y ] = maximum ( A [ X ] * B [ Y ] + maxProductUtil ( X - 1 , Y - 1 , A , B , dp ) , A [ X ] * B [ Y ] , maxProductUtil ( X - 1 , Y , A , B , dp ) , maxProductUtil ( X , Y - 1 , A , B , dp ) ) ; return dp [ X ] [ Y ] ; } function maxProduct ( A , N , B , M ) { let dp = new Array ( N ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } return maxProductUtil ( N - 1 , M - 1 , A , B , dp ) ; } let a = [ - 2 , 6 , - 2 , - 5 ] ; let b = [ - 3 , 4 , - 2 , 8 ] ; let n = a . length ; let m = b . length ; document . write ( maxProduct ( a , n , b , m ) ) ;
Check if one string can be converted to other using given operation | Function that prints whether is it possible to make a string equal to T by performing given operations ; Base case , if we put the last character at front of A ; Base case , if we put the last character at back of A ; Condition if current sequence is matchable ; Condition for front move to ( i - 1 ) th character ; Condition for back move to ( i - 1 ) th character ; Condition if it is possible to make string A equal to string T ; Print final answer ; Driver Code
function twoStringsEquality ( s , t ) { var n = s . length ; var dp = Array . from ( Array ( n ) , ( ) => Array ( n + 1 ) . fill ( 0 ) ) ; if ( s [ n - 1 ] == t [ 0 ] ) dp [ n - 1 ] [ 1 ] = 1 ; if ( s [ n - 1 ] == t [ n - 1 ] ) dp [ n - 1 ] [ 0 ] = 1 ; for ( var i = n - 1 ; i > 0 ; i -- ) { for ( var j = 0 ; j <= n - i ; j ++ ) { if ( dp [ i ] [ j ] ) { if ( s [ i - 1 ] == t [ j ] ) dp [ i - 1 ] [ j + 1 ] = 1 ; if ( s [ i - 1 ] == t [ i + j - 1 ] ) dp [ i - 1 ] [ j ] = 1 ; } } } var ans = false ; for ( var i = 0 ; i <= n ; i ++ ) { if ( dp [ 0 ] [ i ] == 1 ) { ans = true ; break ; } } if ( ans == true ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } var S = " " ; var T = " " ; twoStringsEquality ( S , T ) ;
Maximize sum of all elements which are not a part of the Longest Increasing Subsequence | Function to find maximum sum ; Find total sum of array ; Maintain a 2D array ; Update the dp array along with sum in the second row ; In case of greater length Update the length along with sum ; In case of equal length find length update length with minimum sum ; Find the sum that need to be subtracted from total sum ; Return the sum ; Driver code
function findSum ( arr , n ) { var totalSum = 0 ; for ( var i = 0 ; i < n ; i ++ ) { totalSum += arr [ i ] ; } var dp = Array . from ( Array ( 2 ) , ( ) => Array ( n ) ) ; for ( var i = 0 ; i < n ; i ++ ) { dp [ 0 ] [ i ] = 1 ; dp [ 1 ] [ i ] = arr [ i ] ; } for ( var i = 1 ; i < n ; i ++ ) { for ( var j = 0 ; j < i ; j ++ ) { if ( arr [ i ] > arr [ j ] ) { if ( dp [ 0 ] [ i ] < dp [ 0 ] [ j ] + 1 ) { dp [ 0 ] [ i ] = dp [ 0 ] [ j ] + 1 ; dp [ 1 ] [ i ] = dp [ 1 ] [ j ] + arr [ i ] ; } else if ( dp [ 0 ] [ i ] == dp [ 0 ] [ j ] + 1 ) { dp [ 1 ] [ i ] = Math . min ( dp [ 1 ] [ i ] , dp [ 1 ] [ j ] + arr [ i ] ) ; } } } } var maxm = 0 ; var subtractSum = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( dp [ 0 ] [ i ] > maxm ) { maxm = dp [ 0 ] [ i ] ; subtractSum = dp [ 1 ] [ i ] ; } else if ( dp [ 0 ] [ i ] == maxm ) { subtractSum = Math . min ( subtractSum , dp [ 1 ] [ i ] ) ; } } return totalSum - subtractSum ; } var arr = [ 4 , 6 , 1 , 2 , 3 , 8 ] ; var n = arr . length ; document . write ( findSum ( arr , n ) ) ;
Count of possible permutations of a number represented as a sum of 2 ' s , ▁ 4' s and 6 's only | Returns number of ways to reach score n ; table [ i ] will store count of solutions for value i . ; Initialize all table values as 0 ; Base case ( If given value is 0 , 1 , 2 , or 4 ) ; Driver Code
function count ( n ) { if ( n == 2 ) return 1 ; else if ( n == 4 ) return 2 ; else if ( n == 6 ) return 4 ; let table = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; let i ; for ( i = 0 ; i < n + 1 ; i ++ ) table [ i ] = 0 ; table [ 0 ] = 0 ; table [ 2 ] = 1 ; table [ 4 ] = 2 ; table [ 6 ] = 4 ; for ( i = 8 ; i <= n ; i = i + 2 ) { table [ i ] = table [ i - 2 ] + table [ i - 4 ] + table [ i - 6 ] ; } return table [ n ] ; } let n = 8 ; document . write ( count ( n ) ) ;
Count of ways to split a given number into prime segments | Javascript implementation to count total number of ways to split a string to get prime numbers ; Function to check whether a number is a prime number or not ; Function to find the count of ways to split String into prime numbers ; 1 based indexing ; Consider every suffix up to 6 digits ; Number should not have a leading zero and it should be a prime number ; Return the final result ; Driver code
let MOD = 1000000007 ; function isPrime ( number ) { let num = parseInt ( number ) ; for ( let i = 2 ; i * i <= num ; i ++ ) { if ( ( num % i ) == 0 ) return false ; } return num > 1 ? true : false ; } function countPrimeStrings ( number , i ) { if ( i == 0 ) return 1 ; let cnt = 0 ; for ( let j = 1 ; j <= 6 ; j ++ ) { if ( i - j >= 0 && number [ i - j ] != ' ' && isPrime ( number . substring ( i - j , i ) ) ) { cnt += countPrimeStrings ( number , i - j ) ; cnt %= MOD ; } } return cnt ; } let s1 = " " ; let l = s1 . length ; document . write ( countPrimeStrings ( s1 , l ) ) ;
Count of ways to split a given number into prime segments | Javascript implementation to count total number of ways to split a String to get prime numbers ; Function to build sieve ; If p is a prime ; Update all multiples of p as non prime ; Function to check whether a number is a prime number or not ; Function to find the count of ways to split String into prime numbers ; Number should not have a leading zero and it should be a prime number ; Function to count the number of prime Strings ; Driver code
let MOD = 1000000007 ; let sieve = new Array ( 1000000 ) ; function buildSieve ( ) { for ( let i = 0 ; i < sieve . length ; i ++ ) { sieve [ i ] = true ; } sieve [ 0 ] = false ; sieve [ 1 ] = false ; for ( let p = 2 ; p * p <= 1000000 ; p ++ ) { if ( sieve [ p ] == true ) { for ( let i = p * p ; i < 1000000 ; i += p ) sieve [ i ] = false ; } } } function isPrime ( number ) { let num = parseInt ( number ) ; return sieve [ num ] ; } function rec ( number , i , dp ) { if ( dp [ i ] != - 1 ) return dp [ i ] ; let cnt = 0 ; for ( let j = 1 ; j <= 6 ; j ++ ) { if ( i - j >= 0 && number [ i - j ] != ' ' && isPrime ( number . substring ( i - j , i ) ) ) { cnt += rec ( number , i - j , dp ) ; cnt %= MOD ; } } return dp [ i ] = cnt ; } function countPrimeStrings ( number ) { let n = number . length ; let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i < ( n + 1 ) ; i ++ ) { dp [ i ] = - 1 ; } dp [ 0 ] = 1 ; return rec ( number , n , dp ) ; } buildSieve ( ) ; let s1 = " " ; document . write ( countPrimeStrings ( s1 ) + " " ) ;
Check if a matrix contains a square submatrix with 0 as boundary element | Function checks if square with all 0 's in boundary exists in the matrix ; r1 is the top row , c1 is the left col r2 is the bottom row , c2 is the right ; Function checks if the boundary of the square consists of 0 's ; Driver Code
function squareOfZeroes ( matrix ) { var lastIdx = matrix . length - 1 ; var cache = new Map ( ) ; return hasSquareOfZeroes ( matrix , 0 , 0 , lastIdx , lastIdx , cache ) ; } function hasSquareOfZeroes ( matrix , r1 , c1 , r2 , c2 , cache ) { if ( r1 >= r2 c1 >= c2 ) return false ; var key = ( r1 . toString ( ) ) + ' ' + ( c1 . toString ( ) ) + ' ' + ( r2 . toString ( ) ) + ' ' + ( c2 . toString ( ) ) ; if ( cache . has ( key ) ) return cache . get ( key ) ; cache [ key ] = isSquareOfZeroes ( matrix , r1 , c1 , r2 , c2 ) || hasSquareOfZeroes ( matrix , r1 + 1 , c1 + 1 , r2 - 1 , c2 - 1 , cache ) || hasSquareOfZeroes ( matrix , r1 , c1 + 1 , r2 - 1 , c2 , cache ) || hasSquareOfZeroes ( matrix , r1 + 1 , c1 , r2 , c2 - 1 , cache ) || hasSquareOfZeroes ( matrix , r1 + 1 , c1 + 1 , r2 , c2 , cache ) || hasSquareOfZeroes ( matrix , r1 , c1 , r2 - 1 , c2 - 1 , cache ) ; return cache [ key ] ; } function isSquareOfZeroes ( matrix , r1 , c1 , r2 , c2 ) { for ( var row = r1 ; row < r2 + 1 ; row ++ ) { if ( matrix [ row ] [ c1 ] != 0 matrix [ row ] [ c2 ] != 0 ) return false ; } for ( var col = c1 ; col < c2 + 1 ; col ++ ) { if ( matrix [ r1 ] [ col ] != 0 matrix [ r2 ] [ col ] != 0 ) return false ; } return true ; } var matrix = [ [ 1 , 1 , 1 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 1 ] , [ 0 , 1 , 1 , 1 , 0 , 1 ] , [ 0 , 0 , 0 , 1 , 0 , 1 ] , [ 0 , 1 , 1 , 1 , 0 , 1 ] , [ 0 , 0 , 0 , 0 , 0 , 1 ] ] ; var ans ; ans = squareOfZeroes ( matrix ) ; if ( ans == 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Number of ways to obtain each numbers in range [ 1 , b + c ] by adding any two numbers in range [ a , b ] and [ b , c ] | Javascript program to calculate the number of ways ; Initialising the array with zeros . You can do using memset too . ; Printing the array ; Driver code
function CountWays ( a , b , c ) { let x = b + c + 1 ; let arr = new Array ( x ) ; arr . fill ( 0 ) ; for ( let i = a ; i <= b ; i ++ ) { for ( let j = b ; j <= c ; j ++ ) { arr [ i + j ] ++ ; } } for ( let i = 1 ; i < x ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; } let a = 1 ; let b = 2 ; let c = 2 ; CountWays ( a , b , c ) ;
Number of ways to obtain each numbers in range [ 1 , b + c ] by adding any two numbers in range [ a , b ] and [ b , c ] | Javascript program to calculate the number of ways ; 2 is added because sometimes we will decrease the value out of bounds . ; Initialising the array with zeros . You can do using memset too . ; Printing the array ; Driver code
function CountWays ( a , b , c ) { let x = b + c + 2 ; let arr = new Array ( x ) ; arr . fill ( 0 ) ; for ( let i = 1 ; i <= b ; i ++ ) { arr [ i + b ] ++ ; arr [ i + c + 1 ] -- ; } for ( let i = 1 ; i < x - 1 ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; } let a = 1 ; let b = 2 ; let c = 2 ; CountWays ( a , b , c ) ;
Count of distinct possible strings after performing given operations | Function that prints the number of different strings that can be formed ; Computing the length of the given string ; Base case ; Traverse the given string ; If two consecutive 1 ' s ▁ ▁ or ▁ 2' s are present ; Otherwise take the previous value ; Driver Code
function differentStrings ( s ) { var n = s . length ; var dp = Array ( n + 1 ) ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( var i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] && ( s [ i ] == ' ' s [ i ] == ' ' ) ) dp [ i + 1 ] = dp [ i ] + dp [ i - 1 ] ; else dp [ i + 1 ] = dp [ i ] ; } document . write ( dp [ n ] + " " ) ; } var S = " " ; differentStrings ( S ) ;
Sum of cost of all paths to reach a given cell in a Matrix | Javascript implementation to find the sum of cost of all paths to reach ( M , N ) ; Function for computing combination ; Function to find the factorial of N ; Loop to find the factorial of a given number ; Function for coumputing the sum of all path cost ; Loop to find the contribution of each ( i , j ) in the all possible ways ; Count number of times ( i , j ) visited ; Add the contribution of grid [ i ] [ j ] in the result ; Driver Code ; Function Call
var Col = 3 ; function nCr ( n , r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } function fact ( n ) { var res = 1 ; for ( var i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } function sumPathCost ( grid , m , n ) { var sum = 0 , count ; for ( var i = 0 ; i <= m ; i ++ ) { for ( var j = 0 ; j <= n ; j ++ ) { count = nCr ( i + j , i ) * nCr ( m + n - i - j , m - i ) ; sum += count * grid [ i ] [ j ] ; } } return sum ; } var m = 2 ; var n = 2 ; var grid = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; document . write ( sumPathCost ( grid , m , n ) ) ;
Count numbers in a range with digit sum divisible by K having first and last digit different | Javascript program to count numbers in a range with digit sum divisible by K having first and last digit different ; For calculating the upper bound of the sequence . ; Checking whether the sum of digits = 0 or not and the corner case as number equal to 1 ; If the state is visited then return the answer of this state directly . ; For checking whether digit to be placed is up to upper bound at the position pos or upto 9 ; Calculating new digit sum modulo k ; Check if there is a prefix of 0 s and current digit != 0 ; Then current digit will be the starting digit ; Update the bool flag that the starting digit has been found ; At n - 1 , check if current digit and starting digit are the same then no need to calculate this answer . ; Else find the answer ; Function to find the required count ; Setting up the upper bound ; Calculating F ( R ) ; Setting up the upper bound ; calculating F ( L - 1 ) ; Driver code
var K ; var N ; var v = [ ] ; var dp = Array . from ( Array ( 20 ) ) ; function init ( x ) { for ( var i = 0 ; i < 20 ; i ++ ) { dp [ i ] = Array . from ( Array ( 1000 ) , ( ) => Array ( 10 ) ) ; for ( var j = 0 ; j < 1000 ; j ++ ) { for ( var k = 0 ; k < 10 ; k ++ ) { dp [ i ] [ j ] [ k ] = Array . from ( Array ( 2 ) , ( ) => Array ( 2 ) . fill ( - 1 ) ) ; } } } v = [ ] ; while ( x > 0 ) { v . push ( x % 10 ) ; x = parseInt ( x / 10 ) ; } v . reverse ( ) ; N = v . length ; } function fun ( pos , sum , st , check , f ) { if ( pos == N ) { return ( sum == 0 && check == 1 ) ? 1 : 0 ; } if ( dp [ pos ] [ sum ] [ st ] [ check ] [ f ] != - 1 ) return dp [ pos ] [ sum ] [ st ] [ check ] [ f ] ; var lmt = 9 ; if ( f == 0 ) lmt = v [ pos ] ; var ans = 0 ; for ( var digit = 0 ; digit <= lmt ; digit ++ ) { var nf = f ; var new_sum = ( sum + digit ) % K ; var new_check = check ; var new_st = st ; if ( f == 0 && digit < lmt ) nf = 1 ; if ( check == 0 && digit != 0 ) { new_st = digit ; new_check = 1 ; } if ( pos == N - 1 && new_st == digit ) continue ; ans += fun ( pos + 1 , new_sum , new_st , new_check , nf ) ; } return dp [ pos ] [ sum ] [ st ] [ check ] [ f ] = ans ; } function findCount ( L , R , K ) { init ( R ) ; var r_ans = fun ( 0 , 0 , 0 , 0 , 0 ) ; init ( L - 1 ) ; var l_ans = fun ( 0 , 0 , 0 , 0 , 0 ) ; document . write ( r_ans - l_ans ) ; } var L = 10 ; var R = 20 ; K = 2 ; findCount ( L , R , K ) ;
Split a binary string into K subsets minimizing sum of products of occurrences of 0 and 1 | Function to return the minimum sum of products of occurences of 0 and 1 in each segments ; Store the length of the string ; Not possible to generate subsets greater than the length of string ; If the number of subsets equals the length ; Precompute the sum of products for all index ; Calculate the minimum sum of products for K subsets ; Driver code
function minSumProd ( S , K ) { let len = S . length ; if ( K > len ) return - 1 ; if ( K == len ) return 0 ; let dp = new Array ( len ) ; let count_zero = 0 , count_one = 0 ; for ( let j = 0 ; j < len ; j ++ ) { ( S [ j ] == ' ' ) ? count_zero ++ : count_one ++ ; dp [ j ] = count_zero * count_one ; } for ( let i = 1 ; i < K ; i ++ ) { for ( let j = len ; j >= i ; j -- ) { count_zero = 0 , count_one = 0 ; dp [ j ] = Number . MAX_SAFE_INTEGER ; for ( let k = j ; k >= i ; k -- ) { ( S [ k ] == ' ' ) ? count_zero ++ : count_one ++ ; dp [ j ] = Math . min ( dp [ j ] , count_zero * count_one + dp [ k - 1 ] ) ; } } } return dp [ len - 1 ] ; } let S = " " ; let K = 5 ; document . write ( minSumProd ( S , K ) ) ;
Minimize prize count required such that smaller value gets less prize in an adjacent pair | Function to find the minimum number of required such that adjacent smaller elements gets less number of prizes ; Loop to compute the smaller elements at the left ; Loop to find the smaller elements at the right ; Loop to find the minimum prizes required ; Driver code
function minPrizes ( arr , n ) { let dpLeft = new Array ( n ) ; dpLeft [ 0 ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) { dpLeft [ i ] = dpLeft [ i - 1 ] + 1 ; } else { dpLeft [ i ] = 1 ; } } let dpRight = new Array ( n ) ; dpRight [ n - 1 ] = 1 ; for ( let i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > arr [ i + 1 ] ) { dpRight [ i ] = dpRight [ i + 1 ] + 1 ; } else { dpRight [ i ] = 1 ; } } let totalPrizes = 0 ; for ( let i = 0 ; i < n ; i ++ ) { totalPrizes += Math . max ( dpLeft [ i ] , dpRight [ i ] ) ; } document . write ( totalPrizes + " " ) ; return 0 ; } let arr = [ 1 , 2 , 2 , 3 ] ; let n = arr . length ; minPrizes ( arr , n ) ;
Maximum sum subsequence with values differing by at least 2 | function to find maximum sum subsequence such that two adjacent values elements are not selected ; map to store the frequency of array elements ; make a dp array to store answer upto i th value ; base cases ; iterate for all possible values of arr [ i ] ; return the last value ; Driver function
function get_max_sum ( arr , n ) { var freq = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( freq . has ( arr [ i ] ) ) freq . set ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) else freq . set ( arr [ i ] , 1 ) } var dp = Array ( 100001 ) . fill ( 0 ) ; dp [ 0 ] = 0 ; dp [ 1 ] = ( freq . has ( 0 ) ? freq . get ( 0 ) : 0 ) ; for ( var i = 2 ; i <= 100000 ; i ++ ) { dp [ i ] = Math . max ( dp [ i - 1 ] , dp [ i - 2 ] + i * ( freq . has ( i ) ? freq . get ( i ) : 0 ) ) ; } return dp [ 100000 ] ; } var N = 3 ; var arr = [ 2 , 2 , 3 ] ; document . write ( get_max_sum ( arr , N ) ) ;