codeparrot/xlcost-text-to-code huggingface dataset:
Print the number of set bits in each node of a Binary Tree | A Binary Tree Node ; Utility function that allocates a new Node ; Function to print the number of set bits in each node of the binary tree ; Print the number of set bits of current node using __builtin_popcount ( ) ; Traverse Left Subtree ; Traverse Right Subtree ; Driver code
class Node { constructor ( data ) { this . left = null ; this . right = null ; this . data = data ; } } function newNode ( data ) { let node = new Node ( data ) ; return ( node ) ; } function bitCount ( x ) { let setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } function printSetBit ( root ) { if ( root == null ) return ; document . write ( " " + root . data + " " + bitCount ( root . data ) + " " ) ; printSetBit ( root . left ) ; printSetBit ( root . right ) ; } let root = newNode ( 16 ) ; root . left = newNode ( 13 ) ; root . left . left = newNode ( 14 ) ; root . left . right = newNode ( 12 ) ; root . right = newNode ( 11 ) ; root . right . left = newNode ( 10 ) ; root . right . right = newNode ( 16 ) ; printSetBit ( root ) ;
Find bitwise AND ( & ) of all possible sub | function to return AND of sub - arrays ; Driver code ; size of the array ; print and of all subarrays
function AND ( a , n ) { let ans = a [ 0 ] ; for ( let i = 0 ; i < n ; ++ i ) ans &= a [ i ] ; return ans ; } let a = [ 1 , 2 , 3 ] ; let n = a . length ; document . write ( AND ( a , n ) ) ;
2 's complement for a givin string using XOR | Javascript program to find 2 's complement using XOR. ; A flag used to find if a 1 bit is seen or not . ; xor operator is used to flip the ; bits after converting in to ASCII values ; if there is no 1 in the string so just add 1 in starting of string and return ; Driver code
function TwoscomplementbyXOR ( str ) { let n = str . length ; let check_bit = 0 ; for ( let i = n - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == ' ' && check_bit == 0 ) { continue ; } else { if ( check_bit == 1 ) { if ( str . charAt ( i ) == ' ' ) str = str . substr ( 0 , i ) + ' ' + str . substr ( i + 1 ) ; else str = str . substr ( 0 , i ) + ' ' + str . substr ( i + 1 ) ; } check_bit = 1 ; } } if ( check_bit == 0 ) return " " + str ; else return str ; } let str = " " ; document . write ( TwoscomplementbyXOR ( str ) ) ;
Check whether bits are in alternate pattern in the given range | Set | function to check whether rightmost kth bit is set or not in ' n ' ; function to set the rightmost kth bit in ' n ' ; kth bit of n is being set by this operation ; function to check if all the bits are set or not in the binary representation of ' n ' ; if true , then all bits are set ; else all bits are not set ; function to check if a number has bits in alternate pattern ; to check if all bits are set in ' num ' ; function to check whether bits are in alternate pattern in the given range ; preparing a number ' num ' and ' left _ shift ' which can be further used for the check of alternate pattern in the given range ; unset all the bits which are left to the rth bit of ( r + 1 ) th bit ; right shift ' num ' by ( l - 1 ) bits ; Driver program to test above
function isKthBitSet ( n , k ) { if ( ( n >> ( k - 1 ) ) & 1 ) return true ; return false ; } function setKthBit ( n , k ) { return ( ( 1 << ( k - 1 ) ) n ) ; } function allBitsAreSet ( n ) { if ( ( ( n + 1 ) & n ) == 0 ) return true ; return false ; } function bitsAreInAltOrder ( n ) { var num = n ^ ( n >> 1 ) ; return allBitsAreSet ( num ) ; } function bitsAreInAltPatrnInGivenRange ( n , l , r ) { var num , left_shift ; if ( isKthBitSet ( n , r ) ) { num = n ; left_shift = r ; } else { num = setKthBit ( n , ( r + 1 ) ) ; left_shift = r + 1 ; } num = num & ( ( 1 << left_shift ) - 1 ) ; num = num >> ( l - 1 ) ; return bitsAreInAltOrder ( num ) ; } var n = 18 ; var l = 1 , r = 3 ; if ( bitsAreInAltPatrnInGivenRange ( n , l , r ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check whether bits are in alternate pattern in the given range | function to check whether bits are in alternate pattern in the given range ; right shift n by ( l - 1 ) bits ; get the bit at the last position in ' num ' ; right shift ' num ' by 1 ; loop until there are bits in the given range ; get the bit at the last position in ' num ' ; if true , then bits are not in alternate pattern ; update ' prev ' ; right shift ' num ' by 1 ; bits are in alternate pattern in the given range ; Driver program to test above
function bitsAreInAltPatrnInGivenTRange ( n , l , r ) { var num , prev , curr ; num = n >> ( l - 1 ) ; prev = num & 1 ; num = num >> 1 ; for ( var i = 1 ; i <= ( r - l ) ; i ++ ) { curr = num & 1 ; if ( curr == prev ) return false ; prev = curr ; num = num >> 1 ; } return true ; } var n = 18 ; var l = 1 , r = 3 ; if ( bitsAreInAltPatrnInGivenTRange ( n , l , r ) ) document . write ( " " ) ; else document . write ( " " ) ;
Increment a number without using ++ or + | function that increment the value . ; Invert bits and apply negative sign ; Driver code
function increment ( i ) { i = - ( ~ i ) ; return i ; } let n = 3 ; document . write ( increment ( n ) ) ;
First element greater than or equal to X in prefix sum of N numbers using Binary Lifting | function to make prefix sums array ; function to find lower_bound of x in prefix sums array using binary lifting . ; initialize position ; find log to the base 2 value of n . ; if x less than first number . ; starting from most significant bit . ; if value at this position less than x then updateposition Here ( 1 << i ) is similar to 2 ^ i . ; + 1 because ' pos ' will have position of largest value less than ' x ' ; given array ; value to find ; size of array ; to store prefix sum ; call for prefix sum ; function call
function MakePreSum ( arr , presum , n ) { presum [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) presum [ i ] = presum [ i - 1 ] + arr [ i ] ; } function BinaryLifting ( presum , n , k ) { let pos = 0 ; let LOGN = Math . log ( n ) ; if ( x <= presum [ 0 ] ) return 0 ; for ( let i = LOGN ; i >= 0 ; i -- ) { if ( pos + ( 1 << i ) < n && presum [ pos + ( 1 << i ) ] < x ) { pos += ( 1 << i ) ; } } return pos + 1 ; } let arr = [ 2 , 5 , 7 , 1 , 6 , 9 , 12 , 4 , 6 ] ; let x = 8 ; let n = arr . length ; let presum = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { presum [ i ] = 0 ; } MakePreSum ( arr , presum , n ) ; document . write ( BinaryLifting ( presum , n , x ) ) ;
Maximum sum by adding numbers with same number of set bits | count the number of bits for each element of array ; Count the number of set bits ; Function to return the the maximum sum ; Calculate the ; Assuming the number to be a maximum of 32 bits ; Add the number to the number of set bits ; Find the maximum sum ; Driver code
function bit_count ( n ) { var count = 0 ; while ( n ) { count ++ ; n = n & ( n - 1 ) ; } return count ; } function maxsum ( arr , n ) { var bits = Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { bits [ i ] = bit_count ( arr [ i ] ) ; } var sum = Array ( 32 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { sum [ bits [ i ] ] += arr [ i ] ; } var maximum = 0 ; for ( var i = 0 ; i < 32 ; i ++ ) { maximum = Math . max ( sum [ i ] , maximum ) ; } return maximum ; } var arr = [ 2 , 3 , 8 , 5 , 6 , 7 ] ; var n = arr . length ; document . write ( maxsum ( arr , n ) ) ;
Sum of XOR of sum of all pairs in an array | Javascript program to find XOR of pair sums . ; Driver code
function xorPairSum ( ar , n ) { var sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum = sum ^ ar [ i ] ; return 2 * sum ; } var arr = [ 1 , 2 , 3 ] ; var n = arr . length ; document . write ( xorPairSum ( arr , n ) ) ;
Count pairs with Bitwise OR as Even number | Javascript program to count pairs with even OR ; Count total even numbers in array . ; return count of even pair ; Driver Code
function findEvenPair ( A , N ) { let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) if ( ( ! ( ( A [ i ] & 1 ) > 0 ) ) ) count ++ ; return count * ( count - 1 ) / 2 ; } let A = [ 5 , 6 , 2 , 8 ] ; let N = A . length ; document . write ( findEvenPair ( A , N ) ) ;
Check whether all the bits are unset in the given range | function to check whether all the bits are unset in the given range or not ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; new number which could only have one or more set bits in the range l to r and nowhere else ; if true , then all bits are unset in the given range ; else all bits are not unset in the given range ; Driver program to test above
function allBitsSetInTheGivenRange ( n , l , r ) { let num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; let new_num = n & num ; if ( new_num == 0 ) return true ; return false ; } let n = 17 ; let l = 2 , r = 4 ; if ( allBitsSetInTheGivenRange ( n , l , r ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if a number has same number of set and unset bits | Function to check if a number has same setbits and unset bits ; iterate for all bits of a number ; if set ; if unset ; right shift number by 1 ; is number of set bits are equal to unset bits ; driver code ; function to check
function checkSame ( n ) { let set = 0 , unset = 0 ; while ( n ) { if ( n & 1 ) set ++ ; else unset ++ ; n = n >> 1 ; } if ( set == unset ) return true ; else return false ; } let n = 12 ; if ( checkSame ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if concatenation of two strings is balanced or not | Check if given string is balanced bracket sequence or not . ; If current bracket is an opening bracket push it to stack . ; If current bracket is a closing bracket then pop from stack if it is not empty . If stack is empty then sequence is not balanced . ; If stack is not empty , then sequence is not balanced . ; Function to check if string obtained by concatenating two bracket sequences is balanced or not . ; Check if s1 + s2 is balanced or not . ; Check if s2 + s1 is balanced or not . ; Driver code
function isBalanced ( s ) { let st = [ ] ; let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) { st . push ( s [ i ] ) ; } else if ( st . length == 0 ) { return false ; } else { st . pop ( ) ; } } if ( st . length != 0 ) { return false ; } return true ; } function isBalancedSeq ( s1 , s2 ) { if ( isBalanced ( s1 + s2 ) ) { return true ; } return isBalanced ( s2 + s1 ) ; } let s1 = " " ; let s2 = " " ; if ( isBalancedSeq ( s1 , s2 ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Find iâ €™ th index character in a binary string obtained after n iterations | Set 2 | Function to find the i - th character ; distance between two consecutive elements after N iterations ; binary representation of M ; kth digit will be derived from root for sure ; Check whether there is need to flip root or not ; driver program
function KthCharacter ( m , n , k ) { let distance = Math . pow ( 2 , n ) ; let Block_number = Math . floor ( k / distance ) ; let remaining = k % distance ; let s = new Array ( 32 ) . fill ( 0 ) ; let x = 0 ; for ( ; m > 0 ; x ++ ) { s [ x ] = m % 2 ; m = Math . floor ( m / 2 ) ; } let root = s [ x - 1 - Block_number ] ; if ( remaining == 0 ) { document . write ( root ) ; return ; } let flip = true ; while ( remaining > 1 ) { if ( ( remaining & 1 ) > 0 ) { flip = ! flip ; } remaining = remaining >> 1 ; } if ( flip ) { document . write ( ( root > 0 ) ? 0 : 1 ) ; } else { document . write ( root ) ; } } let m = 5 , k = 5 , n = 3 ; KthCharacter ( m , n , k ) ;
Check whether the number has only first and last bits set | Set 2 | function to check whether the number has only first and last bits set ; Driver Code
function onlyFirstAndLastAreSet ( n ) { if ( n == 1 ) return true ; if ( n == 2 ) return false ; return ( ( ( n - 1 ) & ( n - 2 ) ) == 0 ) ; } var n = 9 ; if ( onlyFirstAndLastAreSet ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Number with set bits only between L | Function to return the integer with all the bits set in range L - R ; iterate from L to R and add all powers of 2 ; Driver Code
function getInteger ( L , R ) { var number = 0 ; for ( var i = L ; i <= R ; i ++ ) number += Math . pow ( 2 , i ) ; return number ; } var L = 2 , R = 5 ; document . write ( getInteger ( L , R ) ) ;
Number with set bits only between L | Function to return the integer with all the bits set in range L - R ; Driver Code
function setbitsfromLtoR ( L , R ) { return ( 1 << ( R + 1 ) ) - ( 1 << L ) ; } var L = 2 , R = 5 ; document . write ( setbitsfromLtoR ( L , R ) ) ;
XOR of Sum of every possible pair of an array | Function to find XOR of sum of all pairs ; Calculate xor of all the elements ; Return twice of xor value ; Drivers code
function findXor ( arr , n ) { let xoR = 0 ; for ( let i = 0 ; i < n ; i ++ ) { xoR = xoR ^ arr [ i ] ; } return xoR * 2 ; } let arr = [ 1 , 5 , 6 ] ; let n = arr . length ; document . write ( findXor ( arr , n ) ) ;
Largest set with bitwise OR equal to n | function to find the largest set with bitwise OR equal to n ; If the bitwise OR of n and i is equal to n , then include i in the set ; Driver Code
function setBitwiseORk ( n ) { var v = [ ] ; for ( var i = 0 ; i <= n ; i ++ ) { if ( ( i n ) == n ) v . push ( i ) ; } for ( var i = 0 ; i < v . length ; i ++ ) document . write ( v [ i ] + ' ' ) ; } var n = 5 ; setBitwiseORk ( n ) ;
Two odd occurring elements in an array where all other occur even times | Javascript code to find two odd occurring elements in an array where all other elements appear even number of times . ; Find XOR of all numbers ; Find a set bit in the XOR ( We find rightmost set bit here ) ; Traverse through all numbers and divide them in two groups ( i ) Having set bit set at same position as the only set bit in set_bit ( ii ) Having 0 bit at same position as the only set bit in set_bit ; XOR of two different sets are our required numbers . ; Driver code
function printOdds ( arr , n ) { let res = 0 ; for ( let i = 0 ; i < n ; i ++ ) res = res ^ arr [ i ] ; let set_bit = res & ( ~ ( res - 1 ) ) ; let x = 0 , y = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & set_bit ) x = x ^ arr [ i ] ; else y = y ^ arr [ i ] ; } document . write ( x + " " + y ) ; } let arr = [ 2 , 3 , 3 , 4 , 4 , 5 ] ; let n = arr . length ; printOdds ( arr , n ) ;
Maximum subset with bitwise OR equal to k | function to find the maximum subset with bitwise OR equal to k ; If the bitwise OR of k and element is equal to k , then include that element in the subset ; Store the bitwise OR of elements in v ; If ans is not equal to k , subset doesn 't exist ; Driver Code
function subsetBitwiseORk ( arr , n , k ) { var v = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] k ) == k ) v . push ( arr [ i ] ) ; } var ans = 0 ; for ( var i = 0 ; i < v . length ; i ++ ) ans |= v [ i ] ; if ( ans != k ) { document . write ( " " ) ; return ; } for ( var i = 0 ; i < v . length ; i ++ ) document . write ( v [ i ] + ' ' ) ; } var k = 3 ; var arr = [ 1 , 4 , 2 ] ; var n = arr . length ; subsetBitwiseORk ( arr , n , k ) ;
Number whose XOR sum with given array is a given number k | This function returns the number to be inserted in the given array ; initialise the answer with k ; XOR of all elements in the array ; Driver Code
function findEletobeInserted ( A , n , k ) { var ans = k ; for ( var i = 0 ; i < n ; i ++ ) ans ^= A [ i ] ; return ans ; } var A = [ 1 , 2 , 3 , 4 , 5 ] ; var n = A . length ; var k = 10 ; document . write ( findEletobeInserted ( A , n , k ) + " " + " " + " " + k ) ;
Range query for count of set bits | 2 - D array that will stored the count of bits set in element of array ; Function store the set bit count in BitCount Array ; traverse over all bits ; mark elements with i 'th bit set ; Check whether the current bit is set or not if it 's  set then mark it. ; store cumulative sum of bits ; Function to process queries ; Driver Code
var BitCount = Array . from ( { length : 10000 } , ( _ , i ) => 0 ) ; function fillSetBitsMatrix ( arr , n ) { for ( i = 0 ; i < 32 ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { var temp = arr [ j ] >> i ; if ( temp % 2 != 0 ) BitCount [ j ] += 1 ; } } for ( i = 1 ; i < n ; i ++ ) BitCount [ i ] += BitCount [ i - 1 ] ; } function Query ( Q , q ) { for ( i = 0 ; i < q ; i ++ ) document . write ( ( BitCount [ Q [ i ] [ 1 ] ] - BitCount [ Q [ i ] [ 0 ] - 1 ] ) + " " ) ; } var Arr = [ 1 , 5 , 6 , 10 , 9 , 4 , 67 ] ; var n = Arr . length ; fillSetBitsMatrix ( Arr , n ) ; var q = 2 ; var Q = [ [ 1 , 5 ] , [ 2 , 6 ] ] ; Query ( Q , q ) ;
Generate n | Function to convert decimal to binary ; leftmost digits are filled with 0 ; Function to generate gray code ; generate gray code of corresponding binary number of integer i . ; printing gray code ; Drivers code
function decimalToBinaryNumber ( x , n ) { var binaryNumber = Array ( x ) ; var i = 0 ; while ( x > 0 ) { binaryNumber [ i ] = x % 2 ; x = parseInt ( x / 2 ) ; i ++ ; } for ( var j = 0 ; j < n - i ; j ++ ) document . write ( ' ' ) ; for ( var j = i - 1 ; j >= 0 ; j -- ) document . write ( binaryNumber [ j ] ) ; } function generateGrayarr ( n ) { var N = 1 << n ; for ( var i = 0 ; i < N ; i ++ ) { var x = i ^ ( i >> 1 ) ; decimalToBinaryNumber ( x , n ) ; document . write ( " " ) ; } } var n = 3 ; generateGrayarr ( n ) ;
Sum of bitwise AND of all possible subsets of given set | javascript program to calculate sum of Bit - wise and sum of all subsets of an array ; assuming representation of each element is in 32 bit ; iterating array element ; Counting the set bit of array in ith position ; counting subset which produce sum when particular bit position is set . ; multiplying every position subset with 2 ^ i to count the sum . ; Drivers code
var BITS = 32 ; function andSum ( arr , n ) { var ans = 0 ; for ( i = 0 ; i < BITS ; i ++ ) { var countSetBits = 0 ; for ( j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) != 0 ) countSetBits ++ ; } var subset = ( 1 << countSetBits ) - 1 ; subset = ( subset * ( 1 << i ) ) ; ans += subset ; } return ans ; } var arr = [ 1 , 2 , 3 ] ; var size = 3 ; document . write ( andSum ( arr , size ) ) ;
Minimum number using set bits of a given number | An efficient Javascript program to find minimum number formed by bits of a given number . ; Returns minimum number formed by bits of a given number . ; _popcnt32 ( a ) gives number of 1 's present in binary  representation of a. ; Driver Code .
function _popcnt32 ( number ) { let count = 0 ; while ( number > 0 ) { count += number & 1 ; number >>= 1 ; } return count ; } function minimize ( a ) { let n = _popcnt32 ( a ) ; return ( Math . pow ( 2 , n ) - 1 ) ; } let a = 11 ; document . write ( minimize ( a ) ) ;
Maximum steps to transform 0 to X with bitwise AND | Function to get no of set bits in binary representation of positive integer n ; Driver code
function countSetBits ( n ) { var count = 0 ; while ( n ) { count += n & 1 ; n >>= 1 ; } return count ; } var i = 3 ; document . write ( countSetBits ( i ) ) ;
Check a number is odd or even without modulus operator | Returns true if n is even , else odd ; Driver code
function isEven ( n ) { let isEven = true ; for ( let i = 1 ; i <= n ; i ++ ) isEven = ! isEven ; return isEven ; } let n = 101 ; if ( isEven ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check a number is odd or even without modulus operator | Returns true if n is even , else odd ; Return true if n / 2 does not result in a float value . ; Driver code
function isEven ( n ) { return ( parseInt ( n / 2 , 10 ) * 2 == n ) ; } let n = 101 ; isEven ( n ) ? document . write ( " " ) : document . write ( " " ) ;
Bitwise recursive addition of two integers | Javascript program to do recursive addition of two integers ; If bitwise & is 0 , then there is not going to be any carry . Hence result of XOR is addition . ; Driver code
function add ( x , y ) { let keep = ( x & y ) << 1 ; let res = x ^ y ; if ( keep == 0 ) return res ; return add ( keep , res ) ; } document . write ( add ( 15 , 38 ) ) ;
Count pairs in an array which have at least one digit common | Returns true if the pair is valid , otherwise false ; converting integers to strings ; Iterate over the strings and check if a character in first string is also present in second string , return true ; No common digit found ; Returns the number of valid pairs ; Iterate over all possible pairs ; Driver Code to test above functions
function checkValidPair ( num1 , num2 ) { var s1 = num1 . toString ( ) ; var s2 = num2 . toString ( ) ; var i , j ; for ( i = 0 ; i < s1 . length ; i ++ ) for ( j = 0 ; j < s2 . length ; j ++ ) if ( s1 [ i ] == s2 [ j ] ) return true ; return false ; } function countPairs ( arr , n ) { var numberOfPairs = 0 ; for ( i = 0 ; i < n ; i ++ ) for ( j = i + 1 ; j < n ; j ++ ) if ( checkValidPair ( arr [ i ] , arr [ j ] ) ) numberOfPairs ++ ; return numberOfPairs ; } var arr = [ 10 , 12 , 24 ] ; var n = arr . length ; ; document . write ( countPairs ( arr , n ) ) ;
Check if bitwise AND of any subset is power of two | Javascript Program to check if Bitwise AND of any subset is power of two ; Check for power of 2 or not ; Check if there exist a subset whose bitwise AND is power of 2. ; if there is only one element in the set . ; Finding a number with all bit sets . ; check all the positions at which the bit is set . ; include all those elements whose i - th bit is set ; check for the set contains elements make a power of 2 or not ; Driver Program
var NUM_BITS = 32 ; function isPowerOf2 ( num ) { return ( num && ! ( num & ( num - 1 ) ) ) ; } function checkSubsequence ( arr , n ) { if ( n == 1 ) return isPowerOf2 ( arr [ 0 ] ) ; var total = 0 ; for ( var i = 0 ; i < NUM_BITS ; i ++ ) total = total | ( 1 << i ) ; for ( var i = 0 ; i < NUM_BITS ; i ++ ) { var ans = total ; for ( var j = 0 ; j < n ; j ++ ) { if ( arr [ j ] & ( 1 << i ) ) ans = ans & arr [ j ] ; } if ( isPowerOf2 ( ans ) ) return true ; } return false ; } var arr = [ 12 , 13 , 7 ] ; var n = arr . length ; if ( checkSubsequence ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Levelwise Alternating OR and XOR operations in Segment Tree | A utility function to get the middle index from corner indexes . ; A recursive function that constructs Segment Tree for array [ ss . . se ] . si is index of current node in segment tree st operation denotes which operation is carried out at that level to merge the left and right child . It 's either 0 or 1. ; If there is one element in array , store it in current node of segment tree and return ; If there are more than one elements , then recur for left and right subtrees and store the sum of values in this node ; Build the left and the right subtrees by using the fact that operation at level ( i + 1 ) = ! ( operation at level i ) ; Merge the left and right subtrees by checking the operation to be carried . If operation = 1 , then do OR else XOR ; OR operation ; XOR operation ; Function to construct segment tree from given array . This function allocates memory for segment tree and calls constructSTUtil ( ) to fill the allocated memory ; Height of segment tree ; Maximum size of segment tree ; Allocate memory ; Operation = 1 ( XOR ) if Height of tree is even else it is 0 ( OR ) for the root node ; Fill the allocated memory st ; Return the constructed segment tree ; Leaf nodes ; Build the segment tree ; Root node is at index 0 considering 0 - based indexing in segment Tree ; Print value at rootIndex
function getMid ( s , e ) { return s + Math . floor ( ( e - s ) / 2 ) ; } function constructSTUtil ( arr , ss , se , st , si , operation ) { if ( ss == se ) { st [ si ] = arr [ ss ] ; return ; } let mid = getMid ( ss , se ) ; constructSTUtil ( arr , ss , mid , st , si * 2 + 1 , ! operation ) ; constructSTUtil ( arr , mid + 1 , se , st , si * 2 + 2 , ! operation ) ; if ( operation ) { st [ si ] = ( st [ 2 * si + 1 ] st [ 2 * si + 2 ] ) ; } else { st [ si ] = ( st [ 2 * si + 1 ] ^ st [ 2 * si + 2 ] ) ; } } function constructST ( arr , n ) { let x = Math . ceil ( Math . log ( n ) / Math . log ( 2 ) ) ; let max_size = 2 * Math . pow ( 2 , x ) - 1 ; let st = new Array ( max_size ) ; let operationAtRoot = ! ( x % 2 == 0 ) ; constructSTUtil ( arr , 0 , n - 1 , st , 0 , operationAtRoot ) ; return st ; } let leaves = [ 1 , 6 , 3 , 7 , 5 , 9 , 10 , 4 ] ; let n = leaves . length ; let segmentTree = constructST ( leaves , n ) ; let rootIndex = 0 ; document . write ( " " + segmentTree [ rootIndex ] ) ;