codeparrot/xlcost-text-to-code huggingface dataset:
Search , insert and delete in an unsorted array | Function to insert a given key in the array . This function returns n + 1 if insertion is successful , else n . ; Cannot insert more elements if n is already more than or equal to capcity ; Driver Code ; Inserting key
function insertSorted ( arr , n , key , capacity ) { if ( n >= capacity ) return n ; arr [ n ] = key ; return ( n + 1 ) ; } let arr = new Array ( 20 ) ; arr [ 0 ] = 12 ; arr [ 1 ] = 16 ; arr [ 2 ] = 20 ; arr [ 3 ] = 40 ; arr [ 4 ] = 50 ; arr [ 5 ] = 70 ; let capacity = 20 ; let n = 6 ; let i , key = 26 ; document . write ( " " ) ; for ( i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; n = insertSorted ( arr , n , key , capacity ) ; document . write ( " " ) ; for ( i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Search , insert and delete in an unsorted array | function to search a key to be deleted ; Function to delete an element ; Find position of element to be deleted ; Deleting element ; Driver Code
function findElement ( arr , n , key ) { let i ; for ( i = 0 ; i < n ; i ++ ) if ( arr [ i ] == key ) return i ; return - 1 ; } function deleteElement ( arr , n , key ) { let pos = findElement ( arr , n , key ) ; if ( pos == - 1 ) { document . write ( " " ) ; return n ; } let i ; for ( i = pos ; i < n - 1 ; i ++ ) arr [ i ] = arr [ i + 1 ] ; return n - 1 ; } let i ; let arr = [ 10 , 50 , 30 , 40 , 20 ] ; let n = arr . length ; let key = 30 ; document . write ( " " ) ; for ( i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; n = deleteElement ( arr , n , key ) ; document . write ( " " ) ; for ( i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Search , insert and delete in a sorted array | function to implement binary search ; low + ( high - low ) / 2 ; ; Driver program Let us search 3 in below array
function binarySearch ( arr , low , high , key ) { if ( high < low ) return - 1 ; let mid = Math . trunc ( ( low + high ) / 2 ) ; if ( key == arr [ mid ] ) return mid ; if ( key > arr [ mid ] ) return binarySearch ( arr , ( mid + 1 ) , high , key ) ; return binarySearch ( arr , low , ( mid - 1 ) , key ) ; } let arr = [ 5 , 6 , 7 , 8 , 9 , 10 ] ; let n , key ; n = arr . length ; key = 10 ; document . write ( " " + binarySearch ( arr , 0 , n - 1 , key ) + " " ) ;
Queries on number of Binary sub | Javascript Program to answer queries on number of submatrix of given size ; Return the minimum of three numbers ; Solve each query on matrix ; For each of the cell . ; finding submatrix size of oth row and column . ; intermediate cells . ; Find frequency of each distinct size for 0 s and 1 s . ; Find the Cumulative Sum . ; Output the answer for each query ; Driver Program
var MAX = 100 var N = 5 var M = 4 function min ( a , b , c ) { return Math . min ( a , Math . min ( b , c ) ) ; } function solveQuery ( n , m , mat , q , a , binary ) { var dp = Array . from ( Array ( n ) , ( ) => Array ( m ) ) ; var max = 1 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < m ; j ++ ) { if ( i == 0 j == 0 ) dp [ i ] [ j ] = 1 ; else if ( ( mat [ i ] [ j ] == mat [ i - 1 ] [ j ] ) && ( mat [ i ] [ j ] == mat [ i ] [ j - 1 ] ) && ( mat [ i ] [ j ] == mat [ i - 1 ] [ j - 1 ] ) ) { dp [ i ] [ j ] = Math . min ( dp [ i - 1 ] [ j ] , dp [ i - 1 ] [ j - 1 ] , dp [ i ] [ j - 1 ] ) + 1 ; if ( max < dp [ i ] [ j ] ) max = dp [ i ] [ j ] ; } else dp [ i ] [ j ] = 1 ; } } var freq0 = Array ( MAX ) . fill ( 0 ) ; var freq1 = Array ( MAX ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < m ; j ++ ) { if ( mat [ i ] [ j ] == 0 ) freq0 [ dp [ i ] [ j ] ] ++ ; else freq1 [ dp [ i ] [ j ] ] ++ ; } } for ( var i = max - 1 ; i >= 0 ; i -- ) { freq0 [ i ] += freq0 [ i + 1 ] ; freq1 [ i ] += freq1 [ i + 1 ] ; } for ( var i = 0 ; i < q ; i ++ ) { if ( binary [ i ] == 0 ) document . write ( freq0 [ a [ i ] ] + " " ) ; else document . write ( freq1 [ a [ i ] ] + " " ) ; } } var n = 5 , m = 4 ; var mat = [ [ 0 , 0 , 1 , 1 ] , [ 0 , 0 , 1 , 0 ] , [ 0 , 1 , 1 , 1 ] , [ 1 , 1 , 1 , 1 ] , [ 0 , 1 , 1 , 1 ] ] ; var q = 2 ; var a = [ 2 , 2 ] ; var binary = [ 1 , 0 ] ; solveQuery ( n , m , mat , q , a , binary ) ;
Dynamic Programming | Wildcard Pattern Matching | Linear Time and Constant Space | Function that matches input text with given wildcard pattern ; empty pattern can only match with empty string . Base Case : ; step - 1 : initialize markers : ; For step - ( 2 , 5 ) ; For step - ( 3 ) ; For step - ( 4 ) ; For step - ( 5 ) ; For step - ( 6 ) ; For step - ( 7 ) ; Final Check ; Driver code
function strmatch ( txt , pat , n , m ) { if ( m == 0 ) return ( n == 0 ) ; let i = 0 , j = 0 , index_txt = - 1 , index_pat = - 1 ; while ( i < n ) { if ( j < m && txt [ i ] == pat [ j ] ) { i ++ ; j ++ ; } else if ( j < m && pat [ j ] == ' ' ) { i ++ ; j ++ ; } else if ( j < m && pat [ j ] == ' ' ) { index_txt = i ; index_pat = j ; j ++ ; } else if ( index_pat != - 1 ) { j = index_pat + 1 ; i = index_txt + 1 ; index_txt ++ ; } else { return false ; } } while ( j < m && pat [ j ] == ' ' ) { j ++ ; } if ( j == m ) { return true ; } return false ; } let str = " " . split ( ' ' ) ; let pattern = " " . split ( ' ' ) ; if ( strmatch ( str , pattern , str . length , pattern . length ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; let pattern2 = " " . split ( ' ' ) ; if ( strmatch ( str , pattern2 , str . length , pattern2 . length ) ) document . write ( " " + " " ) ; else document . write ( " " ) ;
Find n | function to find nth stern '  diatomic series ; Initializing the DP array ; SET the Base case ; Traversing the array from 2 nd Element to nth Element ; Case 1 : for even n ; Case 2 : for odd n ; Driver code
function findSDSFunc ( n ) { let DP = [ ] ; DP [ 0 ] = 0 ; DP [ 1 ] = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) DP [ i ] = DP [ i / 2 ] ; else DP [ i ] = DP [ ( i - 1 ) / 2 ] + DP [ ( i + 1 ) / 2 ] ; } return DP [ n ] ; } let n = 15 ; document . write ( findSDSFunc ( n ) ) ;
Check if any valid sequence is divisible by M | Javascript implementation of above approach ; Calculate modulo for this call ; Base case ; Check if sum is divisible by M ; check if the current state is already computed ; 1. Try placing ' + ' ; 2. Try placing ' - ' ; Calculate value of res for recursive case ; Store the value for res for current states and return for parent call ; Driver code ; MAX is the Maximum value M can take
const MAX = 100 ; function isPossible ( n , index , modulo , M , arr , dp ) { modulo = ( ( modulo % M ) + M ) % M ; if ( index == n ) { if ( modulo == 0 ) { return 1 ; } return 0 ; } if ( dp [ index ] [ modulo ] != - 1 ) { return dp [ index ] [ modulo ] ; } var placeAdd = isPossible ( n , index + 1 , modulo + arr [ index ] , M , arr , dp ) ; var placeMinus = isPossible ( n , index + 1 , modulo - arr [ index ] , M , arr , dp ) ; var res = placeAdd ; dp [ index ] [ modulo ] = res ; return res ; } var arr = [ 1 , 2 , 3 , 4 , 6 ] ; var n = arr . length ; var M = 4 ; var dp = Array ( n + 1 ) ; for ( i = 0 ; i < n + 1 ; i ++ ) { dp [ i ] = Array ( MAX ) . fill ( - 1 ) ; } var res ; if ( isPossible ( n , 1 , arr [ 0 ] , M , arr , dp ) == 1 ) { res = true ; } else { res = false ; } document . write ( res ? " " : " " ) ;
Find common elements in three sorted arrays | This function prints common elements in ar1 ; Initialize starting indexes for ar1 [ ] , ar2 [ ] and ar3 [ ] ; Iterate through three arrays while all arrays have elements ; If x = y and y = z , print any of them and move ahead in all arrays ; x < y ; y < z ; We reach here when x > y and z < y , i . e . , z is smallest ; Driver code
function findCommon ( ar1 , ar2 , ar3 , n1 , n2 , n3 ) { var i = 0 , j = 0 , k = 0 ; while ( i < n1 && j < n2 && k < n3 ) { if ( ar1 [ i ] == ar2 [ j ] && ar2 [ j ] == ar3 [ k ] ) { document . write ( ar1 [ i ] + " " ) ; i ++ ; j ++ ; k ++ ; } else if ( ar1 [ i ] < ar2 [ j ] ) i ++ ; else if ( ar2 [ j ] < ar3 [ k ] ) j ++ ; else k ++ ; } } var ar1 = [ 1 , 5 , 10 , 20 , 40 , 80 ] ; var ar2 = [ 6 , 7 , 20 , 80 , 100 ] ; var ar3 = [ 3 , 4 , 15 , 20 , 30 , 70 , 80 , 120 ] ; var n1 = ar1 . length ; var n2 = ar2 . length ; var n3 = ar3 . length ; document . write ( " " ) ; findCommon ( ar1 , ar2 , ar3 , n1 , n2 , n3 ) ;
Dynamic Programming on Trees | Set | JavaScript code to find the maximum path sum ; function for dfs traversal and to store the maximum value in [ ] dp for every node till the leaves ; initially dp [ u ] is always a [ u ] ; stores the maximum value from nodes ; traverse the tree ; if child is parent , then we continue without recursing further ; call dfs for further traversal ; store the maximum of previous visited node and present visited node ; add the maximum value returned to the parent node ; function that returns the maximum value ; Number of nodes ; adjacency list ; create undirected edges initialize the tree given in the diagram ; values of node 1 , 2 , 3. ... 14 ; function call
var dp = Array ( 100 ) . fill ( 0 ) ; function dfs ( a , v , u , parent ) { dp [ u ] = a [ u - 1 ] ; var maximum = 0 ; for ( var child of v [ u ] ) { if ( child == parent ) continue ; dfs ( a , v , child , u ) ; maximum = Math . max ( maximum , dp [ child ] ) ; } dp [ u ] += maximum ; } function maximumValue ( a , v ) { dfs ( a , v , 1 , 0 ) ; return dp [ 1 ] ; } var n = 14 ; var v = Array . from ( Array ( n + 1 ) , ( ) => Array ( ) ) ; for ( var i = 0 ; i < v . length ; i ++ ) v [ i ] = [ ] ; v [ 1 ] . push ( 2 ) ; v [ 2 ] . push ( 1 ) ; v [ 1 ] . push ( 3 ) ; v [ 3 ] . push ( 1 ) ; v [ 1 ] . push ( 4 ) ; v [ 4 ] . push ( 1 ) ; v [ 2 ] . push ( 5 ) ; v [ 5 ] . push ( 2 ) ; v [ 2 ] . push ( 6 ) ; v [ 6 ] . push ( 2 ) ; v [ 3 ] . push ( 7 ) ; v [ 7 ] . push ( 3 ) ; v [ 4 ] . push ( 8 ) ; v [ 8 ] . push ( 4 ) ; v [ 4 ] . push ( 9 ) ; v [ 9 ] . push ( 4 ) ; v [ 4 ] . push ( 10 ) ; v [ 10 ] . push ( 4 ) ; v [ 5 ] . push ( 11 ) ; v [ 11 ] . push ( 5 ) ; v [ 5 ] . push ( 12 ) ; v [ 12 ] . push ( 5 ) ; v [ 7 ] . push ( 13 ) ; v [ 13 ] . push ( 7 ) ; v [ 7 ] . push ( 14 ) ; v [ 14 ] . push ( 7 ) ; var a = [ 3 , 2 , 1 , 10 , 1 , 3 , 9 , 1 , 5 , 3 , 4 , 5 , 9 , 8 ] ; document . write ( maximumValue ( a , v ) ) ;
Find common elements in three sorted arrays | This function prints common elements in ar1 ; Initialize starting indexes for ar1 [ ] , ar2 [ ] and ar3 [ ] ; Declare three variables prev1 , prev2 , prev3 to track previous element ; Initialize prev1 , prev2 , prev3 with INT_MIN ; Iterate through three arrays while all arrays have elements ; If ar1 [ i ] = prev1 and i < n1 , keep incrementing i ; If ar2 [ j ] = prev2 and j < n2 , keep incrementing j ; If ar3 [ k ] = prev3 and k < n3 , keep incrementing k ; If x = y and y = z , print any of them , update prev1 prev2 , prev3 and move ahead in each array ; If x < y , update prev1 and increment i ; If y < z , update prev2 and increment j ; We reach here when x > y and z < y , i . e . , z is smallest update prev3 and imcrement k ; Driver code
function findCommon ( ar1 , ar2 , ar3 , n1 , n2 , n3 ) { var i = 0 , j = 0 , k = 0 ; var prev1 , prev2 , prev3 ; prev1 = prev2 = prev3 = - 1000000000 ; while ( i < n1 && j < n2 && k < n3 ) { while ( ar1 [ i ] == prev1 && i < n1 ) i ++ ; while ( ar2 [ j ] == prev2 && j < n2 ) j ++ ; while ( ar3 [ k ] == prev3 && k < n3 ) k ++ ; if ( ar1 [ i ] == ar2 [ j ] && ar2 [ j ] == ar3 [ k ] ) { document . write ( ar1 [ i ] + " " ) ; prev1 = ar1 [ i ] ; prev2 = ar2 [ j ] ; prev3 = ar3 [ k ] ; i ++ ; j ++ ; k ++ ; } else if ( ar1 [ i ] < ar2 [ j ] ) { prev1 = ar1 [ i ] ; i ++ ; } else if ( ar2 [ j ] < ar3 [ k ] ) { prev2 = ar2 [ j ] ; j ++ ; } else { prev3 = ar3 [ k ] ; k ++ ; } } } var ar1 = [ 1 , 5 , 10 , 20 , 40 , 80 , 80 ] ; var ar2 = [ 6 , 7 , 20 , 80 , 80 , 100 ] ; var ar3 = [ 3 , 4 , 15 , 20 , 30 , 70 , 80 , 80 , 120 ] ; var n1 = ar1 . length ; var n2 = ar2 . length ; var n3 = ar3 . length ; document . write ( " " ) ; findCommon ( ar1 , ar2 , ar3 , n1 , n2 , n3 ) ;
Jacobsthal and Jacobsthal | Return nth Jacobsthal number . ; Base case ; Return nth Jacobsthal - Lucas number . ; Base case ; Driver code
function Jacobsthal ( n ) { let dp = new Array ( n + 1 ) ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( let i = 2 ; i <= n ; i ++ ) dp [ i ] = dp [ i - 1 ] + 2 * dp [ i - 2 ] ; return dp [ n ] ; } function Jacobsthal_Lucas ( n ) { let dp = new Array ( n + 1 ) ; dp [ 0 ] = 2 ; dp [ 1 ] = 1 ; for ( let i = 2 ; i <= n ; i ++ ) dp [ i ] = dp [ i - 1 ] + 2 * dp [ i - 2 ] ; return dp [ n ] ; } let n = 5 ; document . write ( " " + Jacobsthal ( n ) + " " ) ; document . write ( " " + Jacobsthal_Lucas ( n ) ) ;
Find position of an element in a sorted array of infinite numbers | Simple binary search algorithm ; function takes an infinite size array and a key to be searched and returns its position if found else - 1. We don 't know size of arr[] and we can assume size to be infinite in this function. NOTE THAT THIS FUNCTION ASSUMES arr[] TO BE OF INFINITE SIZE THEREFORE, THERE IS NO INDEX OUT OF BOUND CHECKING ; Find h to do binary search ; store previous high ; double high index ; update new val ; at this point we have updated low and high indices , Thus use binary search between them ; Driver program
function binarySearch ( arr , l , r , x ) { if ( r >= l ) { let mid = l + Math . floor ( ( r - l ) / 2 ) ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; } function findPos ( arr , key ) { let l = 0 , h = 1 ; let val = arr [ 0 ] ; while ( val < key ) { l = h ; h = 2 * h ; val = arr [ h ] ; } return binarySearch ( arr , l , h , key ) ; } let arr = [ 3 , 5 , 7 , 9 , 10 , 90 , 100 , 130 , 140 , 160 , 170 ] ; let ans = findPos ( arr , 10 ) ; if ( ans == - 1 ) document . write ( " " ) ; else document . write ( " " + ans ) ;
Count of possible hexagonal walks | Javascript implementation of counting number of possible hexagonal walks ; We initialize our origin with 1 ; For each N = 1 to 14 , we traverse in all possible direction . Using this 3D array we calculate the number of ways at each step and the total ways for a given step shall be found at ways [ step number ] [ 8 ] [ 8 ] because all the steps after that will be used to trace back to the original point index 0 : 0 according to the image . ; This array stores the number of ways possible for a given step ; Driver program to test above function ; Preprocessing all possible ways
let depth = 14 ; let ways = new Array ( 16 ) ; for ( let i = 0 ; i < 16 ; i ++ ) { ways [ i ] = new Array ( 16 ) ; for ( let j = 0 ; j < 16 ; j ++ ) { ways [ i ] [ j ] = new Array ( 16 ) ; for ( let k = 0 ; k < 16 ; k ++ ) { ways [ i ] [ j ] [ k ] = 0 ; } } } let stepNum ; function preprocess ( list ) { ways [ 0 ] [ 8 ] [ 8 ] = 1 ; for ( let N = 1 ; N <= 14 ; N ++ ) { for ( let i = 1 ; i < depth ; i ++ ) { for ( let j = 1 ; j < depth ; j ++ ) { ways [ N ] [ i ] [ j ] = ways [ N - 1 ] [ i ] [ j + 1 ] + ways [ N - 1 ] [ i ] [ j - 1 ] + ways [ N - 1 ] [ i + 1 ] [ j ] + ways [ N - 1 ] [ i - 1 ] [ j ] + ways [ N - 1 ] [ i + 1 ] [ j - 1 ] + ways [ N - 1 ] [ i - 1 ] [ j + 1 ] ; } } list [ N ] = ways [ N ] [ 8 ] [ 8 ] ; } } let list = new Array ( 15 ) ; list . fill ( 0 ) ; preprocess ( list ) ; let steps = 4 ; document . write ( " " + " " + list [ steps ] ) ;
Number of n digit stepping numbers | Function that calculates the answer ; dp [ i ] [ j ] stores count of i digit stepping numbers ending with digit j . ; If n is 1 then answer will be 10. ; Initialize values for count of digits equal to 1. ; Compute values for count of digits more than 1. ; If ending digit is 0 ; If ending digit is 9 ; For other digits . ; Stores the final answer ; Driver Code
function answer ( n ) { let dp = new Array ( n + 1 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } if ( n == 1 ) return 10 ; for ( let j = 0 ; j <= 9 ; j ++ ) dp [ 1 ] [ j ] = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { for ( let j = 0 ; j <= 9 ; j ++ ) { if ( j == 0 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j + 1 ] ; else if ( j == 9 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] + dp [ i - 1 ] [ j + 1 ] ; } } let sum = 0 ; for ( let j = 1 ; j <= 9 ; j ++ ) sum += dp [ n ] [ j ] ; return sum ; } let n = 2 ; document . write ( answer ( n ) ) ;
Print Longest Palindromic Subsequence | Returns LCS X and Y ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; Following code is used to print LCS ; Create a String length index + 1 and fill it with \ 0 ; Start from the right - most - bottom - most corner and one by one store characters in lcs [ ] ; If current character in X [ ] and Y are same , then current character is part of LCS ; Put current character in result ; reduce values of i , j and index ; If not same , then find the larger of two and go in the direction of larger value ; Returns longest palindromic subsequence of str ; Find reverse of str ; Return LCS of str and its reverse ; convert String to character array by using toCharArray ; Driver program to test above function
function lcs ( a , b ) { let m = a . length ; let n = b . length ; let X = a . split ( ' ' ) ; let Y = b . split ( ' ' ) ; 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 ] + 1 ; } else { L [ i ] [ j ] = Math . max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } } let index = L [ m ] [ n ] ; let lcs = new Array ( index + 1 ) ; lcs . fill ( ' ' ) ; let i = m , j = n ; while ( i > 0 && j > 0 ) { if ( X [ i - 1 ] == Y [ j - 1 ] ) { lcs [ index - 1 ] = X [ i - 1 ] ; i -- ; j -- ; index -- ; } else if ( L [ i - 1 ] [ j ] > L [ i ] [ j - 1 ] ) { i -- ; } else { j -- ; } } let ans = " " ; for ( let x = 0 ; x < lcs . length ; x ++ ) { ans += lcs [ x ] ; } return ans ; } function longestPalSubseq ( str ) { let rev = str ; rev = reverse ( rev ) ; return lcs ( str , rev ) ; } function reverse ( str ) { let ans = " " ; let try1 = str . split ( ' ' ) ; for ( let i = try1 . length - 1 ; i >= 0 ; i -- ) { ans += try1 [ i ] ; } return ans ; } let str = " " ; document . write ( longestPalSubseq ( str ) ) ;
Count all subsequences having product less than K | Function to count numbers of such subsequences having product less than k . ; number of subsequence using j - 1 terms ; if arr [ j - 1 ] > i it will surely make product greater thus it won 't contribute  then ; number of subsequence using 1 to j - 1 terms and j - th term ; Driver code
function productSubSeqCount ( arr , k ) { let n = arr . length ; let dp = new Array ( k + 1 ) ; for ( let i = 0 ; i < k + 1 ; i ++ ) { dp [ i ] = new Array ( n + 1 ) ; for ( let j = 0 ; j < n + 1 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let i = 1 ; i <= k ; i ++ ) { for ( let j = 1 ; j <= n ; j ++ ) { dp [ i ] [ j ] = dp [ i ] [ j - 1 ] ; if ( arr [ j - 1 ] <= i && arr [ j - 1 ] > 0 ) dp [ i ] [ j ] += dp [ parseInt ( i / arr [ j - 1 ] , 10 ) ] [ j - 1 ] + 1 ; } } return dp [ k ] [ n ] ; } let A = [ 1 , 2 , 3 , 4 ] ; let k = 10 ; document . write ( productSubSeqCount ( A , k ) ) ;
Find the element that appears once in an array where every other element appears twice | Return the maximum Sum of difference between consecutive elements . ; Do XOR of all elements and return ; Driver code
function findSingle ( ar , ar_size ) { let res = ar [ 0 ] ; for ( let i = 1 ; i < ar_size ; i ++ ) res = res ^ ar [ i ] ; return res ; } let ar = [ 2 , 3 , 5 , 4 , 5 , 3 , 4 ] ; let n = ar . length ; document . write ( " " + findSingle ( ar , n ) ) ;
Find the element that appears once in an array where every other element appears twice | function which find number ; applying the formula . ; Driver code
function singleNumber ( nums , n ) { let m = new Map ( ) ; let sum1 = 0 , sum2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! m . has ( nums [ i ] ) ) { sum1 += nums [ i ] ; m . set ( nums [ i ] , 1 ) ; } sum2 += nums [ i ] ; } return ( 2 * ( sum1 ) - sum2 ) ; } let a = [ 2 , 3 , 5 , 4 , 5 , 3 , 4 ] ; let n = 7 ; document . write ( singleNumber ( a , n ) + " " ) ; let b = [ 15 , 18 , 16 , 18 , 16 , 15 , 89 ] ; document . write ( singleNumber ( b , n ) ) ;
Maximum Subarray Sum Excluding Certain Elements | Function to check the element present in array B ; Utility function for findMaxSubarraySum ( ) with the following parameters A = > Array A , B = > Array B , n = > Number of elements in Array A , m = > Number of elements in Array B ; set max_so_far to LET_MIN ; if the element is present in B , set current max to 0 and move to the next element ; Proceed as in Kadane 's Algorithm ; Wrapper for findMaxSubarraySumUtil ( ) ; This case will occour when all elements of A are present in B , thus no subarray can be formed ; Driver code ; Function call
function isPresent ( B , m , x ) { for ( let i = 0 ; i < m ; i ++ ) if ( B [ i ] == x ) return true ; return false ; } function findMaxSubarraySumUtil ( A , B , n , m ) { let max_so_far = - 2147483648 , curr_max = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( isPresent ( B , m , A [ i ] ) ) { curr_max = 0 ; continue ; } curr_max = Math . max ( A [ i ] , curr_max + A [ i ] ) ; max_so_far = Math . max ( max_so_far , curr_max ) ; } return max_so_far ; } function findMaxSubarraySum ( A , B , n , m ) { let maxSubarraySum = findMaxSubarraySumUtil ( A , B , n , m ) ; if ( maxSubarraySum == - 2147483648 ) { document . write ( " " + " " + " " ) ; } else { document . write ( " " + maxSubarraySum ) ; } } let A = [ 3 , 4 , 5 , - 4 , 6 ] ; let B = [ 1 , 8 , 5 ] ; let n = A . length ; let m = B . length ; findMaxSubarraySum ( A , B , n , m ) ;
Number of n | Function that returns count of non - decreasing numbers with n digits ; a [ i ] [ j ] = count of all possible number with i digits having leading digit as j ; Initialization of all 0 - digit number ; Initialization of all i - digit non - decreasing number leading with 9 ; for all digits we should calculate number of ways depending upon leading digits ; driver program
function nonDecNums ( n ) { let a = new Array ( n + 1 ) for ( let i = 0 ; i < n + 1 ; i ++ ) { a [ i ] = new Array ( 10 ) ; } for ( let i = 0 ; i <= 9 ; i ++ ) a [ 0 ] [ i ] = 1 ; for ( let i = 1 ; i <= n ; i ++ ) a [ i ] [ 9 ] = 1 ; for ( let i = 1 ; i <= n ; i ++ ) for ( let j = 8 ; j >= 0 ; j -- ) a [ i ] [ j ] = a [ i - 1 ] [ j ] + a [ i ] [ j + 1 ] ; return a [ n ] [ 0 ] ; } let n = 2 ; document . write ( " " + nonDecNums ( n ) ) ;
Count Balanced Binary Trees of Height h | Javascript program to count number of balanced binary trees of height h . ; base cases ; Driver program
let MOD = 1000000007 ; function countBT ( h ) { let dp = new Array ( h + 1 ) ; dp . fill ( 0 ) ; dp [ 0 ] = 1 ; dp [ 1 ] = 1 ; for ( let i = 2 ; i <= h ; ++ i ) dp [ i ] = ( dp [ i - 1 ] * ( ( 2 * dp [ i - 2 ] ) % MOD + dp [ i - 1 ] ) % MOD ) % MOD ; return dp [ h ] ; } let h = 3 ; document . write ( " " + countBT ( h ) ) ;