codeparrot/xlcost-text-to-code huggingface dataset:
Count of integers from the range [ 0 , N ] whose digit sum is a multiple of K | Javascript implementation of the approach ; To store the states of the dp ; Function to return the count of numbers from the range [ 0 , n ] whose digit sum is a multiple of k using bottom - up dp ; The digit in this index can only be from [ 0 , num [ idx ] ] ; The digit in this index can be anything from [ 0 , 9 ] ; new_tight is the flag value for the next position ; res can 't be negative ; Function to process the string to a vector of digits from MSD to LSD ; For large input number n ; Total number of digits in n ; Process the string to a vector of digits from MSD to LSD
var MAX = 100005 ; var MOD = 1000000007 ; var dp = Array . from ( Array ( MAX ) , ( ) => Array ( 101 ) ) ; for ( var i = 0 ; i < MAX ; i ++ ) for ( var j = 0 ; j < 101 ; j ++ ) dp [ i ] [ j ] = new Array ( 2 ) . fill ( - 1 ) ; function countNum ( idx , sum , tight , num , len , k ) { if ( len == idx ) { if ( sum == 0 ) return 1 ; else return 0 ; } if ( dp [ idx ] [ sum ] [ tight ] != - 1 ) return dp [ idx ] [ sum ] [ tight ] ; var res = 0 , limit ; if ( tight == 0 ) { limit = num [ idx ] ; } else { limit = 9 ; } for ( var i = 0 ; i <= limit ; i ++ ) { var new_tight = tight ; if ( tight == 0 && i < limit ) new_tight = 1 ; res += countNum ( idx + 1 , ( sum + i ) % k , new_tight , num , len , k ) ; res %= MOD ; } if ( res < 0 ) res += MOD ; return dp [ idx ] [ sum ] [ tight ] = res ; } function process ( s ) { var num = [ ] ; for ( var i = 0 ; i < s . length ; i ++ ) { num . push ( s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; } return num ; } var n = " " ; var len = n . length ; var k = 58 ; var num = process ( n ) ; document . write ( countNum ( 0 , 0 , 0 , num , len , k ) ) ;
Number of Subsequences with Even and Odd Sum | JavaScript implementation to find the number of Subsequences with Even and Odd Sum ; Arrays to store the count of even subsequences and odd subsequences ; Initialising countEVEN [ 0 ] and countODD [ 0 ] to 0 since as there is no subsequence before the iteration with even or odd count . ; Here countODD [ i ] denotes count of odd subsequences till i ; if the number is even ; if the number is odd ; Driver Code
function countSum ( arr , n ) { var countODD = Array ( n + 1 ) . fill ( 0 ) ; var countEVEN = Array ( n + 1 ) . fill ( 0 ) ; countODD [ 0 ] = 0 ; countEVEN [ 0 ] = 0 ; for ( var i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] % 2 == 0 ) { countEVEN [ i ] = countEVEN [ i - 1 ] + countEVEN [ i - 1 ] + 1 ; countODD [ i ] = countODD [ i - 1 ] + countODD [ i - 1 ] ; } else { countEVEN [ i ] = countEVEN [ i - 1 ] + countODD [ i - 1 ] ; countODD [ i ] = countODD [ i - 1 ] + countEVEN [ i - 1 ] + 1 ; } } var ans = [ 0 , 0 ] ; ans [ 0 ] = countEVEN [ n ] ; ans [ 1 ] = countODD [ n ] ; return ans ; } var arr = [ 1 , 2 , 2 , 3 ] ; var n = 4 ; var ans = countSum ( arr , n ) ; document . write ( " " + ans [ 0 ] ) ; document . write ( " " + ans [ 1 ] ) ;
Double Knapsack | Dynamic Programming | Javascript implementation of the above approach ; 3D array to store states of DP ; w1_r represents remaining capacity of 1 st knapsack w2_r represents remaining capacity of 2 nd knapsack i represents index of the array arr we are working on ; Base case ; Variables to store the result of three parts of recurrence relation ; Store the state in the 3D array ; Input array ; Number of elements in the array ; Capacity of knapsacks ; Function to be called
var maxN = 31 var maxW = 31 var dp = Array ( maxN ) ; for ( var i = 0 ; i < maxN ; i ++ ) { dp [ i ] = Array ( maxW ) ; for ( var j = 0 ; j < maxW ; j ++ ) { dp [ i ] [ j ] = Array ( maxW ) . fill ( - 1 ) ; } } function maxWeight ( arr , n , w1_r , w2_r , i ) { if ( i == n ) return 0 ; if ( dp [ i ] [ w1_r ] [ w2_r ] != - 1 ) return dp [ i ] [ w1_r ] [ w2_r ] ; var fill_w1 = 0 , fill_w2 = 0 , fill_none = 0 ; if ( w1_r >= arr [ i ] ) fill_w1 = arr [ i ] + maxWeight ( arr , n , w1_r - arr [ i ] , w2_r , i + 1 ) ; if ( w2_r >= arr [ i ] ) fill_w2 = arr [ i ] + maxWeight ( arr , n , w1_r , w2_r - arr [ i ] , i + 1 ) ; fill_none = maxWeight ( arr , n , w1_r , w2_r , i + 1 ) ; dp [ i ] [ w1_r ] [ w2_r ] = Math . max ( fill_none , Math . max ( fill_w1 , fill_w2 ) ) ; return dp [ i ] [ w1_r ] [ w2_r ] ; } var arr = [ 8 , 2 , 3 ] ; var n = arr . length ; var w1 = 10 , w2 = 3 ; document . write ( maxWeight ( arr , n , w1 , w2 , 0 ) ) ;
Sum of bitwise AND of all submatrices | Javascript program to find sum of Bit - wise AND of all submatrices ; Function to find prefix - count for each row from right to left ; Function to find the number of submatrices with all 1 s ; Array to store required prefix count of 1 s from right to left for boolean array ; Variable to store the final answer ; For each index of a column , determine the number of sub - matrices starting from that index and has all 1 s ; Stack to store elements and the count of the numbers they popped First part of pair is value of inserted element Second part is count of the number of elements pushed before with a greater value ; variable to store the number of submatrices with all 1 s ; Function to find the sum of Bitwise - AND of all submatrices ; matrix to store the status of ith bit of each element of matrix arr ; Driver Code
var n = 3 ; function findPrefixCount ( p_arr , set_bit ) { for ( var i = 0 ; i < n ; i ++ ) { for ( var j = n - 1 ; j >= 0 ; j -- ) { if ( ! set_bit [ i ] [ j ] ) continue ; if ( j != n - 1 ) p_arr [ i ] [ j ] += p_arr [ i ] [ j + 1 ] ; p_arr [ i ] [ j ] += set_bit [ i ] [ j ] ; } } } function matrixAllOne ( set_bit ) { var p_arr = Array . from ( Array ( n ) , ( ) => Array ( n ) . fill ( 0 ) ) ; findPrefixCount ( p_arr , set_bit ) ; var ans = 0 ; for ( var j = 0 ; j < n ; j ++ ) { var i = n - 1 ; var q = [ ] ; var to_sum = 0 ; while ( i >= 0 ) { var c = 0 ; while ( q . length != 0 && q [ q . length - 1 ] [ 0 ] > p_arr [ i ] [ j ] ) { to_sum -= ( q [ q . length - 1 ] [ 1 ] + 1 ) * ( q [ q . length - 1 ] [ 0 ] - p_arr [ i ] [ j ] ) ; c += q [ q . length - 1 ] [ 1 ] + 1 ; q . pop ( ) ; } to_sum += p_arr [ i ] [ j ] ; ans += to_sum ; q . push ( [ p_arr [ i ] [ j ] , c ] ) ; i -- ; } } return ans ; } function sumAndMatrix ( arr ) { var sum = 0 ; var mul = 1 ; for ( var i = 0 ; i < 30 ; i ++ ) { var set_bit = Array . from ( Array ( n ) , ( ) => Array ( n ) ) ; for ( var R = 0 ; R < n ; R ++ ) for ( var C = 0 ; C < n ; C ++ ) set_bit [ R ] [ C ] = ( ( arr [ R ] [ C ] & ( 1 << i ) ) != 0 ) ; sum += ( mul * matrixAllOne ( set_bit ) ) ; mul *= 2 ; } return sum ; } var arr = [ [ 9 , 7 , 4 ] , [ 8 , 9 , 2 ] , [ 11 , 11 , 5 ] ] ; document . write ( sumAndMatrix ( arr ) ) ;
Count ways to reach a score using 1 and 2 with no consecutive 2 s | Bottom up approach for counting ways to reach a score using 1 and 2 with consecutive 2 allowed ; noOfWays [ i ] will store count for last 3 values before i . ; Loop till " n + 1" to compute value for " n " ; number of ways if first run is 1 ; number of ways if first run is 2 and second run is 1 ; Remember last 3 values ; Driver code
function CountWays ( n ) { var noOfWays = Array ( 3 ) . fill ( 0 ) ; noOfWays [ 0 ] = 1 ; noOfWays [ 1 ] = 1 ; noOfWays [ 2 ] = 1 + 1 ; for ( var i = 3 ; i < n + 1 ; i ++ ) { noOfWays [ i ] = noOfWays [ 3 - 1 ] + noOfWays [ 3 - 3 ] ; noOfWays [ 0 ] = noOfWays [ 1 ] ; noOfWays [ 1 ] = noOfWays [ 2 ] ; noOfWays [ 2 ] = noOfWays [ i ] ; } return noOfWays [ n ] ; } var n = 5 ; document . write ( CountWays ( n ) ) ;
Maximum size subset with given sum | Returns size of maximum sized subset if there is a subset of set [ ] with sun equal to given sum . It returns - 1 if there is no subset with given sum . ; The value of subset [ i ] [ j ] will be true if there is a subset of set [ 0. . j - 1 ] with sum equal to i ; If sum is 0 , then answer is true ; If sum is not 0 and set is empty , then answer is false ; Fill the subset table in bottom up manner ; Driver Code
function isSubsetSum ( set , n , sum ) { let subset = new Array ( sum + 1 ) ; for ( var i = 0 ; i < subset . length ; i ++ ) { subset [ i ] = new Array ( 2 ) ; } let count = new Array ( sum + 1 ) ; for ( var i = 0 ; i < count . length ; i ++ ) { count [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i <= n ; i ++ ) { subset [ 0 ] [ i ] = true ; count [ 0 ] [ i ] = 0 ; } for ( let i = 1 ; i <= sum ; i ++ ) { subset [ i ] [ 0 ] = false ; count [ i ] [ 0 ] = - 1 ; } for ( let i = 1 ; i <= sum ; i ++ ) { for ( let j = 1 ; j <= n ; j ++ ) { subset [ i ] [ j ] = subset [ i ] [ j - 1 ] ; count [ i ] [ j ] = count [ i ] [ j - 1 ] ; if ( i >= set [ j - 1 ] ) { subset [ i ] [ j ] = subset [ i ] [ j ] || subset [ i - set [ j - 1 ] ] [ j - 1 ] ; if ( subset [ i ] [ j ] ) count [ i ] [ j ] = Math . max ( count [ i ] [ j - 1 ] , count [ i - set [ j - 1 ] ] [ j - 1 ] + 1 ) ; } } } return count [ sum ] [ n ] ; } let set = [ 2 , 3 , 5 , 10 ] ; let sum = 20 ; let n = set . length ; document . write ( isSubsetSum ( set , n , sum ) ) ;
Print all longest common sub | Javascript program to find all LCS of two strings in sorted order . ; length of lcs ; dp matrix to store result of sub calls for lcs ; A memoization based function that returns LCS of str1 [ i . . len1 - 1 ] and str2 [ j . . len2 - 1 ] ; base condition ; if lcs has been computed ; if characters are same return previous + 1 else max of two sequences after removing i ' th ▁ and ▁ j ' th char one by one ; Function to print all routes common sub - sequences of length lcslen ; if currlcs is equal to lcslen then print it ; if we are done with all the characters of both string ; here we have to print all sub - sequences lexicographically , that ' s ▁ why ▁ we ▁ start ▁ from ▁ ' a ' to ' z ' if this character is  present in both of them then append it in data[] and same  remaining part ; done is a flag to tell that we have printed all the subsequences corresponding to current character ; if character ch is present in str1 then check if it is present in str2 ; if ch is present in both of them and remaining length is equal to remaining lcs length then add ch in sub - sequenece ; If we found LCS beginning with current character . ; This function prints all LCS of str1 and str2 in lexicographic order . ; Find lengths of both strings ; Find length of LCS ; Print all LCS using recursive backtracking data [ ] is used to store individual LCS . ; Driver code
let MAX = 100 ; let lcslen = 0 ; let dp = new Array ( MAX ) ; function lcs ( str1 , str2 , len1 , len2 , i , j ) { let ret = dp [ i ] [ j ] ; if ( i == len1 j == len2 ) return ret = 0 ; if ( ret != - 1 ) return ret ; ret = 0 ; if ( str1 [ i ] == str2 [ j ] ) ret = 1 + lcs ( str1 , str2 , len1 , len2 , i + 1 , j + 1 ) ; else ret = Math . max ( lcs ( str1 , str2 , len1 , len2 , i + 1 , j ) , lcs ( str1 , str2 , len1 , len2 , i , j + 1 ) ) ; return ret ; } function printAll ( str1 , str2 , len1 , len2 , data , indx1 , indx2 , currlcs ) { if ( currlcs == lcslen ) { data [ currlcs ] = null ; document . write ( data . join ( " " ) + " " ) ; return ; } if ( indx1 == len1 indx2 == len2 ) return ; for ( let ch = ' ' . charCodeAt ( 0 ) ; ch <= ' ' . charCodeAt ( 0 ) ; ch ++ ) { let done = false ; for ( let i = indx1 ; i < len1 ; i ++ ) { if ( ch == str1 [ i ] . charCodeAt ( 0 ) ) { for ( let j = indx2 ; j < len2 ; j ++ ) { if ( ch == str2 [ j ] . charCodeAt ( 0 ) && lcs ( str1 , str2 , len1 , len2 , i , j ) == lcslen - currlcs ) { data [ currlcs ] = String . fromCharCode ( ch ) ; printAll ( str1 , str2 , len1 , len2 , data , i + 1 , j + 1 , currlcs + 1 ) ; done = true ; break ; } } } if ( done ) break ; } } } function prinlAllLCSSorted ( str1 , str2 ) { let len1 = str1 . length , len2 = str2 . length ; for ( let i = 0 ; i < MAX ; i ++ ) { dp [ i ] = new Array ( MAX ) ; for ( let j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } lcslen = lcs ( str1 , str2 , len1 , len2 , 0 , 0 ) ; let data = new Array ( MAX ) ; printAll ( str1 , str2 , len1 , len2 , data , 0 , 0 , 0 ) ; } let str1 = " " , str2 = " " ; prinlAllLCSSorted ( str1 , str2 ) ;
Check for Majority Element in a sorted array | Javascript Program to check for majority element in a sorted array ; Get last index according to n ( even or odd ) ; Search for first occurrence of x in arr [ ] ; Check if x is present and is present more than n / 2 times ; Driver code
function isMajority ( arr , n , x ) { let i , last_index = 0 ; last_index = ( n % 2 == 0 ) ? parseInt ( n / 2 , 10 ) : parseInt ( n / 2 , 10 ) + 1 ; for ( i = 0 ; i < last_index ; i ++ ) { if ( arr [ i ] == x && arr [ i + parseInt ( n / 2 , 10 ) ] == x ) return true ; } return false ; } let arr = [ 1 , 2 , 3 , 4 , 4 , 4 , 4 ] ; let n = arr . length ; let x = 4 ; if ( isMajority ( arr , n , x ) == true ) document . write ( x + " " + parseInt ( n / 2 , 10 ) + " " ) ; else document . write ( x + " " + parseInt ( n / 2 , 10 ) + " " ) ;
Check for Majority Element in a sorted array | If x is present in arr [ low ... high ] then returns the index of first occurrence of x , otherwise returns - 1 ; Check if arr [ mid ] is the first occurrence of x . arr [ mid ] is first occurrence if x is one of the following is true : ( i ) mid == 0 and arr [ mid ] = = x ( ii ) arr [ mid - 1 ] < x and arr [ mid ] == x ; This function returns true if the x is present more than n / 2 times in arr [ ] of size n ; Find the index of first occurrence of x in arr [ ] ; If element is not present at all , return false ; check if the element is present more than n / 2 times ; Driver code
function _binarySearch ( arr , low , high , x ) { if ( high >= low ) { let mid = parseInt ( ( low + high ) / 2 , 10 ) ; if ( ( mid == 0 x > arr [ mid - 1 ] ) && ( arr [ mid ] == x ) ) return mid ; else if ( x > arr [ mid ] ) return _binarySearch ( arr , ( mid + 1 ) , high , x ) ; else return _binarySearch ( arr , low , ( mid - 1 ) , x ) ; } return - 1 ; } function isMajority ( arr , n , x ) { let i = _binarySearch ( arr , 0 , n - 1 , x ) ; if ( i == - 1 ) return false ; if ( ( ( i + parseInt ( n / 2 , 10 ) ) <= ( n - 1 ) ) && arr [ i + parseInt ( n / 2 , 10 ) ] == x ) return true ; else return false ; } let arr = [ 1 , 2 , 3 , 3 , 3 , 3 , 10 ] ; let n = arr . length ; let x = 3 ; if ( isMajority ( arr , n , x ) == true ) document . write ( x + " " + parseInt ( n / 2 , 10 ) + " " ) ; else document . write ( x + " " + parseInt ( n / 2 , 10 ) + " " ) ;
Check for Majority Element in a sorted array |  ;
function isMajorityElement ( arr , n , key ) { if ( arr [ parseInt ( n / 2 , 10 ) ] == key ) return true ; else return false ; } let arr = [ 1 , 2 , 3 , 3 , 3 , 3 , 10 ] ; let n = arr . length ; let x = 3 ; if ( isMajorityElement ( arr , n , x ) ) document . write ( x + " " + parseInt ( n / 2 , 10 ) + " " ) ; else document . write ( x + " " + " " + parseInt ( n / 2 , 10 ) + " " ) ;
Cutting a Rod | DP | Returns the best obtainable price for a rod of length n and price [ ] as prices of different pieces ; Build the table val [ ] in bottom up manner and return the last entry from the table ; Driver Code
function cutRod ( price , n ) { let val = new Array ( n + 1 ) ; val [ 0 ] = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { let max_val = Number . MIN_VALUE ; for ( let j = 0 ; j < i ; j ++ ) max_val = Math . max ( max_val , price [ j ] + val [ i - j - 1 ] ) ; val [ i ] = max_val ; } return val [ n ] ; } let arr = [ 1 , 5 , 8 , 9 , 10 , 17 , 17 , 20 ] ; let size = arr . length ; document . write ( " " + cutRod ( arr , size ) + " " ) ;
Cutting a Rod | DP | Global Array for the purpose of memoization . ; A recursive program , using , memoization , to implement the rod cutting problem ( Top - Down ) . ; The maximum price will be zero , when either the length of the rod is zero or price is zero . ; If the length of the rod is less than the maximum length , Max_lene will consider it . Now depending upon the profit , either Max_lene we will take it or discard it . ; If the length of the rod is greater than the permitted size , Max_len we will not consider it . ; Max_lene Max_lenill return the maximum value obtained , Max_lenhich is present at the nth roMax_len and Max_lenth column . ; Driver code ; Function Call
let t = new Array ( 9 ) ; for ( var i = 0 ; i < t . length ; i ++ ) { t [ i ] = new Array ( 2 ) ; } function un_kp ( price , length , Max_len , n ) { if ( n == 0 Max_len == 0 ) { return 0 ; } if ( length [ n - 1 ] <= Max_len ) { t [ n ] [ Max_len ] = Math . max ( price [ n - 1 ] + un_kp ( price , length , Max_len - length [ n - 1 ] , n ) , un_kp ( price , length , Max_len , n - 1 ) ) ; } else { t [ n ] [ Max_len ] = un_kp ( price , length , Max_len , n - 1 ) ; } return t [ n ] [ Max_len ] ; } let price = [ 1 , 5 , 8 , 9 , 10 , 17 , 17 , 20 ] ; let n = price . length ; let length = Array ( n ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { length [ i ] = i + 1 ; } let Max_len = n ; document . write ( " " + un_kp ( price , length , n , Max_len ) ) ;
Modify array to another given array by replacing array elements with the sum of the array | Function to check if the arr can be converted to target by replacing any element in arr by the sum of arr ; Store the maximum element ; Store the index of the maximum element ; Traverse the array target ; If current element is greater than max ; If max element is 1 ; Traverse the array , target ; If current index is not equal to maximum element index ; Update max ; If max is less than or equal to 0 , ; Update the maximum element ; Recursively call the function ; Driver Code
function isPossible ( target ) { var max = 0 ; var index = 0 ; for ( i = 0 ; i < target . length ; i ++ ) { if ( max < target [ i ] ) { max = target [ i ] ; index = i ; } } if ( max == 1 ) return true ; for ( i = 0 ; i < target . length ; i ++ ) { if ( i != index ) { max -= target [ i ] ; if ( max <= 0 ) return false ; } } target [ index ] = max ; return isPossible ( target ) ; } var target = [ 9 , 3 , 5 ] ; res = isPossible ( target ) ; if ( res ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Sum of all products of the Binomial Coefficients of two numbers up to K | Function returns nCr i . e . Binomial Coefficient ; Initialize res with 1 ; Since C ( n , r ) = C ( n , n - r ) ; Evaluating expression ; Driver code
function nCr ( n , r ) { let res = 1 ; if ( r > n - r ) r = n - r ; for ( let i = 0 ; i < r ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } let n = 3 , m = 2 , k = 2 ; document . write ( nCr ( n + m , k ) ) ;
Check if N can be obtained from 1 by repetitively multiplying by 10 or 20 | Function to check if N can be obtained or not ; Count and remove trailing zeroes ; Check if remaining N is a power of 2 ; To check the condition to prlong YES or NO ; Driver code
function Is_possible ( N ) { let C = 0 ; let D = 0 ; while ( N % 10 == 0 ) { N = N / 10 ; C += 1 ; } if ( Math . pow ( 2 , ( Math . log ( N ) / ( Math . log ( 2 ) ) ) ) == N ) { D = ( Math . log ( N ) / ( Math . log ( 2 ) ) ) ; if ( C >= D ) document . write ( " " ) ; else document . write ( " " ) ; } else document . write ( " " ) ; } let N = 2000000000000 ; Is_possible ( N ) ;
Central polygonal numbers | Function to find N - th term in the series ; Driver code
function findNthTerm ( n ) { document . write ( n * n - n + 1 ) ; } N = 4 ; findNthTerm ( N ) ;
Anti | Iterative function to reverse digits of num ; Return the reversed num ; Function to calculate sum of reverse all proper divisors ; Final result of summation of divisors ; Find all divisors of num ; If ' i ' is divisor of ' num ' ; If both divisors are same then add it only once else add both ; Add 1 to the result as 1 is also a divisor ; Function to check if N is anti - perfect or not ; Given Number N ; Function Call
function rev ( num ) { var rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = Math . floor ( num / 10 ) ; } return rev_num ; } function divSum ( num ) { var result = 0 ; for ( var i = 2 ; i <= Math . floor ( Math . sqrt ( num ) ) ; i ++ ) { if ( num % i == 0 ) { if ( i == ( num / i ) ) result += rev ( i ) ; else result += ( rev ( i ) + rev ( num / i ) ) ; } } result += 1 ; return result ; } function isAntiPerfect ( n ) { return divSum ( n ) == n ; } var N = 244 ; if ( isAntiPerfect ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to print the series 1 , 3 , 4 , 8 , 15 , 27 , 50 â €¦ till N terms | Function to print the series ; Generate the ith term and print it ; Driver Code ; Function Call
function printSeries ( n , a , b , c ) { let d ; if ( n == 1 ) { document . write ( a + " " ) ; return ; } if ( n == 2 ) { document . write ( a + " " + b + " " ) ; return ; } document . write ( a + " " + b + " " + c + " " ) ; for ( let i = 4 ; i <= n ; i ++ ) { d = a + b + c ; document . write ( d + " " ) ; a = b ; b = c ; c = d ; } } let N = 7 , a = 1 , b = 3 ; let c = 4 ; printSeries ( N , a , b , c ) ;
Diameter of a Binary Indexed Tree with N nodes | Function to find diameter of BIT with N + 1 nodes ; L is size of subtree just before subtree in which N lies ; H is the height of subtree just before subtree in which N lies ; Base Cases ; Size of subtree are power of 2 ; 3 Cases as explained in Approach ; Driver Code
function diameter ( n ) { var L , H , templen ; L = 1 ; H = 0 ; if ( n == 1 ) { return 1 ; } if ( n == 2 ) { return 2 ; } if ( n == 3 ) { return 3 ; } while ( L * 2 <= n ) { L *= 2 ; H ++ ; } if ( n >= L * 2 - 1 ) return 2 * H + 1 ; else if ( n >= L + ( L / 2 ) - 1 ) return 2 * H ; return 2 * H - 1 ; } var n = 15 ; document . write ( diameter ( n ) ) ;
Find the larger exponential among two exponentials | Function to find whether a ^ b is greater or c ^ d ; Find b * log ( a ) ; Find d * log ( c ) ; Compare both values ; Driver code
function compareValues ( a , b , c , d ) { let log1 = Math . log ( a ) / Math . log ( 10 ) ; let num1 = log1 * b ; let log2 = Math . log ( c ) / Math . log ( 10 ) ; let num2 = log2 * d ; if ( num1 > num2 ) document . write ( a + " " + b ) ; else document . write ( c + " " + d ) ; } let a = 8 , b = 29 , c = 60 , d = 59 ; compareValues ( a , b , c , d ) ;
Sum of prime numbers without odd prime digits | Javascript program for above approach ; Find all prime numbers ; Store all prime numbers ; Function to check if a digit is odd prime or not ; Function to find sum ; To store required answer ; Get all prime numbers ; Traverse through all the prime numbers ; Flag stores 1 if a number does not contain any odd primes ; Find all digits of a number ; If number does not contain any odd primes ; Return the required answer ; Driver code ; Function call
const MAX = 100005 ; function addPrimes ( ) { let n = MAX ; let prime = new Array ( n + 1 ) . fill ( true ) ; for ( let p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } let ans = [ ] ; for ( let p = 2 ; p <= n ; p ++ ) if ( prime [ p ] ) ans . push ( p ) ; return ans ; } function is_prime ( n ) { return ( n == 3 n == 5 n == 7 ) ; } function find_Sum ( n ) { let sum = 0 ; let v = addPrimes ( ) ; for ( let i = 0 ; i < v . length && n > 0 ; i ++ ) { let flag = 1 ; let a = v [ i ] ; while ( a != 0 ) { let d = a % 10 ; a = parseInt ( a / 10 ) ; if ( is_prime ( d ) ) { flag = 0 ; break ; } } if ( flag == 1 ) { n -- ; sum = sum + v [ i ] ; } } return sum ; } let n = 7 ; document . write ( find_Sum ( n ) ) ;