codeparrot/xlcost-text-to-code huggingface dataset:
Count of numbers between range having only non | JavaScript implementation of the approach ; states - position , sum , rem , tight sum can have values upto 162 , if we are dealing with numbers upto 10 ^ 18 when all 18 digits are 9 , then sum is 18 * 9 = 162 ; n is the sum of digits and number should be divisible by m ; Function to return the count of required numbers from 0 to num ; Last position ; If this result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is 1 , means number has already become smaller so we can place any digit , otherwise num [ pos ] ; If the current digit is zero and nonz is 1 , we can 't place it ; Function to convert x into its digit vector and uses count ( ) function to return the required count ; Initialize dp ; Driver code
var M = 20 ; var dp = Array . from ( Array ( M ) , ( ) => Array ( 165 ) ) ; var n , m ; function count ( pos , sum , rem , tight , nonz , num ) { if ( pos == num . length ) { if ( rem == 0 && sum == n ) return 1 ; return 0 ; } if ( dp [ pos ] [ sum ] [ rem ] [ tight ] != - 1 ) return dp [ pos ] [ sum ] [ rem ] [ tight ] ; var ans = 0 ; var limit = ( tight ? 9 : num [ pos ] ) ; for ( var d = 0 ; d <= limit ; d ++ ) { if ( d == 0 && nonz ) continue ; var currSum = sum + d ; var currRem = ( rem * 10 + d ) % m ; var currF = ( tight != 0 || ( d < num [ pos ] ) ) ? 1 : 0 ; ans += count ( pos + 1 , currSum , currRem , currF , ( nonz != 0 d != 0 ) ? 1 : 0 , num ) ; } dp [ pos ] [ sum ] [ rem ] [ tight ] = ans ; return ans ; } function solve ( x ) { var num = [ ] ; while ( x ) { num . push ( x % 10 ) ; x = parseInt ( x / 10 ) ; } num . reverse ( ) ; for ( var i = 0 ; i < M ; i ++ ) for ( var j = 0 ; j < 165 ; j ++ ) dp [ i ] [ j ] = Array . from ( Array ( M ) , ( ) => Array ( 2 ) . fill ( - 1 ) ) ; return count ( 0 , 0 , 0 , 0 , 0 , num ) ; } var L = 1 , R = 100 ; n = 8 , m = 2 ; document . write ( solve ( R ) - solve ( L ) ) ;
Count of Numbers in Range where the number does not contain more than K non zero digits | Javascript Program to find the count of numbers in a range where the number does not contain more than K non zero digits ; states - position , count , tight ; K is the number of non zero digits ; This function returns the count of required numbers from 0 to num ; Last position ; If count of non zero digits is less than or equal to K ; If this result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is 1 , means number has already become smaller so we can place any digit , otherwise num [ pos ] ; If the current digit is nonzero increment currCnt ; At this position , number becomes smaller ; Next recursive call ; This function converts a number into its digit vector and uses above function to compute the answer ; Initialize dp ; Driver Code to test above functions
let M = 20 ; let dp = new Array ( M ) ; for ( let i = 0 ; i < M ; i ++ ) { dp [ i ] = new Array ( M ) ; for ( let j = 0 ; j < M ; j ++ ) { dp [ i ] [ j ] = new Array ( 2 ) ; for ( let k = 0 ; k < 2 ; k ++ ) dp [ i ] [ j ] [ k ] = 0 ; } } let K ; let num ; function countInRangeUtil ( pos , cnt , tight ) { if ( pos == num . length ) { if ( cnt <= K ) return 1 ; return 0 ; } if ( dp [ pos ] [ cnt ] [ tight ] != - 1 ) return dp [ pos ] [ cnt ] [ tight ] ; let ans = 0 ; let limit = ( tight != 0 ? 9 : num [ pos ] ) ; for ( let dig = 0 ; dig <= limit ; dig ++ ) { let currCnt = cnt ; if ( dig != 0 ) currCnt ++ ; let currTight = tight ; if ( dig < num [ pos ] ) currTight = 1 ; ans += countInRangeUtil ( pos + 1 , currCnt , currTight ) ; } return dp [ pos ] [ cnt ] [ tight ] = ans ; } function countInRange ( x ) { num = [ ] ; while ( x != 0 ) { num . push ( x % 10 ) ; x = Math . floor ( x / 10 ) ; } num . reverse ( ) ; for ( let i = 0 ; i < M ; i ++ ) for ( let j = 0 ; j < M ; j ++ ) for ( let k = 0 ; k < 2 ; k ++ ) dp [ i ] [ j ] [ k ] = - 1 ; return countInRangeUtil ( 0 , 0 , 0 ) ; } let L = 1 , R = 1000 ; K = 3 ; document . write ( ( countInRange ( R ) - countInRange ( L - 1 ) ) + " " ) ; L = 9995 ; R = 10005 ; K = 2 ; document . write ( ( countInRange ( R ) - countInRange ( L - 1 ) ) + " " ) ;
Balanced expressions such that given positions have opening brackets | Set 2 | Javascript implementation of above approach using memoization ; Function to find Number of proper bracket expressions ; If open - closed brackets < 0 ; If index reaches the end of expression ; If brackets are balanced ; If already stored in dp ; If the current index has assigned open bracket ; Move forward increasing the length of open brackets ; Move forward by inserting open as well as closed brackets on that index ; Return the answer ; DP array to precompute the answer ; Open brackets at position 1 ; Calling the find function to calculate the answer
let N = 1000 ; function find ( index , openbrk , n , dp , adj ) { if ( openbrk < 0 ) { return 0 ; } if ( index == n ) { if ( openbrk == 0 ) { return 1 ; } else { return 0 ; } } if ( dp [ index ] [ openbrk ] != - 1 ) { return dp [ index ] [ openbrk ] ; } if ( adj [ index ] == 1 ) { dp [ index ] [ openbrk ] = find ( index + 1 , openbrk + 1 , n , dp , adj ) ; } else { dp [ index ] [ openbrk ] = find ( index + 1 , openbrk + 1 , n , dp , adj ) + find ( index + 1 , openbrk - 1 , n , dp , adj ) ; } return dp [ index ] [ openbrk ] ; } 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 ] = - 1 ; } } let n = 2 ; let adj = [ 1 , 0 , 0 , 0 ] ; document . write ( find ( 0 , 0 , 2 * n , dp , adj ) ) ;
Maximize array elements upto given number | variable to store maximum value that can be obtained . ; Utility function to find maximum possible value ; If entire array is traversed , then compare current value in num to overall maximum obtained so far . ; Case 1 : Subtract current element from value so far if result is greater than or equal to zero . ; Case 2 : Add current element to value so far if result is less than or equal to maxLimit . ; Function to find maximum possible value that can be obtained using array elements and given number . ; Variable to store current index position . ; Call to utility function to find maximum possible value that can be obtained . ; Driver code
let ans = 0 ; function findMaxValUtil ( arr , n , num , maxLimit , ind ) { if ( ind == n ) { ans = Math . max ( ans , num ) ; return ; } if ( num - arr [ ind ] >= 0 ) { findMaxValUtil ( arr , n , num - arr [ ind ] , maxLimit , ind + 1 ) ; } if ( num + arr [ ind ] <= maxLimit ) { findMaxValUtil ( arr , n , num + arr [ ind ] , maxLimit , ind + 1 ) ; } } function findMaxVal ( arr , n , num , maxLimit ) { let ind = 0 ; findMaxValUtil ( arr , n , num , maxLimit , ind ) ; return ans ; } let num = 1 ; let arr = [ 3 , 10 , 6 , 4 , 5 ] ; let n = arr . length ; let maxLimit = 15 ; document . write ( findMaxVal ( arr , n , num , maxLimit ) ) ;
Print equal sum sets of array ( Partition problem ) | Set 1 | Function to print the equal sum sets of the array . ; Print set 1. ; Print set 2. ; Utility function to find the sets of the array which have equal sum . ; If entire array is traversed , compare both the sums . ; If sums are equal print both sets and return true to show sets are found . ; If sums are not equal then return sets are not found . ; Add current element to set 1. ; Recursive call after adding current element to set 1. ; If this inclusion results in equal sum sets partition then return true to show desired sets are found . ; If not then backtrack by removing current element from set1 and include it in set 2. ; Recursive call after including current element to set 2. ; Return true if array arr can be partitioned into two equal sum sets or not . ; Calculate sum of elements in array . ; If sum is odd then array cannot be partitioned . ; Declare vectors to store both the sets . ; Find both the sets . ; Driver code
function printSets ( set1 , set2 ) { var i ; for ( i = 0 ; i < set1 . length ; i ++ ) { document . write ( set1 [ i ] + " " ) ; } document . write ( " " ) ; for ( i = 0 ; i < set2 . length ; i ++ ) { document . write ( set2 [ i ] + " " ) ; } } function findSets ( arr , n , set1 , set2 , sum1 , sum2 , pos ) { if ( pos == n ) { if ( sum1 == sum2 ) { printSets ( set1 , set2 ) ; return true ; } else return false ; } set1 . push ( arr [ pos ] ) ; var res = findSets ( arr , n , set1 , set2 , sum1 + arr [ pos ] , sum2 , pos + 1 ) ; if ( res ) return res ; set1 . pop ( ) ; set2 . push ( arr [ pos ] ) ; res = findSets ( arr , n , set1 , set2 , sum1 , sum2 + arr [ pos ] , pos + 1 ) ; if ( res == false ) if ( ! set2 . length == 0 ) set2 . pop ( ) ; return res ; } function isPartitionPoss ( arr , n ) { var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( sum % 2 != 0 ) return false ; var set1 = [ ] ; var set2 = [ ] ; return findSets ( arr , n , set1 , set2 , 0 , 0 , 0 ) ; } var arr = [ 5 , 5 , 1 , 11 ] ; var n = arr . length ; if ( ! isPartitionPoss ( arr , n ) ) { document . write ( " " ) ; }
Maximum subarray sum in O ( n ) using prefix sum | Function to compute maximum subarray sum in linear time . ; Initialize minimum prefix sum to 0. ; Initialize maximum subarray sum so far to - infinity . ; Initialize and compute the prefix sum array . ; loop through the array , keep track of minimum prefix sum so far and maximum subarray sum . ; Test case 1 ; Test case 2
function maximumSumSubarray ( arr , n ) { let min_prefix_sum = 0 ; let res = Number . MIN_VALUE ; let prefix_sum = [ ] ; prefix_sum [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i ] ; for ( let i = 0 ; i < n ; i ++ ) { res = Math . max ( res , prefix_sum [ i ] - min_prefix_sum ) ; min_prefix_sum = Math . min ( min_prefix_sum , prefix_sum [ i ] ) ; } return res ; } let arr1 = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ] ; let n1 = arr1 . length ; document . write ( maximumSumSubarray ( arr1 , n1 ) + " " ) ; let arr2 = [ 4 , - 8 , 9 , - 4 , 1 , - 8 , - 1 , 6 ] ; let n2 = arr2 . length ; document . write ( maximumSumSubarray ( arr2 , n2 ) ) ;
Newman | Function to find the n - th element ; Declare array to store sequence ; Driver Code
function sequence ( n ) { let i ; let f = [ ] ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; f [ 2 ] = 1 ; for ( let i = 3 ; i <= n ; i ++ ) f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ; return f [ n ] ; } let n = 10 ; document . write ( sequence ( n ) ) ;
Maximum product of an increasing subsequence | JavaScript program implementation ; Returns product of maximum product increasing subsequence . ; Initialize MPIS values ; Compute optimized MPIS values considering every element as ending element of sequence ; Pick maximum of all product values using for loop ; Driver Code
of maximum product of an increasing function lis ( arr , n ) { let mpis = [ ] ; let max = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) mpis [ i ] = arr [ i ] ; for ( let i = 1 ; i < n ; i ++ ) for ( let j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && mpis [ i ] < ( mpis [ j ] * arr [ i ] ) ) mpis [ i ] = mpis [ j ] * arr [ i ] ; for ( let k = 0 ; k < mpis . length ; k ++ ) { if ( mpis [ k ] > max ) { max = mpis [ k ] ; } } return max ; } let arr = [ 3 , 100 , 4 , 5 , 150 , 6 ] ; let n = arr . length ; document . write ( lis ( arr , n ) ) ;
Hosoya 's Triangle | JavaScript program to implement the above approach ; Base case ; Recursive step ; Print the Hosoya triangle of height n . ; Driver code
function Hosoya ( n , m ) { if ( ( n == 0 && m == 0 ) || ( n == 1 && m == 0 ) || ( n == 1 && m == 1 ) || ( n == 2 && m == 1 ) ) return 1 ; if ( n > m ) return Hosoya ( n - 1 , m ) + Hosoya ( n - 2 , m ) ; else if ( m == n ) return Hosoya ( n - 1 , m - 1 ) + Hosoya ( n - 2 , m - 2 ) ; else return 0 ; } function printHosoya ( n ) { for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j <= i ; j ++ ) document . write ( Hosoya ( i , j ) + " " ) ; document . write ( " " ) ; } } let n = 5 ; printHosoya ( n ) ;
Eulerian Number | Return eulerian number A ( n , m ) ; Driver code
function eulerian ( n , m ) { if ( m >= n n == 0 ) return 0 ; if ( m == 0 ) return 1 ; return ( n - m ) * eulerian ( n - 1 , m - 1 ) + ( m + 1 ) * eulerian ( n - 1 , m ) ; } let n = 3 , m = 1 ; document . write ( eulerian ( n , m ) ) ;
Largest divisible pairs subset | Function to find the longest Subsequence ; dp [ i ] is going to store size of largest divisible subset beginning with a [ i ] . ; Since last element is largest , d [ n - 1 ] is 1 ; Fill values for smaller elements . ; Find all multiples of a [ i ] and consider the multiple that has largest subset beginning with it . ; Return maximum value from dp [ ] ; Driver code
function largestSubset ( a , n ) { let dp = [ ] ; dp [ n - 1 ] = 1 ; for ( let i = n - 2 ; i >= 0 ; i -- ) { let mxm = 0 ; for ( let j = i + 1 ; j < n ; j ++ ) { if ( a [ j ] % a [ i ] == 0 a [ i ] % a [ j ] == 0 ) { mxm = Math . max ( mxm , dp [ j ] ) ; } } dp [ i ] = 1 + mxm ; } return Math . max ( ... dp ) ; } let a = [ 1 , 3 , 6 , 13 , 17 , 18 ] ; let n = a . length ; document . write ( largestSubset ( a , n ) ) ;
How to solve a Dynamic Programming Problem ? | initialize to - 1 ; this function returns the number of arrangements to form ' n ' ; base case ; checking if already calculated ; storing the result and returning
let dp = new Array ( MAXN ) ; function solve ( n ) { if ( n < 0 ) return 0 ; if ( n == 0 ) return 1 ; if ( dp [ n ] != - 1 ) return dp [ n ] ; return dp [ n ] = solve ( n - 1 ) + solve ( n - 3 ) + solve ( n - 5 ) ; }
Friends Pairing Problem | Returns count of ways n people can remain single or paired up . ; Filling dp [ ] in bottom - up manner using recursive formula explained above . ; Driver Code
function countFriendsPairings ( n ) { let dp = [ ] ; for ( let i = 0 ; i <= n ; i ++ ) { if ( i <= 2 ) dp [ i ] = i ; else dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ; } return dp [ n ] ; } let n = 4 ; document . write ( countFriendsPairings ( n ) ) ;
Friends Pairing Problem | Returns count of ways n people can remain single or paired up . ; Driver code
function countFriendsPairings ( n ) { let a = 1 , b = 2 , c = 0 ; if ( n <= 2 ) { return n ; } for ( let i = 3 ; i <= n ; i ++ ) { c = b + ( i - 1 ) * a ; a = b ; b = c ; } return c ; } let n = 4 ; document . write ( countFriendsPairings ( n ) ) ;
LCS ( Longest Common Subsequence ) of three strings | Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] and Z [ 0. . o - 1 ] ; Following steps build L [ m + 1 ] [ n + 1 ] [ o + 1 ] in bottom up fashion . Note that L [ i ] [ j ] [ k ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] and Z [ 0. . ... k - 1 ] ; L [ m ] [ n ] [ o ] contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ] and Z [ 0. . o - 1 ] ; Driver program to test above function
function lcsOf3 ( X , Y , Z , m , n , o ) { let L = new Array ( m + 1 ) ; for ( let i = 0 ; i < m + 1 ; i ++ ) { L [ i ] = new Array ( n + 1 ) ; for ( let j = 0 ; j < n + 1 ; j ++ ) { L [ i ] [ j ] = new Array ( o + 1 ) ; for ( let k = 0 ; k < o + 1 ; k ++ ) { L [ i ] [ j ] [ k ] = 0 ; } } } for ( let i = 0 ; i <= m ; i ++ ) { for ( let j = 0 ; j <= n ; j ++ ) { for ( let k = 0 ; k <= o ; k ++ ) { if ( i == 0 j == 0 k == 0 ) L [ i ] [ j ] [ k ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] && X [ i - 1 ] == Z [ k - 1 ] ) L [ i ] [ j ] [ k ] = L [ i - 1 ] [ j - 1 ] [ k - 1 ] + 1 ; else L [ i ] [ j ] [ k ] = Math . max ( Math . max ( L [ i - 1 ] [ j ] [ k ] , L [ i ] [ j - 1 ] [ k ] ) , L [ i ] [ j ] [ k - 1 ] ) ; } } } return L [ m ] [ n ] [ o ] ; } let X = " " ; let Y = " " ; let Z = " " ; let m = X . length ; let n = Y . length ; let o = Z . length ; document . write ( " " + lcsOf3 ( X , Y , Z , m , n , o ) ) ;
Printing Shortest Common Supersequence | returns shortest supersequence of X and Y ; dp [ i ] [ j ] contains length of shortest supersequence for X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; Fill table in bottom up manner ; Below steps follow recurrence relation ; string to store the shortest supersequence ; Start from the bottom right corner and one by one push characters in output string ; If current character in X and Y are same , then current character is part of shortest supersequence ; Put current character in result ; reduce values of i , j and index ; If current character in X and Y are different ; Put current character of Y in result ; reduce values of j and index ; Put current character of X in result ; reduce values of i and index ; If Y reaches its end , put remaining characters of X in the result string ; If X reaches its end , put remaining characters of Y in the result string ; reverse the string and return it ; Swap values of left and right ; Driver code
function printShortestSuperSeq ( X , Y ) { let m = X . length ; let n = Y . length ; let dp = new Array ( m + 1 ) ; for ( let i = 0 ; i < ( m + 1 ) ; i ++ ) { dp [ i ] = new Array ( n + 1 ) ; for ( let j = 0 ; j < ( n + 1 ) ; j ++ ) dp [ i ] [ j ] = 0 ; } for ( let i = 0 ; i <= m ; i ++ ) { for ( let j = 0 ; j <= n ; j ++ ) { if ( i == 0 ) { dp [ i ] [ j ] = j ; } else if ( j == 0 ) { dp [ i ] [ j ] = i ; } else if ( X [ i - 1 ] == Y [ j - 1 ] ) { dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; } else { dp [ i ] [ j ] = 1 + Math . min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } } let str = " " ; let i = m , j = n ; while ( i > 0 && j > 0 ) { if ( X [ i - 1 ] == Y [ j - 1 ] ) { str += ( X [ i - 1 ] ) ; i -- ; j -- ; } else if ( dp [ i - 1 ] [ j ] > dp [ i ] [ j - 1 ] ) { str += ( Y [ j - 1 ] ) ; j -- ; } else { str += ( X [ i - 1 ] ) ; i -- ; } } while ( i > 0 ) { str += ( X [ i - 1 ] ) ; i -- ; } while ( j > 0 ) { str += ( Y [ j - 1 ] ) ; j -- ; } str = reverse ( str ) ; return str ; } function reverse ( input ) { let temparray = input . split ( " " ) ; let left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { let temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return ( temparray ) . join ( " " ) ; } let X = " " ; let Y = " " ; document . write ( printShortestSuperSeq ( X , Y ) ) ;
Longest Repeating Subsequence | This function mainly returns LCS ( str , str ) with a condition that same characters at same index are not considered . ; Create and initialize DP table ; Fill dp table ( similar to LCS loops ) ; If characters match and indexes are not same ; If characters do not match ; Driver Code
function findLongestRepeatingSubSeq ( str ) { var n = str . length ; var dp = new Array ( n + 1 ) ; for ( var i = 0 ; i <= n ; i ++ ) { dp [ i ] = new Array ( n + 1 ) ; for ( var j = 0 ; j <= n ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = 1 ; j <= n ; j ++ ) { if ( ( str [ i - 1 ] == str [ j - 1 ] ) && ( i != j ) ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = Math . max ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] ) ; } } return dp [ n ] [ n ] ; } var str = " " ; document . write ( " " + findLongestRepeatingSubSeq ( str ) ) ;
Count total number of N digit numbers such that the difference between sum of even and odd digits is 1 | A lookup table used for memoization . ; Memoization based recursive function to count numbers with even and odd digit sum difference as 1. This function considers leading zero as a digit ; Base Case ; If current subproblem is already computed ; Initialize result ; If current digit is odd , then add it to odd sum and recur ; else Add to even sum and recur ; Store current result in lookup table and return the same ; This is mainly a wrapper over countRec . It explicitly handles leading digit and calls countRec ( ) for remaining digits . ; Initialize number digits considered so far ; Initialize all entries of lookup table ; Initialize final answer ; Initialize even and odd sums ; Explicitly handle first digit and call recursive function countRec for remaining digits . Note that the first digit is considered as even digit . ; Driver program
let lookup = new Array ( 50 ) ; for ( let i = 0 ; i < 50 ; i ++ ) { lookup [ i ] = new Array ( 1000 ) ; for ( let j = 0 ; j < 1000 ; j ++ ) { lookup [ i ] [ j ] = new Array ( 1000 ) ; for ( let k = 0 ; k < 1000 ; k ++ ) { lookup [ i ] [ j ] [ k ] = new Array ( 2 ) ; } } } function countRec ( digits , esum , osum , isOdd , n ) { if ( digits == n ) return ( esum - osum == 1 ) ? 1 : 0 ; if ( lookup [ digits ] [ esum ] [ osum ] [ isOdd ] != - 1 ) return lookup [ digits ] [ esum ] [ osum ] [ isOdd ] ; let ans = 0 ; if ( isOdd == 1 ) for ( let i = 0 ; i <= 9 ; i ++ ) ans += countRec ( digits + 1 , esum , osum + i , 0 , n ) ; for ( let i = 0 ; i <= 9 ; i ++ ) ans += countRec ( digits + 1 , esum + i , osum , 1 , n ) ; return lookup [ digits ] [ esum ] [ osum ] [ isOdd ] = ans ; } function finalCount ( n ) { let digits = 0 ; for ( let i = 0 ; i < 50 ; i ++ ) for ( let j = 0 ; j < 1000 ; j ++ ) for ( let k = 0 ; k < 1000 ; k ++ ) for ( let l = 0 ; l < 2 ; l ++ ) lookup [ i ] [ j ] [ k ] [ l ] = - 1 ; let ans = 0 ; let esum = 0 , osum = 0 ; for ( let i = 1 ; i <= 9 ; i ++ ) ans += countRec ( digits + 1 , esum + i , osum , 1 , n ) ; return ans ; } let n = 3 ; document . write ( " " + n + " " + finalCount ( n ) ) ;
Count all possible paths from top left to bottom right of a mXn matrix | Returns count of possible paths to reach cell at row number m and column number n from the topmost leftmost cell ( cell at 1 , 1 ) ; Create a 1D array to store results of subproblems ; Driver program to test above function
function numberOfPaths ( m , n ) { dp = Array . from ( { length : n } , ( _ , i ) => 0 ) ; dp [ 0 ] = 1 ; for ( i = 0 ; i < m ; i ++ ) { for ( j = 1 ; j < n ; j ++ ) { dp [ j ] += dp [ j - 1 ] ; } } return dp [ n - 1 ] ; } document . write ( numberOfPaths ( 3 , 3 ) ) ;
Longest Arithmetic Progression | DP | Returns length of the longest AP subset in a given set ; Driver Code
function Solution ( A ) { let ans = 2 ; let n = A . length ; if ( n <= 2 ) { return n ; } let llap = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { llap [ i ] = 2 ; } A . sort ( function ( a , b ) { return a - b } ) ; for ( let j = n - 2 ; j >= 0 ; j -- ) { let i = j - 1 ; let k = j + 1 ; while ( i >= 0 && k < n ) { if ( A [ i ] + A [ k ] == ( 2 * A [ j ] ) ) { llap [ j ] = Math . max ( ( llap [ k ] + 1 ) , llap [ j ] ) ; ans = Math . max ( ans , llap [ j ] ) ; i -= 1 ; k += 1 ; } else if ( A [ i ] + A [ k ] < 2 * A [ j ] ) k += 1 ; else i -= 1 ; } } return ans ; } let a = [ 9 , 4 , 7 , 2 , 10 ] ; document . write ( Solution ( a ) + " " ) ;
Minimum numbers to be appended such that mean of Array is equal to 1 | Function to calculate minimum Number of operations ; Storing sum of array arr [ ] ; Driver Code ; Function Call
function minumumOperation ( N , arr ) { let sum_arr = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum_arr = sum_arr + arr [ i ] ; } if ( sum_arr >= N ) document . write ( sum_arr - N + " " ) ; else document . write ( 1 + " " ) ; } let N = 4 ; let arr = [ 8 , 4 , 6 , 2 ] ; minumumOperation ( N , arr ) ;
Find Nth term of series 1 , 4 , 15 , 72 , 420. . . | Function to find factorial of N ; Base condition ; Use recursion ; Calculate Nth term of series ; Driver Code
function factorial ( N ) { if ( N == 0 N == 1 ) return 1 ; return N * factorial ( N - 1 ) ; } function nthTerm ( N ) { return ( factorial ( N ) * ( N + 2 ) / 2 ) ; } let N = 6 ; document . write ( nthTerm ( N ) ) ;
Check if a string is substring of another | var counter = 0 ; pointing s2 ; Special case where character preceding the i 'th character is duplicate ; Driver code
< ! -- Javascript program for the above approach -- > function Substr ( s2 , s1 ) { var i = 0 ; for ( ; i < s1 . length ; i ++ ) { if ( counter == s2 . length ) { break ; } if ( s2 [ counter ] == s1 [ i ] ) { counter ++ ; } else { if ( counter > 0 ) { i -= counter ; } counter = 0 ; } } return counter < s2 . length ? - 1 : i - counter ; } var s1 = " " ; document . write ( Substr ( " " , s1 ) ) ; < ! -- this code is contributed by Nirajgusain5 -- >