codeparrot/xlcost-text-to-code huggingface dataset:
Bitwise operations on Subarrays of size K | Function to convert bit array to decimal number ; Function to find maximum values of each bitwise OR operation on element of subarray of size K ; Maintain an integer array bit [ ] of size 32 all initialized to 0 ; Create a sliding window of size k ; Function call ; Perform operation for removed element ; Perform operation for added_element ; Taking maximum value ; Return the result ; Given array [ ] arr ; Given subarray size K ; Function Call
function build_num ( bit ) { let ans = 0 ; for ( let i = 0 ; i < 32 ; i ++ ) if ( bit [ i ] > 0 ) ans += ( 1 << i ) ; return ans ; } function maximumOR ( arr , n , k ) { let bit = new Array ( 32 ) ; bit . fill ( 0 ) ; for ( let i = 0 ; i < k ; i ++ ) { for ( let j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } } let max_or = build_num ( bit ) ; for ( let i = k ; i < n ; i ++ ) { for ( let j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i - k ] & ( 1 << j ) ) > 0 ) bit [ j ] -- ; } for ( let j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } max_or = Math . max ( build_num ( bit ) , max_or ) ; } return max_or ; } let arr = [ 2 , 5 , 3 , 6 , 11 , 13 ] ; let k = 3 ; let n = arr . length ; document . write ( maximumOR ( arr , n , k ) ) ;
Maximum XOR path of a Binary Tree | Binary tree node ; Function to create a new node ; Function calculate the value of Math . max - xor ; Updating the xor value with the xor of the path from root to the node ; Check if node is leaf node ; Check if the left node exist in the tree ; Check if the right node exist in the tree ; Function to find the required count ; Recursively traverse the tree and compute the max_xor ; Return the result ; Create the binary tree
class Node { constructor ( ) { this . data = 0 ; this . left = null ; this . right = null ; } } ; function newNode ( data ) { var newNode = new Node ; newNode . data = data ; newNode . left = newNode . right = null ; return ( newNode ) ; } function Solve ( root , xr , max_xor ) { xr = xr ^ root . data ; if ( root . left == null && root . right == null ) { max_xor = Math . max ( max_xor , xr ) ; return max_xor ; } if ( root . left != null ) { max_xor = Solve ( root . left , xr , max_xor ) ; } if ( root . right != null ) { max_xor = Solve ( root . right , xr , max_xor ) ; } return max_xor ; } function findMaxXor ( root ) { var xr = 0 , max_xor = 0 ; max_xor = Solve ( root , xr , max_xor ) ; return max_xor ; } var root = newNode ( 2 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 4 ) ; root . left . left = newNode ( 10 ) ; root . left . right = newNode ( 8 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 10 ) ; document . write ( findMaxXor ( root ) ) ;
XOR of every element of an Array with a given number K | Function to construct new array ; Traverse the array and compute XOR with K ; Print new array ; Driver code
function constructXORArray ( A , n , K ) { let B = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) B [ i ] = A [ i ] ^ K ; for ( let i = 0 ; i < n ; i ++ ) document . write ( B [ i ] + " " ) ; document . write ( " " ) } let A = [ 2 , 4 , 1 , 3 , 5 ] ; let K = 5 ; let n = A . length ; constructXORArray ( A , n , K ) ; let B = [ 4 , 75 , 45 , 42 ] ; K = 2 ; n = B . length ; constructXORArray ( B , n , K ) ;
Bitwise AND of all the elements of array | Function to calculate bitwise AND ; Initialise ans variable is arr [ 0 ] ; Traverse the array compute AND ; Return ans ; Driver Code ; Function Call to find AND
function find_and ( arr ) { let ans = arr [ 0 ] ; for ( let i = 0 ; i < arr . length ; i ++ ) { ans = ( ans & arr [ i ] ) ; } return ans ; } let arr = [ 1 , 3 , 5 , 9 , 11 ] ; document . write ( find_and ( arr ) ) ;
Construct XOR tree by Given leaf nodes of Perfect Binary Tree | Maximum size for xor tree ; Allocating space to xor tree ; A recursive function that constructs xor tree for vector array [ start ... . . end ] . x is index of current node in XOR tree ; If there is one element in vector array , store it in current node of XOR tree ; cout << xor_tree [ x ] << " ▁ x " ; ; for left subtree ; for right subtree ; for getting the middle index from corner indexes . ; Build the left and the right subtrees by xor operation ; merge the left and right subtrees by XOR operation ; Function to construct XOR tree from the given vector array . This function calls construct_Xor_Tree_Util ( ) to fill the allocated memory of xor_tree vector array ; leaf nodes of Perfect Binary Tree ; Build the xor tree ; Height of xor tree ; Maximum size of xor tree ; Root node is at index 0 considering 0 - based indexing in XOR Tree ; print value at root node
var maxsize = 100005 ; var xor_tree = Array ( maxsize ) ; function construct_Xor_Tree_Util ( current , start , end , x ) { if ( start == end ) { xor_tree [ x ] = current [ start ] ; return ; } var left = x * 2 + 1 ; var right = x * 2 + 2 ; var mid = start + parseInt ( ( end - start ) / 2 ) ; construct_Xor_Tree_Util ( current , start , mid , left ) ; construct_Xor_Tree_Util ( current , mid + 1 , end , right ) ; xor_tree [ x ] = ( xor_tree [ left ] ^ xor_tree [ right ] ) ; } function construct_Xor_Tree ( arr , n ) { construct_Xor_Tree_Util ( arr , 0 , n - 1 , 0 ) ; } var leaf_nodes = [ 40 , 32 , 12 , 1 , 4 , 3 , 2 , 7 ] ; var n = leaf_nodes . length ; construct_Xor_Tree ( leaf_nodes , n ) ; var x = ( Math . ceil ( Math . log2 ( n ) ) ) ; var max_size = 2 * Math . pow ( 2 , x ) - 1 ; document . write ( " " ) ; for ( var i = 0 ; i < max_size ; i ++ ) { document . write ( xor_tree [ i ] + " " ) ; } var root = 0 ; document . write ( " " + xor_tree [ root ] ) ;
Maximize distance between any two consecutive 1 ' s ▁ after ▁ flipping ▁ M ▁ 0' s | Function to return the count ; Flipping zeros at distance " d " ; Function to implement binary search ; Check for valid distance i . e mid ; Driver code
function check ( arr , n , m , d ) { var i = 0 ; while ( i < n && m > 0 ) { m -- ; i += d ; } return m == 0 ? true : false ; } function maximumDistance ( arr , n , m ) { var low = 1 , high = n - 1 ; var ans = 0 ; while ( low <= high ) { var mid = parseInt ( ( low + high ) / 2 ) ; var flag = check ( arr , n , m , mid ) ; if ( flag ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } var n = 5 , m = 3 ; var arr = new Array ( n ) ; arr . fill ( 0 ) ; document . write ( maximumDistance ( arr , n , m ) ) ;
Maximum XOR value of maximum and second maximum element among all possible subarrays | Function to return the maximum possible xor ; To store the readonly answer ; Borward traversal ; Backward traversal ; Driver code
function maximumXor ( arr , n ) { let sForward = [ ] ; let sBackward = [ ] ; let ans = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { while ( sForward . length != 0 && arr [ i ] < arr [ sForward [ sForward . length - 1 ] ] ) { ans = Math . max ( ans , arr [ i ] ^ arr [ sForward [ sForward . length - 1 ] ] ) ; sForward . pop ( ) ; } sForward . push ( i ) ; while ( sBackward . length != 0 && arr [ n - i - 1 ] < arr [ sBackward [ sBackward . length - 1 ] ] ) { ans = Math . max ( ans , arr [ n - i - 1 ] ^ arr [ sBackward [ sBackward . length - 1 ] ] ) ; sBackward . pop ( ) ; } sBackward . push ( n - i - 1 ) ; } return ans ; } let arr = [ 8 , 1 , 2 ] ; let n = arr . length ; document . write ( maximumXor ( arr , n ) ) ;
Minimum count of Full Binary Trees such that the count of leaves is N | Function to return the minimum count of trees required ; To store the count of set bits in n ; Driver code
function minTrees ( n ) { let count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } let n = 7 ; document . write ( minTrees ( n ) ) ;
Count number of steps to cover a distance if steps can be taken in powers of 2 | Function to count the minimum number of steps ; count the number of set bits in a number ; Driver Code
function getMinSteps ( K ) { return countSetBits ( K ) ; } function countSetBits ( x ) { let setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } let n = 343 ; document . write ( getMinSteps ( n ) ) ;
Game Theory in Balanced Ternary Numeral System | ( Moving 3 k steps at a time ) | Function that returns true if the game cannot be won ; Driver Code ; Common length
function isDefeat ( s1 , s2 , n ) { for ( let i = 0 ; i < n ; i ++ ) { if ( ( s1 [ i ] == ' ' && s2 [ i ] == ' ' ) || ( s1 [ i ] == ' ' && s2 [ i ] == ' ' ) ) continue ; else if ( ( s1 [ i ] == ' ' && s2 [ i ] == ' ' ) || ( s1 [ i ] == ' ' && s2 [ i ] == ' ' ) ) continue ; else { return true ; } } return false ; } let s1 = ( " " ) ; let s2 = ( " " ) ; let n = 10 ; if ( isDefeat ( s1 , s2 , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if matrix A can be converted to B by changing parity of corner elements of any submatrix | javascript implementation of the above approach ; Boolean function that returns true or false ; Traverse for all elements ; If both are not equal ; Change the parity of all corner elements ; Check if A is equal to B ; Not equal ; First binary matrix ; Second binary matrix
var N = 3 , M = 3 ; function check ( a , b ) { for ( i = 1 ; i < N ; i ++ ) { for ( j = 1 ; j < M ; j ++ ) { if ( a [ i ] [ j ] != b [ i ] [ j ] ) { a [ i ] [ j ] ^= 1 ; a [ 0 ] [ 0 ] ^= 1 ; a [ 0 ] [ j ] ^= 1 ; a [ i ] [ 0 ] ^= 1 ; } } } for ( i = 0 ; i < N ; i ++ ) { for ( j = 0 ; j < M ; j ++ ) { if ( a [ i ] [ j ] != b [ i ] [ j ] ) return false ; } } return true ; } var a = [ [ 0 , 1 , 0 ] , [ 0 , 1 , 0 ] , [ 1 , 0 , 0 ] ] ; var b = [ [ 1 , 0 , 0 ] , [ 1 , 0 , 0 ] , [ 1 , 0 , 0 ] ] ; if ( check ( a , b ) ) document . write ( " " ) ; else document . write ( " " ) ;
Position of the K | Function that returns the Kth set bit ; Traverse in the binary ; Check if the last bit is set or not ; Check if count is equal to k then return the index ; Increase the index as we move right ; Right shift the number by 1 ; Driver Code
function FindIndexKthBit ( n , k ) { let cnt = 0 ; let ind = 0 ; while ( n > 0 ) { if ( n & 1 > 0 ) cnt ++ ; if ( cnt == k ) return ind ; ind ++ ; n = n >> 1 ; } return - 1 ; } let n = 15 , k = 3 ; let ans = FindIndexKthBit ( n , k ) ; if ( ans != - 1 ) document . write ( ans ) ; else document . write ( " " ) ;
Check if the binary representation of a number has equal number of 0 s and 1 s in blocks | Function to check ; Converting integer to its equivalent binary number ; If adjacent character are same then increase counter ; Driver Code
function hasEqualBlockFrequency ( N ) { let S = N . toString ( 2 ) ; let p = new Set ( ) ; let c = 1 ; for ( let i = 0 ; i < S . length - 1 ; i ++ ) { if ( S [ i ] == S [ i + 1 ] ) c += 1 ; else { p . add ( c ) ; c = 1 ; } p . add ( c ) ; } if ( p . size == 1 ) document . write ( " " ) ; else document . write ( " " ) ; } let N = 5 ; hasEqualBlockFrequency ( N ) ;
Distance between two nodes of binary tree with node values from 1 to N | Function to get minimum path distance ; count bit length of n1 and n2 ; find bit difference and maxBit ; calculate result by formula ; Driver program
function minDistance ( n1 , n2 ) { var bitCount1 = Math . floor ( Math . log2 ( n1 ) ) + 1 ; var bitCount2 = Math . floor ( Math . log2 ( n2 ) ) + 1 ; var bitDiff = Math . abs ( bitCount1 - bitCount2 ) ; var maxBitCount = Math . max ( bitCount1 , bitCount2 ) ; if ( bitCount1 > bitCount2 ) { n2 = n2 * Math . pow ( 2 , bitDiff ) ; } else { n1 = n1 * Math . pow ( 2 , bitDiff ) ; } var xorValue = n1 ^ n2 ; var bitCountXorValue ; if ( xorValue == 0 ) bitCountXorValue = 1 ; else { bitCountXorValue = Math . floor ( Math . log2 ( xorValue ) ) + 1 ; } var disSimilarBitPosition = maxBitCount - bitCountXorValue ; var result = bitCount1 + bitCount2 - 2 * disSimilarBitPosition ; return result ; } var n1 = 12 , n2 = 5 ; document . write ( minDistance ( n1 , n2 ) ) ;
Remove one bit from a binary number to get maximum value | Function to find the maximum binary number ; Traverse the binary number ; Try finding a 0 and skip it ; Get the binary number ; Find the maximum binary number
function printMaxAfterRemoval ( s ) { let flag = false ; let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' && flag == false ) { flag = true ; continue ; } else document . write ( s [ i ] ) ; } return 0 ; } let s = " " ; printMaxAfterRemoval ( s ) ;
Find position of left most dis | Function to find first dis - similar bit ; Return zero for equal number ; Find the 1 st dis - similar bit count bit length of n1 and n2 ; find bit difference and maxBit ; Driver code
function bitPos ( n1 , n2 ) { if ( n1 == n2 ) return 0 ; let bitCount1 = Math . floor ( Math . log2 ( n1 ) ) + 1 ; let bitCount2 = Math . floor ( Math . log2 ( n2 ) ) + 1 ; let bitDiff = Math . abs ( bitCount1 - bitCount2 ) ; let maxBitCount = Math . max ( bitCount1 , bitCount2 ) ; if ( bitCount1 > bitCount2 ) { n2 = n2 * Math . pow ( 2 , bitDiff ) ; } else { n1 = n1 * Math . pow ( 2 , bitDiff ) ; } let xorValue = n1 ^ n2 ; let bitCountXorValue = Math . floor ( Math . log2 ( xorValue ) ) + 1 ; let disSimilarBitPosition = maxBitCount - bitCountXorValue + 1 ; return disSimilarBitPosition ; } let n1 = 53 , n2 = 55 ; document . write ( bitPos ( n1 , n2 ) ) ;
Number of pairs with Bitwise OR as Odd number | Function to count pairs with odd OR ; Count total even numbers in array ; Even pair count ; Total pairs ; Return Odd pair count ; Driver main
function countOddPair ( A , N ) { let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) if ( ! ( A [ i ] & 1 ) ) count ++ ; let evenPairCount = parseInt ( count * ( count - 1 ) / 2 ) ; let totPairs = parseInt ( N * ( N - 1 ) / 2 ) ; return totPairs - evenPairCount ; } let A = [ 5 , 6 , 2 , 8 ] ; let N = A . length ; document . write ( countOddPair ( A , N ) ) ;
Replace every array element by Bitwise Xor of previous and next element | Javascript program to update every array element with sum of previous and next numbers in array ; Nothing to do when array size is 1 ; Store current value of arr [ 0 ] and update it ; Update rest of the array elements ; Store current value of next interaction ; Update current value using previous value ; Update previous value ; Update last array element separately ; Driver code ; Print the modified array
function ReplaceElements ( arr , n ) { if ( n <= 1 ) return ; let prev = arr [ 0 ] ; arr [ 0 ] = arr [ 0 ] ^ arr [ 1 ] ; for ( let i = 1 ; i < n - 1 ; i ++ ) { let curr = arr [ i ] ; arr [ i ] = prev ^ arr [ i + 1 ] ; prev = curr ; } arr [ n - 1 ] = prev ^ arr [ n - 1 ] ; } let arr = [ 2 , 3 , 4 , 5 , 6 ] ; let n = arr . length ; ReplaceElements ( arr , n ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Lucky alive person in a circle | Set | JavaScript implementation of the above approach ; to calculate the number of bits in the binary equivalent of n ; Finding complement by inverting the bits one by one from last ; XOR of n2 with ( 1 << i ) to flip the last ( i + 1 ) th bit of binary equivalent of n2 ; n2 will be the one 's complement of n ; Driver Code
function luckiest_person ( n ) { let len = parseInt ( Math . log ( n ) / Math . log ( 2 ) ) + 1 ; let n2 = n ; for ( let i = 0 ; i < len ; i ++ ) { n2 = n2 ^ ( 1 << i ) ; } return Math . abs ( n - n2 ) ; } let N = 3 ; let lucky_p = luckiest_person ( N ) ; document . write ( lucky_p ) ;
Find triplets in an array whose AND is maximum | Javascript program to find triplet with maximum bitwise AND . ; Flag Array initially set to true for all numbers ; 2D array for bit representation of all the numbers . Initially all bits are set to 0. ; Finding bit representation of every number and storing it in bits array . ; Checking last bit of the number ; Dividing number by 2. ; maximum And number initially 0. ; Traversing the 2d binary representation . 0 th index represents 32 th bits while 32 th index represents 0 th bit . ; If cnt greater than 3 then ( 32 - i ) th bits of the number will be set . ; Setting flags of the numbers whose ith bit is not set . ; Counting the numbers whose flag are true . ; Driver code
function maxTriplet ( a , n ) { let f = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { f [ i ] = true ; } let bits = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { bits [ i ] = new Array ( 33 ) ; for ( let j = 0 ; j < 33 ; j ++ ) { bits [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < n ; ++ i ) { let num = a [ i ] ; let j = 32 ; while ( num > 0 ) { if ( num % 2 == 1 ) { bits [ i ] [ j ] = 1 ; } j -- ; num >>= 1 ; } } let ans = 0 ; for ( let i = 0 ; i <= 32 ; ++ i ) { let cnt = 0 ; for ( let j = 0 ; j < n ; ++ j ) { if ( bits [ j ] [ i ] == 1 & f [ j ] ) { cnt ++ ; } } if ( cnt >= 3 ) { ans += Math . pow ( 2 , 32 - i ) ; for ( let j = 0 ; j < n ; ++ j ) { if ( bits [ j ] [ i ] != 1 ) { f [ j ] = false ; } } } } let cnt = 0 ; for ( let i = 0 ; i < n ; ++ i ) { if ( f [ i ] ) { cnt ++ ; } } let NumberOfTriplets = ( cnt * ( cnt - 1 ) * ( cnt - 2 ) ) / 6 ; document . write ( NumberOfTriplets + " " + ans ) ; } let a = [ 4 , 11 , 10 , 15 , 26 ] ; let n = a . length ; maxTriplet ( a , n ) ;
Find bitwise OR of all possible sub | function to return OR of sub - arrays ; Driver Code ; print OR of all subarrays
function OR ( a , n ) { var ans = a [ 0 ] ; for ( var i = 1 ; i < n ; ++ i ) ans |= a [ i ] ; return ans ; } var a = [ 1 , 4 , 6 ] ; var n = a . length ; document . write ( OR ( a , n ) ) ;
Maximum set bit sum in array without considering adjacent elements | Function to count total number of set bits in an integer ; Maximum sum of set bits ; Calculate total number of set bits for every element of the array ; find total set bits for each number and store back into the array ; current max excluding i ; current max including i ; return max of incl and excl ; Driver code
function bit ( n ) { let count = 0 ; while ( n > 0 ) { count ++ ; n = n & ( n - 1 ) ; } return count ; } function maxSumOfBits ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { arr [ i ] = bit ( arr [ i ] ) ; } let incl = arr [ 0 ] ; let excl = 0 ; let excl_new ; for ( let i = 1 ; i < n ; i ++ ) { excl_new = ( incl > excl ) ? incl : excl ; incl = excl + arr [ i ] ; excl = excl_new ; } return ( ( incl > excl ) ? incl : excl ) ; } let arr = [ 1 , 2 , 4 , 5 , 6 , 7 , 20 , 25 ] ; let n = arr . length ; document . write ( maxSumOfBits ( arr , n ) ) ;
Increment a number without using ++ or + | function that increment the value . ; Invert bits and apply negative sign ; Driver code
function increment ( i ) { let i1 = - ( ~ ( i ) . charCodeAt ( ) ) ; return String . fromCharCode ( i1 ) ; } let n = ' ' ; document . write ( increment ( n ) ) ;
Count pairs with Bitwise XOR as ODD number | Function to count number of odd pairs ; find all pairs ; return number of odd pair ; Driver Code ; calling function findOddPair and print number of odd pair
function findOddPair ( A , N ) { let i , count = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) count ++ ; } return count * ( N - count ) ; } let a = [ 5 , 4 , 7 , 2 , 1 ] ; let n = a . length ; document . write ( findOddPair ( a , n ) ) ;
Bitwise OR ( or | ) of a range | Returns the Most Significant Bit Position ( MSB ) ; Returns the Bitwise OR of all integers between L and R ; Find the MSB position in L ; Find the MSB position in R ; Add this value until msb_p1 and msb_p2 are same ; ; Calculate msb_p1 and msb_p2 ; Find the max of msb_p1 and msb_p2 ; Set all the bits from msb_p1 upto 0 th bit in the result ; Driver Code
function MSBPosition ( N ) { let msb_p = - 1 ; while ( N > 0 ) { N = N >> 1 ; msb_p ++ ; } return msb_p ; } function findBitwiseOR ( L , R ) { let res = 0 ; let msb_p1 = MSBPosition ( L ) ; let msb_p2 = MSBPosition ( R ) ; while ( msb_p1 == msb_p2 ) { let res_val = ( 1 << msb_p1 ) ; res += res_val ; L -= res_val ; R -= res_val ; msb_p1 = MSBPosition ( L ) ; msb_p2 = MSBPosition ( R ) ; } msb_p1 = Math . max ( msb_p1 , msb_p2 ) ; for ( let i = msb_p1 ; i >= 0 ; i -- ) { let res_val = ( 1 << i ) ; res += res_val ; } return res ; } let L = 12 , R = 18 ; document . write ( findBitwiseOR ( L , R ) ) ;
Maximize the bitwise OR of an array | Function to maximize the bitwise OR sum ; Compute x ^ k ; Find prefix bitwise OR ; Find suffix bitwise OR ; Find maximum OR value ; Drivers code
function maxOR ( arr , n , k , x ) { let preSum = new Array ( n + 1 ) ; let suffSum = new Array ( n + 1 ) ; let res = 0 , pow = 1 ; for ( let i = 0 ; i < k ; i ++ ) pow *= x ; preSum [ 0 ] = 0 ; for ( let i = 0 ; i < n ; i ++ ) preSum [ i + 1 ] = preSum [ i ] | arr [ i ] ; suffSum [ n ] = 0 ; for ( let i = n - 1 ; i >= 0 ; i -- ) suffSum [ i ] = suffSum [ i + 1 ] | arr [ i ] ; res = 0 ; for ( let i = 0 ; i < n ; i ++ ) res = Math . max ( res , preSum [ i ] | ( arr [ i ] * pow ) suffSum [ i + 1 ] ) ; return res ; } let arr = [ 1 , 2 , 4 , 8 ] ; let n = 4 ; let k = 2 , x = 3 ; let ans = maxOR ( arr , n , k , x ) ; document . write ( ans ) ;
How to turn on a particular bit in a number ? | Returns a number that has all bits same as n except the k 'th  bit which is made 1 ; k must be greater than 0 ; Do | of n with a number with all unset bits except the k 'th bit ; Driver Code
function turnOnK ( n , k ) { if ( k <= 0 ) return n ; return ( n | ( 1 << ( k - 1 ) ) ) ; } let n = 4 ; let k = 2 ; document . write ( turnOnK ( n , k ) ) ;
Minimum sum of two numbers formed from digits of an array | Returns sum of two numbers formed from all digits in a [ ] ; sort the elements ; Driver code
function minSum ( a , n ) { a . sort ( ) ; let num1 = 0 ; let num2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) num1 = num1 * 10 + a [ i ] ; else num2 = num2 * 10 + a [ i ] ; } return num2 + num1 ; } let arr = [ 5 , 3 , 0 , 7 , 4 ] ; let n = arr . length ; document . write ( " " + minSum ( arr , n ) ) ;
Find value of k | JavaScript program to find k - th bit from right ; Driver Code ; Function Call
function printKthBit ( n , k ) { document . write ( ( n & ( 1 << ( k - 1 ) ) ) >> ( k - 1 ) ) ; } var n = 13 , k = 2 ; printKthBit ( n , k ) ;
Disjoint Set Union on trees | Set 1 | Javascript code to find maximum subtree such that all nodes are even in weight ; ' id ' : stores parent of a node . ' sz ' : stores size of a DSU tree . ; Function to assign root ; Function to find Union ; Utility function for Union ; Edge between ' u ' and ' v ' ; 0 - indexed nodes ; If weights of both ' u ' and ' v ' are even then we make union of them . ; Function to find maximum size of DSU tree ; Weights of nodes ; Number of nodes in a tree ; Initializing every node as a tree with single node . ; Find maximum size of DSU tree .
let N = 100010 ; let id = new Array ( N ) ; let sz = new Array ( N ) ; function Root ( idx ) { let i = idx ; while ( i != id [ i ] ) { id [ i ] = id [ id [ i ] ] ; i = id [ i ] ; } return i ; } function Union ( a , b ) { let i = Root ( a ) , j = Root ( b ) ; if ( i != j ) { if ( sz [ i ] >= sz [ j ] ) { id [ j ] = i ; sz [ i ] += sz [ j ] ; sz [ j ] = 0 ; } else { id [ i ] = j ; sz [ j ] += sz [ i ] ; sz [ i ] = 0 ; } } } function UnionUtil ( e , W , q ) { for ( let i = 0 ; i < q ; i ++ ) { let u , v ; u = e [ i ] [ 0 ] ; v = e [ i ] [ 1 ] ; u -- ; v -- ; if ( W [ u ] % 2 == 0 && W [ v ] % 2 == 0 ) Union ( u , v ) ; } } function findMax ( n , W ) { let maxi = 0 ; for ( let i = 1 ; i < n ; i ++ ) if ( W [ i ] % 2 == 0 ) maxi = Math . max ( maxi , sz [ i ] ) ; return maxi ; } let W = [ 1 , 2 , 6 , 4 , 2 , 0 , 3 ] ; let n = W . length ; for ( let i = 0 ; i < n ; i ++ ) { id [ i ] = i ; sz [ i ] = 1 ; } let e = [ [ 1 , 2 ] , [ 1 , 3 ] , [ 2 , 4 ] , [ 2 , 5 ] , [ 4 , 6 ] , [ 6 , 7 ] ] ; let q = e . length ; UnionUtil ( e , W , q ) ; let maxi = findMax ( n , W ) ; document . write ( " " ) ; document . write ( " " + maxi ) ;
Odd numbers in N | Function to get no of set bits in binary representation of positive integer n ; Count number of 1 's in binary  representation of n. ; Number of odd numbers in n - th row is 2 raised to power the count . ; Driver code
function countSetBits ( n ) { let count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } function countOfOddsPascal ( n ) { let c = countSetBits ( n ) ; return Math . pow ( 2 , c ) ; } let n = 20 ; document . write ( countOfOddsPascal ( n ) ) ;