codeparrot/xlcost-text-to-code huggingface dataset:
Count of elements which cannot form any pair whose sum is power of 2 | Function to calculate and return the count of elements ; Stores the frequencies of every array element ; Stores the count of removals ; For every element , check if it can form a sum equal to any power of 2 with any other element ; Store Math . pow ( 2 , j ) - a [ i ] ; Check if s is present in the array ; If frequency of s exceeds 1 ; If s has frequency 1 but is different from a [ i ] ; Pair possible ; If no pair possible for the current element ; Return the answer ; Driver code
function powerOfTwo ( a , n ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( a [ i ] ) ) { mp . set ( a [ i ] , mp . get ( a [ i ] ) + 1 ) ; } else { mp . set ( a [ i ] , 1 ) ; } } let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let f = false ; for ( let j = 0 ; j < 31 ; j ++ ) { let s = ( 1 << j ) - a [ i ] ; if ( mp . has ( s ) && ( mp . get ( s ) > 1 || mp . get ( s ) == 1 && s != a [ i ] ) ) f = true ; } if ( f == false ) count ++ ; } return count ; } let a = [ 6 , 2 , 11 ] ; let n = a . length ; document . write ( powerOfTwo ( a , n ) ) ;
Construct the Array using given bitwise AND , OR and XOR | Function to find the array ; Loop through all bits in number ; If bit is set in AND then set it in every element of the array ; If bit is not set in AND ; But set in b ( OR ) ; Set bit position in first element ; If bit is not set in c then set it in second element to keep xor as zero for bit position ; Calculate AND , OR and XOR of array ; Check if values are equal or not ; If not , then array is not possible ; Given Bitwise AND , OR , and XOR ; Function Call
function findArray ( n , a , b , c ) { let arr = new Array ( n + 1 ) ; for ( let bit = 30 ; bit >= 0 ; bit -- ) { let set = a & ( 1 << bit ) ; if ( set ) { for ( let i = 0 ; i < n ; i ++ ) arr [ i ] |= set ; } else { if ( b & ( 1 << bit ) ) { arr [ 0 ] |= ( 1 << bit ) ; if ( ! ( c & ( 1 << bit ) ) ) { arr [ 1 ] |= ( 1 << bit ) ; } } } } let aa = Number . MAX_SAFE_INTEGER , bb = 0 , cc = 0 ; for ( let i = 0 ; i < n ; i ++ ) { aa &= arr [ i ] ; bb |= arr [ i ] ; cc ^= arr [ i ] ; } if ( a == aa && b == bb && c == cc ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } else document . write ( " " ) ; } let n = 3 , a = 4 , b = 6 , c = 6 ; findArray ( n , a , b , c ) ;
Minimum XOR of OR and AND of any pair in the Array | Function to find the minimum value of XOR of AND and OR of any pair in the given array ; Sort the array ; Traverse the array arr [ ] ; Compare and Find the minimum XOR value of an array . ; Return the final answer ; Given array arr [ ] ; Function Call
function maxAndXor ( arr , n ) { let ans = Number . MAX_VALUE ; arr . sort ( ) ; for ( let i = 0 ; i < n - 1 ; i ++ ) { ans = Math . min ( ans , arr [ i ] ^ arr [ i + 1 ] ) ; } return ans ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let N = arr . length ; document . write ( maxAndXor ( arr , N ) ) ;
Count of subarrays of size K with elements having even frequencies | Function to return count of required subarrays ; If K is odd ; Not possible to have any such subarrays ; Stores the starting index of every subarrays ; Stores the count of required subarrays ; Stores Xor of the current subarray . ; Xor of first subarray of size K ; If all elements appear even number of times , increase the count of such subarrays ; Remove the starting element from the current subarray ; Traverse the array for the remaining subarrays ; Update Xor by adding the last element of the current subarray ; Increment i ; If currXor becomes 0 , then increment count ; Update currXor by removing the starting element of the current subarray ; Return count ; Driver Code
function countSubarray ( arr , K , N ) { if ( K % 2 != 0 ) return 0 ; if ( N < K ) return 0 ; var start = 0 ; var i = 0 ; var count = 0 ; var currXor = arr [ i ] ; i ++ ; while ( i < K ) { currXor ^= arr [ i ] ; i ++ ; } if ( currXor == 0 ) count ++ ; currXor ^= arr [ start ] ; start ++ ; while ( i < N ) { currXor ^= arr [ i ] ; i ++ ; if ( currXor == 0 ) count ++ ; currXor ^= arr [ start ] ; start ++ ; } return count ; } var arr = [ 2 , 4 , 4 , 2 , 2 , 4 ] ; var K = 4 ; var N = arr . length ; document . write ( countSubarray ( arr , K , N ) ) ;
Count of even and odd set bit Array elements after XOR with K for Q queries | Javascript program to count number of even and odd set bits elements after XOR with a given element function even , odd ; ; Store the count of set bits ; Brian Kernighan 's algorithm ; Function to solve Q queries ; Store set bits in X ; Count set bits of X ; Driver code
function keep_count ( arr , N ) { var count ; for ( i = 0 ; i < N ; i ++ ) { count = 0 ; while ( arr [ i ] != 0 ) { arr [ i ] = ( arr [ i ] - 1 ) & arr [ i ] ; count ++ ; } if ( count % 2 == 0 ) even ++ ; else odd ++ ; } return ; } function solveQueries ( arr , n , q , m ) { even = 0 ; odd = 0 ; keep_count ( arr , n ) ; for ( i = 0 ; i < m ; i ++ ) { var X = q [ i ] ; var count = 0 ; while ( X != 0 ) { X = ( X - 1 ) & X ; count ++ ; } if ( count % 2 == 0 ) { document . write ( even + " " + odd + " " ) ; } else { document . write ( odd + " " + even + " " ) ; } } } var arr = [ 2 , 7 , 4 , 5 , 3 ] ; var n = arr . length ; var q = [ 3 , 4 , 12 , 6 ] ; var m = q . length ; solveQueries ( arr , n , q , m ) ;
Range Queries for finding the Sum of all even parity numbers | pref [ ] array to precompute the sum of all Even Parity Numbers ; Function that returns true if count of set bits in x is even ; Parity will store the count of set bits ; Function to precompute the sum of all even parity numbers upto 100000 ; isEvenParity ( ) return the number i if i has even parity else return 0 ; Function to print sum for each query ; Function to print sum of all even parity numbers between [ L , R ] ; Function that pre computes the sum of all even parity numbers ; Iterate over all Queries to print sum ; Queries ; Function that print the sum of all even parity numbers in Range [ L , R ]
let pref = new Array ( 100001 ) . fill ( 0 ) ; function isEvenParity ( num ) { let parity = 0 ; let x = num ; while ( x != 0 ) { if ( x & 1 == 1 ) parity ++ ; x = x >> 1 ; } if ( parity % 2 == 0 ) return num ; else return 0 ; } function preCompute ( ) { for ( let i = 1 ; i < 100001 ; i ++ ) { pref [ i ] = pref [ i - 1 ] + isEvenParity ( i ) ; } } function printSum2 ( L , R ) { document . write ( pref [ R ] - pref [ L - 1 ] + " " ) ; } function printSum ( arr , Q ) { preCompute ( ) ; for ( let i = 0 ; i < Q ; i ++ ) { printSum2 ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } let N = 2 ; let Q = [ [ 1 , 10 ] , [ 121 , 211 ] ] ; printSum ( Q , N ) ;
Largest number in the Array having frequency same as value | Function to find the largest number whose frequency is equal to itself . ; Find the maximum element in the array ; Driver code
function findLargestNumber ( arr ) { var k = arr . reduce ( ( a , b ) => Math . max ( a , b ) ) ; var m = Array ( k ) . fill ( 0 ) ; arr . forEach ( n => { ++ m [ n ] ; } ) ; for ( var n = arr . length ; n > 0 ; -- n ) { if ( n == m [ n ] ) return n ; } return - 1 ; } var arr = [ 3 , 2 , 5 , 2 , 4 , 5 ] ; document . write ( findLargestNumber ( arr ) ) ;
Largest number in the Array having frequency same as value | Function to find the largest number whose frequency is equal to itself . ; Adding 65536 to keep the count of the current number ; right shifting by 16 bits to find the count of the number i ; Driver code
function findLargestNumber ( arr ) { arr . forEach ( n => { n &= 0xFFFF ; if ( n <= arr . length ) { arr [ n - 1 ] += 0x10000 ; } } ) ; for ( var i = arr . length ; i > 0 ; -- i ) { if ( ( arr [ i - 1 ] >> 16 ) == i ) return i ; } return - 1 ; } var arr = [ 3 , 2 , 5 , 5 , 2 , 4 , 5 ] ; document . write ( findLargestNumber ( arr ) )
Convert numbers into binary representation and add them without carry | Function returns sum of both the binary number without carry ; XOR of N and M ; Driver code
function NoCarrySum ( N , M ) { return N ^ M ; } let N = 37 ; let M = 12 ; document . write ( NoCarrySum ( N , M ) ) ;
Check if all the set bits of the binary representation of N are at least K places away | Javascript program to check if all the set bits of the binary representation of N are at least K places away . ; Initialize check and count with 0 ; The i - th bit is a set bit ; This is the first set bit so , start calculating all the distances between consecutive bits from here ; If count is less than K return false ; Adding the count as the number of zeroes increase between set bits ; Driver Code
function CheckBits ( N , K ) { var check = 0 ; var count = 0 ; for ( var i = 31 ; i >= 0 ; i -- ) { if ( ( ( 1 << i ) & N ) > 0 ) { if ( check == 0 ) { check = 1 ; } else { if ( count < K ) { return false ; } } count = 0 ; } else { count ++ ; } } return true ; } var N = 5 ; var K = 1 ; if ( CheckBits ( N , K ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Minimize bits to be flipped in X and Y such that their Bitwise OR is equal to Z | This function returns minimum number of bits to be flipped in X and Y to make X | Y = Z ; If current bit in Z is set and is also set in either of X or Y or both ; If current bit in Z is set and is unset in both X and Y ; Set that bit in either X or Y ; If current bit in Z is unset and is set in both X and Y ; Unset the bit in both X and Y ; If current bit in Z is unset and is set in either X or Y ; Unset that set bit ; Driver Code
function minimumFlips ( X , Y , Z ) { var res = 0 ; while ( X > 0 Y > 0 Z > 0 ) { if ( ( ( X & 1 ) || ( Y & 1 ) ) && ( Z & 1 ) ) { X = X >> 1 ; Y = Y >> 1 ; Z = Z >> 1 ; continue ; } else if ( ( X & 1 ) == 0 && ( Y & 1 ) == 0 && ( Z & 1 ) ) { res ++ ; } else if ( ( X & 1 ) || ( Y & 1 ) == 1 ) { if ( ( X & 1 ) && ( Y & 1 ) && ( Z & 1 ) == 0 ) { res += 2 ; } else if ( ( ( X & 1 ) || ( Y & 1 ) ) && ( Z & 1 ) == 0 ) { res ++ ; } } X = X >> 1 ; Y = Y >> 1 ; Z = Z >> 1 ; } return res ; } var X = 5 , Y = 8 , Z = 6 ; document . write ( minimumFlips ( X , Y , Z ) ) ;
Count subsequences with same values of Bitwise AND , OR and XOR | function for finding count of possible subsequence ; creating a map to count the frequency of each element ; store frequency of each element ; iterate through the map ; add all possible combination for key equal zero ; add all ( odd number of elements ) possible combination for key other than zero ; driver function
function countSubseq ( arr , n ) { let count = 0 ; let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { mp [ arr [ i ] ] ++ ; if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) } else { mp . set ( arr [ i ] , 1 ) } } for ( let i of mp ) { if ( i [ 0 ] == 0 ) count += Math . pow ( 2 , i [ 1 ] ) - 1 ; else count += Math . pow ( 2 , i [ 1 ] - 1 ) ; } return count ; } let arr = [ 2 , 2 , 2 , 5 , 6 ] ; let n = arr . length ; document . write ( countSubseq ( arr , n ) ) ;
Choose an integer K such that maximum of the xor values of K with all Array elements is minimized | Function to calculate Minimum possible value of the Maximum XOR in an array ; base case ; Divide elements into two sections ; Traverse all elements of current section and divide in two groups ; Check if one of the sections is empty ; explore both the possibilities using recursion ; Function to calculate minimum XOR value ; Start recursion from the most significant pos position ; Driver code
function calculate ( section , pos ) { if ( pos < 0 ) return 0 ; let on_section = [ ] , off_section = [ ] ; for ( let el = 0 ; el < section . length ; el ++ ) { if ( ( ( section [ el ] >> pos ) & 1 ) == 0 ) off_section . push ( section [ el ] ) ; else on_section . push ( section [ el ] ) ; } if ( off_section . length == 0 ) return calculate ( on_section , pos - 1 ) ; if ( on_section . length == 0 ) return calculate ( off_section , pos - 1 ) ; return Math . min ( calculate ( off_section , pos - 1 ) , calculate ( on_section , pos - 1 ) ) + ( 1 << pos ) ; } function minXorValue ( a , n ) { let section = [ ] ; for ( let i = 0 ; i < n ; i ++ ) section . push ( a [ i ] ) ; return calculate ( section , 30 ) ; } let N = 4 ; let A = [ 3 , 2 , 5 , 6 ] ; document . write ( minXorValue ( A , N ) ) ;
Program to find the Nth natural number with exactly two bits set | Function to find the Nth number with exactly two bits set ; Keep incrementing until we reach the partition of ' N ' stored in bit_L ; set the rightmost bit based on bit_R ; Driver code
function findNthNum ( N ) { let bit_L = 1 , last_num = 0 ; while ( bit_L * ( bit_L + 1 ) / 2 < N ) { last_num = last_num + bit_L ; bit_L ++ ; } let bit_R = N - last_num - 1 ; document . write ( ( 1 << bit_L ) + ( 1 << bit_R ) + " " ) ; } let N = 13 ; findNthNum ( N ) ;
XOR of all Prime numbers in an Array at positions divisible by K | Javascript program to find XOR of all Prime numbers in an Array at positions divisible by K ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to find the required XOR ; To store XOR of the primes ; Traverse the array ; If the number is a prime ; If index is divisible by k ; Print the xor ; Driver code ; Function call
const MAX = 1000005 ; function SieveOfEratosthenes ( prime ) { prime [ 1 ] = false ; prime [ 0 ] = false ; for ( let p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * 2 ; i < MAX ; i += p ) prime [ i ] = false ; } } } function prime_xor ( arr , n , k ) { let prime = new Array ( MAX ) . fill ( true ) ; SieveOfEratosthenes ( prime ) ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { if ( ( i + 1 ) % k == 0 ) { ans ^= arr [ i ] ; } } } document . write ( ans ) ; } let arr = [ 2 , 3 , 5 , 7 , 11 , 8 ] ; let n = arr . length ; let K = 2 ; prime_xor ( arr , n , K ) ;
Find XOR of all elements in an Array | Function to find the XOR of all elements in the array ; Resultant variable ; Iterating through every element in the array ; Find XOR with the result ; Return the XOR ; Driver Code ; Function call
function xorOfArray ( arr , n ) { let xor_arr = 0 ; for ( let i = 0 ; i < n ; i ++ ) { xor_arr = xor_arr ^ arr [ i ] ; } return xor_arr ; } let arr = [ 3 , 9 , 12 , 13 , 15 ] ; let n = arr . length ; document . write ( xorOfArray ( arr , n ) + " " ) ;
Count of even set bits between XOR of two arrays | Function that count the XOR of B [ ] with all the element in A [ ] having even set bit ; Count the set bits in A [ i ] ; check for even or Odd ; To store the count of element for B [ ] such that XOR with all the element in A [ ] having even set bit ; Count set bit for B [ i ] ; check for Even or Odd ; Driver Code
function countEvenBit ( A , B , n , m ) { let i , j , cntOdd = 0 , cntEven = 0 ; for ( i = 0 ; i < n ; i ++ ) { let x = bitCount ( A [ i ] ) ; if ( x & 1 ) { cntEven ++ ; } else { cntOdd ++ ; } } let CountB = new Array ( m ) ; for ( i = 0 ; i < m ; i ++ ) { let x = bitCount ( B [ i ] ) ; if ( x & 1 ) { CountB [ i ] = cntEven ; } else { CountB [ i ] = cntOdd ; } } for ( i = 0 ; i < m ; i ++ ) { document . write ( CountB [ i ] + " " ) ; } } function bitCount ( x ) { let setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } let A = [ 4 , 2 , 15 , 9 , 8 , 8 ] ; let B = [ 3 , 4 , 22 ] ; countEvenBit ( A , B , 6 , 3 ) ;
Check if a Number is Odd or Even using Bitwise Operators | Returns true if n is even , else odd ; n ^ 1 is n + 1 , then even , else odd ; Driver code
function isEven ( n ) { if ( n ^ 1 == n + 1 ) return true ; else return false ; } let n = 100 ; isEven ( n ) ? document . write ( " " ) : document . write ( " " ) ;
Bitwise OR ( | ) of all even number from 1 to N | Function to return the bitwise OR of all the even numbers upto N ; Initialize result as 2 ; Driver code
function bitwiseOrTillN ( n ) { var result = 2 ; for ( var i = 4 ; i <= n ; i = i + 2 ) { result = result | i ; } return result ; } var n = 10 ; document . write ( bitwiseOrTillN ( n ) ) ;
Bitwise OR ( | ) of all even number from 1 to N | Function to return the bitwise OR of all even numbers upto N ; For value less than 2 ; Count total number of bits in bitwise or all bits will be set except last bit ; Compute 2 to the power bitCount and subtract 2 ; Driver code
function bitwiseOrTillN ( n ) { if ( n < 2 ) return 0 ; var bitCount = parseInt ( Math . log2 ( n ) + 1 ) ; return Math . pow ( 2 , bitCount ) - 2 ; } var n = 10 ; document . write ( bitwiseOrTillN ( n ) ) ;
Generating N | Function to Generating N - bit Gray Code starting from K ; Generate gray code of corresponding binary number of integer i . ; Driver code
function genSequence ( n , val ) { for ( let i = 0 ; i < ( 1 << n ) ; i ++ ) { let x = i ^ ( i >> 1 ) ^ val ; document . write ( x + " " ) ; } } let n = 3 , k = 2 ; genSequence ( n , k ) ;
Winner in the Rock | Function to return the winner of the game ; Both the players chose to play the same move ; Player A wins the game ; Function to perform the queries ; Driver code
function winner ( moves ) { let data = new Map ( ) ; data . set ( ' ' , 0 ) ; data . set ( ' ' , 1 ) ; data . set ( ' ' , 2 ) ; if ( moves [ 0 ] == moves [ 1 ] ) { return " " ; } if ( ( ( data . get ( moves [ 0 ] ) | 1 << ( 2 ) ) - ( data . get ( moves [ 1 ] ) | 0 << ( 2 ) ) ) % 3 != 0 ) { return " " ; } return " " ; } function performQueries ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( winner ( arr [ i ] ) + " " ) ; } let arr = [ " " , " " , " " , " " ] ; let n = arr . length ; performQueries ( arr , n ) ;
Maximum number of consecutive 1 's in binary representation of all the array elements | Function to return the count of maximum consecutive 1 s in the binary representation of x ; Initialize result ; Count the number of iterations to reach x = 0. ; This operation reduces length of every sequence of 1 s by one ; Function to return the count of maximum consecutive 1 s in the binary representation among all the elements of arr ; To store the answer ; For every element of the array ; Count of maximum consecutive 1 s in the binary representation of the current element ; Update the maximum count so far ; Driver code
function maxConsecutiveOnes ( x ) { var count = 0 ; while ( x != 0 ) { x = ( x & ( x << 1 ) ) ; count ++ ; } return count ; } function maxOnes ( arr , n ) { var ans = 0 ; for ( i = 0 ; i < n ; i ++ ) { var currMax = maxConsecutiveOnes ( arr [ i ] ) ; ans = Math . max ( ans , currMax ) ; } return ans ; } var arr = [ 1 , 2 , 3 , 4 ] ; var n = arr . length ; document . write ( maxOnes ( arr , n ) ) ;
Maximum Bitwise OR pair from a range | Function to return the maximum bitwise OR possible among all the possible pairs ; Check for every possible pair ; Maximum among all ( i , j ) pairs ; Driver code
function maxOR ( L , R ) { let maximum = Number . MIN_VALUE ; for ( let i = L ; i < R ; i ++ ) for ( let j = i + 1 ; j <= R ; j ++ ) maximum = Math . max ( maximum , ( i j ) ) ; return maximum ; } let L = 4 , R = 5 ; document . write ( maxOR ( L , R ) ) ;
Maximum Bitwise OR pair from a range | Javascript implementation of the approach ; Function to return the maximum bitwise OR possible among all the possible pairs ; If there is only a single value in the range [ L , R ] ; Loop through each bit from MSB to LSB ; MSBs where the bits differ , all bits from that bit are set ; If MSBs are same , then ans bit is same as that of bit of right or left limit ; Driver code
let MAX = 64 ; function maxOR ( L , R ) { if ( L == R ) { return L ; } let ans = 0 ; for ( let i = MAX - 1 ; i >= 0 ; i -- ) { let p , lbit , rbit ; p = 1 << i ; if ( ( rbit == 1 ) && ( lbit == 0 ) ) { ans += ( p << 1 ) - 1 ; break ; } if ( rbit == 1 ) { ans += p ; } } return ans ; } let L = 4 , R = 5 ; document . write ( maxOR ( L , R ) ) ;
Longest subsequence with a given AND value | O ( N ) | Function to return the required length ; To store the filtered numbers ; Filtering the numbers ; If there are no elements to check ; Find the AND of all the filtered elements ; Check if the AND is equal to m ; Driver code
function findLen ( arr , n , m ) { var filter = [ ] ; for ( var i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & m ) == m ) filter . push ( arr [ i ] ) ; if ( filter . length == 0 ) return 0 ; var c_and = filter [ 0 ] ; for ( var i = 1 ; i < filter . length ; i ++ ) c_and &= filter [ i ] ; if ( c_and == m ) return filter . length ; return 0 ; } var arr = [ 7 , 3 , 3 , 1 , 3 ] ; var n = arr . length ; var m = 3 ; document . write ( findLen ( arr , n , m ) ) ;
Longest sub | Function to return the required length ; To store the filtered numbers ; Filtering the numbers ; If there are no elements to check ; Find the OR of all the filtered elements ; Check if the OR is equal to m ; Driver code
function findLen ( arr , n , m ) { var filter = [ ] ; for ( var i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] m ) == m ) filter . push ( arr [ i ] ) ; if ( filter . length == 0 ) return 0 ; var c_or = filter [ 0 ] ; for ( var i = 1 ; i < filter . length ; i ++ ) c_or |= filter [ i ] ; if ( c_or == m ) return filter . length ; return 0 ; } var arr = [ 7 , 3 , 3 , 1 , 3 ] ; var n = arr . length ; var m = 3 ; document . write ( findLen ( arr , n , m ) ) ;
Program to toggle K | Function to toggle the kth bit of n ; Driver code
function toggleBit ( n , k ) { return ( n ^ ( 1 << ( k - 1 ) ) ) ; } var n = 5 , k = 2 ; document . write ( toggleBit ( n , k ) ) ;
Program to clear K | Function to clear the kth bit of n ; Driver code
function clearBit ( n , k ) { return ( n & ( ~ ( 1 << ( k - 1 ) ) ) ) ; } var n = 5 , k = 1 ; document . write ( clearBit ( n , k ) ) ;
Maximize the Expression | Bit Manipulation | Javascript implementation of the approach ; Function to return the value of the maximized expression ; int can have 32 bits ; Consider the ith bit of D to be 1 ; Calculate the value of ( B AND bitOfD ) ; Check if bitOfD satisfies ( B AND D = D ) ; Check if bitOfD can maximize ( A XOR D ) ; Note that we do not need to consider ith bit of D to be 0 because if above condition are not satisfied then value of result will not change which is similar to considering bitOfD = 0 as result XOR 0 = result ; Driver code
let MAX = 32 ; function maximizeExpression ( a , b ) { let result = a ; for ( let bit = MAX - 1 ; bit >= 0 ; bit -- ) { let bitOfD = 1 << bit ; let x = b & bitOfD ; if ( x == bitOfD ) { let y = result & bitOfD ; if ( y == 0 ) { result = result ^ bitOfD ; } } } return result ; } let a = 11 , b = 14 ; document . write ( maximizeExpression ( a , b ) ) ;
Find number of subarrays with XOR value a power of 2 | Javascript Program to count number of subarrays with Bitwise - XOR as power of 2 ; Function to find number of subarrays ; Hash Map to store prefix XOR values ; When no element is selected ; Check for all the powers of 2 , till a MAX value ; Insert Current prefixxor in Hash Map ; Driver Code
var MAX = 10 ; function findSubarray ( array , n ) { var mp = new Map ( ) ; mp . set ( 0 , 1 ) ; var answer = 0 ; var preXor = 0 ; for ( var i = 0 ; i < n ; i ++ ) { var value = 1 ; preXor ^= array [ i ] ; for ( var j = 1 ; j <= MAX ; j ++ ) { var Y = value ^ preXor ; if ( mp . has ( Y ) ) { answer += mp . get ( Y ) ; } value *= 2 ; } if ( mp . has ( preXor ) ) { mp . set ( preXor , mp . get ( preXor ) + 1 ) ; } else { mp . set ( preXor , 1 ) ; } } return answer ; } var array = [ 2 , 6 , 7 , 5 , 8 ] ; var n = array . length ; document . write ( findSubarray ( array , n ) ) ;
Maximum XOR of Two Numbers in an Array | Function to return the maximum xor ; Calculating xor of each pair ; Driver Code
function max_xor ( arr , n ) { let maxXor = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { maxXor = Math . max ( maxXor , arr [ i ] ^ arr [ j ] ) ; } } return maxXor ; } let arr = [ 25 , 10 , 2 , 8 , 5 , 3 ] ; let n = arr . length ; document . write ( max_xor ( arr , n ) ) ;
Count of triplets that satisfy the given equation | Function to return the count of required triplets ; First element of the current sub - array ; XOR every element of the current sub - array ; If the XOR becomes 0 then update the count of triplets ; Driver code
function CountTriplets ( arr , n ) { let ans = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { let first = arr [ i ] ; for ( let j = i + 1 ; j < n ; j ++ ) { first ^= arr [ j ] ; if ( first == 0 ) ans += ( j - i ) ; } } return ans ; } let arr = [ 2 , 5 , 6 , 4 , 2 ] ; let n = arr . length ; document . write ( CountTriplets ( arr , n ) ) ;