codeparrot/xlcost-text-to-code huggingface dataset:
Find a subarray whose sum is divisible by size of the array | Function to find a subarray whose sum is a multiple of N ; Prefix sum array to store cumulative sum ; Single state dynamic programming relation for prefix sum array ; Generating all sub - arrays ; If the sum of the sub - array [ i : j ] is a multiple of N ; If the function reaches here it means there are no subarrays with sum as a multiple of N ; Driver code
function CheckSubarray ( arr , N ) { let presum = new Array ( N + 1 ) . fill ( 0 ) ; for ( let i = 1 ; i <= N ; i += 1 ) { presum [ i ] = presum [ i - 1 ] + arr [ i - 1 ] ; } for ( let i = 1 ; i <= N ; i += 1 ) { for ( let j = i ; j <= N ; j += 1 ) { if ( ( presum [ j ] - presum [ i - 1 ] ) % N == 0 ) { document . write ( ( i - 1 ) + " " + ( j - 1 ) ) ; return ; } } } document . write ( - 1 ) ; } let arr = [ 7 , 5 , 3 , 7 ] ; let N = arr . length ; CheckSubarray ( arr , N ) ;
Find a subarray whose sum is divisible by size of the array | Function to check is there exists a subarray whose sum is a multiple of N ; Prefix sum array to store cumulative sum ; Single state dynamic programming relation for prefix sum array ; Modulo class vector ; Storing the index value in the modulo class vector ; If there exists a sub - array with starting index equal to zero ; In this class , there are more than two presums % N . Hence difference of any two subarrays would be a multiple of N ; 0 based indexing ; Driver code
function CheckSubarray ( arr , N ) { let presum = new Array ( N + 1 ) ; for ( let i = 0 ; i < ( N + 1 ) ; i ++ ) presum [ i ] = 0 ; for ( let i = 1 ; i <= N ; i += 1 ) { presum [ i ] = presum [ i - 1 ] + arr [ i - 1 ] ; } let moduloclass = new Array ( N ) ; for ( let i = 0 ; i < N ; i += 1 ) { moduloclass [ i ] = [ ] ; } for ( let i = 1 ; i <= N ; i += 1 ) { moduloclass [ presum [ i ] % N ] . push ( i - 1 ) ; } if ( moduloclass [ 0 ] . length > 0 ) { document . write ( 0 + " " + moduloclass [ 0 ] [ 0 ] ) ; return ; } for ( let i = 1 ; i < N ; i += 1 ) { if ( moduloclass [ i ] . length >= 2 ) { document . write ( moduloclass [ i ] [ 0 ] + 1 + " " + moduloclass [ i ] [ 1 ] ) ; return ; } } } let arr = [ 7 , 3 , 5 , 2 ] ; let N = arr . length ; CheckSubarray ( arr , N ) ;
Smallest number greater or equals to N such that it has no odd positioned bit set | Function to count the total bits ; Iterate and find the number of set bits ; Right shift the number by 1 ; Function to find the nearest number ; Count the total number of bits ; To get the position ; If the last set bit is at odd position then answer will always be a number with the left bit set ; Set all the even bits which are possible ; If the number still is less than N ; Return the number by setting the next even set bit ; If we have reached this position it means tempsum > n hence turn off even bits to get the first possible number ; Turn off the bit ; If it gets lower than N then set it and return that number ; Driver code
function countBits ( n ) { let count = 0 ; while ( n ) { count ++ ; n >>= 1 ; } return count ; } function findNearestNumber ( n ) { let cnt = countBits ( n ) ; cnt -= 1 ; if ( cnt % 2 ) { return 1 << ( cnt + 1 ) ; } else { let tempnum = 0 ; for ( let i = 0 ; i <= cnt ; i += 2 ) tempnum += 1 << i ; if ( tempnum < n ) { return ( 1 << ( cnt + 2 ) ) ; } else if ( tempnum == n ) return n ; for ( let i = 0 ; i <= cnt ; i += 2 ) { tempnum -= ( 1 << i ) ; if ( tempnum < n ) return tempnum += ( 1 << i ) ; } } } let n = 19 ; document . write ( findNearestNumber ( n ) ) ;
Count of numbers whose 0 th and Nth bits are set | Function to return the count of n - bit numbers whose 0 th and nth bits are set ; Driver code
function countNum ( n ) { if ( n == 1 ) return 1 ; let count = Math . pow ( 2 , n - 2 ) ; return count ; } let n = 3 ; document . write ( countNum ( n ) ) ;
Find a number containing N | Function to return the String generated by appending "10" n - 1 times ; Initialising String as empty ; Function to return the decimal equivalent of the given binary String ; Initializing base value to 1 i . e 2 ^ 0 ; Function that calls the constructString and binarytodecimal and returns the answer ; Driver code
function constructString ( n ) { var s = " " ; for ( var i = 0 ; i < n ; i ++ ) { s += " " ; } return s ; } function binaryToDecimal ( n ) { var num = n ; var dec_value = 0 ; var base = 1 ; var len = num . length ; for ( var i = len - 1 ; i >= 0 ; i -- ) { if ( num . charAt ( i ) == ' ' ) dec_value += base ; base = base * 2 ; } return dec_value ; } function findNumber ( n ) { var s = constructString ( n - 1 ) ; var num = binaryToDecimal ( s ) ; return num ; } var n = 4 ; document . write ( findNumber ( n ) ) ;
Bitonic point in the given linked list | Node for linked list ; Function to insert a node at the head of the linked list ; Function to return the bitonic of the given linked list ; If list is empty ; If list contains only a single node ; Invalid bitonic sequence ; If current node is the bitonic point ; Get to the next node in the list ; Nodes must be in descending starting from here ; Out of order node ; Get to the next node in the list ; Driver Code
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function push ( head_ref , data ) { var new_node = new Node ( ) ; new_node . data = data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } function bitonic_point ( node ) { if ( node == null ) return - 1 ; if ( node . next == null ) return - 1 ; if ( node . data > node . next . data ) return - 1 ; while ( node . next != null ) { if ( node . data > node . next . data ) break ; node = node . next ; } let bitonicPoint = node . data ; while ( node . next != null ) { if ( node . data < node . next . data ) return - 1 ; node = node . next ; } return bitonicPoint ; } var head = null ; head = push ( head , 100 ) ; head = push ( head , 201 ) ; head = push ( head , 399 ) ; head = push ( head , 490 ) ; head = push ( head , 377 ) ; head = push ( head , 291 ) ; head = push ( head , 100 ) ; document . write ( bitonic_point ( head ) ) ;
Find the minimum number of elements that should be removed to make an array good | Function to remove minimum elements to make the given array good ; To store count of each subsequence ; Increase the count of subsequence [ 0 ] ; If Previous element subsequence count is greater than zero then increment subsequence count of current element and decrement subsequence count of the previous element . ; Return the required answer ; Driver code ; Function call
function MinRemove ( a , n , k ) { let cnt = new Array ( k ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) cnt [ 0 ] ++ ; else if ( cnt [ a [ i ] - 1 ] > 0 ) { cnt [ a [ i ] - 1 ] -- ; cnt [ a [ i ] ] ++ ; } } return n - ( k * cnt [ k - 1 ] ) ; } let a = [ 0 , 1 , 2 , 3 , 4 , 0 , 1 , 0 , 1 , 2 , 3 , 4 ] , k = 5 ; let n = a . length ; document . write ( MinRemove ( a , n , k ) ) ;
Print first N Mosaic numbers | Function to return the nth mosaic number ; Iterate from 2 to the number ; If i is the factor of n ; Find the count where i ^ count is a factor of n ; Divide the number by i ; Increase the count ; Multiply the answer with count and i ; Return the answer ; Function to print first N Mosaic numbers ; Driver code
function mosaic ( n ) { var i , ans = 1 ; for ( i = 2 ; i <= n ; i ++ ) { if ( n % i == 0 && n > 0 ) { var count = 0 ; while ( n % i == 0 ) { n /= i ; count ++ ; } ans *= count * i ; } } return ans ; } function nMosaicNumbers ( n ) { for ( var i = 1 ; i <= n ; i ++ ) document . write ( mosaic ( i ) + " " ) ; } var n = 10 ; nMosaicNumbers ( n ) ;
Count of nodes which are at a distance X from root and leaves | Function to find the count of the required nodes ; Height of the complete binary tree with n nodes ; If X > height then no node can be present at that level ; Corner case ; Maximum total nodes that are possible in complete binary tree with height h ; Nodes at the last level ; To store the count of nodes x dist away from root ; To store the count of nodes x dist away from leaf ; If X = h then print nodes at last level else nodes at Xth level ; 2 ^ X ; Number of left leaf nodes at ( h - 1 ) th level observe that if nodes are not present at last level then there are a / 2 leaf nodes at ( h - 1 ) th level ; If X = h then print leaf nodes at the last h level + leaf nodes at ( h - 1 ) th level ; First calculate nodes for leaves present at height h ; Then calculate nodes for leaves present at height h - 1 ; Add both the resuls ; Driver Code
function countNodes ( N , X ) { let height = Math . floor ( Math . log ( N ) / Math . log ( 2 ) ) ; if ( X > height ) { document . write ( " " ) ; return ; } if ( N == 1 ) { document . write ( " " ) ; return ; } let max_total_nodes = ( 1 << ( height + 1 ) ) - 1 ; let nodes_last_level = ( 1 << height ) - ( max_total_nodes - N ) ; let from_root ; let from_leaf ; if ( X == height ) from_root = nodes_last_level ; else from_root = 1 << X ; let left_leaf_nodes = Math . floor ( ( ( 1 << height ) - nodes_last_level ) / 2 ) ; if ( X == 0 ) { from_leaf = nodes_last_level + left_leaf_nodes ; } else { let i = X ; while ( nodes_last_level > 1 && i > 0 ) { nodes_last_level = Math . floor ( Math . ceil ( nodes_last_level / 2 ) ) ; i -- ; } from_leaf = nodes_last_level ; i = X ; while ( left_leaf_nodes > 1 && i > 0 ) { left_leaf_nodes = Math . floor ( Math . ceil ( left_leaf_nodes / 2 ) ) ; i -- ; } from_leaf += left_leaf_nodes ; } document . write ( from_root + " " + from_leaf ) ; } let N = 38 , X = 3 ; countNodes ( N , X ) ;
Number of ways of writing N as a sum of 4 squares | Number of ways of writing n as a sum of 4 squares ; Sum of odd and even factor ; Iterate from 1 to the number ; If i is the factor of n ; If factor is even ; If factor is odd ; n / i is also a factor ; If factor is even ; If factor is odd ; If n is odd ; If n is even ; Driver code
function sum_of_4_squares ( n ) { var i , odd = 0 , even = 0 ; for ( i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i % 2 == 0 ) even += i ; else odd += i ; if ( ( n / i ) != i ) { if ( ( n / i ) % 2 == 0 ) even += ( n / i ) ; else odd += ( n / i ) ; } } } if ( n % 2 == 1 ) return 8 * ( odd + even ) ; else return 24 * ( odd ) ; } var n = 4 ; document . write ( sum_of_4_squares ( n ) ) ;
Find the Nth Mosaic number | Function to return the nth mosaic number ; Iterate from 2 to the number ; If i is the factor of n ; Find the count where i ^ count is a factor of n ; Divide the number by i ; Increase the count ; Multiply the answer with count and i ; Return the answer ; Driver code
function mosaic ( n ) { let i , ans = 1 ; for ( i = 2 ; i <= n ; i ++ ) { if ( n % i == 0 && n > 0 ) { let count = 0 ; while ( n % i == 0 ) { n = parseInt ( n / i , 10 ) ; count ++ ; } ans *= count * i ; } } return ans ; } let n = 36 ; document . write ( mosaic ( n ) ) ;
Seating arrangement of N boys sitting around a round table such that two particular boys sit together | Function to return the total count of ways ; Find ( n - 1 ) factorial ; Return ( n - 1 ) ! * 2 ! ; Driver code
function Total_Ways ( n ) { var fac = 1 ; for ( i = 2 ; i <= n - 1 ; i ++ ) { fac = fac * i ; } return ( fac * 2 ) ; } var n = 5 ; document . write ( Total_Ways ( n ) ) ;
Find the maximum number of elements divisible by 3 | Function to find the maximum number of elements divisible by 3 ; To store frequency of each number ; Store modulo value ; Store frequency ; Add numbers with zero modulo to answer ; Find minimum of elements with modulo frequency one and zero ; Add k to the answer ; Remove them from frequency ; Add numbers possible with remaining frequency ; Return the required answer ; Driver code ; Function call
function MaxNumbers ( a , n ) { let fre = new Array ( 3 ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { a [ i ] %= 3 ; fre [ a [ i ] ] ++ ; } let ans = fre [ 0 ] ; let k = Math . min ( fre [ 1 ] , fre [ 2 ] ) ; ans += k ; fre [ 1 ] -= k ; fre [ 2 ] -= k ; ans += parseInt ( fre [ 1 ] / 3 ) + parseInt ( fre [ 2 ] / 3 ) ; return ans ; } let a = [ 1 , 4 , 10 , 7 , 11 , 2 , 8 , 5 , 9 ] ; let n = a . length ; document . write ( MaxNumbers ( a , n ) ) ;
Count pairs with set bits sum equal to K | Function to return the count of set bits in n ; Function to return the count of required pairs ; To store the count ; Sum of set bits in both the integers ; If current pair satisfies the given condition ; Driver code
function countSetBits ( n ) { var count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } function pairs ( arr , n , k ) { var count = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { var sum = countSetBits ( arr [ i ] ) + countSetBits ( arr [ j ] ) ; if ( sum == k ) count ++ ; } } return count ; } var arr = [ 1 , 2 , 3 , 4 , 5 ] ; var n = arr . length ; var k = 4 ; document . write ( pairs ( arr , n , k ) ) ;
Count pairs with set bits sum equal to K | javascript implementation of the approach ; Function to return the count of set bits in n ; Function to return the count of required pairs ; To store the count ; Frequency array ; If current pair satisfies the given condition ; ( arr [ i ] , arr [ i ] ) cannot be a valid pair ; Driver code
var MAX = 32 ; function countSetBits ( n ) { var count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } function pairs ( arr , n , k ) { var count = 0 ; var f = Array . from ( { length : MAX + 1 } , ( _ , i ) => 0 ) ; for ( i = 0 ; i < n ; i ++ ) f [ countSetBits ( arr [ i ] ) ] ++ ; for ( i = 0 ; i <= MAX ; i ++ ) { for ( j = i ; j <= MAX ; j ++ ) { if ( i + j == k ) { if ( i == j ) count += ( ( f [ i ] * ( f [ i ] - 1 ) ) / 2 ) ; else count += ( f [ i ] * f [ j ] ) ; } } } return count ; } var arr = [ 1 , 2 , 3 , 4 , 5 ] ; var n = arr . length ; var k = 4 ; document . write ( pairs ( arr , n , k ) ) ;
Print Lower Hessenberg matrix of order N | Function to print the Lower Hessenberg matrix of order n ; If element is above super - diagonal then print 0 ; Print a random digit for every non - zero element ; Driver code
function LowerHessenbergMatrix ( n ) { for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = 1 ; j <= n ; j ++ ) { if ( j > i + 1 ) document . write ( ' ' + " " ) ; else document . write ( Math . floor ( Math . random ( ) * 10 ) + " " ) ; } document . write ( " " ) ; } } var n = 4 ; LowerHessenbergMatrix ( n ) ;
Print Upper Hessenberg matrix of order N | Function to print the Upper Hessenberg matrix of order n ; If element is below sub - diagonal then print 0 ; Print a random digit for every non - zero element ; Driver code
function UpperHessenbergMatrix ( n ) { for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = 1 ; j <= n ; j ++ ) { if ( i > j + 1 ) document . write ( ' ' + " " ) ; else document . write ( Math . floor ( Math . random ( ) * 10 ) + " " ) ; } document . write ( " " ) ; } } var n = 4 ; UpperHessenbergMatrix ( n ) ;
Find the total number of composite factor for a given number | Function to return the count of prime factors of n ; Initialise array with 0 ; Stored i value into an array ; Every non - zero value at a [ i ] denotes that i is a factor of n ; Find if i is prime ; If i is a factor of n and i is not prime ; Driver code
function composite_factors ( n ) { var count = 0 ; var i , j ; var a = Array ( n + 1 ) . fill ( 0 ) ; for ( i = 1 ; i <= n ; ++ i ) { if ( n % i == 0 ) { a [ i ] = i ; } } for ( i = 2 ; i <= n ; i ++ ) { j = 2 ; var p = 1 ; while ( j < a [ i ] ) { if ( a [ i ] % j == 0 ) { p = 0 ; break ; } j ++ ; } if ( p == 0 && a [ i ] != 0 ) { count ++ ; } } return count ; } var n = 100 ; document . write ( composite_factors ( n ) ) ;
Sum of all mersenne numbers present in an array | Function that returns true if n is a Mersenne number ; Function to return the sum of all the Mersenne numbers from the given array ; To store the required sum ; If current element is a Mersenne number ; Driver Code
function isMersenne ( n ) { while ( n != 0 ) { let r = n % 2 ; if ( r == 0 ) return false ; n = Math . floor ( n / 2 ) ; } return true ; } function sumOfMersenne ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 && isMersenne ( arr [ i ] ) ) { sum += arr [ i ] ; } } return sum ; } let arr = [ 17 , 6 , 7 , 63 , 3 ] ; let n = arr . length ; document . write ( sumOfMersenne ( arr , n ) ) ;
Sum of all perfect numbers present in an array | Function to return the sum of all the proper factors of n ; f is the factor of n ; Function to return the required sum ; To store the sum ; If current element is non - zero and equal to the sum of proper factors of itself ; Driver code
function sumOfFactors ( n ) { let sum = 0 ; for ( let f = 1 ; f <= n / 2 ; f ++ ) { if ( n % f == 0 ) { sum += f ; } } return sum ; } function getSum ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 && arr [ i ] == sumOfFactors ( arr [ i ] ) ) { sum += arr [ i ] ; } } return sum ; } let arr = [ 17 , 6 , 10 , 6 , 4 ] ; let n = arr . length ; document . write ( getSum ( arr , n ) ) ;
Check if two numbers have same number of digits | Function that return true if A and B have same number of digits ; Both must be 0 now if they had same lengths ; Driver code
function sameLength ( A , B ) { while ( A > 0 && B > 0 ) { A = parseInt ( A / 10 ) ; B = parseInt ( B / 10 ) ; } if ( A == 0 && B == 0 ) return true ; return false ; } let A = 21 , B = 1 ; if ( sameLength ( A , B ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find a way to fill matrix with 1 ' s ▁ and ▁ 0' s in blank positions | Javascript implementation of the approach ; Function to generate and print the required matrix ; Replace the ' . ' ; Toggle number ; For each row , change the starting number ; Driver code
const N = 2 ; const M = 2 ; function Matrix ( a ) { let ch = ' ' ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( a [ i ] [ j ] == ' ' ) a [ i ] [ j ] = ch ; ch = ( ch == ' ' ) ? ' ' : ' ' ; document . write ( a [ i ] [ j ] + " " ) ; } document . write ( " " ) ; if ( i % 2 == 0 ) ch = ' ' ; else ch = ' ' ; } } let a = [ [ ' ' , ' ' ] , [ ' ' , ' ' ] ] ; Matrix ( a ) ;
Queries to check whether all the elements can be made positive by flipping signs exactly K times | To store the count of negative integers ; Check if zero exists ; Function to find the count of negative integers and check if zero exists in the array ; Function that returns true if array elements can be made positive by changing signs exactly k times ; Driver code ; Pre - processing ; Perform queries
let cnt_neg = 0 ; let exists_zero = false ; function preProcess ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) cnt_neg ++ ; if ( arr [ i ] == 0 ) exists_zero = true ; } } function isPossible ( k ) { if ( ! exists_zero ) { if ( k >= cnt_neg && ( k - cnt_neg ) % 2 == 0 ) return true ; else return false ; } else { if ( k >= cnt_neg ) return true ; else return false ; } } let arr = [ - 1 , 2 , - 3 , 4 , 5 ] ; let n = arr . length ; preProcess ( arr , n ) ; let queries = [ 1 , 2 , 3 , 4 ] ; let q = queries . length ; for ( let i = 0 ; i < q ; i ++ ) { if ( isPossible ( queries [ i ] ) ) document . write ( " " ) ; else document . write ( " " ) ; }
Sum of XOR of all sub | Sum of XOR of all K length sub - array of an array ; If the length of the array is less than k ; Array that will store xor values of subarray from 1 to i ; Traverse through the array ; If i is greater than zero , store xor of all the elements from 0 to i ; If it is the first element ; If i is greater than k ; Xor of values from 0 to i ; Now to find subarray of length k that ends at i , xor sum with x [ i - k ] ; Add the xor of elements from i - k + 1 to i ; Return the resultant sum ; ; Driver code
function FindXorSum ( arr , k , n ) { if ( n < k ) return 0 ; let x = new Array ( n ) . fill ( 0 ) ; let result = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( i > 0 ) x [ i ] = x [ i - 1 ] ^ arr [ i ] ; else x [ i ] = arr [ i ] ; if ( i >= k - 1 ) { let sum = 0 ; sum = x [ i ] ; if ( i - k > - 1 ) sum ^= x [ i - k ] ; result += sum ; } } return result ; } let arr = [ 1 , 2 , 3 , 4 ] ; let n = 4 , k = 2 ; document . write ( FindXorSum ( arr , k , n ) ) ;
Count the number of digits of palindrome numbers in an array | Function to return the reverse of n ; Function that returns true if n is a palindrome ; Function to return the count of digits of n ; Function to return the count of digits in all the palindromic numbers of arr [ ] ; If arr [ i ] is a one digit number or it is a palindrome ; Driver code
function reverse ( n ) { let rev = 0 ; while ( n > 0 ) { let d = n % 10 ; rev = rev * 10 + d ; n = parseInt ( n / 10 ) ; } return rev ; } function isPalin ( n ) { return ( n == reverse ( n ) ) ; } function countDigits ( n ) { let c = 0 ; while ( n > 0 ) { n = parseInt ( n / 10 ) ; c ++ ; } return c ; } function countPalinDigits ( arr , n ) { let s = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 10 || isPalin ( arr [ i ] ) ) { s += countDigits ( arr [ i ] ) ; } } return s ; } let arr = [ 121 , 56 , 434 ] ; let n = arr . length ; document . write ( countPalinDigits ( arr , n ) ) ;
Sum of all palindrome numbers present in an Array | Function to reverse a number n ; Function to check if a number n is palindrome ; If n is equal to the reverse of n it is a palindrome ; Function to calculate sum of all array elements which are palindrome ; Summation of all palindrome numbers present in array ; Driver Code
function reverse ( n ) { let d = 0 , s = 0 ; while ( n > 0 ) { d = n % 10 ; s = s * 10 + d ; n = Math . floor ( n / 10 ) ; } return s ; } function isPalin ( n ) { return n == reverse ( n ) ; } function sumOfArray ( arr , n ) { let s = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] > 10 ) && isPalin ( arr [ i ] ) ) { s += arr [ i ] ; } } return s ; } let n = 6 ; let arr = [ 12 , 313 , 11 , 44 , 9 , 1 ] ; document . write ( sumOfArray ( arr , n ) ) ;
Queries for the difference between the count of composite and prime numbers in a given range | Javascript implementation of the approach ; Function to update prime [ ] such prime [ i ] stores the count of prime numbers <= i ; Initialization ; 0 and 1 are not primes ; Mark composite numbers as false and prime numbers as true ; Update prime [ ] such that prime [ i ] will store the count of all the prime numbers <= i ; Function to return the absolute difference between the number of primes and the number of composite numbers in the range [ l , r ] ; Total elements in the range ; Count of primes in the range [ l , r ] ; Count of composite numbers in the range [ l , r ] ; Return the sbsolute difference ; Driver code ; Perform queries
const MAX = 1000000 ; let prime = new Array ( MAX + 1 ) ; function updatePrimes ( ) { for ( let i = 2 ; i <= MAX ; i ++ ) { prime [ i ] = 1 ; } prime [ 0 ] = prime [ 1 ] = 0 ; for ( let i = 2 ; i * i <= MAX ; i ++ ) { if ( prime [ i ] == 1 ) { for ( let j = i * i ; j <= MAX ; j += i ) { prime [ j ] = 0 ; } } } for ( let i = 1 ; i <= MAX ; i ++ ) { prime [ i ] += prime [ i - 1 ] ; } } function getDifference ( l , r ) { let total = r - l + 1 ; let primes = prime [ r ] - prime [ l - 1 ] ; let composites = total - primes ; return ( Math . abs ( primes - composites ) ) ; } let queries = [ [ 1 , 10 ] , [ 5 , 30 ] ] ; let q = queries . length ; updatePrimes ( ) ; for ( let i = 0 ; i < q ; i ++ ) document . write ( getDifference ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) + " " ) ;
Check if the given Prufer sequence is valid or not | Function that returns true if given Prufer sequence is valid ; Iterate in the Prufer sequence ; If out of range ; Driver code
function isValidSeq ( a , n ) { var nodes = n + 2 ; for ( var i = 0 ; i < n ; i ++ ) { if ( a [ i ] < 1 a [ i ] > nodes ) return false ; } return true ; } var a = [ 4 , 1 , 3 , 4 ] ; var n = a . length ; if ( isValidSeq ( a , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to Calculate e ^ x by Recursion ( using Taylor Series ) | Recursive Function with static variables p and f ; Termination condition ; Recursive call ; Update the power of x ; Factorial ; Driver Code
p = 1 , f = 1 ; function e ( x , n ) { var r ; if ( n == 0 ) return 1 ; r = e ( x , n - 1 ) ; p = p * x ; f = f * n ; return ( r + p / f ) ; } var x = 4 , n = 15 ; var res = e ( x , n ) ; document . write ( res . toFixed ( 6 ) ) ;