codeparrot/xlcost-text-to-code huggingface dataset:
Euler 's Factorization method |  ; Recursive function to return gcd of a and b ; Function to return N as the sum of two squares in two possible ways ; Iterate a loop from 1 to Math . sqrt ( n ) ; If i * i is square check if there exists another integer such that h is a perfect square and i * i + h = n ; If h is perfect square ; Store in the sorted way ; If there is already a pair check if pairs are equal or not ; Insert the first pair ; If two pairs are found ; Function to find the factors ; Get pairs where a ^ 2 + b ^ 2 = n ; Number cannot be represented as sum of squares in two ways ; Assign a , b , c , d ; Swap if a < c because if a - c < 0 , GCD cant be computed . ; Compute the values of k , h , l , m using the formula mentioned in the approach ; Print the values of a , b , c , d and k , l , m , h ; Printing the factors ; Driver code
class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } function sumOfSquares ( n , vp ) { for ( let i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { let h = n - i * i , h1 = Math . floor ( Math . sqrt ( h ) ) ; if ( h1 * h1 == h ) { let a = Math . max ( h1 , i ) , b = Math . min ( h1 , i ) ; if ( vp . length == 1 && a != vp [ 0 ] . first ) vp . push ( new pair ( a , b ) ) ; if ( vp . length == 0 ) vp . push ( new pair ( a , b ) ) ; if ( vp . length == 2 ) return ; } } } function findFactors ( n ) { let vp = [ ] ; sumOfSquares ( n , vp ) ; if ( vp . length != 2 ) document . write ( " " ) ; let a , b , c , d ; a = vp [ 0 ] . first ; b = vp [ 0 ] . second ; c = vp [ 1 ] . first ; d = vp [ 1 ] . second ; if ( a < c ) { let t = a ; a = c ; c = t ; t = b ; b = d ; d = t ; } let k , h , l , m ; k = __gcd ( a - c , d - b ) ; h = __gcd ( a + c , d + b ) ; l = ( a - c ) / k ; m = ( d - b ) / k ; document . write ( " " + a + " " + ( a - c ) + " " + k + " " ) ; document . write ( " " + b + " " + ( a + c ) + " " + h + " " ) ; document . write ( " " + c + " " + ( d - b ) + " " + l + " " ) ; document . write ( " " + d + " " + ( d + b ) + " " + m + " " ) ; if ( k % 2 == 0 && h % 2 == 0 ) { k = k / 2 ; h = h / 2 ; document . write ( " " + ( ( k ) * ( k ) + ( h ) * ( h ) ) + " " + ( l * l + m * m ) + " " ) ; } else { l = l / 2 ; m = m / 2 ; document . write ( " " + ( ( l ) * ( l ) + ( m ) * ( m ) ) + " " + ( k * k + h * h ) + " " ) ; } } let n = 100000 ; findFactors ( n ) ;
Print the nodes of the Binary Tree whose height is a Prime number | Javascript implementation of nodes at prime height in the given tree ; To store Prime Numbers ; To store height of each node ; Function to find the prime numbers till 10 ^ 5 ; Traverse all multiple of i and make it false ; Function to perform dfs ; Store the height of node ; Function to find the nodes at prime height ; To precompute prime number till 10 ^ 5 ; Check if height [ node ] is prime ; Number of nodes ; Edges of the tree
let MAX = 100000 ; let graph = [ ] for ( let i = 0 ; i < MAX + 1 ; i ++ ) { graph . push ( [ ] ) } let Prime = new Array ( MAX + 1 ) . fill ( true ) ; let height = new Array ( MAX + 1 ) ; function SieveOfEratosthenes ( ) { let i , j ; Prime [ 0 ] = Prime [ 1 ] = false ; for ( i = 2 ; i * i <= MAX ; i ++ ) { if ( Prime [ i ] ) { for ( j = 2 * i ; j < MAX ; j += i ) { Prime [ j ] = false ; } } } } function dfs ( node , parent , h ) { height [ node ] = h ; for ( let to of graph [ node ] ) { if ( to == parent ) continue ; dfs ( to , node , h + 1 ) ; } } function primeHeightNode ( N ) { SieveOfEratosthenes ( ) ; for ( let i = 1 ; i <= N ; i ++ ) { if ( Prime [ height [ i ] ] ) { document . write ( i + " " ) ; } } } let N = 5 ; graph [ 1 ] . push ( 2 ) ; graph [ 1 ] . push ( 3 ) ; graph [ 2 ] . push ( 4 ) ; graph [ 2 ] . push ( 5 ) ; dfs ( 1 , 1 , 0 ) ; primeHeightNode ( N ) ;
Find Prime Adam integers in the given range [ L , R ] | JavaScript program to find all prime adam numbers in the given range ; reversing a number by taking remainder at a time ; Function to check if a number is a prime or not ; Iterating till the number ; Checking for factors ; Returning 1 if the there are no factors of the number other than 1 or itself ; Function to check whether a number is an adam number or not ; Reversing given number ; Squaring given number ; Squaring reversed number ; Reversing the square of the reversed number ; Checking if the square of the number and the square of its reverse are equal or not ; Function to find all the prime adam numbers in the given range ; If the first number is greater than the second number , print invalid ; Iterating through all the numbers in the given range ; Checking for prime number ; Checking for Adam number ; Driver code
function reverse ( a ) { let rev = 0 ; while ( a != 0 ) { let r = a % 10 ; rev = rev * 10 + r ; a = parseInt ( a / 10 , 10 ) ; } return ( rev ) ; } function prime ( a ) { let k = 0 ; for ( let i = 2 ; i < a ; i ++ ) { if ( a % i == 0 ) { k = 1 ; break ; } } if ( k == 1 ) { return ( 0 ) ; } else { return ( 1 ) ; } } function adam ( a ) { let r1 = reverse ( a ) ; let s1 = a * a ; let s2 = r1 * r1 ; let r2 = reverse ( s2 ) ; if ( s1 == r2 ) { return ( 1 ) ; } else { return ( 0 ) ; } } function find ( m , n ) { if ( m > n ) { document . write ( " " + " " ) ; } else { let c = 0 ; for ( let i = m ; i <= n ; i ++ ) { let l = prime ( i ) ; let k = adam ( i ) ; if ( ( l == 1 ) && ( k == 1 ) ) { document . write ( i + " " ) ; } } } } let L = 5 , R = 100 ; find ( L , R ) ;
Determine whether the given integer N is a Peculiar Number or not | Function to find sum of digits of a number ; Function to check if the number is peculiar ; Store a duplicate of n ; Driver code
function sumDig ( n ) { var s = 0 ; while ( n != 0 ) { s = s + ( n % 10 ) ; n = parseInt ( n / 10 ) ; } return s ; } function Pec ( n ) { var dup = n ; var dig = sumDig ( n ) ; if ( dig * 3 == dup ) return true ; else return false ; } var n = 36 ; if ( Pec ( n ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Find N numbers such that a number and its reverse are divisible by sum of its digits | Function to calculate the sum of digits ; Loop to iterate through every digit of the number ; Returning the sum of digits ; Function to calculate the reverse of a number ; Loop to calculate the reverse of the number ; Return the reverse of the number ; Function to print the first N numbers such that every number and the reverse of the number is divisible by its sum of digits ; Loop to continuously check and generate number until there are n outputs ; Variable to hold the sum of the digit of the number ; Computing the reverse of the number ; Checking if the condition satisfies . Increment the count and print the number if it satisfies . ; Driver code
function digit_sum ( n ) { let sum = 0 , m ; while ( n > 0 ) { m = n % 10 ; sum = sum + m ; n = parseInt ( n / 10 , 10 ) ; } return ( sum ) ; } function reverse ( n ) { let r = 0 ; while ( n != 0 ) { r = r * 10 ; r = r + n % 10 ; n = parseInt ( n / 10 , 10 ) ; } return ( r ) ; } function operation ( n ) { let i = 1 , a , count = 0 , r ; while ( count < n ) { a = digit_sum ( i ) ; r = reverse ( i ) ; if ( i % a == 0 && r % a == 0 ) { document . write ( i + " " ) ; count ++ ; i ++ ; } else i ++ ; } } let n = 10 ; operation ( n ) ;
Split N natural numbers into two sets having GCD of their sums greater than 1 | Function to create and print the two sets ; No such split possible for N <= 2 ; Print the first set consisting of even elements ; Print the second set consisting of odd ones ; Driver Code
function createSets ( N ) { if ( N <= 2 ) { document . write ( " " ) ; return ; } for ( let i = 2 ; i <= N ; i += 2 ) document . write ( i + " " ) ; document . write ( " " ) ; for ( let i = 1 ; i <= N ; i += 2 ) { document . write ( i + " " ) ; } } let N = 6 ; createSets ( N ) ;
Count the nodes in the given tree whose weight is a powerful number | Javascript implementation to Count the nodes in the given tree whose weight is a powerful number ; Function to check if the number is powerful ; First divide the number repeatedly by 2 ; Check if only 2 ^ 1 divides n , then return false ; Check if n is not a power of 2 then this loop will execute ; Find highest power of " factor " that divides n ; Check if only factor ^ 1 divides n , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Function to perform dfs ; Check if weight of the current node is a powerful 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 isPowerful ( n ) { while ( n % 2 == 0 ) { var power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( var factor = 3 ; factor <= Math . sqrt ( n ) ; factor += 2 ) { var power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } function dfs ( node , parent ) { if ( isPowerful ( 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 ) ;
Number of ways to color boundary of each block of M * N table | Function to compute all way to fill the boundary of all sides of the unit square ; Count possible ways to fill all upper and left side of the rectangle M * N ; Count possible ways to fill all side of the all squares unit size ; Number of rows ; Number of columns
function CountWays ( N , M ) { var count = 1 ; count = Math . pow ( 3 , M + N ) ; count *= Math . pow ( 2 , M * N ) ; return count ; } var N = 3 ; var M = 2 ; document . write ( CountWays ( N , M ) ) ;
Nth positive number whose absolute difference of adjacent digits is at most 1 | Return Nth number with absolute difference between all adjacent digits at most 1. ; To store all such numbers ; Enqueue all integers from 1 to 9 in increasing order . ; Perform the operation N times so that we can get all such N numbers . ; Store the front element of queue , in array and pop it from queue . ; If the last digit of dequeued integer is not 0 , then enqueue the next such number . ; Enqueue the next such number ; If the last digit of dequeued integer is not 9 , then enqueue the next such number . ; Driver Code
function findNthNumber ( N ) { let arr = new Array ( N + 1 ) ; let q = [ ] ; for ( let i = 1 ; i <= 9 ; i ++ ) q . push ( i ) ; for ( let i = 1 ; i <= N ; i ++ ) { arr [ i ] = q [ 0 ] ; q . shift ( ) ; if ( arr [ i ] % 10 != 0 ) q . push ( arr [ i ] * 10 + arr [ i ] % 10 - 1 ) ; q . push ( arr [ i ] * 10 + arr [ i ] % 10 ) ; if ( arr [ i ] % 10 != 9 ) q . push ( arr [ i ] * 10 + arr [ i ] % 10 + 1 ) ; } document . write ( arr [ N ] + " " ) ; } let N = 21 ; findNthNumber ( N ) ;
Unique element in an array where all elements occur K times except one | Set 2 | Function that find the unique element in the array arr [ ] ; Store all unique element in set ; Sum of all element of the array ; Sum of element in the set ; Print the unique element using formula ; Driver Code ; Function call
function findUniqueElements ( arr , N , K ) { var s = new Set ( arr ) ; var arr_sum = arr . reduce ( ( a , b ) => a + b , 0 ) ; var set_sum = 0 ; s . forEach ( function ( value ) { set_sum += value ; } ) document . write ( Math . floor ( ( K * set_sum - arr_sum ) / ( K - 1 ) ) ) ; } var arr = [ 12 , 1 , 12 , 3 , 12 , 1 , 1 , 2 , 3 , 2 , 2 , 3 , 7 ] ; var N = arr . length ; var K = 3 ; findUniqueElements ( arr , N , K ) ;
Form the Cubic equation from the given roots | Function to find the cubic equation whose roots are a , b and c ; Find the value of coefficient ; Print the equation as per the above coefficients ; Driver Code ; Function Call
function findEquation ( a , b , c ) { let X = ( a + b + c ) ; let Y = ( a * b ) + ( b * c ) + ( c * a ) ; let Z = a * b * c ; document . write ( " " + X + " " + Y + " " + Z + " " ) ; } let a = 5 , b = 2 , c = 3 ; findEquation ( a , b , c ) ;
Gill 's 4th Order Method to solve Differential Equations | A sample differential equation " dy / dx ▁ = ▁ ( x ▁ - ▁ y ) /2" ; Finds value of y for a given x using step size h and initial value y0 at x0 ; Count number of iterations using step size or height h ; Value of K_i ; Initial value of y ( 0 ) ; Iterate for number of iteration ; Value of K1 ; Value of K2 ; Value of K3 ; Value of K4 ; Find the next value of y ( n + 1 ) using y ( n ) and values of K in the above steps ; Update next value of x ; Return the final value of dy / dx ; Driver Code
function dydx ( x , y ) { return ( x - y ) / 2 ; } function Gill ( x0 , y0 , x , h ) { let n = ( ( x - x0 ) / h ) ; let k1 , k2 , k3 , k4 ; let y = y0 ; for ( let i = 1 ; i <= n ; i ++ ) { k1 = h * dydx ( x0 , y ) ; k2 = h * dydx ( x0 + 0.5 * h , y + 0.5 * k1 ) ; k3 = h * dydx ( x0 + 0.5 * h , y + 0.5 * ( - 1 + Math . sqrt ( 2 ) ) * k1 + k2 * ( 1 - 0.5 * Math . sqrt ( 2 ) ) ) ; k4 = h * dydx ( x0 + h , y - ( 0.5 * Math . sqrt ( 2 ) ) * k2 + k3 * ( 1 + 0.5 * Math . sqrt ( 2 ) ) ) ; y = y + ( 1.0 / 6 ) * ( k1 + ( 2 - Math . sqrt ( 2 ) ) * k2 + ( 2 + Math . sqrt ( 2 ) ) * k3 + k4 ) ; x0 = x0 + h ; } return y ; } let x0 = 0 , y = 3.0 , x = 5.0 , h = 0.2 ; document . write ( " " , Gill ( x0 , y , x , h ) . toFixed ( 6 ) ) ;
Program to print numbers from N to 1 in reverse order | Recursive function to print from N to 1 ; Driver code
function PrintReverseOrder ( N ) { for ( let i = N ; i > 0 ; i -- ) document . write ( i + " " ) ; } let N = 5 ; PrintReverseOrder ( N ) ;
Find count of numbers from 0 to n which satisfies the given equation for a value K | Function to find the values ; Calculate the LCM ; Calculate the multiples of lcm ; Find the values which satisfies the given condition ; Subtract the extra values ; Return the final result ; Driver code
function findAns ( a , b , n ) { var lcm = ( a * b ) / __gcd ( a , b ) ; var multiples = ( n / lcm ) + 1 ; var answer = Math . max ( a , b ) * multiples ; var lastvalue = lcm * ( n / lcm ) + Math . max ( a , b ) ; if ( lastvalue > n ) answer = answer - ( lastvalue - n - 1 ) ; return answer ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } var a = 1 , b = 13 , n = 500 ; document . write ( findAns ( a , b , n ) + " " ) ;
Program to find if two numbers and their AM and HM are present in an array using STL | Function to find the Arithmetic Mean of 2 numbers ; Function to find the Harmonic Mean of 2 numbers ; Following function checks and computes the desired results based on the means ; Calculate means ; Hash container ( HashMap ) to store elements ; Insertion of array elements in the Set ; Conditionals to check if numbers are present in array by Hashing ; Conditionals to check if the AM and HM of the numbers are present in array ; If all conditions are satisfied , the Geometric Mean is calculated ; If numbers are found but the respective AM and HM are not found in the array ; If none of the conditions are satisfied ; Driver code
function ArithmeticMean ( A , B ) { return ( ( A + B ) / 2 ) ; } function HarmonicMean ( A , B ) { return ( 2 * A * B ) / ( A + B ) ; } function CheckArithmeticHarmonic ( arr , A , B , N ) { let AM = ArithmeticMean ( A , B ) ; let HM = HarmonicMean ( A , B ) ; let Hash = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { Hash . set ( arr [ i ] , 1 ) ; } if ( Hash . get ( A ) != 0 && Hash . get ( B ) != 0 ) { if ( Hash . get ( AM ) != 0 && Hash . get ( HM ) != 0 ) { document . write ( " " ) ; document . write ( Math . sqrt ( AM * HM ) . toFixed ( 2 ) ) ; } else { document . write ( " " ) ; } } else { document . write ( " " ) ; } } let arr = [ 1.0 , 2.0 , 2.5 , 3.0 , 4.0 , 4.5 , 5.0 , 6.0 ] ; let N = ( arr . length ) ; let A = 3.0 ; let B = 6.0 ; CheckArithmeticHarmonic ( arr , A , B , N ) ;
Minimum decrements to make integer A divisible by integer B | Function that print number of moves required ; calculate modulo ; print the required answer ; initialise A and B
function movesRequired ( a , b ) { let total_moves = a % b ; document . write ( total_moves ) ; } let A = 10 , B = 3 ; movesRequired ( A , B ) ;
Pythagorean Triplet with given sum using single loop | Function to calculate the Pythagorean triplet in O ( n ) ; Iterate a from 1 to N - 1. ; Calculate value of b ; The value of c = n - a - b ; Driver code ; Function call
function PythagoreanTriplet ( n ) { let flag = 0 ; for ( let a = 1 ; a < n ; a ++ ) { let b = ( n * n - 2 * n * a ) / ( 2 * n - 2 * a ) ; let c = n - a - b ; if ( a * a + b * b == c * c && b > 0 && c > 0 ) { document . write ( a + " " + b + " " + c ) ; flag = 1 ; break ; } } if ( flag == 0 ) { document . write ( " " ) ; } return ; } let N = 12 ; PythagoreanTriplet ( N ) ;
Check if there exists a number with X factors out of which exactly K are prime | Function to check if such number exists ; To store the sum of powers of prime factors of X which determines the maximum count of numbers whose product can form X ; Determining the prime factors of X ; To check if the number is prime ; If X is 1 , then we cannot form a number with 1 factor and K prime factor ( as K is atleast 1 ) ; If X itself is prime then it can be represented as a power of only 1 prime factor which is X itself so we return true ; If sum of the powers of prime factors of X is greater than or equal to K , which means X can be represented as a product of K numbers , we return true ; In any other case , we return false as we cannot form a number with X factors and K prime factors ; Driver code
function check ( X , K ) { var prime , temp , sqr , i ; prime = 0 ; temp = X ; sqr = parseInt ( Math . sqrt ( X ) ) ; for ( i = 2 ; i <= sqr ; i ++ ) { while ( temp % i == 0 ) { temp = parseInt ( temp / i ) ; prime ++ ; } } if ( temp > 2 ) prime ++ ; if ( X == 1 ) return false ; if ( prime == 1 && K == 1 ) return true ; else if ( prime >= K ) return true ; else return false ; } var X , K ; X = 4 ; K = 2 ; if ( check ( X , K ) ) document . write ( " " ) ; else document . write ( " " ) ;
Print all Coprime path of a Binary Tree | A Tree node ; Utility function to create a new node ; Vector to store all the prime numbers ; Function to store all the prime numbers in an array ; Create a boolean array " prime [ 0 . . N ] " and initialize all the entries in it 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 . ; Function to check whether Path is Co - prime or not ; Iterating through the array to find the maximum element in the array ; Incrementing the variable if any of the value has a factor ; If not co - prime ; Function to print a Co - Prime path ; Function to find co - prime paths of binary tree ; Base case ; Store the value in path vector ; Recursively call for left sub tree ; Recursively call for right sub tree ; Condition to check , if leaf node ; Condition to check , if path co - prime or not ; Print co - prime path ; Remove the last element from the path vector ; Function to find Co - Prime paths In a given binary tree ; To save all prime numbers ; Function call ; Create Binary Tree as shown ; Print Co - Prime Paths
class Node { constructor ( key ) { this . left = null ; this . right = null ; this . key = key ; } } function newNode ( key ) { let temp = new Node ( key ) ; return ( temp ) ; } 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 ; } } } function isPathCo_Prime ( path ) { let max = 0 ; for ( let x = 0 ; x < path . length ; x ++ ) { if ( max < path [ x ] ) max = path [ x ] ; } for ( let i = 0 ; i * prime [ i ] <= parseInt ( max / 2 , 10 ) ; i ++ ) { let ct = 0 ; for ( let x = 0 ; x < path . length ; x ++ ) { if ( path [ x ] % prime [ i ] == 0 ) ct ++ ; } if ( ct > 1 ) { return false ; } } return true ; } function printCoPrimePaths ( path ) { for ( let x = 0 ; x < path . length ; x ++ ) { document . write ( path [ x ] + " " ) ; } document . write ( " " ) ; } function findCo_PrimePaths ( root , path ) { if ( root == null ) return ; path . push ( root . key ) ; findCo_PrimePaths ( root . left , path ) ; findCo_PrimePaths ( root . right , path ) ; if ( root . left == null && root . right == null ) { if ( isPathCo_Prime ( path ) ) { printCoPrimePaths ( path ) ; } } path . pop ( ) ; } function printCo_PrimePaths ( node ) { SieveOfEratosthenes ( ) ; let path = [ ] ; findCo_PrimePaths ( node , path ) ; } let root = newNode ( 10 ) ; root . left = newNode ( 48 ) ; root . right = newNode ( 3 ) ; root . right . left = newNode ( 11 ) ; root . right . right = newNode ( 37 ) ; root . right . left . left = newNode ( 7 ) ; root . right . left . right = newNode ( 29 ) ; root . right . right . left = newNode ( 42 ) ; root . right . right . right = newNode ( 19 ) ; root . right . right . right . left = newNode ( 7 ) ; printCo_PrimePaths ( root ) ;
Number of subsets with same AND , OR and XOR values in an Array | Javascript implementation to find the number of subsets with equal bitwise AND , OR and XOR values ; Function to find the number of subsets with equal bitwise AND , OR and XOR values ; Traverse through all the subsets ; Finding the subsets with the bits of ' i ' which are set ; Computing the bitwise AND ; Computing the bitwise OR ; Computing the bitwise XOR ; Comparing all the three values ; Driver Code
let mod = 1000000007 ; function countSubsets ( a , n ) { let answer = 0 ; for ( let i = 0 ; i < ( 1 << n ) ; i ++ ) { let bitwiseAND = - 1 ; let bitwiseOR = 0 ; let bitwiseXOR = 0 ; for ( let j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) { if ( bitwiseAND == - 1 ) bitwiseAND = a [ j ] ; else bitwiseAND &= a [ j ] ; bitwiseOR |= a [ j ] ; bitwiseXOR ^= a [ j ] ; } } if ( bitwiseAND == bitwiseOR && bitwiseOR == bitwiseXOR ) answer = ( answer + 1 ) % mod ; } return answer ; } let N = 6 ; let A = [ 1 , 3 , 2 , 1 , 2 , 1 ] ; document . write ( countSubsets ( A , N ) ) ;
Count of Subsets containing only the given value K | Function to find the number of subsets formed by the given value K ; Count is used to maintain the number of continuous K 's ; Iterating through the array ; If the element in the array is equal to K ; count * ( count + 1 ) / 2 is the total number of subsets with only K as their element ; Change count to 0 because other element apart from K has been found ; To handle the last set of K 's ; Driver code
function count ( arr , N , K ) { let count = 0 , ans = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == K ) { count = count + 1 ; } else { ans += ( count * ( count + 1 ) ) / 2 ; count = 0 ; } } ans = ans + ( count * ( count + 1 ) ) / 2 ; return ans ; } let arr = [ 1 , 0 , 0 , 1 , 1 , 0 , 0 ] ; let N = arr . length ; let K = 0 ; document . write ( count ( arr , N , K ) ) ;
Ternary number system or Base 3 numbers | Function to convert a decimal number to a ternary number ; Base case ; Finding the remainder when N is divided by 3 ; Recursive function to call the function for the integer division of the value N / 3 ; Handling the negative cases ; Function to convert the decimal to ternary ; If the number is greater than 0 , compute the ternary representation of the number ; Driver Code
function convertToTernary ( N ) { if ( N == 0 ) return ; let x = N % 3 ; N = parseInt ( N / 3 , 10 ) ; if ( x < 0 ) N += 1 ; convertToTernary ( N ) ; if ( x < 0 ) document . write ( x + ( 3 * - 1 ) ) ; else document . write ( x ) ; } function convert ( Decimal ) { document . write ( " " + Decimal + " " ) ; if ( Decimal != 0 ) { convertToTernary ( Decimal ) ; } else document . write ( " " + " " ) ; } let Decimal = 2747 ; convert ( Decimal ) ;