codeparrot/xlcost-text-to-code huggingface dataset:
Count the nodes in the given Tree whose weight is a Perfect Number | Javascript implementation to Count the nodes in the given tree whose weight is a Perfect Number ; Function that returns true if n is perfect ; Variable to store sum of divisors ; Find all divisors and add them ; Check if sum of divisors is equal to n , then n is a perfect number ; Function to perform dfs ; If weight of the current node is a perfect number ; Weights of the node ; Edges of the tree
var ans = 0 ; var graph = Array . from ( Array ( 100 ) , ( ) => Array ( ) ) ; var weight = Array . from ( Array ( 100 ) , ( ) => Array ( ) ) ; function isPerfect ( n ) { var sum = 1 ; for ( var i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i != n ) sum = sum + i + n / i ; else sum = sum + i ; } } if ( sum == n && n != 1 ) return true ; return false ; } function dfs ( node , parent ) { if ( isPerfect ( weight [ node ] ) ) ans += 1 ; graph [ node ] . forEach ( to => { if ( to != parent ) dfs ( to , node ) ; } ) ; } weight [ 1 ] = 5 ; weight [ 2 ] = 10 ; weight [ 3 ] = 11 ; weight [ 4 ] = 8 ; weight [ 5 ] = 6 ; graph [ 1 ] . push ( 2 ) ; graph [ 2 ] . push ( 3 ) ; graph [ 2 ] . push ( 4 ) ; graph [ 1 ] . push ( 5 ) ; dfs ( 1 , 1 ) ; document . write ( ans ) ;
Minimum cost to merge numbers from 1 to N | Function returns the minimum cost ; Min Heap representation ; Add all elements to heap ; First minimum ; Second minimum ; Multiply them ; Add to the cost ; Push the product into the heap again ; Return the optimal cost ; Driver code
function GetMinCost ( N ) { let pq = [ ] ; for ( let i = 1 ; i <= N ; i ++ ) { pq . push ( i ) ; } pq . sort ( function ( a , b ) { return a - b } ) ; let cost = 0 ; while ( pq . length > 1 ) { let mini = pq [ 0 ] ; pq . shift ( ) ; let secondmini = pq [ 0 ] ; pq . shift ( ) ; let current = mini * secondmini ; cost += current ; pq . push ( current ) ; pq . sort ( function ( a , b ) { return a - b } ) ; } return cost ; } let N = 5 ; document . write ( GetMinCost ( N ) ) ;
Permutation of first N positive integers such that prime numbers are at prime indices | Set 2 |  ; Computing count of prime numbers using sieve ; Computing permutations for primes ; Computing permutations for non - primes ; Driver program
var MOD = parseInt ( 1e9 + 7 ) ; function numPrimeArrangements ( n ) { var prime = Array . from ( { length : n + 1 } , ( _ , i ) => true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( var i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( prime [ i ] ) for ( factor = 2 ; factor * i <= n ; factor ++ ) prime [ factor * i ] = false ; } var primeIndices = 0 ; for ( var i = 1 ; i <= n ; i ++ ) if ( prime [ i ] ) primeIndices ++ ; var mod = parseInt ( ( 1e9 + 7 ) ) , res = 1 ; for ( var i = 1 ; i <= primeIndices ; i ++ ) res = ( ( 1 * res * i ) % mod ) ; for ( var i = 1 ; i <= ( n - primeIndices ) ; i ++ ) res = ( ( 1 * res * i ) % mod ) ; return res ; } var N = 5 ; document . write ( numPrimeArrangements ( N ) ) ;
Probability of getting K heads in N coin tosses | Function to calculate factorial ; Apply the formula ; Driver Code ; Call count_heads with n and r
function fact ( n ) { var res = 1 ; for ( var i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } function count_heads ( n , r ) { var output ; output = fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; output = output / ( Math . pow ( 2 , n ) ) ; return output ; } var n = 4 , r = 3 ; document . write ( count_heads ( n , r ) ) ;
Largest component size in a graph formed by connecting non | Javascript program to implement the above approach ; Mark this node as visited ; Apply dfs and add nodes belonging to this component ; Create graph and store in adjacency list form ; Iterate over all pair of nodes ; If not co - prime ; Build undirected graph ; Visited array for dfs ; Utility gcd ; Driver Code
function dfs ( u , adj , vis ) { vis [ u ] = 1 ; let componentSize = 1 ; for ( let it in adj [ u ] ) { if ( vis [ it ] == 0 ) { componentSize += dfs ( it , adj , vis ) ; } } return componentSize ; } function maximumComponentSize ( a , n ) { let adj = new Array ( n ) ; for ( var i = 0 ; i < adj . length ; i ++ ) { adj [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < adj . length ; i ++ ) { for ( var j = 0 ; j < adj . length ; j ++ ) { adj [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( __gcd ( a [ i ] , a [ j ] ) > 1 ) adj [ i ] . push ( j ) ; adj [ j ] . push ( i ) ; } } let answer = 0 ; let vis = Array . from ( { length : n } , ( _ , i ) => 0 ) ; for ( let k = 0 ; k < n ; k ++ ) { vis [ k ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { if ( vis [ i ] == 0 ) { answer = Math . max ( answer , dfs ( i , adj , vis ) ) ; } } return answer ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } let n = 8 ; let A = [ 2 , 3 , 6 , 7 , 4 , 12 , 21 , 39 ] ; document . write ( maximumComponentSize ( A , n ) ) ;
Largest component size in a graph formed by connecting non | smallest prime factor ; Sieve of Eratosthenes ; is spf [ i ] = 0 , then it 's prime ; smallest prime factor of all multiples is i ; Prime factorise n , and store prime factors in set s ; for implementing DSU ; root of component of node i ; finding root as well as applying path compression ; merging two components ; find roots of both components ; if already belonging to the same compenent ; Union by rank , the rank in this case is sizeContainer of the component . Smaller sizeContainer will be merged into larger , so the larger 's root will be  final root ; Function to find the maximum sized container ; intitalise the parents , and component sizeContainer ; intitally all component sizeContainers are 1 ans each node it parent of itself ; store prime factors of a [ i ] in s ; if this prime is seen as a factor for the first time ; if not then merge with that component in which this prime was previously seen ; maximum of sizeContainer of all components ; Driver Code
var spf = Array ( 100005 ) . fill ( 0 ) ; function sieve ( ) { for ( var i = 2 ; i < 100005 ; i ++ ) { if ( spf [ i ] == 0 ) { spf [ i ] = i ; for ( var j = 2 * i ; j < 100005 ; j += i ) { if ( spf [ j ] == 0 ) spf [ j ] = i ; } } } } function factorize ( n , s ) { while ( n > 1 ) { var z = spf [ n ] ; s . add ( z ) ; while ( n % z == 0 ) n = parseInt ( n / z ) ; } return s ; } var id = Array ( 100005 ) . fill ( 0 ) ; var par = Array ( 100005 ) . fill ( 0 ) ; var sizeContainer = Array ( 100005 ) . fill ( 0 ) ; function root ( i ) { if ( par [ i ] == i ) return i ; else return par [ i ] = root ( par [ i ] ) ; } function merge ( a , b ) { var p = root ( a ) ; var q = root ( b ) ; if ( p == q ) return ; if ( sizeContainer [ p ] > sizeContainer [ q ] ) { [ p , q ] = [ q , p ] } par [ p ] = q ; sizeContainer [ q ] += sizeContainer [ p ] ; } function maximumComponentsizeContainer ( a , n ) { for ( var i = 0 ; i < 100005 ; i ++ ) { par [ i ] = i ; sizeContainer [ i ] = 1 ; } sieve ( ) ; for ( var i = 0 ; i < n ; i ++ ) { var s = new Set ( ) ; s = factorize ( a [ i ] , s ) ; s . forEach ( it => { if ( id [ it ] == 0 ) id [ it ] = i + 1 ; else merge ( i + 1 , id [ it ] ) ; } ) ; } var answer = 0 ; for ( var i = 0 ; i < n ; i ++ ) answer = Math . max ( answer , sizeContainer [ i ] ) ; return answer ; } var n = 8 ; var A = [ 2 , 3 , 6 , 7 , 4 , 12 , 21 , 39 ] ; document . write ( maximumComponentsizeContainer ( A , n ) ) ;
Count of nodes in a Binary Tree whose child is its prime factors | JavaScript program for Counting nodes whose immediate children are its factors ; To store all prime numbers ; Create a boolean array " prime [ 0 . . N ] " and initialize all its entries as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiples of p and are less than p ^ 2 are already marked . ; A Tree node ; Utility function to create a new node ; Function to check if immediate children of a node are its factors or not ; Function to get the count of full Nodes in a binary tree ; If tree is empty ; Initialize count of full nodes having children as their factors ; If only right child exist ; If only left child exist ; Both left and right child exist ; Check for left child ; Check for right child ; Create Binary Tree as shown ; To save all prime numbers ; Print Count of all nodes having children as their factors
let N = 1000000 ; let prime = [ ] ; function SieveOfEratosthenes ( ) { let check = new Array ( N + 1 ) ; check . fill ( true ) ; for ( let p = 2 ; p * p <= N ; p ++ ) { if ( check [ p ] == true ) { prime . push ( p ) ; for ( let i = p * p ; i <= N ; i += p ) check [ i ] = false ; } } } class Node { constructor ( key ) { this . left = null ; this . right = null ; this . key = key ; } } function newNode ( key ) { let temp = new Node ( key ) ; return ( temp ) ; } function IsChilrenPrimeFactor ( parent , a ) { if ( prime [ a . key ] > 0 && ( parent . key % a . key == 0 ) ) return true ; else return false ; } function GetCount ( node ) { if ( node == null ) return 0 ; let q = [ ] ; let count = 0 ; q . push ( node ) ; while ( q . length > 0 ) { let temp = q [ 0 ] ; q . shift ( ) ; if ( temp . left == null && temp . right != null ) { if ( IsChilrenPrimeFactor ( temp , temp . right ) ) count ++ ; } else { if ( temp . right == null && temp . left != null ) { if ( IsChilrenPrimeFactor ( temp , temp . left ) ) count ++ ; } else { if ( temp . left != null && temp . right != null ) { if ( IsChilrenPrimeFactor ( temp , temp . right ) && IsChilrenPrimeFactor ( temp , temp . left ) ) count ++ ; } } } if ( temp . left != null ) q . push ( temp . left ) ; if ( temp . right != null ) q . push ( temp . right ) ; } return count ; } let root = newNode ( 10 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 5 ) ; root . right . left = newNode ( 18 ) ; root . right . right = newNode ( 12 ) ; root . right . left . left = newNode ( 2 ) ; root . right . left . right = newNode ( 3 ) ; root . right . right . left = newNode ( 3 ) ; root . right . right . right = newNode ( 14 ) ; root . right . right . right . left = newNode ( 7 ) ; SieveOfEratosthenes ( ) ; document . write ( GetCount ( root ) + " " ) ;
Check if original Array is retained after performing XOR with M exactly K times | Function to check if original Array can be retained by performing XOR with M exactly K times ; Check if O is present or not ; If K is odd and 0 is not present then the answer will always be No . ; Else it will be Yes ; Driver Code
function check ( Arr , n , M , K ) { let flag = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( Arr [ i ] == 0 ) flag = 1 ; } if ( K % 2 != 0 && flag == 0 ) return " " ; else return " " ; } let Arr = [ 1 , 1 , 2 , 4 , 7 , 8 ] ; let M = 5 ; let K = 6 ; let n = Arr . length ; document . write ( check ( Arr , n , M , K ) ) ;
Longest sub | To store the prime numbers ; Function that find prime numbers till limit ; Find primes using Sieve of Eratosthenes ; Function that finds all prime numbers in given range using Segmented Sieve ; Find the limit ; To store the prime numbers ; Comput all primes less than or equals to sqrt ( high ) using Simple Sieve ; Count the elements in the range [ low , high ] ; Declaring boolean for the range [ low , high ] ; Traverse the prime numbers till limit ; Find the minimum number in [ low . . high ] that is a multiple of prime [ i ] ; Mark the multiples of prime [ i ] in [ low , high ] as true ; Element which are not marked in range are Prime ; Function that finds longest subarray of prime numbers ; If element is Non - prime then updated current_max to 0 ; If element is prime , then update current_max and max_so_far ; Return the count of longest subarray ; Driver code ; Find minimum and maximum element ; Find prime in the range [ min_el , max_el ] ; Function call
let allPrimes = new Set ( ) ; function simpleSieve ( limit , prime ) { let mark = Array . from ( { length : limit + 1 } , ( _ , i ) => 0 ) ; for ( let i = 2 ; i <= limit ; ++ i ) { if ( mark [ i ] == false ) { prime . push ( i ) ; for ( let j = i ; j <= limit ; j += i ) { mark [ j ] = true ; } } } } function primesInRange ( low , high ) { let limit = Math . floor ( Math . sqrt ( high ) ) + 1 ; let prime = [ ] ; simpleSieve ( limit , prime ) ; let n = high - low + 1 ; let mark = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < prime . length ; i ++ ) { let loLim = Math . floor ( low / prime [ i ] ) ; loLim *= prime [ i ] ; if ( loLim < low ) { loLim += prime [ i ] ; } if ( loLim == prime [ i ] ) { loLim += prime [ i ] ; } for ( let j = loLim ; j <= high ; j += prime [ i ] ) mark [ j - low ] = true ; } for ( let i = low ; i <= high ; i ++ ) { if ( ! mark [ i - low ] ) { allPrimes . add ( i ) ; } } } function maxPrimeSubarray ( arr , n ) { let current_max = 0 ; let max_so_far = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! allPrimes . has ( arr [ i ] ) ) current_max = 0 ; else { current_max ++ ; max_so_far = Math . max ( current_max , max_so_far ) ; } } return max_so_far ; } let arr = [ 1 , 2 , 4 , 3 , 29 , 11 , 7 , 8 , 9 ] ; let n = arr . length ; let max_el = Number . MIN_VALUE ; let min_el = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < min_el ) { min_el = arr [ i ] ; } if ( arr [ i ] > max_el ) { max_el = arr [ i ] ; } } primesInRange ( min_el , max_el ) ; document . write ( maxPrimeSubarray ( arr , n ) ) ;
Sum of all armstrong numbers lying in the range [ L , R ] for Q queries | pref [ ] array to precompute the sum of all armstrong number ; Function that return number num if num is armstrong else return 0 ; Function to precompute the sum of all armstrong numbers upto 100000 ; checkarmstrong ( ) return the number i if i is armstrong else return 0 ; Function to print the sum for each query ; Function to prsum of all armstrong numbers between [ L , R ] ; Function that pre computes the sum of all armstrong numbers ; Iterate over all Queries to print the sum ; Queries ; Function that print the the sum of all armstrong number in Range [ L , R ]
let pref = new Array ( 100001 ) ; pref . fill ( 0 ) ; function checkArmstrong ( x ) { let n = ( x . toString ( ) ) . length ; let sum1 = 0 ; let temp = x ; while ( temp > 0 ) { let digit = temp % 10 ; sum1 += Math . pow ( digit , n ) ; temp = parseInt ( temp / 10 , 10 ) ; } if ( sum1 == x ) return x ; return 0 ; } function preCompute ( ) { for ( let i = 1 ; i < 100001 ; i ++ ) { pref [ i ] = pref [ i - 1 ] + checkArmstrong ( i ) ; } } function printSum ( L , R ) { document . write ( ( pref [ R ] - pref [ L - 1 ] ) + " " ) ; } function printSumarmstrong ( arr , Q ) { preCompute ( ) ; for ( let i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } let Q = 2 ; let arr = [ [ 1 , 13 ] , [ 121 , 211 ] ] ; printSumarmstrong ( arr , Q ) ;
Count of pairs in an array whose product is a perfect square | Prime [ ] array to calculate Prime Number ; Array k [ ] to store the value of k for each element in arr [ ] ; For value of k , Sieve function is implemented ; Initialize k [ i ] to i ; Prime Sieve ; If i is prime then remove all factors of prime from it ; Update that j is not prime ; Remove all square divisors i . e . if k [ j ] is divisible by i * i then divide it by i * i ; Function that return total count of pairs with perfect square product ; Map used to store the frequency of k ; Store the frequency of k ; The total number of pairs is the summation of ( fi * ( fi - 1 ) ) / 2 ; Driver code ; Size of arr [ ] ; To pre - compute the value of k ; Function that return total count of pairs with perfect square product
let prime = new Array ( 100001 ) . fill ( 0 ) ; let k = new Array ( 100001 ) . fill ( 0 ) ; function Sieve ( ) { for ( let i = 1 ; i < 100001 ; i ++ ) k [ i ] = i ; for ( let i = 2 ; i < 100001 ; i ++ ) { if ( prime [ i ] == 0 ) for ( let j = i ; j < 100001 ; j += i ) { prime [ j ] = 1 ; while ( k [ j ] % ( i * i ) == 0 ) k [ j ] /= ( i * i ) ; } } } function countPairs ( arr , n ) { let freq = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( freq . has ( k [ arr [ i ] ] ) ) { freq . set ( k [ arr [ i ] ] , freq . get ( k [ arr [ i ] ] ) + 1 ) ; } else freq . set ( k [ arr [ i ] ] , 1 ) ; } let sum = 0 ; for ( let i of freq ) { sum += ( ( i [ 1 ] - 1 ) * i [ 1 ] ) / 2 ; } return sum ; } let arr = [ 1 , 2 , 4 , 8 , 5 , 6 ] ; let n = arr . length ; Sieve ( ) ; document . write ( countPairs ( arr , n ) + " " ) ;
Sum of all Perfect Cubes lying in the range [ L , R ] for Q queries | Array to precompute the sum of cubes from 1 to 100010 so that for every query , the answer can be returned in O ( 1 ) . ; Function to check if a number is a perfect Cube or not ; Find floating point value of cube root of x . ; If cube root of x is cr return the x , else 0 ; Function to precompute the perfect Cubes upto 100000. ; Function to print the sum for each query ; To calculate the precompute function ; Calling the printSum function for every query
var pref = Array ( 100010 ) . fill ( 0 ) ; function isPerfectCube ( x ) { var cr = Math . round ( Math . cbrt ( x ) ) ; return ( cr * cr * cr == x ) ? x : 0 ; } function compute ( ) { for ( var i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isPerfectCube ( i ) ; } } function printSum ( L , R ) { var sum = pref [ R ] - pref [ L - 1 ] ; document . write ( sum + " " ) ; } compute ( ) ; var Q = 4 ; var arr = [ [ 1 , 10 ] , [ 1 , 100 ] , [ 2 , 25 ] , [ 4 , 50 ] ] ; for ( var i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; }
Total number of Subsets of size at most K | Function for the binomial coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to calculate sum of nCj from j = 1 to k ; Calling the nCr function for each value of j ; Driver code
function binomialCoeff ( n , k ) { var C = new Array ( n + 1 ) ; for ( var i = 0 ; i < C . length ; i ++ ) { C [ i ] = new Array ( k + 1 ) ; } var i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } function count ( n , k ) { var sum = 0 ; for ( var j = 1 ; j <= k ; j ++ ) { sum = sum + binomialCoeff ( n , j ) ; } return sum ; } var n = 3 ; var k = 2 ; document . write ( count ( n , k ) ) ; var n = 5 ; var k = 2 ; document . write ( count ( n , k ) ) ;
Find smallest number with given number of digits and sum of digits under given constraints | Function to find the number having sum of digits as s and d number of digits such that the difference between the maximum and the minimum digit the minimum possible ; To store the final number ; To store the value that is evenly distributed among all the digits ; To store the remaining sum that still remains to be distributed among d digits ; rem stores the value that still remains to be distributed To keep the difference of digits minimum last rem digits are incremented by 1 ; In the last rem digits one is added to the value obtained by equal distribution ; num = num + toString ( val ) ;
function findNumber ( s , d ) { var num = [ ] ; var val = parseInt ( s / d ) ; var rem = s % d ; for ( var i = 1 ; i <= d - rem ; i ++ ) { num . push ( val . toString ( ) ) ; } if ( rem != 0 ) { val ++ ; for ( var i = d - rem + 1 ; i <= d ; i ++ ) { num . push ( val . toString ( ) ) ; } } return num ; } var s = 25 , d = 4 ; var n = findNumber ( s , d ) ; for ( var i = 0 ; i < n . length ; i ++ ) { document . write ( n [ i ] ) ; }
Find Kth smallest value for b such that a + b = a | b | Function to find the kth smallest value for b ; res will store final answer ; skip when j ' th ▁ position ▁ ▁ has ▁ 1 ▁ in ▁ binary ▁ representation ▁ ▁ as ▁ in ▁ res , ▁ j ' th position will be 0. ; j 'th bit is set ; i 'th bit is set ; proceed to next bit ; Driver Code
function kthSmallest ( a , k ) { var res = 0 ; var j = 0 ; for ( var i = 0 ; i < 32 ; i ++ ) { while ( j < 32 && ( a & ( 1 << j ) ) ) j ++ ; if ( k & ( 1 << i ) ) res |= ( 1 << j ) ; j ++ ; } return res ; } var a = 10 , k = 3 ; document . write ( kthSmallest ( a , k ) ) ;
Find the sum of power of bit count raised to the power B | Function to calculate a ^ b mod m using fast - exponentiation method ; * Function to get no of set bits in binary representation of positive integer n ; Function to calculate sum ; Itereate for all values of array A ; Calling fast - exponentiation and appending ans to sum ; Driver code
function fastmod ( base , exp , mod ) { if ( exp == 0 ) return 1 ; else if ( exp % 2 == 0 ) { var ans = fastmod ( base , exp / 2 , mod ) ; return ( ans % mod * ans % mod ) % mod ; } else return ( fastmod ( base , exp - 1 , mod ) % mod * base % mod ) % mod ; } function countSetBits ( n ) { var count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } function findPowerSum ( n , ar ) { var mod = parseInt ( 1e9 + 7 ) ; var sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { var base = countSetBits ( ar [ i ] ) ; var exp = ar [ i ] ; sum += fastmod ( base , exp , mod ) ; sum %= mod ; } return sum ; } var n = 3 ; var ar = [ 1 , 2 , 3 ] ; document . write ( findPowerSum ( n , ar ) ) ;
Count the total number of triangles after Nth operation | Method to return the total no . of Triangles ; For every subtriangle formed there are possibilities of generating ( curr * 3 ) + 2 ; Changing the curr value to Tri_count ; driver code
function CountTriangles ( n ) { var curr = 1 ; var Tri_count = 0 ; for ( i = 1 ; i <= n ; i ++ ) { Tri_count = ( curr * 3 ) + 2 ; curr = Tri_count ; } return Tri_count ; } var n = 10 ; document . write ( CountTriangles ( n ) ) ;
In how many ways the ball will come back to the first boy after N turns | Function to return the number of sequences that get back to Bob ; Driver code
function numSeq ( n ) { return Math . floor ( ( Math . pow ( 3 , n ) + 3 * Math . pow ( - 1 , n ) ) / 4 ) ; } let N = 10 ; document . write ( numSeq ( N ) ) ;
Count subarrays such that remainder after dividing sum of elements by K gives count of elements | Function to return the number of subarrays of the given array such that the remainder when dividing the sum of its elements by K is equal to the number of its elements ; To store prefix sum ; We are dealing with zero indexed array ; Taking modulus value ; Prefix sum ; To store the required answer , the left index and the right index ; To store si - i value ; Include sum ; Increment the right index ; If subarray has at least k elements ; Return the required answer ; Driver code ; Function call
function sub_arrays ( a , n , k ) { let sum = new Array ( n + 2 ) ; for ( let i = 0 ; i < n + 2 ; i ++ ) { sum [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { a [ i ] -- ; a [ i ] %= k ; sum [ i + 1 ] += sum [ i ] + a [ i ] ; sum [ i + 1 ] %= k ; } let ans = 0 , l = 0 , r = 0 ; let mp = new Map ( ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) { if ( ! mp . has ( sum [ i ] ) ) mp . set ( sum [ i ] , 0 ) ; } let temp ; for ( let i = 0 ; i < n + 1 ; i ++ ) { ans += mp . get ( sum [ i ] ) ; temp = mp . get ( sum [ i ] ) + 1 ; mp . set ( sum [ i ] , temp ) ; r ++ ; if ( r - l >= k ) { temp = mp . get ( sum [ l ] ) - 1 ; mp . set ( sum [ i ] , temp ) ; l ++ ; } } return ans ; } let a = [ 1 , 4 , 2 , 3 , 5 ] ; let n = a . length ; let k = 4 ; document . write ( sub_arrays ( a , n , k ) ) ;
Sum of all the prime numbers with the maximum position of set bit â ‰¤ D | Function for Sieve of Eratosthenes ; Function to return the sum of the required prime numbers ; Maximum number of the required range ; Sieve of Eratosthenes ; To store the required sum ; If current element is prime ; Driver code
function sieve ( prime , n ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( var p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( var i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } } function sumPrime ( d ) { var maxVal = Math . pow ( 2 , d ) - 1 ; var prime = new Array ( maxVal + 1 ) ; prime . fill ( true ) ; sieve ( prime , maxVal ) ; var sum = 0 ; for ( var i = 2 ; i <= maxVal ; i ++ ) { if ( prime [ i ] ) { sum += i ; } } return sum ; } var d = 8 ; document . write ( sumPrime ( d ) ) ;