codeparrot/xlcost-text-to-code huggingface dataset:
Check if a string can be rearranged to form special palindrome | driver code ; creating a array which stores the frequency of each character ; Checking if a character is uppercase or not ; Increasing by 1 if uppercase ; Decreasing by 1 if lower case ; Storing the sum of positive numbers in the frequency array ; Storing the sum of negative numbers in the frequency array ; If all character balances out then its Yes ; If there is only 1 character which does not balances then also it is Yes
let s = " " ; let u = Array ( 26 ) . fill ( 0 ) ; let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] . toUpperCase ( ) ) { u [ s [ i ] - 65 ] += 1 ; } else { u [ s [ i ] - 97 ] -= 1 ; } } let f1 = true ; let po = 0 ; let ne = 0 ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( u [ i ] > 0 ) po += u [ i ] ; if ( u [ i ] < 0 ) ne += u [ i ] ; } if ( po == 0 && ne == 0 ) document . write ( " " ) ; else if ( po == 1 && ne == 0 ) document . write ( " " ) ; else if ( po == 0 && ne == - 1 ) document . write ( " " ) ; else document . write ( " " ) ;
Modify the string such that it contains all vowels at least once | JavaScript implementation of the above approach ; All vowels ; List to store distinct vowels ; if length of String is less than 5 then always Impossible ; Storing the distinct vowels in the String by checking if it in the list of String and not in the list of distinct vowels ; Storing the vowels which are not present in the String ; No replacement needed condition ; Replacing the characters to get all Vowels ; copy th rest of the string ; Driver Code
function find ( x , c ) { for ( let i = 0 ; i < x . length ; i ++ ) if ( x [ i ] == c ) return true ; return false ; } function addAllVowel ( str ) { let x = [ ' ' , ' ' , ' ' , ' ' , ' ' ] ; let y = [ ] ; let length = str . length ; if ( length < 5 ) document . write ( " " ) ; else { for ( let i = 0 ; i < length ; i ++ ) { if ( find ( x , str [ i ] ) && ! find ( y , str [ i ] ) ) y . push ( str [ i ] ) ; } let z = [ ] ; for ( let i = 0 ; i < 5 ; i ++ ) if ( ! find ( y , x [ i ] ) ) z . push ( x [ i ] ) ; if ( z . length == 0 ) document . write ( str + " " ) ; else { let cc = 0 ; let y1 = [ ] ; let ans = " " ; for ( let i = 0 ; i < length ; i ++ ) { if ( find ( x , str [ i ] ) && ! find ( y1 , str [ i ] ) ) { ans += str [ i ] ; y1 . push ( str [ i ] ) ; } else { ans += z [ cc ] ; cc ++ ; } if ( cc == z . length ) { for ( let j = i + 1 ; j < length ; j ++ ) ans += str [ j ] ; break ; } } document . write ( ans ) ; } } } let str = " " ; addAllVowel ( str ) ;
Minimum number of days required to complete the work | Function to find the minimum number days required ; initialising ans to least value possible ; sort by first i . e D ( i ) ; Calculate the minimum possible days ; return the answer ; Number of works ; D1 [ i ] ; D2 [ i ]
function minimumDays ( N , D1 , D2 ) { var ans = - 1000000000 ; var vect = [ ] ; for ( var i = 0 ; i < N ; i ++ ) vect . push ( [ D1 [ i ] , D2 [ i ] ] ) ; vect . sort ( ( a , b ) => a - b ) for ( var i = 0 ; i < N ; i ++ ) { if ( vect [ i ] [ 1 ] >= ans ) ans = vect [ i ] [ 1 ] ; else ans = vect [ i ] [ 0 ] ; } return ans ; } var N = 3 ; var D1 = [ 6 , 5 , 4 ] ; var D2 = [ 1 , 2 , 3 ] ; document . write ( minimumDays ( N , D1 , D2 ) ) ;
Count substrings made up of a single distinct character | Function to count the number of substrings made up of a single distinct character ; Stores the required count ; Stores the count of substrings possible by using current character ; Stores the previous character ; Traverse the string ; If current character is same as the previous character ; Increase count of substrings possible with current character ; Reset count of substrings possible with current character ; Update count of substrings ; Update previous character ; Driver code
function countSubstrings ( s ) { let ans = 0 ; let subs = 1 ; let pre = ' ' ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( pre == s [ i ] ) { subs += 1 ; } else { subs = 1 ; } ans += subs ; pre = s [ i ] ; } document . write ( ans ) ; } let s = " " ; countSubstrings ( s ) ;
Sum of minimum difference between consecutive elements of an array | function to find minimum sum of difference of consecutive element ; ul to store upper limit ll to store lower limit ; storethe lower range in ll and upper range in ul ; initialize the answer with 0 ; iterate for all ranges ; case 1 , in this case the difference will be 0 ; change upper limit and lower limit ; case 2 ; store the difference ; case 3 ; store the difference ; array of range
function solve ( v , n ) { let ans , ul , ll ; let first = 0 ; let second = 1 ; ll = v [ 0 ] [ first ] ; ul = v [ 0 ] [ second ] ; ans = 0 ; for ( let i = 1 ; i < n ; i ++ ) { if ( ( v [ i ] [ first ] <= ul && v [ i ] [ first ] >= ll ) || ( v [ i ] [ second ] >= ll && v [ i ] [ second ] <= ul ) ) { if ( v [ i ] [ first ] > ll ) { ll = v [ i ] [ first ] ; } if ( v [ i ] [ second ] < ul ) { ul = v [ i ] [ second ] ; } } else if ( v [ i ] [ first ] > ul ) { ans += Math . abs ( ul - v [ i ] [ first ] ) ; ul = v [ i ] [ first ] ; ll = v [ i ] [ first ] ; } else if ( v [ i ] [ second ] < ll ) { ans += Math . abs ( ll - v [ i ] [ second ] ) ; ul = v [ i ] [ second ] ; ll = v [ i ] [ second ] ; } } return ans ; } let v = [ [ 1 , 3 ] , [ 2 , 5 ] , [ 6 , 8 ] , [ 1 , 2 ] , [ 2 , 3 ] ] ; let n = 5 ; document . write ( solve ( v , n ) ) ;
Find the Largest Cube formed by Deleting minimum Digits from a number | Returns vector of Pre Processed perfect cubes ; convert the cube to String and push into preProcessedCubes vector ; Utility function for findLargestCube ( ) . Returns the Largest cube number that can be formed ; reverse the preProcessed cubes so that we have the largest cube in the beginning of the vector ; iterate over all cubes ; check if the current digit of the cube matches with that of the number num ; if control reaches here , the its not possible to form a perfect cube ; wrapper for findLargestCubeUtil ( ) ; pre process perfect cubes ; convert number n to String ; Driver Code
function preProcess ( n ) { let preProcessedCubes = [ ] ; for ( let i = 1 ; i * i * i <= n ; i ++ ) { let iThCube = i * i * i ; let cubeString = ( iThCube ) . toString ( ) ; preProcessedCubes . push ( cubeString ) ; } return preProcessedCubes ; } function findLargestCubeUtil ( num , preProcessedCubes ) { ( preProcessedCubes ) . reverse ( ) ; let totalCubes = preProcessedCubes . length ; for ( let i = 0 ; i < totalCubes ; i ++ ) { let currCube = preProcessedCubes [ i ] ; let digitsInCube = currCube . length ; let index = 0 ; let digitsInNumber = num . length ; for ( let j = 0 ; j < digitsInNumber ; j ++ ) { if ( num [ j ] == currCube [ index ] ) { index ++ ; } if ( digitsInCube == index ) { return currCube ; } } } return " " ; } function findLargestCube ( n ) { let preProcessedCubes = preProcess ( n ) ; let num = ( n ) . toString ( ) ; let ans = findLargestCubeUtil ( num , preProcessedCubes ) ; document . write ( " " + n + " " + ans + " " ) ; } let n ; n = 4125 ; findLargestCube ( n ) ; n = 876 ; findLargestCube ( n ) ;
Water Connection Problem | number of houses and number of pipes ; Array rd stores the ending vertex of pipe ; Array wd stores the value of diameters between two pipes ; Array cd stores the starting end of pipe ; arraylist a , b , c are used to store the final output ; Function to perform calculations . ; If a pipe has no ending vertex but has starting vertex i . e is an outgoing pipe then we need to start DFS with this vertex . ; We put the details of component in final output array ; main function ; set the value of the araray to zero
let n , p ; let rd = new Array ( 1100 ) ; let wt = new Array ( 1100 ) ; let cd = new Array ( 1100 ) ; let a = [ ] ; let b = [ ] ; let c = [ ] ; let ans ; function dfs ( w ) { if ( cd [ w ] == 0 ) return w ; if ( wt [ w ] < ans ) ans = wt [ w ] ; return dfs ( cd [ w ] ) ; } function solve ( arr ) { let i = 0 ; while ( i < p ) { let q = arr [ i ] [ 0 ] ; let h = arr [ i ] [ 1 ] ; let t = arr [ i ] [ 2 ] ; cd [ q ] = h ; wt [ q ] = t ; rd [ h ] = q ; i ++ ; } a = [ ] ; b = [ ] ; c = [ ] ; for ( let j = 1 ; j <= n ; ++ j ) if ( rd [ j ] == 0 && cd [ j ] > 0 ) { ans = 1000000000 ; let w = dfs ( j ) ; a . push ( j ) ; b . push ( w ) ; c . push ( ans ) ; } document . write ( a . length + " " ) ; for ( let j = 0 ; j < a . length ; ++ j ) document . write ( a [ j ] + " " + b [ j ] + " " + c [ j ] + " " ) ; } n = 9 ; p = 6 ; for ( let i = 0 ; i < 1100 ; i ++ ) rd [ i ] = cd [ i ] = wt [ i ] = 0 ; let arr = [ [ 7 , 4 , 98 ] , [ 5 , 9 , 72 ] , [ 4 , 6 , 10 ] , [ 2 , 8 , 22 ] , [ 9 , 7 , 17 ] , [ 3 , 1 , 66 ] ] ; solve ( arr ) ;
Print a closest string that does not contain adjacent duplicates | Function to print simple string ; If any two adjacent characters are equal ; Initialize it to ' a ' ; Traverse the loop until it is different from the left and right letter . ; driver code
function noAdjacentDup ( s1 ) { let n = s1 . length ; let s = s1 . split ( ' ' ) ; for ( let i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] ) { s [ i ] = ' ' ; while ( s [ i ] == s [ i - 1 ] || ( i + 1 < n && s [ i ] == s [ i + 1 ] ) ) s [ i ] ++ ; i ++ ; } } return ( s ) ; } let s = " " ; document . write ( noAdjacentDup ( s ) ) ;
Array element moved by k using single moves | Javascript program to find winner of game ; If the number of steps is more then n - 1 , ; Initially the best is 0 and no of wins is 0. ; Traverse through all the numbers ; If the value of array is more then that of previous best ; best is replaced by a [ i ] ; If not the first index ; No of wins is 1 now ; If it wins ; If any position has more then k wins then return ; Maximum element will be winner because we move smaller element at end and repeat the process . ; Driver code
function winner ( a , n , k ) { if ( k >= n - 1 ) return n ; let best = 0 , times = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] > best ) { best = a [ i ] ; if ( i ) times = 1 ; } else times += 1 ; if ( times >= k ) return best ; } return best ; } let a = [ 2 , 1 , 3 , 4 , 5 ] ; let n = a . length ; let k = 2 ; document . write ( winner ( a , n , k ) ) ;
Paper Cut into Minimum Number of Squares | Returns min number of squares needed ; swap if a is small size side . ; Iterate until small size side is greater then 0 ; Update result ; Driver code
function minimumSquare ( a , b ) { let result = 0 , rem = 0 ; if ( a < b ) { let temp = a ; a = b ; b = temp ; } while ( b > 0 ) { result += parseInt ( a / b ) ; let rem = a % b ; a = b ; b = rem ; } return result ; } let n = 13 , m = 29 ; document . write ( minimumSquare ( n , m ) ) ;
Count of non decreasing Arrays with ith element in range [ A [ i ] , B [ i ] ] | Function to count the total number of possible valid arrays ; Make a 2D DP table ; Make a 2D prefix sum table ; Base Case ; Initialize the prefix values ; Iterate over the range and update the dp table accordingly ; Add the dp values to the prefix sum ; Update the prefix sum table ; Find the result count of arrays formed ; Return the total count of arrays ; Driver Code
const totalValidArrays = ( a , b , N ) => { let dp = new Array ( N + 1 ) . fill ( 0 ) . map ( ( ) => new Array ( b [ N - 1 ] + 1 ) . fill ( 0 ) ) ; let pref = new Array ( N + 1 ) . fill ( 0 ) . map ( ( ) => new Array ( b [ N - 1 ] + 1 ) . fill ( 0 ) ) ; dp [ 0 ] [ 0 ] = 1 ; for ( let i = 0 ; i <= b [ N - 1 ] ; i ++ ) { pref [ 0 ] [ i ] = 1 ; } for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = a [ i - 1 ] ; j <= b [ i - 1 ] ; j ++ ) { dp [ i ] [ j ] += pref [ i - 1 ] [ j ] ; pref [ i ] [ j ] += dp [ i ] [ j ] ; } for ( let j = 0 ; j <= b [ N - 1 ] ; j ++ ) { if ( j > 0 ) { pref [ i ] [ j ] += pref [ i ] [ j - 1 ] ; } } } let ans = 0 ; for ( let i = a [ N - 1 ] ; i <= b [ N - 1 ] ; i ++ ) { ans += dp [ N ] [ i ] ; } return ans ; } let A = [ 1 , 1 ] ; let B = [ 2 , 3 ] ; let N = A . length ; document . write ( totalValidArrays ( A , B , N ) ) ;
Minimize cost of swapping set bits with unset bits in a given Binary string | Javascript program for the above approach ; Function to find the minimum cost required to swap every set bit with an unset bit ; Stores the indices of set and unset bits of the string S ; Traverse the string S ; Store the indices ; Initialize a dp table of size n1 * n2 ; Set unreachable states to INF ; Fill the dp Table according to the given recurrence relation ; Update the value of dp [ i ] [ j ] ; Return the minimum cost ; Driver Code
let INF = 1000000000 ; function minimumCost ( s ) { let N = s . length ; let A = [ ] , B = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { if ( s [ i ] == " " ) { A . push ( i ) ; } else { B . push ( i ) ; } } let n1 = A . length ; let n2 = B . length ; let dp = new Array ( n1 + 1 ) . fill ( 0 ) . map ( ( ) => new Array ( n2 + 1 ) . fill ( 0 ) ) ; for ( let i = 1 ; i <= n1 ; i ++ ) { dp [ i ] [ 0 ] = INF ; } for ( let i = 1 ; i <= n1 ; i ++ ) { for ( let j = 1 ; j <= n2 ; j ++ ) { dp [ i ] [ j ] = Math . min ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j - 1 ] + Math . abs ( A [ i - 1 ] - B [ j - 1 ] ) ) ; } } return dp [ n1 ] [ n2 ] ; } let S = " " ; document . write ( minimumCost ( S ) ) ;
Queries to find minimum absolute difference between adjacent array elements in given ranges | JavaScript program for the above approach ; ; Function to find the minimum difference between adjacent array element over the given range [ L , R ] for Q Queries ; Find the sum of all queries ; Left and right boundaries of current range ; Print the sum of the current query range ; Function to find the minimum absolute difference of adjacent array elements for the given range ; Stores the absolute difference of adjacent elements ; Find the minimum difference of adjacent elements ; Driver Code
let MAX = 5000 ; function minDifference ( arr , n , q , m ) { for ( let i = 0 ; i < m ; i ++ ) { let L = q [ i ] [ 0 ] , R = q [ i ] [ 1 ] ; let ans = MAX ; for ( let i = L ; i < R ; i ++ ) { ans = Math . min ( ans , arr [ i ] ) ; } document . write ( ans + ' ' ) ; } } function minimumDifference ( arr , q , N , m ) { let diff = new Array ( N ) ; for ( let i = 0 ; i < N - 1 ; i ++ ) diff [ i ] = Math . abs ( arr [ i ] - arr [ i + 1 ] ) ; minDifference ( diff , N - 1 , q , m ) ; } let arr = [ 2 , 6 , 1 , 8 , 3 , 4 ] ; let N = arr . length ; let Q = [ [ 0 , 3 ] , [ 1 , 5 ] , [ 4 , 5 ] ] ; let M = Q . length ; minimumDifference ( arr , Q , N , M ) ;
Count of N digit numbers which contains all single digit primes | Javascript program for the above approach ; Stores the index of prime numbers ; Function ; If index = N + 1 ; Find count of distinct prime numbers by counting number of set bits . ; If count of distinct prime numbers is equal to 4 return 1. ; If the state has already been computed ; If current position is 1 , then any digit from [ 1 - 9 ] can be placed . If N = 1 , 0 can be also placed . ; If the digit is a prime number , set the index of the digit to 1 in the bitmask . ; For remaining positions , any digit from [ 0 - 9 ] can be placed ; If the digit is a prime number , set the index of the digit to 1 in the bitmask . ; Return the answer . ; Initializing dp array with - 1. ; Indexing prime numbers in ascending order ; Given Input ; Function call
let dp = new Array ( 100 ) . fill ( 0 ) . map ( ( ) => new Array ( 1 << 4 ) ) ; let primeIndex = new Map ( ) ; function countSetBits ( n ) { let count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } function countOfNumbers ( index , mask , N ) { if ( index == N + 1 ) { let countOfPrimes = countSetBits ( mask ) ; if ( countOfPrimes == 4 ) { return 1 ; } return 0 ; } let val = dp [ index ] [ mask ] ; if ( val != - 1 ) { return val ; } val = 0 ; if ( index == 1 ) { for ( let digit = N == 1 ? 0 : 1 ; digit <= 9 ; ++ digit ) { if ( primeIndex . has ( digit ) ) { let newMask = mask | ( 1 << primeIndex . get ( digit ) ) ; val += countOfNumbers ( index + 1 , newMask , N ) ; } else { val += countOfNumbers ( index + 1 , mask , N ) ; } } } else { for ( let digit = 0 ; digit <= 9 ; ++ digit ) { if ( primeIndex . has ( digit ) ) { let newMask = mask | ( 1 << primeIndex . get ( digit ) ) ; val += countOfNumbers ( index + 1 , newMask , N ) ; } else { val += countOfNumbers ( index + 1 , mask , N ) ; } } } return val ; } for ( let i = 0 ; i < 100 ; i ++ ) { for ( let j = 0 ; j < 1 << 4 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } primeIndex . set ( 2 , 0 ) ; primeIndex . set ( 3 , 1 ) ; primeIndex . set ( 5 , 2 ) ; primeIndex . set ( 7 , 3 ) ; let N = 4 ; document . write ( countOfNumbers ( 1 , 0 , N ) ) ;
Minimum number of flips or swaps of adjacent characters required to make two strings equal | Function to count the minimum number of operations required to make strings A and B equal ; Stores all dp - states ; Iterate over the range [ 1 , N ] ; If A [ i - 1 ] equals to B [ i - 1 ] ; Assign Dp [ i - 1 ] to Dp [ i ] ; Otherwise ; Update dp [ i ] ; If swapping is possible ; Update dp [ i ] ; Return the minimum number of steps required ; Given Input ; Function Call
function countMinSteps ( A , B , N ) { let dp = new Array ( N + 1 ) . fill ( 0 ) ; for ( let i = 1 ; i <= N ; i ++ ) { if ( A [ i - 1 ] == B [ i - 1 ] ) { dp [ i ] = dp [ i - 1 ] ; } else { dp [ i ] = dp [ i - 1 ] + 1 ; } if ( i >= 2 && A [ i - 2 ] == B [ i - 1 ] && A [ i - 1 ] == B [ i - 2 ] ) { dp [ i ] = Math . min ( dp [ i ] , dp [ i - 2 ] + 1 ) ; } } return dp [ N ] ; } let A = " " ; let B = " " ; let N = A . length ; document . write ( countMinSteps ( A , B , N ) ) ;
Count of N | JavaScript program for the above approach ; Function to find the number of N digit numbers such that at least one digit occurs more than once ; Base Case ; If repeated is true , then for remaining positions any digit can be placed ; If the current state has already been computed , then return it ; Stores the count of number for the current recursive calls ; If n = 1 , 0 can be also placed ; If a digit has occurred for the second time , then set repeated to 1 ; Otherwise ; For remaining positions any digit can be placed ; If a digit has occurred for the second time , then set repeated to 1 ; Return the resultant count for the current recursive call ; Function to count all the N - digit numbers having at least one digit 's occurrence more than once ; Function to count all possible number satisfying the given criteria ; Driver Code
let dp = new Array ( 50 ) . fill ( 0 ) . map ( ( ) => new Array ( 1 << 10 ) . fill ( 0 ) . map ( ( ) => new Array ( 2 ) . fill ( - 1 ) ) ) ; function countOfNumbers ( digit , mask , repeated , n ) { if ( digit == n + 1 ) { if ( repeated == true ) { return 1 ; } return 0 ; } if ( repeated == true ) { return Math . pow ( 10 , n - digit + 1 ) ; } let val = dp [ digit ] [ mask ] [ repeated ] ; if ( val != - 1 ) { return val ; } val = 0 ; if ( digit == 1 ) { for ( let i = ( n == 1 ? 0 : 1 ) ; i <= 9 ; ++ i ) { if ( mask & ( 1 << i ) ) { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 1 , n ) ; } else { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 0 , n ) ; } } } else { for ( let i = 0 ; i <= 9 ; ++ i ) { if ( mask & ( 1 << i ) ) { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 1 , n ) ; } else { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 0 , n ) ; } } } return val ; } function countNDigitNumber ( N ) { document . write ( countOfNumbers ( 1 , 0 , 0 , N ) ) ; } let N = 2 ; countNDigitNumber ( N ) ;
Construct the largest number whose sum of cost of digits is K | Function to find the maximum number among the two numbers S and T ; If "0" exists in the string S ; If "0" exists in the string T ; Else return the maximum number formed ; Recursive function to find maximum number formed such that the sum of cost of digits of formed number is K ; Base Case ; Return the stored state ; Including the digit ( idx + 1 ) ; Excluding the digit ( idx + 1 ) ; Store the result and return ; Function to find the maximum number formed such that the sum of the cost digits in the formed number is K ; Stores all Dp - states ; Recursive Call ; Return the result ; Driver Code
function getMaximum ( S , T ) { if ( S . indexOf ( " " ) > - 1 ) return T ; if ( T . indexOf ( " " ) > - 1 ) return S ; return S . length > T . length ? S : T ; } function recursion ( arr , idx , N , K , dp ) { if ( K == 0 ) { return " " ; } if ( K < 0 idx == N ) { return " " ; } if ( dp [ idx ] [ K ] != " " ) return dp [ idx ] [ K ] ; let include = String ( idx + 1 ) + recursion ( arr , 0 , N , K - arr [ idx ] , dp ) ; let exclude = recursion ( arr , idx + 1 , N , K , dp ) ; return ( dp [ idx ] [ K ] = getMaximum ( include , exclude ) ) ; } function largestNumber ( arr , N , K ) { let dp = new Array ( N + 1 ) . fill ( 0 ) . map ( ( ) => new Array ( K + 1 ) . fill ( - 1 ) ) ; let ans = recursion ( arr , 0 , N , K , dp ) ; return ans == " " ? " " : ans ; } let arr = [ 3 , 12 , 9 , 5 , 3 , 4 , 6 , 5 , 10 ] ; let K = 14 ; let N = arr . length ; document . write ( largestNumber ( arr , N , K ) ) ;
Count of N | Function to calculate count of ' N ' digit numbers such that bitwise AND of adjacent digits is 0. ; If digit = n + 1 , a valid n - digit number has been formed ; If the state has already been computed ; If current position is 1 , then any digit from [ 1 - 9 ] can be placed . If n = 1 , 0 can be also placed . ; For remaining positions , any digit from [ 0 - 9 ] can be placed after checking the conditions . ; Check if bitwise AND of current digit and previous digit is 0. ; Return answer ; Initialize dp array with - 1. ; Given Input ; Function call
function countOfNumbers ( digit , prev , n ) { if ( digit == n + 1 ) { return 1 ; } let val = dp [ digit ] [ prev ] ; if ( val != - 1 ) { return val ; } val = 0 ; if ( digit == 1 ) { for ( let i = ( n == 1 ? 0 : 1 ) ; i <= 9 ; ++ i ) { val += countOfNumbers ( digit + 1 , i , n ) ; } } else { for ( let i = 0 ; i <= 9 ; ++ i ) { if ( ( i & prev ) == 0 ) { val += countOfNumbers ( digit + 1 , i , n ) ; } } } return val ; } let dp = Array ( 100 ) . fill ( ) . map ( ( ) => Array ( 10 ) . fill ( - 1 ) ) ; let N = 3 ; document . write ( countOfNumbers ( 1 , 0 , N ) + " " ) ;