codeparrot/xlcost-text-to-code huggingface dataset:
Count ways to remove pairs from a matrix such that remaining elements can be grouped in vertical or horizontal pairs | Function to count ways to remove pairs such that the remaining elements can be arranged in pairs vertically or horizontally ; Store the size of matrix ; If N is odd , then no such pair exists ; Store the number of required pairs ; Initialize an auxiliary matrix and fill it with 0 s ; Traverse the matrix v [ ] [ ] ; Check if i + j is odd or even ; Increment the value dp [ v [ i ] [ j ] - 1 ] [ 0 ] by 1 ; Increment the value dp [ v [ i ] [ j ] - 1 ] [ 1 ] by 1 ; Iterate in range [ 0 , k - 1 ] using i ; Iterate in range [ i + 1 , k - 1 ] using j ; Update the ans ; Print the answer ; Driver code
function numberofpairs ( v , k ) { let n = v . length ; if ( n % 2 == 1 ) { document . write ( 0 ) ; return ; } let ans = 0 ; let dp = new Array ( k ) ; for ( let i = 0 ; i < k ; i ++ ) { dp [ i ] = new Array ( 2 ) ; for ( let j = 0 ; j < 2 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( ( i + j ) % 2 == 0 ) dp [ v [ i ] [ j ] - 1 ] [ 0 ] ++ ; else dp [ v [ i ] [ j ] - 1 ] [ 1 ] ++ ; } } for ( let i = 0 ; i < k ; i ++ ) { for ( let j = i + 1 ; j < k ; j ++ ) { ans += dp [ i ] [ 0 ] * dp [ j ] [ 1 ] ; ans += dp [ i ] [ 1 ] * dp [ j ] [ 0 ] ; } } document . write ( ans + " " ) ; } let mat = [ [ 1 , 2 ] , [ 3 , 4 ] ] ; let K = 4 ; numberofpairs ( mat , K ) ;
Maximize sum by selecting X different | Store overlapping subproblems of the recurrence relation ; Function to find maximum sum of at most N with different index array elements such that at most X are from A [ ] , Y are from B [ ] and Z are from C [ ] ; Base Cases ; If the subproblem already computed ; Selecting i - th element from A [ ] ; Selecting i - th element from B [ ] ; Selecting i - th element from C [ ] ; i - th elements not selected from any of the arrays ; Select the maximum sum from all the possible calls ; Given X , Y and Z ; Given A [ ] ; Given B [ ] ; Given C [ ] ; Given Size ; Function Call
let dp = new Array ( 50 ) ; function FindMaxS ( X , Y , Z , n , A , B , C ) { if ( X < 0 Y < 0 Z < 0 ) return Number . MIN_VALUE ; if ( n < 0 ) return 0 ; if ( dp [ n ] [ X ] [ Y ] [ Z ] != - 1 ) { return dp [ n ] [ X ] [ Y ] [ Z ] ; } let ch = A [ n ] + FindMaxS ( X - 1 , Y , Z , n - 1 , A , B , C ) ; let ca = B [ n ] + FindMaxS ( X , Y - 1 , Z , n - 1 , A , B , C ) ; let co = C [ n ] + FindMaxS ( X , Y , Z - 1 , n - 1 , A , B , C ) ; let no = FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ; let maximum = Math . max ( ch , Math . max ( ca , Math . max ( co , no ) ) ) ; dp [ n ] [ X ] [ Y ] [ Z ] = maximum ; return dp [ n ] [ X ] [ Y ] [ Z ] ; } let X = 1 ; let Y = 1 ; let Z = 1 ; let A = [ 10 , 0 , 5 ] ; let B = [ 5 , 10 , 0 ] ; let C = [ 0 , 5 , 10 ] ; let n = B . length ; for ( let i = 0 ; i < 50 ; i ++ ) { dp [ i ] = new Array ( 50 ) ; for ( let j = 0 ; j < 50 ; j ++ ) { dp [ i ] [ j ] = new Array ( 50 ) ; for ( let k = 0 ; k < 50 ; k ++ ) { dp [ i ] [ j ] [ k ] = new Array ( 50 ) ; for ( let l = 0 ; l < 50 ; l ++ ) { dp [ i ] [ j ] [ k ] [ l ] = - 1 ; } } } } document . write ( FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ) ;
Minimize swaps of adjacent characters to sort every possible rearrangement of given Binary String | Javascript program for the above approach ; Precalculate the values of power of 2 ; Function to calculate 2 ^ N % mod ; Function to find sum of inversions ; Initialise a list of 0 s and ? s ; Traverse the string in the reversed manner ; If the current character is 1 ; Effectively calculate a * b ^ ( b - 1 ) ; If the current character is 0 ; Increment count of zeroes ; Double count the zeroes ; Find b * 2 ^ ( b - 1 ) ; Increment count of questions ; Return the final count ; Given string S ; Function Call
let MOD = 1000000007 ; let MEM = [ 1 , 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 , 512 , 1024 , 2048 , 4096 ] ; function mod_pow2 ( n ) { while ( n >= MEM . length ) MEM . push ( ( MEM [ MEM . length - 1 ] * 2 ) % MOD ) ; return MEM [ n ] ; } function inversions ( bstr ) { let total = 0 , zeros = 0 , questions = 0 ; bstr . reverse ( ) ; for ( let i = 0 ; i < bstr . length ; i ++ ) { let q ; if ( bstr [ i ] == ' ' ) { let z = zeros * mod_pow2 ( questions ) ; if ( questions == 0 ) q = 0 ; else q = questions * mod_pow2 ( questions - 1 ) ; total = ( total + z + q ) % MOD ; } else if ( bstr [ i ] == ' ' ) { zeros += 1 ; } else { total *= 2 ; let z = zeros * mod_pow2 ( questions ) ; if ( questions == 0 ) q = 0 ; else q = questions * mod_pow2 ( questions - 1 ) ; total = ( total + z + q ) % MOD ; questions += 1 ; } } return total ; } let S = " " . split ( ' ' ) ; document . write ( inversions ( S ) ) ;
Minimum removals required to convert given array to a Mountain Array | Utility function to count array elements required to be removed to make array a mountain array ; Stores length of increasing subsequence from [ 0 , i - 1 ] ; Stores length of increasing subsequence from [ i + 1 , n - 1 ] ; Iterate for each position up to N - 1 to find the length of subsequence ; If j is less than i , then i - th position has leftIncreasing [ j ] + 1 lesser elements including itself ; Check if it is the maximum obtained so far ; Search for increasing subsequence from right ; Find the position following the peak condition and have maximum leftIncreasing [ i ] + rightIncreasing [ i ] ; Function to count elements to be removed to make array a mountain array ; Print the answer ; Given array ; Function Call
function minRemovalsUtil ( arr , n ) { var result = 0 ; if ( n < 3 ) { return - 1 ; } var leftIncreasing = Array ( n ) . fill ( 0 ) ; var rightIncreasing = Array ( n ) . fill ( 0 ) ; for ( var i = 1 ; i < n ; i ++ ) { for ( var j = 0 ; j < i ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { leftIncreasing [ i ] = Math . max ( leftIncreasing [ i ] , leftIncreasing [ j ] + 1 ) ; } } } for ( var i = n - 2 ; i >= 0 ; i -- ) { for ( var j = n - 1 ; j > i ; j -- ) { if ( arr [ j ] < arr [ i ] ) { rightIncreasing [ i ] = Math . max ( rightIncreasing [ i ] , rightIncreasing [ j ] + 1 ) ; } } } for ( var i = 0 ; i < n ; i ++ ) { if ( leftIncreasing [ i ] != 0 && rightIncreasing [ i ] != 0 ) { result = Math . max ( result , leftIncreasing [ i ] + rightIncreasing [ i ] ) ; } } return n - ( result + 1 ) ; } function minRemovals ( arr , n ) { var ans = minRemovalsUtil ( arr , n ) ; document . write ( ans ) ; } var arr = [ 2 , 1 , 1 , 5 , 6 , 2 , 3 , 1 ] ; var n = arr . length ; minRemovals ( arr , n ) ;
Count of numbers from range [ L , R ] whose sum of digits is Y | Set 2 | Function to find the sum of digits of numbers in the range [ 0 , X ] ; Check if count of digits in a number greater than count of digits in X ; Check Iif sum of digits of a number is equal to Y ; Stores count of numbers whose sum of digits is Y ; Check if the number exceeds Y or not ; Iterate over all possible values of i - th digits ; Update res ; Return res ; Utility function to count the numbers in the range [ L , R ] whose sum of digits is Y ; Base Case ; Stores numbers in the form of its equivalent String ; Stores count of numbers in the range [ 0 , R ] ; Update str ; Stores count of numbers in the range [ 0 , L - 1 ] ; Driver Code
function cntNum ( X , i , sum , tight ) { if ( i >= X . length sum < 0 ) { if ( sum == 0 ) { return 1 ; } return 0 ; } var res = 0 ; var end = tight != 0 ? X [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) : 9 ; for ( var j = 0 ; j <= end ; j ++ ) { res += cntNum ( X , i + 1 , sum - j , ( tight > 0 & ( j == end ) ) == true ? 1 : 0 ) ; } return res ; } function UtilCntNumRange ( L , R , Y ) { if ( R == 0 && Y == 0 ) { return 1 ; } var str = ( R ) . toString ( ) ; var cntR = cntNum ( str , 0 , Y , 1 ) ; str = ( L - 1 ) . toString ( ) ; var cntL = cntNum ( str , 0 , Y , 1 ) ; return ( cntR - cntL ) ; } var L = 20 , R = 10000 , Y = 14 ; document . write ( UtilCntNumRange ( L , R , Y ) ) ;
Minimum number of jumps to obtain an element of opposite parity | Bfs for odd numbers are source ; Initialize queue ; Stores for each node , the nodes visited and their distances ; If parity is 0 -> odd Otherwise -> even ; Perform multi - source bfs ; Extract the front element of the queue ; Traverse nodes connected to the current node ; If u is not visited ; If element with opposite parity is obtained ; Store its distance from source in ans [ ] ; Push the current neighbour to the queue ; Function to find the minimum jumps required by each index to reach element of opposite parity ; Initialise Inverse Graph ; Stores the result for each index ; For the jumped index ; If the ind is valid then add reverse directed edge ; Multi - source bfs with odd numbers as source by passing 0 ; Multi - source bfs with even numbers as source by passing 1 ; Print the answer ; Driver Code
function bfs ( n , a , invGr , ans , parity ) { var q = [ ] ; var vis = Array ( n + 1 ) . fill ( 0 ) ; var dist = Array ( n + 1 ) . fill ( 0 ) ; for ( var i = 1 ; i <= n ; i ++ ) { if ( ( a [ i ] + parity ) & 1 ) { q . push ( i ) ; vis [ i ] = 1 ; } } while ( q . length != 0 ) { var v = q [ 0 ] ; q . shift ( ) ; invGr [ v ] . forEach ( u => { if ( ! vis [ u ] ) { dist [ u ] = dist [ v ] + 1 ; vis [ u ] = 1 ; if ( ( a [ u ] + parity ) % 2 == 0 ) { if ( ans [ u ] == - 1 ) ans [ u ] = dist [ u ] ; } q . push ( u ) ; } } ) ; } return ans ; } function minJumps ( a , jump , n ) { var invGr = Array . from ( Array ( n + 1 ) , ( ) => Array ( ) ) var ans = Array ( n + 1 ) . fill ( - 1 ) ; for ( var i = 1 ; i <= n ; i ++ ) { [ i + jump [ i ] , i - jump [ i ] ] . forEach ( ind => { if ( ind >= 1 && ind <= n ) { invGr [ ind ] . push ( i ) ; } } ) ; } ans = bfs ( n , a , invGr , ans , 0 ) ; ans = bfs ( n , a , invGr , ans , 1 ) ; for ( var i = 1 ; i <= n ; i ++ ) { document . write ( ans [ i ] + ' ' ) ; } } var arr = [ 0 , 4 , 2 , 5 , 2 , 1 ] ; var jump = [ 0 , 1 , 2 , 3 , 1 , 2 ] ; var N = arr . length ; minJumps ( arr , jump , N - 1 ) ;
Longest subsequence having maximum GCD between any pair of distinct elements | Recursive function to return gcd of a and b ; Function to find GCD of pair with maximum GCD ; Stores maximum element of arr ; Find the maximum element ; Maintain a count array ; Store the frequency of arr ; Stores the multiples of a number ; Iterate over the range [ MAX , 1 ] ; Iterate from current potential GCD till it is less than MAX ; A multiple found ; Increment potential GCD by itself io check i , 2 i , 3 i ... ; If 2 multiples found max GCD found ; Function to find longest subsequence such that GCD of any two distinct integers is maximum ; Base Cases ; Compare current GCD to the maximum GCD ; If true increment and move the pointer ; Return max of either subsequences ; Driver Code ; Sorted array ; Function call to calculate GCD of pair with maximum GCD ; Prvar the result
function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } function findMaxGCD ( arr , N ) { var high = 0 ; for ( i = 0 ; i < N ; i ++ ) { high = Math . max ( high , arr [ i ] ) ; } var count = Array ( high + 1 ) . fill ( 0 ) ; for ( i = 0 ; i < N ; i ++ ) { count [ arr [ i ] ] += 1 ; } var counter = 0 ; for ( i = high ; i > 0 ; i -- ) { var j = i ; while ( j <= high ) { if ( count [ j ] > 0 ) counter += count [ j ] ; j += i ; if ( counter == 2 ) return i ; } counter = 0 ; } return 0 ; } function maxlen ( i , j , arr , arr1 , N , maxgcd ) { var a = 1 ; if ( i >= N j >= N ) return 0 ; if ( __gcd ( arr [ i ] , arr1 [ j ] ) == maxgcd && arr [ i ] != arr1 [ j ] ) { a = Math . max ( a , 1 + maxlen ( i , j + 1 , arr , arr1 , N , maxgcd ) ) ; return a ; } return Math . max ( maxlen ( i + 1 , j , arr , arr1 , N , maxgcd ) , maxlen ( i , j + 1 , arr , arr1 , N , maxgcd ) ) ; } var arr = [ 1 , 2 , 8 , 5 , 6 ] ; var arr1 = [ 1 , 2 , 8 , 5 , 6 ] ; var n = arr . length ; arr . sort ( ) ; var maxgcd = findMaxGCD ( arr , n ) ; document . write ( maxlen ( 0 , 0 , arr , arr1 , n , maxgcd ) ) ;
Count unique paths is a matrix whose product of elements contains odd number of divisors | Stores the results ; Count of unique product paths ; Function to check whether number is perfect square or not ; If square root is an integer ; Function to calculate and store all the paths product in vector ; Store the value a [ 0 ] [ 0 ] ; Initialize first row of dp ; Find prefix product ; Initialize first column of dp ; Find the prefix product ; Iterate over range ( 1 , 1 ) to ( N , M ) ; Copy dp [ i - 1 ] [ j ] in top [ ] ; Copy dp [ i ] [ j - 1 ] into left [ ] ; Compute the values of current state and store it in curr [ ] ; Find the product of a [ i ] [ j ] with elements at top [ ] ; Find the product of a [ i ] [ j ] with elements at left [ ] ; Update the current state ; Traverse dp [ m - 1 ] [ n - 1 ] ; Check if perfect square ; Driver code ; Given matrix mat [ ] [ ] ; Function Call ; Print the final count
let dp = [ ] ; let countPaths = 0 ; function isPerfectSquare ( n ) { let sr = Math . sqrt ( n ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } function countUniquePaths ( a , m , n , ans ) { dp [ 0 ] [ 0 ] . push ( a [ 0 ] [ 0 ] ) ; for ( let i = 1 ; i < m ; i ++ ) { a [ i ] [ 0 ] *= a [ i - 1 ] [ 0 ] ; dp [ i ] [ 0 ] . push ( a [ i ] [ 0 ] ) ; } for ( let i = 1 ; i < n ; i ++ ) { a [ 0 ] [ i ] *= a [ 0 ] [ i - 1 ] ; dp [ 0 ] [ i ] . push ( a [ 0 ] [ i ] ) ; } for ( let i = 1 ; i < m ; i ++ ) { for ( let j = 1 ; j < n ; j ++ ) { let top = dp [ i - 1 ] [ j ] ; let left = dp [ i ] [ j - 1 ] ; let curr = [ ] ; for ( let k = 0 ; k < top . length ; k ++ ) { curr . push ( top [ k ] * a [ i ] [ j ] ) ; } for ( let k = 0 ; k < left . length ; k ++ ) { curr . push ( left [ k ] * a [ i ] [ j ] ) ; } dp [ i ] [ j ] = curr ; } } for ( let i = 0 ; i < dp [ m - 1 ] [ n - 1 ] . length ; i ++ ) { if ( isPerfectSquare ( dp [ m - 1 ] [ n - 1 ] [ i ] ) ) { countPaths ++ ; } } } let M = 3 , N = 4 ; let mat = [ [ 1 , 2 , 3 , 1 ] , [ 3 , 1 , 2 , 4 ] , [ 2 , 3 , 1 , 1 ] ] ; for ( let i = 0 ; i < 105 ; i ++ ) { dp . push ( [ ] ) ; for ( let j = 0 ; j < 105 ; j ++ ) dp [ i ] . push ( [ ] ) ; } countUniquePaths ( mat , M , N , 1 ) ; document . write ( countPaths ) ;
Generate a combination of minimum coins that sums to a given value | Javascript program for the above approach ; dp array to memoize the results ; List to store the result ; Function to find the minimum number of coins to make the sum equals to X ; Base case ; If previously computed subproblem occurred ; Initialize result ; Try every coin that has smaller value than n ; Check for INT_MAX to avoid overflow and see if result can be minimized ; Memoizing value of current state ; Function to find the possible combination of coins to make the sum equal to X ; Base Case ; Print Solutions ; Try every coin that has value smaller than n ; Add current denominations ; Backtrack ; Function to find the minimum combinations of coins for value X ; Initialize dp with - 1 ; Min coins ; If no solution exists ; Backtrack to find the solution ; Driver code ; Set of possible denominations ; Function Call
var MAX = 100000 ; var dp = Array ( MAX + 1 ) ; var denomination = [ ] ; function countMinCoins ( n , C , m ) { if ( n == 0 ) { dp [ 0 ] = 0 ; return 0 ; } if ( dp [ n ] != - 1 ) return dp [ n ] ; var ret = 1000000000 ; for ( var i = 0 ; i < m ; i ++ ) { if ( C [ i ] <= n ) { var x = countMinCoins ( n - C [ i ] , C , m ) ; if ( x != 1000000000 ) ret = Math . min ( ret , 1 + x ) ; } } dp [ n ] = ret ; return ret ; } function findSolution ( n , C , m ) { if ( n == 0 ) { denomination . forEach ( it => { document . write ( it + ' ' ) ; } ) ; return ; } for ( var i = 0 ; i < m ; i ++ ) { if ( n - C [ i ] >= 0 && dp [ n - C [ i ] ] + 1 == dp [ n ] ) { denomination . push ( C [ i ] ) ; findSolution ( n - C [ i ] , C , m ) ; break ; } } } function countMinCoinsUtil ( X , C , N ) { dp = Array ( MAX + 1 ) . fill ( - 1 ) ; var isPossible = countMinCoins ( X , C , N ) ; if ( isPossible == 1000000000 ) { document . write ( " " ) ; } else { findSolution ( X , C , N ) ; } } var X = 21 ; var arr = [ 2 , 3 , 4 , 5 ] ; var N = arr . length ; countMinCoinsUtil ( X , arr , N ) ;
Count all possible paths from top left to bottom right of a Matrix without crossing the diagonal | Function to calculate Binomial Coefficient C ( n , r ) ; C ( n , r ) = C ( n , n - r ) ; [ n * ( n - 1 ) * -- - * ( n - r + 1 ) ] / [ r * ( r - 1 ) * -- -- * 1 ] ; Function to calculate the total possible paths ; Update n to n - 1 as ( N - 1 ) catalan number is the result ; Stores 2 nCn ; Stores Nth Catalan number ; Stores the required answer ; Driver Code
function binCoff ( n , r ) { var val = 1 ; var i ; if ( r > ( n - r ) ) { r = ( n - r ) ; } for ( i = 0 ; i < r ; i ++ ) { val *= ( n - i ) ; val /= ( i + 1 ) ; } return val ; } function findWays ( n ) { n -- ; var a , b , ans ; a = binCoff ( 2 * n , n ) ; b = a / ( n + 1 ) ; ans = b ; return ans ; } var n = 4 ; document . write ( findWays ( n ) ) ;
Minimum operations to transform given string to another by moving characters to front or end | Javascript program for the above approach ; Function that finds the minimum number of steps to find the minimum characters must be moved to convert string s to t ; r = maximum value over all dp [ i ] [ j ] computed so far ; dp [ i ] [ j ] stores the longest contiguous suffix of T [ 0. . j ] that is subsequence of S [ 0. . i ] ; Update the maximum length ; Return the resulting length ; Given string s , t ; Function Call
var dp = Array . from ( Array ( 1010 ) , ( ) => Array ( 1010 ) ) ; function solve ( s , t ) { var n = s . length ; var r = 0 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] = 0 ; if ( i > 0 ) { dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j ] ) ; } if ( s [ i ] == t [ j ] ) { var ans = 1 ; if ( i > 0 && j > 0 ) { ans = 1 + dp [ i - 1 ] [ j - 1 ] ; } dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , ans ) ; r = Math . max ( r , dp [ i ] [ j ] ) ; } } } return ( n - r ) ; } var s = " " ; var t = " " ; document . write ( solve ( s , t ) ) ;
Longest substring whose characters can be rearranged to form a Palindrome | Function to get the length of longest substring whose characters can be arranged to form a palindromic string ; To keep track of the last index of each xor ; Initialize answer with 0 ; Now iterate through each character of the string ; Convert the character from [ a , z ] to [ 0 , 25 ] ; Turn the temp - th bit on if character occurs odd number of times and turn off the temp - th bit off if the character occurs ever number of times ; If a mask is present in the index Therefore a palindrome is found from index [ mask ] to i ; If x is not found then add its position in the index dict . ; Check for the palindrome of odd length ; We cancel the occurrence of a character if it occurs odd number times ; Given String ; Length of given string ; Function call
function longestSubstring ( s , n ) { var index = new Map ( ) ; var answer = 0 ; var mask = 0 ; index [ mask ] = - 1 ; for ( var i = 0 ; i < n ; i ++ ) { var temp = s [ i ] . charCodeAt ( 0 ) - 97 ; mask ^= ( 1 << temp ) ; if ( index [ mask ] ) { answer = Math . max ( answer , i - index [ mask ] ) ; } else index [ mask ] = i ; for ( var j = 0 ; j < 26 ; j ++ ) { var mask2 = mask ^ ( 1 << j ) ; if ( index [ mask2 ] ) { answer = Math . max ( answer , i - index [ mask2 ] ) ; } } } return answer ; } var s = " " ; var n = s . length ; document . write ( longestSubstring ( s , n ) ) ;
Count of N | Function to find count of N - digit numbers with single digit XOR ; dp [ i ] [ j ] stores the number of i - digit numbers with XOR equal to j ; For 1 - 9 store the value ; Iterate till N ; Calculate XOR ; Store in DP table ; Initialize count ; Prvar the answer ; Given number N ; Function Call
function countNums ( N ) { var dp = Array ( N ) ; for ( var i = 0 ; i < N ; i ++ ) dp [ i ] = Array ( 16 ) . fill ( 0 ) ; for ( i = 1 ; i <= 9 ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( i = 1 ; i < N ; i ++ ) { for ( j = 0 ; j < 10 ; j ++ ) { for ( k = 0 ; k < 16 ; k ++ ) { var xor = j ^ k ; dp [ i ] [ xor ] += dp [ i - 1 ] [ k ] ; } } } var count = 0 ; for ( i = 0 ; i < 10 ; i ++ ) count += dp [ N - 1 ] [ i ] ; document . write ( count ) ; } var N = 1 ; countNums ( N ) ;
Print the Maximum Subarray Sum | Function to print the elements of Subarray with maximum sum ; Initialize currMax and globalMax with first value of nums ; Initialize endIndex startIndex , globalStartIndex ; Iterate for all the elemensts of the array ; Update currMax and startIndex ; startIndex = i ; update the new startIndex ; Update currMax ; Update globalMax anf globalMaxStartIndex ; Printing the elements of subarray with max sum ; Given array arr [ ] ; Function call
function SubarrayWithMaxSum ( nums ) { let currMax = nums [ 0 ] , globalMax = nums [ 0 ] ; let endIndex = 0 ; let startIndex = 0 , globalMaxStartIndex = 0 ; for ( let i = 1 ; i < nums . length ; ++ i ) { if ( nums [ i ] > nums [ i ] + currMax ) { currMax = nums [ i ] ; } else if ( nums [ i ] < nums [ i ] + currMax ) { currMax = nums [ i ] + currMax ; } if ( currMax > globalMax ) { globalMax = currMax ; endIndex = i ; globalMaxStartIndex = startIndex ; } } for ( let i = globalMaxStartIndex ; i <= endIndex ; ++ i ) { document . write ( nums [ i ] + " " ) ; } } let arr = [ ] ; arr . push ( - 2 ) ; arr . push ( - 5 ) ; arr . push ( 6 ) ; arr . push ( - 2 ) ; arr . push ( - 3 ) ; arr . push ( 1 ) ; arr . push ( 5 ) ; arr . push ( - 6 ) ; SubarrayWithMaxSum ( arr ) ;
Count of numbers upto M divisible by given Prime Numbers | Function to count the numbers that are divisible by the numbers in the array from range 1 to M ; Initialize the count variable ; Iterate over [ 1 , M ] ; Iterate over array elements arr [ ] ; Check if i is divisible by a [ j ] ; Increment the count ; Return the answer ; Given array arr [ ] ; Given Number M ; Function Call
function count ( a , M , N ) { let cnt = 0 ; for ( let i = 1 ; i <= M ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) { if ( i % a [ j ] == 0 ) { cnt ++ ; break ; } } } return cnt ; } let arr = [ 2 , 3 , 5 , 7 ] ; let m = 100 ; let n = arr . length ; document . write ( count ( arr , m , n ) ) ;
Count of numbers upto N digits formed using digits 0 to K | Javascript implementation to count the numbers upto N digits such that no two zeros are adjacent ; Function to count the numbers upto N digits such that no two zeros are adjacent ; Condition to check if only one element remains ; If last element is non zero , return K - 1 ; If last element is 0 ; Condition to check if value calculated already ; If last element is non zero , then two cases arise , current element can be either zero or non zero ; Memoize this case ; If last is 0 , then current can only be non zero ; Memoize and return ; Given N and K ; Function Call
var dp = Array . from ( Array ( 15 ) , ( ) => Array ( 10 ) . fill ( 0 ) ) ; function solve ( n , last , k ) { if ( n == 1 ) { if ( last == k ) { return ( k - 1 ) ; } else { return 1 ; } } if ( ( dp [ n ] [ last ] ) != 0 ) return dp [ n ] [ last ] ; if ( last == k ) { return dp [ n ] [ last ] = ( k - 1 ) * solve ( n - 1 , k , k ) + ( k - 1 ) * solve ( n - 1 , 1 , k ) ; } else { dp [ n ] [ last ] = solve ( n - 1 , k , k ) ; return dp [ n ] [ last ] ; } } var n = 2 , k = 3 ; var x = solve ( n , k , k ) + solve ( n , 1 , k ) ; document . write ( x ) ;