codeparrot/xlcost-text-to-code huggingface dataset:
Number of ways to insert a character to increase the LCS by one | JavaScript Program for Number of ways to insert a character to increase LCS by one ; Return the Number of ways to insert a character to increase the Longest Common Subsequence by one ; Insert all positions of all characters in string B . ; Longest Common Subsequence ; Longest Common Subsequence from reverse ; inserting character between position i and i + 1 ; Driver Code
let MAX = 256 ; function numberofways ( A , B , N , M ) { let pos = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) pos [ i ] = [ ] ; for ( let i = 0 ; i < M ; i ++ ) pos [ B [ i ] . charCodeAt ( 0 ) ] . push ( i + 1 ) ; let dpl = new Array ( N + 2 ) ; for ( let i = 0 ; i < N + 2 ; i ++ ) { dpl [ i ] = new Array ( M + 2 ) ; for ( let j = 0 ; j < M + 2 ; j ++ ) dpl [ i ] [ j ] = 0 ; } for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = 1 ; j <= M ; j ++ ) { if ( A [ i - 1 ] == B [ j - 1 ] ) dpl [ i ] [ j ] = dpl [ i - 1 ] [ j - 1 ] + 1 ; else dpl [ i ] [ j ] = Math . max ( dpl [ i - 1 ] [ j ] , dpl [ i ] [ j - 1 ] ) ; } } let LCS = dpl [ N ] [ M ] ; let dpr = new Array ( N + 2 ) ; for ( let i = 0 ; i < N + 2 ; i ++ ) { dpr [ i ] = new Array ( M + 2 ) ; for ( let j = 0 ; j < M + 2 ; j ++ ) dpr [ i ] [ j ] = 0 ; } for ( let i = N ; i >= 1 ; i -- ) { for ( let j = M ; j >= 1 ; j -- ) { if ( A [ i - 1 ] == B [ j - 1 ] ) dpr [ i ] [ j ] = dpr [ i + 1 ] [ j + 1 ] + 1 ; else dpr [ i ] [ j ] = Math . max ( dpr [ i + 1 ] [ j ] , dpr [ i ] [ j + 1 ] ) ; } } let ans = 0 ; for ( let i = 0 ; i <= N ; i ++ ) { for ( let j = 0 ; j < MAX ; j ++ ) { for ( let x = 0 ; x < pos [ j ] . length ; x ++ ) { if ( dpl [ i ] [ pos [ j ] [ x ] - 1 ] + dpr [ i + 1 ] [ pos [ j ] [ x ] + 1 ] == LCS ) { ans ++ ; break ; } } } } return ans ; } let A = " " , B = " " ; let N = A . length , M = B . length ; document . write ( numberofways ( A , B , N , M ) ) ;
Minimum cost to make two strings identical by deleting the digits | Function to returns cost of removing the identical characters in LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains cost of removing identical characters in LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; If both characters are same , add both of them ; Otherwise find the maximum cost among them ; Returns cost of making X [ ] and Y [ ] identical ; Find LCS of X [ ] and Y [ ] ; Initialize the cost variable ; Find cost of all characters in both strings ; Driver function
function lcs ( X , Y , m , n ) { let L = new Array ( m + 1 ) ; for ( let i = 0 ; i <= m ; ++ i ) { L [ i ] = new Array ( n + 1 ) ; for ( let j = 0 ; j <= n ; 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 ] + 2 * ( X [ i - 1 ] - ' ' ) ; else L [ i ] [ j ] = L [ i - 1 ] [ j ] > L [ i ] [ j - 1 ] ? L [ i - 1 ] [ j ] : L [ i ] [ j - 1 ] ; } } return L [ m ] [ n ] ; } function findMinCost ( X , Y ) { let m = X . length , n = Y . length ; let cost = 0 ; for ( let i = 0 ; i < m ; ++ i ) cost += X [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; for ( let i = 0 ; i < n ; ++ i ) cost += Y [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; return cost - lcs ( X , Y , m , n ) ; } let X = ( " " ) . split ( ' ' ) ; let Y = ( " " ) . split ( ' ' ) ; document . write ( " " + " " + findMinCost ( X , Y ) ) ;
Given a large number , check if a subsequence of digits is divisible by 8 | Function to calculate any permutation divisible by 8. If such permutation exists , the function will return that permutation else it will return - 1 ; Converting string to integer array for ease of computations ( Indexing in arr [ ] is considered to be starting from 1 ) ; Generating all possible permutations and checking if any such permutation is divisible by 8 ; Driver Code
function isSubSeqDivisible ( str ) { let i , j , k , l = str . length ; let arr = [ ] ; for ( i = 0 ; i < l ; i ++ ) arr [ i ] = str [ i ] - ' ' ; for ( i = 0 ; i < l ; i ++ ) { for ( j = i ; j < l ; j ++ ) { for ( k = j ; k < l ; k ++ ) { if ( arr [ i ] % 8 == 0 ) return true ; else if ( ( arr [ i ] * 10 + arr [ j ] ) % 8 == 0 && i != j ) return true ; else if ( ( arr [ i ] * 100 + arr [ j ] * 10 + arr [ k ] ) % 8 == 0 && i != j && j != k && i != k ) return true ; } } } return false ; } let str = " " ; if ( isSubSeqDivisible ( str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Given a large number , check if a subsequence of digits is divisible by 8 | Function takes in an array of numbers , dynamically goes on the location and makes combination of numbers . ; Converting string to integer array for ease of computations ( Indexing in arr [ ] is considered to be starting from 1 ) ; If we consider the number in our combination , we add it to the previous combination ; If we exclude the number from our combination ; If at dp [ i ] [ 0 ] , we find value 1 / true , it shows that the number exists at the value of ' i ' ; Driver function
function isSubSeqDivisible ( str ) { let n = str . length ; let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i < 10 ; i ++ ) { dp [ i ] = new Array ( 10 ) ; for ( let j = 0 ; j < 10 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } let arr = new Array ( n + 1 ) ; for ( let i = 1 ; i <= n ; i ++ ) arr [ i ] = ( str [ i - 1 ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ; for ( let i = 1 ; i <= n ; i ++ ) { dp [ i ] [ arr [ i ] % 8 ] = 1 ; for ( let j = 0 ; j < 8 ; j ++ ) { if ( dp [ i - 1 ] [ j ] > dp [ i ] [ ( j * 10 + arr [ i ] ) % 8 ] ) dp [ i ] [ ( j * 10 + arr [ i ] ) % 8 ] = dp [ i - 1 ] [ j ] ; if ( dp [ i - 1 ] [ j ] > dp [ i ] [ j ] ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } } for ( let i = 1 ; i <= n ; i ++ ) { if ( dp [ i ] [ 0 ] == 1 ) return true ; } return false ; } let str = " " ; if ( isSubSeqDivisible ( str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Given a large number , check if a subsequence of digits is divisible by 8 | Driver code ; map key will be tens place digit of number that is divisible by 8 and value will be units place digit ; For filling the map let start with initial value 8 ; key is digit at tens place and value is digit at units place mp . add ( { key , value } ) if ( mp . containsKey ( ( no / 10 ) % 10 ) ) ; Create a hash to check if we visited a number ; Iterate from last index to 0 th index ; If 8 is present in String then 8 divided 8 hence print yes ; considering present character as the second digit of two digits no we check if the value of this key is marked in hash or not If marked then we a have a number divisible by 8 ; If no subsequence divisible by 8
let str = " " ; let mp = new Map ( ) ; let no = 8 ; while ( no < 100 ) { no = no + 8 ; mp . set ( ( Math . floor ( no / 10 ) ) % 10 , no % 10 ) ; } let visited = new Array ( 10 ) ; for ( let i = 0 ; i < visited . length ; i ++ ) { visited [ i ] = false ; } let i ; for ( i = str . length - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == ' ' ) { document . write ( " " ) ; break ; } if ( visited [ mp . get ( str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ] ) { document . write ( " " ) ; break ; } visited [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] = true ; } if ( i == - 1 ) document . write ( " " ) ;
Length of Longest Balanced Subsequence | Javascript program to find length of the longest balanced subsequence . ; Considering all balanced substrings of length 2 ; Considering all other substrings ; Driver Code
function maxLength ( s , 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 ; } } for ( let i = 0 ; i < n - 1 ; i ++ ) if ( s [ i ] == ' ' && s [ i + 1 ] == ' ' ) dp [ i ] [ i + 1 ] = 2 ; for ( let l = 2 ; l < n ; l ++ ) { for ( let i = 0 , j = l ; j < n ; i ++ , j ++ ) { if ( s [ i ] == ' ' && s [ j ] == ' ' ) dp [ i ] [ j ] = 2 + dp [ i + 1 ] [ j - 1 ] ; for ( let k = i ; k < j ; k ++ ) dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i ] [ k ] + dp [ k + 1 ] [ j ] ) ; } } return dp [ 0 ] [ n - 1 ] ; } let s = " " ; let n = s . length ; document . write ( maxLength ( s , n ) ) ;
Maximum sum bitonic subarray | Javascript implementation to find the maximum sum bitonic subarray ; Function to find the maximum sum bitonic subarray . ; To store the maximum sum bitonic subarray ; Find the longest increasing subarray starting at i . ; Now we know that a [ i . . j ] is an increasing subarray . Remove non - positive elements from the left side as much as possible . ; Find the longest decreasing subarray starting at j . ; Now we know that a [ j . . k ] is a decreasing subarray . Remove non - positive elements from the right side as much as possible . last is needed to keep the last seen element . ; Compute the max sum of the increasing part . ; Compute the max sum of the decreasing part . ; The overall max sum is the sum of both parts minus the peak element , because it was counted twice . ; If the next element is equal to the current , i . e . arr [ i + 1 ] == arr [ i ] , last == i . To ensure the algorithm has progress , get the max of last and i + 1. ; Required maximum sum ; The example from the article , the answer is 19. ; Always increasing , the answer is 15. ; Always decreasing , the answer is 15. ; All are equal , the answer is 5. ; The whole array is bitonic , but the answer is 7. ; The answer is 4 ( the tail ) .
function find_partial_sum ( arr , start , end ) { let sum = 0 ; for ( let i = start ; i < end ; i ++ ) sum += arr [ i ] ; return sum ; } function maxSumBitonicSubArr ( arr , n ) { let max_sum = - 1000000 ; let i = 0 ; while ( i < n ) { let j = i ; while ( j + 1 < n && arr [ j ] < arr [ j + 1 ] ) j ++ ; while ( i < j && arr [ i ] <= 0 ) i ++ ; let k = j ; while ( k + 1 < n && arr [ k ] > arr [ k + 1 ] ) k ++ ; let last = k ; while ( k > j && arr [ k ] <= 0 ) k -- ; let sum_inc = find_partial_sum ( arr , i , j + 1 ) ; let sum_dec = find_partial_sum ( arr , j , k + 1 ) ; let sum_all = sum_inc + sum_dec - arr [ j ] ; max_sum = Math . max ( Math . max ( max_sum , sum_inc ) , Math . max ( sum_dec , sum_all ) ) ; i = Math . max ( last , i + 1 ) ; } return max_sum ; } let arr = [ 5 , 3 , 9 , 2 , 7 , 6 , 4 ] ; let n = arr . length ; document . write ( " " + maxSumBitonicSubArr ( arr , n ) + " " ) ; let arr2 = [ 1 , 2 , 3 , 4 , 5 ] ; let n2 = arr2 . length ; document . write ( " " + maxSumBitonicSubArr ( arr2 , n2 ) + " " ) ; let arr3 = [ 5 , 4 , 3 , 2 , 1 ] ; let n3 = arr3 . length ; document . write ( " " + maxSumBitonicSubArr ( arr3 , n3 ) + " " ) ; let arr4 = [ 5 , 5 , 5 , 5 ] ; let n4 = arr4 . length ; document . write ( " " + maxSumBitonicSubArr ( arr4 , n4 ) + " " ) ; let arr5 = [ - 1 , 0 , 1 , 2 , 3 , 1 , 0 , - 1 , - 10 ] ; let n5 = arr5 . length ; document . write ( " " + maxSumBitonicSubArr ( arr5 , n5 ) + " " ) ; let arr6 = [ - 1 , 0 , 1 , 2 , 0 , - 1 , - 2 , 0 , 1 , 3 ] ; let n6 = arr6 . length ; document . write ( " " + maxSumBitonicSubArr ( arr6 , n6 ) + " " ) ;
Smallest sum contiguous subarray | function to find the smallest sum contiguous subarray ; to store the minimum value that is ending up to the current index ; to store the minimum value encountered so far ; traverse the array elements ; if min_ending_here > 0 , then it could not possibly contribute to the minimum sum further ; else add the value arr [ i ] to min_ending_here ; update min_so_far ; required smallest sum contiguous subarray value ; Driver method
function smallestSumSubarr ( arr , n ) { let min_ending_here = 2147483647 ; let min_so_far = 2147483647 ; for ( let i = 0 ; i < n ; i ++ ) { if ( min_ending_here > 0 ) min_ending_here = arr [ i ] ; else min_ending_here += arr [ i ] ; min_so_far = Math . min ( min_so_far , min_ending_here ) ; } return min_so_far ; } let arr = [ 3 , - 4 , 2 , - 3 , - 1 , 7 , - 5 ] ; let n = arr . length ; document . write ( " " + smallestSumSubarr ( arr , n ) ) ;
n | javascript code to find nth number with digits 0 , 1 , 2 , 3 , 4 , 5 ; If the Number is less than 6 return the number as it is . ; Call the function again and again the get the desired result . And convert the number to base 6. ; Decrease the Number by 1 and Call ans function to convert N to base 6 ; Driver code
function ans ( n ) { if ( n < 6 ) { return n ; } return n % 6 + 10 * ( ans ( parseInt ( n / 6 ) ) ) ; } function getSpecialNumber ( N ) { return ans ( -- N ) ; } var N = 17 ; var answer = getSpecialNumber ( N ) ; document . write ( answer ) ;
Paper Cut into Minimum Number of Squares | Set 2 | Javascript program to find minimum number of squares to cut a paper using Dynamic Programming ; Returns min number of squares needed ; Initializing max values to vertical_min and horizontal_min ; N = 11 & M = 13 is a special case ; If the given rectangle is already a square ; If the answer for the given rectangle is previously calculated return that answer ; The rectangle is cut horizontally and vertically into two parts and the cut with minimum value is found for every recursive call . ; Calculating the minimum answer for the rectangles with width equal to n and length less than m for finding the cut point for the minimum answer ; Calculating the minimum answer for the rectangles with width less than n and length equal to m for finding the cut point for the minimum answer ; Minimum of the vertical cut or horizontal cut to form a square is the answer ; Driver code ; Function call
let dp = new Array ( 300 ) ; for ( let i = 0 ; i < 300 ; i ++ ) { dp [ i ] = new Array ( 300 ) ; for ( let j = 0 ; j < 300 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } function minimumSquare ( m , n ) { let vertical_min = Number . MAX_VALUE ; let horizontal_min = Number . MAX_VALUE ; if ( n == 13 && m == 11 ) return 6 ; if ( m == 13 && n == 11 ) return 6 ; if ( m == n ) return 1 ; if ( dp [ m ] [ n ] != 0 ) return dp [ m ] [ n ] ; for ( let i = 1 ; i <= parseInt ( m / 2 , 10 ) ; i ++ ) { horizontal_min = Math . min ( minimumSquare ( i , n ) + minimumSquare ( m - i , n ) , horizontal_min ) ; } for ( let j = 1 ; j <= parseInt ( n / 2 , 10 ) ; j ++ ) { vertical_min = Math . min ( minimumSquare ( m , j ) + minimumSquare ( m , n - j ) , vertical_min ) ; } dp [ m ] [ n ] = Math . min ( vertical_min , horizontal_min ) ; return dp [ m ] [ n ] ; } let m = 30 , n = 35 ; document . write ( minimumSquare ( m , n ) ) ;
Number of n | Returns factorial of n ; returns nCr ; Driver code
function fact ( n ) { var res = 1 ; for ( var i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } function nCr ( n , r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } var n = 2 ; document . write ( " " + nCr ( n + 9 , 9 ) ) ;
Painting Fence Algorithm | Returns count of ways to color k posts using k colors ; There are k ways to color first post ; There are 0 ways for single post to violate ( same color_ and k ways to not violate ( different color ) ; Fill for 2 posts onwards ; Current same is same as previous diff ; We always have k - 1 choices for next post ; Total choices till i . ; Driver code
function countWays ( n , k ) { let total = k ; let mod = 1000000007 ; let same = 0 , diff = k ; for ( let i = 2 ; i <= n ; i ++ ) { same = diff ; diff = total * ( k - 1 ) ; diff = diff % mod ; total = ( same + diff ) % mod ; } return total ; } let n = 3 , k = 2 ; document . write ( countWays ( n , k ) ) ;
Sum of all substrings of a string representing a number | Set 2 ( Constant Extra Space ) | Returns sum of all substring of num ; Initialize result ; Here traversing the array in reverse order . Initializing loop from last element . mf is multiplying factor . ; Each time sum is added to its previous sum . Multiplying the three factors as explained above . s [ i ] - '0' is done to convert char to int . ; Making new multiplying factor as explained above . ; Driver Code
function sumOfSubstrings ( num ) { let sum = 0 ; let mf = 1 ; for ( let i = num . length - 1 ; i >= 0 ; i -- ) { sum += ( num [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) * ( i + 1 ) * mf ; mf = mf * 10 + 1 ; } return sum ; } let num = " " ; document . write ( sumOfSubstrings ( num ) ) ;
Largest sum subarray with at | Returns maximum sum of a subarray with at - least k elements . ; maxSum [ i ] is going to store maximum sum till index i such that a [ i ] is part of the sum . ; We use Kadane 's algorithm to fill maxSum[]  Below code is taken from method 3 of  https:www.geeksforgeeks.org/largest-sum-contiguous-subarray/ ; Sum of first k elements ; Use the concept of sliding window ; Compute sum of k elements ending with a [ i ] . ; Update result if required ; Include maximum sum till [ i - k ] also if it increases overall max . ; Driver code
function maxSumWithK ( a , n , k ) { let maxSum = new Array ( n ) ; maxSum [ 0 ] = a [ 0 ] ; let curr_max = a [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { curr_max = Math . max ( a [ i ] , curr_max + a [ i ] ) ; maxSum [ i ] = curr_max ; } let sum = 0 ; for ( let i = 0 ; i < k ; i ++ ) sum += a [ i ] ; let result = sum ; for ( let i = k ; i < n ; i ++ ) { sum = sum + a [ i ] - a [ i - k ] ; result = Math . max ( result , sum ) ; result = Math . max ( result , sum + maxSum [ i - k ] ) ; } return result ; } let arr = [ 1 , 2 , 3 , - 10 , - 3 ] ; let k = 4 ; document . write ( maxSumWithK ( arr , arr . length , k ) ) ;
Ways to sum to N using array elements with repetition allowed | JavaScript implementation to count ways to sum up to a given value N ; method to count the total number of ways to sum up to ' N ' ; base case ; count ways for all values up to ' N ' and store the result ; if i >= arr [ j ] then accumulate count for value ' i ' as ways to form value ' i - arr [ j ] ' ; required number of ways ; Driver code
let arr = [ 1 , 5 , 6 ] ; function countWays ( N ) { let count = new Array ( N + 1 ) ; count . fill ( 0 ) ; count [ 0 ] = 1 ; for ( let i = 1 ; i <= N ; i ++ ) for ( let j = 0 ; j < arr . length ; j ++ ) if ( i >= arr [ j ] ) count [ i ] += count [ i - arr [ j ] ] ; return count [ N ] ; } let N = 7 ; document . write ( " " + countWays ( N ) ) ;
Sequences of given length where every element is more than or equal to twice of previous | Recursive function to find the number of special sequences ; A special sequence cannot exist if length n is more than the maximum value m . ; If n is 0 , found an empty special sequence ; There can be two possibilities : ( 1 ) Reduce last element value ( 2 ) Consider last element as m and reduce number of terms ; Driver Code
function getTotalNumberOfSequences ( m , n ) { if ( m < n ) return 0 ; if ( n == 0 ) return 1 ; return getTotalNumberOfSequences ( m - 1 , n ) + getTotalNumberOfSequences ( m / 2 , n - 1 ) ; } let m = 10 ; let n = 4 ; document . write ( " " , getTotalNumberOfSequences ( m , n ) ) ;
Sequences of given length where every element is more than or equal to twice of previous | DP based function to find the number of special sequences ; define T and build in bottom manner to store number of special sequences of length n and maximum value m ; Base case : If length of sequence is 0 or maximum value is 0 , there cannot exist any special sequence ; if length of sequence is more than the maximum value , special sequence cannot exist ; If length of sequence is 1 then the number of special sequences is equal to the maximum value For example with maximum value 2 and length 1 , there can be 2 special sequences { 1 } , { 2 } ; otherwise calculate ; Driver Code
function getTotalNumberOfSequences ( m , n ) { let T = new Array ( m + 1 ) ; for ( let i = 0 ; i < m + 1 ; i ++ ) { T [ i ] = new Array ( n + 1 ) ; for ( let j = 0 ; j < n + 1 ; j ++ ) { if ( i == 0 j == 0 ) T [ i ] [ j ] = 0 ; else if ( i < j ) T [ i ] [ j ] = 0 ; else if ( j == 1 ) T [ i ] [ j ] = i ; else T [ i ] [ j ] = T [ i - 1 ] [ j ] + T [ parseInt ( i / 2 , 10 ) ] [ j - 1 ] ; } } return T [ m ] [ n ] ; } let m = 10 ; let n = 4 ; document . write ( " " + getTotalNumberOfSequences ( m , n ) ) ;
Minimum number of deletions and insertions to transform one string into another | Returns length of length common subsequence for str1 [ 0. . m - 1 ] , str2 [ 0. . n - 1 ] ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains length of LCS of str1 [ 0. . i - 1 ] and str2 [ 0. . j - 1 ] ; L [ m ] [ n ] contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ] ; function to find minimum number of deletions and insertions ; Driver code ; Function Call
function lcs ( str1 , str2 , m , n ) { let L = new Array ( m + 1 ) ; let i , j ; for ( i = 0 ; i <= m ; i ++ ) { L [ i ] = new Array ( n + 1 ) ; for ( j = 0 ; j <= n ; j ++ ) { L [ i ] [ j ] = 0 ; } } for ( i = 0 ; i <= m ; i ++ ) { for ( j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i ] [ j ] = 0 ; else if ( str1 [ i - 1 ] == str2 [ 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 ] ) ; } } return L [ m ] [ n ] ; } function printMinDelAndInsert ( str1 , str2 ) { let m = str1 . length ; let n = str2 . length ; let len = lcs ( str1 , str2 , m , n ) ; document . write ( " " + " " ) ; document . write ( ( m - len ) + " " ) ; document . write ( " " + " " ) ; document . write ( ( n - len ) + " " ) ; } let str1 = " " ; let str2 = " " ; printMinDelAndInsert ( str1 , str2 ) ;
Minimum number of deletions to make a sorted sequence | lis ( ) returns the length of the longest increasing subsequence in arr [ ] of size n ; Initialize LIS values for all indexes ; Compute optimized LIS values in bottom up manner ; Pick resultimum of all LIS values ; function to calculate minimum number of deletions ; Find longest increasing subsequence ; After removing elements other than the lis , we get sorted sequence . ; Driver Code
function lis ( arr , n ) { let result = 0 ; let lis = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) lis [ i ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) for ( let j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; for ( let i = 0 ; i < n ; i ++ ) if ( result < lis [ i ] ) result = lis [ i ] ; return result ; } function minimumNumberOfDeletions ( arr , n ) { let len = lis ( arr , n ) ; return ( n - len ) ; } let arr = [ 30 , 40 , 2 , 5 , 1 , 7 , 45 , 50 , 8 ] ; let n = arr . length ; document . write ( " " + minimumNumberOfDeletions ( arr , n ) ) ;
Clustering / Partitioning an array such that sum of square differences is minimum | Javascript program to find minimum cost k partitions of array . ; Returns minimum cost of partitioning a [ ] in k clusters . ; Create a dp [ ] [ ] table and initialize all values as infinite . dp [ i ] [ j ] is going to store optimal partition cost for arr [ 0. . i - 1 ] and j partitions ; Fill dp [ ] [ ] in bottom up manner ; Current ending position ( After i - th iteration result for a [ 0. . i - 1 ] is computed . ; j is number of partitions ; Picking previous partition for current i . ; Driver code
let inf = 1000000000 ; function minCost ( a , n , k ) { let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i <= n ; i ++ ) { dp [ i ] = new Array ( k + 1 ) ; for ( let j = 0 ; j <= k ; j ++ ) dp [ i ] [ j ] = inf ; } dp [ 0 ] [ 0 ] = 0 ; for ( let i = 1 ; i <= n ; i ++ ) for ( let j = 1 ; j <= k ; j ++ ) for ( let m = i - 1 ; m >= 0 ; m -- ) dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , dp [ m ] [ j - 1 ] + ( a [ i - 1 ] - a [ m ] ) * ( a [ i - 1 ] - a [ m ] ) ) ; return dp [ n ] [ k ] ; } let k = 2 ; let a = [ 1 , 5 , 8 , 10 ] ; let n = a . length ; document . write ( minCost ( a , n , k ) ) ;