codeparrot/xlcost-text-to-code huggingface dataset:
Check if it is possible to reach ( X , Y ) from ( 1 , 0 ) by given steps | Function to find the GCD of two numbers a and b ; Base Case ; Recursively find the GCD ; Function to check if ( x , y ) can be reached from ( 1 , 0 ) from given moves ; If GCD is 1 , then print " Yes " ; Given X and Y ; Function call
function GCD ( a , b ) { if ( b == 0 ) return a ; else return GCD ( b , a % b ) ; } function check ( x , y ) { if ( GCD ( x , y ) == 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let X = 2 , Y = 7 ; check ( X , Y ) ;
Probability of Euler 's Totient Function in a range [L, R] to be divisible by M | JavaScript Program to implement the above approach ; Seieve of Erotosthenes to compute all primes ; If prime ; Mark all its multiples as non - prime ; Function to find the probability of Euler 's Totient Function in a given range ; Initializing two arrays with values from L to R for Euler 's totient ; Indexing from 0 ; If the current number is prime ; Checking if i is prime factor of numbers in range L to R ; Update all the numbers which has prime factor i ; If number in range has a prime factor > Math . Sqrt ( number ) ; Count those which are divisible by M ; Return the result ; Driver Code
let size = 1000001 ; let prime = new Array ( size , 0 ) ; function seiveOfEratosthenes ( ) { prime [ 0 ] = 1 ; prime [ 1 ] = 0 ; for ( let i = 2 ; i * i < 1000001 ; i ++ ) { if ( prime [ i ] == 0 ) { for ( let j = i * i ; j < 1000001 ; j += i ) { prime [ j ] = 1 ; } } } } function probabiltyEuler ( L , R , M ) { let arr = new Array ( size , 0 ) ; let eulerTotient = new Array ( size , 0 ) ; let count = 0 ; for ( let i = L ; i <= R ; i ++ ) { eulerTotient [ i - L ] = i ; arr [ i - L ] = i ; } for ( let i = 2 ; i < 1000001 ; i ++ ) { if ( prime [ i ] == 0 ) { for ( let j = ( L / i ) * i ; j <= R ; j += i ) { if ( j - L >= 0 ) { eulerTotient [ j - L ] = eulerTotient [ j - L ] / i * ( i - 1 ) ; while ( arr [ j - L ] % i == 0 ) { arr [ j - L ] /= i ; } } } } } for ( let i = L ; i <= R ; i ++ ) { if ( arr [ i - L ] > 1 ) { eulerTotient [ i - L ] = ( eulerTotient [ i - L ] / arr [ i - L ] ) * ( arr [ i - L ] - 1 ) ; } } for ( let i = L ; i <= R ; i ++ ) { if ( ( eulerTotient [ i - L ] % M ) == 0 ) { count ++ ; } } count /= 2 ; return 1.0 * count / ( R + 1 - L ) ; } seiveOfEratosthenes ( ) ; let L = 1 ; let R = 7 ; let M = 3 ; document . write ( probabiltyEuler ( L , R , M ) . toFixed ( 7 ) ) ;
Largest odd divisor Game to check which player wins | Function to find the Largest Odd Divisior Game to check which player wins ; Check if n == 1 then player 2 will win ; Check if n == 2 or n is odd ; While n is greater than k and divisible by 2 keep incrementing tha val ; Loop to find greatest odd divisor ; Check if n is a power of 2 ; Check if cnt is not one then player 1 wins ; Driver Code
function findWinner ( n , k ) { let cnt = 0 ; if ( n == 1 ) document . write ( " " ) ; else if ( ( n & 1 ) != 0 n == 2 ) document . write ( " " ) ; else { let tmp = n ; let val = 1 ; while ( tmp > k && tmp % 2 == 0 ) { tmp /= 2 ; val *= 2 ; } for ( let i = 3 ; i <= Math . sqrt ( tmp ) ; i ++ ) { while ( tmp % i == 0 ) { cnt ++ ; tmp /= i ; } } if ( tmp > 1 ) cnt ++ ; if ( val == n ) document . write ( " " ) ; else if ( n / tmp == 2 && cnt == 1 ) document . write ( " " ) ; else document . write ( " " ) ; } } let n = 1 , k = 1 ; findWinner ( n , k ) ;
Find all numbers up to N which are both Pentagonal and Hexagonal | Function to prvar numbers upto N which are both pentagonal as well as hexagonal numbers ; Calculate i - th pentagonal number ; Check if the pentagonal number pn is hexagonal or not ; Driver code
function pen_hex ( n ) { var pn = 1 ; for ( i = 1 ; i < n ; i ++ ) { pn = parseInt ( i * ( 3 * i - 1 ) / 2 ) ; if ( pn > n ) break ; var seqNum = ( 1 + Math . sqrt ( 8 * pn + 1 ) ) / 4 ; if ( seqNum == parseInt ( seqNum ) ) document . write ( pn + " " ) ; } } var N = 1000000 ; pen_hex ( N ) ;
Check if row | Function to check if row - major order traversal of the matrix is is palindrome ; Loop to check if the matrix is matrix is palindrome or not ; Driver Code
function isPal ( a , n , m ) { for ( let i = 0 ; i < n / 2 ; i ++ ) { for ( let j = 0 ; j < m - 1 ; j ++ ) { if ( a [ i ] [ j ] != a [ n - 1 - i ] [ m - 1 - j ] ) return false ; } } return true ; } let n = 3 , m = 3 ; let a = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 4 ] , [ 3 , 2 , 1 ] ] ; if ( isPal ( a , n , m ) ) { document . write ( " " + " " ) ; } else { document . write ( " " + " " ) ; }
Find the smallest number whose sum of digits is N | Function to get sum of digits ; Function to find the smallest number whose sum of digits is also N ; Checking if number has sum of digits = N ; Driver code
function getSum ( n ) { let sum = 0 ; while ( n != 0 ) { sum = sum + n % 10 ; n = Math . floor ( n / 10 ) ; } return sum ; } function smallestNumber ( N ) { let i = 1 ; while ( 1 ) { if ( getSum ( i ) == N ) { document . write ( i ) ; break ; } i ++ ; } } let N = 10 ; smallestNumber ( N ) ;
Rare Numbers | Iterative function to reverse digits of num ; Function to check if N is perfect square ; Find floating point value of square root of x . ; If square root is an integer ; Function to check if N is an Rare number ; Find reverse of N ; Number should be non - palindromic ; Driver code
function reversDigits ( num ) { let rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = parseInt ( num / 10 ) ; } return rev_num ; } function isPerfectSquare ( x ) { let sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } function isRare ( N ) { let reverseN = reversDigits ( N ) ; if ( reverseN == N ) return false ; return isPerfectSquare ( N + reverseN ) && isPerfectSquare ( N - reverseN ) ; } let n = 65 ; if ( isRare ( n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2 | Function which will calculate the elements in the given range ; Store the current power of 2 ; Store the current power of 3 ; power23 [ ] will store pairwise product of elements of power2 and power3 that are <= r ; Insert in power23 ] [ ] only if mul <= r ; Store the required answer ; Print the result ; Driver code
function calc_ans ( l , r ) { var power2 = [ ] , power3 = [ ] ; var mul2 = 1 ; while ( mul2 <= r ) { power2 . push ( mul2 ) ; mul2 *= 2 ; } var mul3 = 1 ; while ( mul3 <= r ) { power3 . push ( mul3 ) ; mul3 *= 3 ; } var power23 = [ ] ; for ( var x = 0 ; x < power2 . length ; x ++ ) { for ( var y = 0 ; y < power3 . length ; y ++ ) { var mul = power2 [ x ] * power3 [ y ] ; if ( mul == 1 ) continue ; if ( mul <= r ) power23 . push ( mul ) ; } } var ans = 0 ; power23 . forEach ( x => { if ( x >= l && x <= r ) ans ++ ; } ) ; document . write ( ans ) ; } var l = 1 , r = 10 ; calc_ans ( l , r ) ;
Count of K length subsequence whose product is even | Function to calculate nCr ; Returns factorial of n ; Function for finding number of K length subsequences whose product is even number ; counting odd numbers in the array ; Driver code
function nCr ( n , r ) { if ( r > n ) return 0 ; return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } function fact ( n ) { var res = 1 ; for ( var i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } function countSubsequences ( arr , n , k ) { var countOdd = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 ) countOdd ++ ; } var ans = nCr ( n , k ) - nCr ( countOdd , k ) ; return ans ; } var arr = [ 2 , 4 ] ; var K = 1 ; var N = arr . length ; document . write ( countSubsequences ( arr , N , K ) ) ;
Find most significant bit of a number X in base Y | Function to find the first digit of X in base Y ; calculating number of digits of x in base y ; finding first digit of x in base y ; Driver code
function first_digit ( x , y ) { var length = parseInt ( Math . log ( x ) / Math . log ( y ) ) + 1 ; var first_digit = parseInt ( x / Math . pow ( y , length - 1 ) ) ; document . write ( first_digit ) ; } var X = 55 , Y = 3 ; first_digit ( X , Y ) ;
Curzon Numbers | Function to check if a number is a Curzon number or not ; Find 2 ^ N + 1 ; Find 2 * N + 1 ; Check for divisibility ; Driver code
function checkIfCurzonNumber ( N ) { var powerTerm , productTerm ; powerTerm = Math . pow ( 2 , N ) + 1 ; productTerm = 2 * N + 1 ; if ( powerTerm % productTerm == 0 ) { document . write ( " " + " " ) ; } else { document . write ( " " ) ; } } var N = 5 ; checkIfCurzonNumber ( N ) ; N = 10 ; checkIfCurzonNumber ( N ) ;
Minimum count of numbers required ending with 7 to sum as a given number | Function to return the count of minimum numbers ending with 7 required such that the sum of these numbers is n ; hasharr [ i ] will store the minimum numbers ending with 7 so that it sums to number ending with digit i ; Its always possible to write numbers > 69 to write as numbers ending with 7 ; If the number is atleast equal to the sum of minimum numbers ending with 7 ; Driver code
function minCount ( n ) { let hasharr = [ 10 , 3 , 6 , 9 , 2 , 5 , 8 , 1 , 4 , 7 ] ; if ( n > 69 ) return hasharr [ n % 10 ] ; else { if ( n >= hasharr [ n % 10 ] * 7 ) return ( hasharr [ n % 10 ] ) ; else return - 1 ; } } let n = 38 ; document . write ( minCount ( n ) ) ;
Program to print modified Binary triangle pattern | Function to print the modified binary pattern ; Loop to traverse the rows ; Loop to traverse the numbers in each row ; Check if j is 1 or i In either case print 1 ; Else print 0 ; Change the cursor to next line after each row ; Driver code ; Function call
function modifiedBinaryPattern ( n ) { for ( let i = 1 ; i <= n ; i ++ ) { for ( let j = 1 ; j <= i ; j ++ ) { if ( j == 1 j == i ) document . write ( 1 ) ; else document . write ( 0 ) ; } document . write ( " " ) ; } } let n = 7 ; modifiedBinaryPattern ( n ) ;
Find the real and imaginary part of a Complex number | Function to find real and imaginary parts of a complex number ; string length stored in variable l ; variable for the index of the separator ; Storing the index of ' + ' ; else storing the index of ' - ' ; Finding the real part of the complex number ; Finding the imaginary part of the complex number ; Driver code
function findRealAndImag ( s ) { let l = s . length - 1 ; let i ; if ( s . indexOf ( ' ' ) != - 1 ) { i = s . indexOf ( ' ' ) ; } else { i = s . indexOf ( ' ' ) ; } let real = s . substr ( 0 , i ) ; let imaginary = s . substr ( i + 1 , l - 2 ) ; document . write ( " " + real + " " ) ; document . write ( " " + imaginary ) ; } let s = " " ; findRealAndImag ( s ) ;
Distinct powers of a number N such that the sum is equal to K | Function to return the highest power of N not exceeding K ; Loop to find the highest power less than K ; Initializing the PowerArray with all 0 's. ; Function to print the distinct powers of N that add upto K ; Getting the highest power of n before k ; To check if the power is being used twice or not ; Prlet - 1 if power is being used twice ; If the power is not visited , then mark the power as visited ; Decrementing the value of K ; Prleting the powers of N that sum up to K ; Driver Code
function highestPower ( n , k ) { let i = 0 ; let a = Math . pow ( n , i ) ; while ( a <= k ) { i += 1 ; a = Math . pow ( n , i ) ; } return i - 1 ; } let b = Array . from ( { length : 50 } , ( _ , i ) => 0 ) ; function PowerArray ( n , k ) { while ( k > 0 ) { let t = highestPower ( n , k ) ; if ( b [ t ] > 0 ) { document . write ( - 1 ) ; return 0 ; } else b [ t ] = 1 ; k -= Math . pow ( n , t ) ; } for ( let i = 0 ; i < 50 ; i ++ ) { if ( b [ i ] > 0 ) { document . write ( i + " " ) ; } } return 0 ; } let N = 3 ; let K = 40 ; PowerArray ( N , K ) ;
Sum of elements in an array having composite frequency | JavaScript program to find sum of elements in an array having composite frequency ; Function to create Sieve to check primes ; If composite [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to composite ; Function to return the sum of elements in an array having composite frequency ; Map is used to store element frequencies ; To store sum ; Traverse the map using iterators ; Count the number of elements having composite frequencies ; Driver code ; Function call
let N = 100005 function SieveOfEratosthenes ( composite ) { for ( let i = 0 ; i < N ; i ++ ) composite [ i ] = false ; for ( let p = 2 ; p * p < N ; p ++ ) { if ( ! composite [ p ] ) { for ( let i = p * 2 ; i < N ; i += p ) composite [ i ] = true ; } } } function sumOfElements ( arr , n ) { let composite = new Array ( N ) ; SieveOfEratosthenes ( composite ) ; let m = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) if ( m . has ( arr [ i ] ) ) { m [ arr [ i ] ] = m [ arr [ i ] ] + 1 ; } else { m . set ( arr [ i ] , 1 ) ; } let sum = 0 ; m . forEach ( ( value , key ) => { if ( composite [ key ] ) { sum += value ; } } ) return sum ; } let arr = [ 1 , 2 , 1 , 1 , 1 , 3 , 3 , 2 , 4 ] ; let n = arr . length ; document . write ( sumOfElements ( arr , n ) ) ;
Delete all odd frequency elements from an Array | Function that removes the elements which have odd frequencies in the array ; Create a map to store the frequency of each element ; Remove the elements which have odd frequencies ; If the element has odd frequency then skip ; Driver code ; Function call
function remove ( arr , n ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . set ( arr [ i ] , 1 ) ; } } for ( let i = 0 ; i < n ; i ++ ) { if ( ( mp . has ( arr [ i ] ) && mp . get ( arr [ i ] ) % 2 == 1 ) ) continue ; document . write ( arr [ i ] + " " ) ; } } let arr = [ 3 , 3 , 3 , 2 , 2 , 4 , 7 , 7 ] ; let n = arr . length ; remove ( arr , n ) ;
Maximize the first element of the array such that average remains constant | Maximum value of the first array element that can be attained ; Variable to store the sum ; Loop to find the sum of array ; Desired maximum value ; Driver Code
function getmax ( arr , n , x ) { let s = 0 ; for ( let i = 0 ; i < n ; i ++ ) { s = s + arr [ i ] ; } document . write ( Math . min ( s , x ) ) ; } let arr = [ 1 , 2 , 3 , 4 ] ; let x = 5 ; let arr_size = arr . length ; getmax ( arr , arr_size , x ) ;
Minimum length of the shortest path of a triangle | function to get the minimum length of the shorter side of the triangle ; traversing through each polets on the plane ; if sum of a polets is greater than the previous one , the maximum gets replaced ; prlet the length ; initialize the number of polets ; polets on the plane
function shortestLength ( n , x , y ) { let answer = 0 ; let i = 0 ; while ( n != 0 && i < x . length ) { if ( x [ i ] + y [ i ] > answer ) answer = x [ i ] + y [ i ] ; i ++ ; } document . write ( " " + answer + " " ) ; document . write ( " " + " " + answer + " " + " " + answer + " " ) ; } let n = 4 ; let x = [ 1 , 4 , 2 , 1 ] ; let y = [ 4 , 1 , 1 , 2 ] ; shortestLength ( n , x , y ) ;
Intersecting rectangle when bottom | Function to find intersection rectangle of given two rectangles . ; Gives bottom - left point of intersection rectangle ; Gives top - right point of intersection rectangle ; No intersection ; Gives top - left point of intersection rectangle ; Gives bottom - right point of intersection rectangle ; bottom - left and top - right corners of first rectangle ; bottom - left and top - right corners of first rectangle ; Function call
function FindPoints ( x1 , y1 , x2 , y2 , x3 , y3 , x4 , y4 ) { var x5 = Math . max ( x1 , x3 ) ; var y5 = Math . max ( y1 , y3 ) ; var x6 = Math . min ( x2 , x4 ) ; var y6 = Math . min ( y2 , y4 ) ; if ( x5 > x6 y5 > y6 ) { document . write ( " " ) ; return ; } document . write ( " " + x5 + " " + y5 + " " ) ; document . write ( " " + x6 + " " + y6 + " " ) ; var x7 = x5 ; var y7 = y6 ; document . write ( " " + x7 + " " + y7 + " " ) ; var x8 = x6 ; var y8 = y5 ; document . write ( " " + x8 + " " + y8 + " " ) ; } var x1 = 0 , y1 = 0 , x2 = 10 , y2 = 8 ; var x3 = 2 , y3 = 3 , x4 = 7 , y4 = 9 ; FindPoints ( x1 , y1 , x2 , y2 , x3 , y3 , x4 , y4 ) ;
Find Corners of Rectangle using mid points | Structure to represent a co - ordinate point ; This function receives two points and length of the side of rectangle and prints the 4 corner points of the rectangle ; horizontal rectangle ; vertical rectangle ; slanted rectangle ; calculate slope of the side ; calculate displacements along axes ; Driver code
class Point { constructor ( a , b ) { this . x = a ; this . y = b ; } } function printCorners ( p , q , l ) { let a = new Point ( ) , b = new Point ( ) , c = new Point ( ) , d = new Point ( ) ; if ( p . x == q . x ) { a . x = ( p . x - ( l / 2.0 ) ) ; a . y = p . y ; d . x = ( p . x + ( l / 2.0 ) ) ; d . y = p . y ; b . x = ( q . x - ( l / 2.0 ) ) ; b . y = q . y ; c . x = ( q . x + ( l / 2.0 ) ) ; c . y = q . y ; } else if ( p . y == q . y ) { a . y = ( p . y - ( l / 2.0 ) ) ; a . x = p . x ; d . y = ( p . y + ( l / 2.0 ) ) ; d . x = p . x ; b . y = ( q . y - ( l / 2.0 ) ) ; b . x = q . x ; c . y = ( q . y + ( l / 2.0 ) ) ; c . x = q . x ; } else { let m = ( p . x - q . x ) / ( q . y - p . y ) ; let dx = ( ( l / Math . sqrt ( 1 + ( m * m ) ) ) * 0.5 ) ; let dy = m * dx ; a . x = p . x - dx ; a . y = p . y - dy ; d . x = p . x + dx ; d . y = p . y + dy ; b . x = q . x - dx ; b . y = q . y - dy ; c . x = q . x + dx ; c . y = q . y + dy ; } document . write ( a . x + " " + a . y + " " + b . x + " " + b . y + " " + c . x + " " + c . y + " " + d . x + " " + d . y + " " ) ; } let p1 = new Point ( 1 , 0 ) , q1 = new Point ( 1 , 2 ) ; printCorners ( p1 , q1 , 2 ) ; let p = new Point ( 1 , 1 ) , q = new Point ( - 1 , - 1 ) ; printCorners ( p , q , ( 2 * Math . sqrt ( 2 ) ) ) ;
Minimize cost to modify the Array such that even indices have even elements and vice versa | Function to find the minimum cost to modify the array according to the given criteria ; Count of wrong positioned odd and even elements ; Odd Count ; Even Count ; Swapping Cost ; Decrementing cost after swapping ; Only decrementing cost ; Return the minimum cost of the two cases ; Driver Code
function minimumCost ( arr , N , X , Y ) { let even_count = 0 , odd_count = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & 1 ) && ( i % 2 == 0 ) ) { odd_count ++ ; } if ( ( arr [ i ] % 2 ) == 0 && ( i & 1 ) ) { even_count ++ ; } } let cost1 = X * Math . min ( odd_count , even_count ) ; let cost2 = Y * ( Math . max ( odd_count , even_count ) - Math . min ( odd_count , even_count ) ) ; let cost3 = ( odd_count + even_count ) * Y ; return Math . min ( cost1 + cost2 , cost3 ) ; } let arr = [ 5 , 3 , 7 , 2 , 1 ] , X = 10 , Y = 2 ; let N = arr . length ; document . write ( minimumCost ( arr , N , X , Y ) ) ;
Minimum product of maximum and minimum element over all possible subarrays | Function to find the minimum product of the minimum and maximum among all the possible subarrays ; Stores resultant minimum product ; Traverse the given array arr [ ] ; Min of product of all two pair of consecutive elements ; Return the resultant value ; Driver Code
function findMinMax ( a ) { let min_val = 1000000000 ; for ( let i = 1 ; i < a . length ; ++ i ) { min_val = Math . min ( min_val , a [ i ] * a [ i - 1 ] ) ; } return min_val ; } let arr = [ 6 , 4 , 5 , 6 , 2 , 4 , 1 ] ; document . write ( findMinMax ( arr ) )
Sum of all nodes with smaller values at a distance K from a given node in a BST | Structure of Tree ; Constructor ; Function to add the node to the sum below the target node ; Base Case ; If Kth distant node is reached ; Recur for the left and the right subtrees ; Function to find the K distant nodes from target node , it returns - 1 if target node is not present in tree ; Base Case 1 ; If target is same as root . ; Recurr for the left subtree ; Tree is BST so reduce the search space ; Check if target node was found in left subtree ; If root is at distance k from the target ; Node less than target will be present in left ; When node is not present in the left subtree ; If Kth distant node is reached ; Node less than target at k distance maybe present in the left tree ; If target was not present in the left nor in right subtree ; Function to insert a node in BST ; If root is null ; Insert the data in right half ; Insert the data in left half ; Return the root node ; Function to find the sum of K distant nodes from the target node having value less than target node ; Stores the sum of nodes having values < target at K distance ; Print the resultant sum ; Driver Code ; Create the Tree
let sum = 0 ; class TreeNode { constructor ( data = " " , left = null , right = null ) { this . data = data ; this . left = left ; this . right = right ; } } function kDistanceDownSum ( root , k ) { if ( root == null k < 0 ) { return } if ( k == 0 ) { sum += root . data ; return ; } kDistanceDownSum ( root . left , k - 1 ) ; kDistanceDownSum ( root . right , k - 1 ) ; } function kDistanceSum ( root , target , k ) { if ( root == null ) return - 1 ; if ( root . data == target ) { kDistanceDownSum ( root . left , k - 1 ) ; return 0 ; } let dl = - 1 ; if ( target < root . data ) { dl = kDistanceSum ( root . left , target , k ) ; } if ( dl != - 1 ) { if ( dl + 1 == k ) sum += root . data ; return - 1 ; } let dr = - 1 ; if ( target > root . data ) { dr = kDistanceSum ( root . right , target , k ) ; } if ( dr != - 1 ) { if ( dr + 1 == k ) sum += root . data ; else kDistanceDownSum ( root . left , k - dr - 2 ) ; return 1 + dr ; } return - 1 ; } function insertNode ( data , root ) { if ( root == null ) { let node = new TreeNode ( data ) ; return node ; } else if ( data > root . data ) { root . right = insertNode ( data , root . right ) ; } else if ( data <= root . data ) { root . left = insertNode ( data , root . left ) ; } return root ; } function findSum ( root , target , K ) { kDistanceSum ( root , target , K , sum ) ; document . write ( sum ) ; } let root = null ; let N = 11 ; let tree = [ 3 , 1 , 7 , 0 , 2 , 5 , 10 , 4 , 6 , 9 , 8 ] ; for ( let i = 0 ; i < N ; i ++ ) { root = insertNode ( tree [ i ] , root ) ; } let target = 7 ; let K = 2 ; findSum ( root , target , K ) ;
Find Nth item distributed from infinite items of infinite types based on given conditions | Function to find the type of the item given out according to the given rules ; Stores the count of item given out at each step ; Iterate over the days from 1 ; Iterate over type of item on that day ; Count of items given out should exceed n ; Driver Code
function itemType ( n ) { let count = 0 ; for ( let day = 1 ; ; day ++ ) { for ( let type = day ; type > 0 ; type -- ) { count += type ; if ( count >= n ) return type ; } } } let N = 10 ; document . write ( itemType ( N ) ) ;
Find the sum of all array elements that are equidistant from two consecutive powers of 2 | Function to print the sum of array elements that are equidistant from two consecutive powers of 2 ; Stores the resultant sum of the array elements ; Traverse the array arr [ ] ; Stores the power of 2 of the number arr [ i ] ; Stores the number which is power of 2 and lesser than or equal to arr [ i ] ; Stores the number which is power of 2 and greater than or equal to arr [ i ] ; If arr [ i ] - LesserValue is the same as LargerValue - arr [ i ] ; Increment res by arr [ i ] ; Return the resultant sum res ; Driver Code
function FindSum ( arr , N ) { let res = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let power = Math . floor ( Math . log2 ( arr [ i ] ) ) ; let LesserValue = Math . pow ( 2 , power ) ; let LargerValue = Math . pow ( 2 , power + 1 ) ; if ( ( arr [ i ] - LesserValue ) == ( LargerValue - arr [ i ] ) ) { res += arr [ i ] ; } } return res ; } let arr = [ 10 , 24 , 17 , 3 , 8 ] ; let N = arr . length ; document . write ( FindSum ( arr , N ) ) ;