codeparrot/xlcost-text-to-code huggingface dataset:
Find the nearest power of 2 for every array element | Function to find the nearest power of two for every element of the given array ; Traverse the array ; Calculate log of the current array element ; Find the nearest ; Driver Code
function nearestPowerOfTwo ( arr , N ) { for ( i = 0 ; i < N ; i ++ ) { var lg = parseInt ( ( Math . log ( arr [ i ] ) / Math . log ( 2 ) ) ) ; var a = parseInt ( ( Math . pow ( 2 , lg ) ) ) ; var b = parseInt ( ( Math . pow ( 2 , lg + 1 ) ) ) ; if ( ( arr [ i ] - a ) < ( b - arr [ i ] ) ) document . write ( a + " " ) ; else document . write ( b + " " ) ; } } var arr = [ 5 , 2 , 7 , 12 ] ; var N = arr . length ; nearestPowerOfTwo ( arr , N ) ;
Flip bits of the sum of count of set bits of two given numbers | Function to count number of set bits in integer ; Variable for counting set bits ; Function to invert bits of a number ; Calculate number of bits of N - 1 ; ; Function to invert the sum of set bits in A and B ; Stores sum of set bits ; Driver Code
function countSetBits ( n ) { var count = 0 ; while ( n != 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } function invertBits ( n ) { var x = parseInt ( ( Math . log ( n ) / Math . log ( 2 ) ) ) ; var m = 1 << x ; m = m | m - 1 ; n = n ^ m ; return n ; } function invertSum ( A , B ) { var temp = countSetBits ( A ) + countSetBits ( B ) ; document . write ( invertBits ( temp ) ) ; } var A = 5 ; var B = 7 ; invertSum ( A , B ) ;
Sum of Bitwise XOR of each array element with all other array elements | Function to calculate for each array element , sum of its Bitwise XOR with all other array elements ; Declare an array of size 64 to store count of each bit ; Traversing the array ; Check if bit is present of not ; Increase the bit position ; Reduce the number to half ; Traverse the array ; Stores the bit position ; Stores the sum of Bitwise XOR ; Check if bit is present of not ; Reduce the number to its half ; Print the sum for A [ i ] ; Given array ; Given N ; Function Call
function XOR_for_every_i ( A , N ) { let frequency_of_bits = new Uint8Array ( 32 ) ; for ( let i = 0 ; i < N ; i ++ ) { let bit_position = 0 ; let M = A [ i ] ; while ( M != 0 ) { if ( ( M & 1 ) != 0 ) { frequency_of_bits [ bit_position ] += 1 ; } bit_position += 1 ; M >>= 1 ; } } for ( let i = 0 ; i < N ; i ++ ) { let M = A [ i ] ; let value_at_that_bit = 1 ; let XOR_sum = 0 ; for ( let bit_position = 0 ; bit_position < 32 ; bit_position ++ ) { if ( ( M & 1 ) != 0 ) { XOR_sum += ( N - frequency_of_bits [ bit_position ] ) * value_at_that_bit ; } else { XOR_sum += ( frequency_of_bits [ bit_position ] ) * value_at_that_bit ; } M >>= 1 ; value_at_that_bit <<= 1 ; } document . write ( XOR_sum + " " ) ; } return ; } let A = [ 1 , 2 , 3 ] ; let N = A . length ; XOR_for_every_i ( A , N ) ;
Minimize the maximum difference of any pair by doubling odd elements and reducing even elements by half | Function to minimize the maximum difference between any pair of elements of the array by the given operations ; Traverse the array ; If current element is even ; Insert it into even ; Otherwise ; Make it even by multiplying by 2 and insert it into set ; Calculate difference between first and the last element of the set ; Iterate until difference is minimized ; Erase the current element ; Reduce current element by half and insert it into the Set ; Update difference ; Return the resultant difference ; Driver Code
function minimumMaxDiff ( nums ) { var s = new Set ( ) ; for ( var i = 0 ; i < nums . length ; i ++ ) { if ( nums [ i ] % 2 == 0 ) s . add ( nums [ i ] ) ; else s . add ( nums [ i ] * 2 ) ; } var tmp = [ ... s ] . sort ( ( a , b ) => a - b ) var res = tmp [ tmp . length - 1 ] - tmp [ 0 ] ; while ( tmp [ tmp . length - 1 ] % 2 == 0 ) { var x = tmp [ tmp . length - 1 ] ; s . delete ( x ) ; s . add ( parseInt ( x / 2 ) ) ; tmp = [ ... s ] . sort ( ( a , b ) => a - b ) res = Math . min ( res , tmp [ tmp . length - 1 ] - tmp [ 0 ] ) ; } return res ; } var arr = [ 1 , 2 , 5 , 9 ] ; document . write ( minimumMaxDiff ( arr ) ) ;
XOR of all even numbers from a given range | Function to calculate XOR of numbers in the range [ 1 , n ] ; If n is divisible by 4 ; If n mod 4 is equal to 1 ; If n mod 4 is equal to 2 ; Function to find XOR of even numbers in the range [ l , r ] ; Stores XOR of even numbers in the range [ 1 , l - 1 ] ; Stores XOR of even numbers in the range [ 1 , r ] ; Update xor_r ; Update xor_l ; Driver Code
function bitwiseXorRange ( n ) { if ( n % 4 == 0 ) return n ; if ( n % 4 == 1 ) return 1 ; if ( n % 4 == 2 ) return n + 1 ; return 0 ; } function evenXorRange ( l , r ) { let xor_l ; let xor_r ; xor_r = 2 * bitwiseXorRange ( Math . floor ( r / 2 ) ) ; xor_l = 2 * bitwiseXorRange ( Math . floor ( ( l - 1 ) / 2 ) ) ; return xor_l ^ xor_r ; } let l = 10 ; let r = 20 ; document . write ( evenXorRange ( l , r ) ) ;
Program to calculate Variance of first N Natural Numbers | Function to calculate Variance of first N natural numbers ; Driver Code
function find_Variance ( n ) { var numerator = n * n - 1 var ans = ( numerator * 1.0 ) / 12 return ans } var N = 5 ; document . write ( find_Variance ( N ) . toFixed ( 6 ) ) ;
Count digits present in each element of a given Matrix | JavaScript program to implement the above approach ; Function to count the number of digits in each element of the given matrix ; Traverse each row of arr [ ] [ ] ; Traverse each column of arr [ ] [ ] ; Store the current matrix element ; Count the number of digits ; Prlet the result ; Given matrix
let M = 3 ; let N = 3 ; function countDigit ( arr ) { for ( let i = 0 ; i < M ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) { let X = arr [ i ] [ j ] ; let d = ( Math . floor ( Math . log10 ( X ) * 1.0 ) + 1 ) ; document . write ( d + " " ) ; } document . write ( " " ) ; } } let arr = [ [ 27 , 173 , 5 ] , [ 21 , 6 , 624 ] , [ 5 , 321 , 49 ] ] ; countDigit ( arr ) ;
Difference between sum of odd and even frequent elements in an Array | Function to find the sum of all even and odd frequent elements in an array ; Stores the frequency of array elements ; Traverse the array ; Update frequency of current element ; Stores sum of odd and even frequent elements ; Traverse the map ; If frequency is odd ; Add sum of all occurrences of current element to sum_odd ; If frequency is even ; Add sum of all occurrences of current element to sum_even ; Calculate difference between their sum ; Return diff ; Driver Code
function findSum ( arr , N ) { var mp = { } ; for ( let i = 0 ; i < N ; i ++ ) mp [ arr [ i ] ] = 0 ; for ( let i = 0 ; i < N ; i ++ ) { mp [ arr [ i ] ] ++ ; } var sum_odd = 0 , sum_even = 0 ; for ( let itr in mp ) { if ( mp [ itr ] % 2 != 0 ) { sum_odd += ( itr ) * ( mp [ itr ] ) ; } if ( mp [ itr ] % 2 == 0 ) { sum_even += ( itr ) * ( mp [ itr ] ) ; } } var diff = sum_even - sum_odd ; return diff ; } var arr = new Array ( 1 , 5 , 5 , 2 , 4 , 3 , 3 ) ; var N = arr . length ; console . log ( findSum ( arr , N ) ) ;
Count N | following the given condition ; Initialize answer ; Calculate nPm ; Prvar ans ; Given N and M ; Function Call
function noOfArraysPossible ( N , M ) { var ans = 1 ; for ( i = 0 ; i < N ; ++ i ) { ans = ans * ( M - i ) ; } document . write ( ans ) ; } var N = 2 , M = 3 ; noOfArraysPossible ( N , M ) ;
Permutations of an array having sum of Bitwise AND of adjacent elements at least K | Function to print all permutations of arr [ ] such that the sum of Bitwise AND of all adjacent element is at least K ; To check if any permutation exists or not ; Sort the given array ; Find all possible permutations ; Stores the sum of bitwise AND of adjacent elements of the current permutation ; Traverse the current permutation of arr [ ] ; Update the sum ; If the sum is at least K , then print the current permutation ; Set the flag variable ; Print the current permutation ; If flag is unset , then print - 1 ; Driver code ; Function Call
function printPermutation ( arr , n , k ) { let flag = false ; arr . sort ( ) ; do { let sum = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { sum += arr [ i ] & arr [ i + 1 ] ; } if ( sum >= k ) { flag = true ; for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; } } while ( next_permutation ( arr ) ) ; if ( flag == false ) { System . out . print ( " " ) ; } } function next_permutation ( p ) { for ( let a = p . length - 2 ; a >= 0 ; -- a ) if ( p [ a ] < p [ a + 1 ] ) for ( let b = p . length - 1 ; ; -- b ) if ( p [ b ] > p [ a ] ) { let t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } return false ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let K = 8 ; let N = arr . length ; printPermutation ( arr , N , K ) ;
Sum of the first N terms of XOR Fibonacci series | Function to calculate the sum of the first N terms of XOR Fibonacci Series ; Base Case ; Stores the sum of the first N terms ; Iterate from [ 0 , n - 3 ] ; Store XOR of last 2 elements ; Update sum ; Update the first element ; Update the second element ; Prlet the const sum ; Driver Code ; Function Call
function findSum ( a , b , n ) { if ( n == 1 ) { document . write ( a ) ; return ; } let s = a + b ; for ( i = 0 ; i < n - 2 ; i ++ ) { let x = a ^ b ; s += x ; a = b ; b = x ; } document . write ( s ) ; } let a = 2 , b = 5 , N = 8 ; findSum ( a , b , N ) ;
Sum of the first N terms of XOR Fibonacci series | Function to calculate sum of the first N terms of XOR Fibonacci Series ; Store the sum of first n terms ; Store XOR of a and b ; Case 1 : If n is divisible by 3 ; Case 2 : If n % 3 leaves remainder 1 ; Case 3 : If n % 3 leaves remainder 2 on division by 3 ; Print the final sum ; Driver Code ; Function Call
function findSum ( a , b , n ) { var sum = 0 ; var x = a ^ b ; if ( n % 3 == 0 ) { sum = parseInt ( n / 3 ) * ( a + b + x ) ; } else if ( n % 3 == 1 ) { sum = parseInt ( n / 3 ) * ( a + b + x ) + a ; } else { sum = parseInt ( n / 3 ) * ( a + b + x ) + a + b ; } document . write ( sum ) ; } var a = 2 , b = 5 , N = 8 ; findSum ( a , b , N ) ;
Length of the longest subarray whose Bitwise XOR is K | Function to find the length of the longest subarray whose bitwise XOR is equal to K ; Stores prefix XOR of the array ; Stores length of longest subarray having bitwise XOR equal to K ; Stores index of prefix XOR of the array ; Insert 0 into the map ; Traverse the array ; Update prefixXOR ; If ( prefixXOR ^ K ) present in the map ; Update maxLen ; If prefixXOR not present in the Map ; Insert prefixXOR into the map ; Driver Code
function LongestLenXORK ( arr , N , K ) { var prefixXOR = 0 ; var maxLen = 0 ; var mp = new Map ( ) ; mp . set ( 0 , - 1 ) ; for ( var i = 0 ; i < N ; i ++ ) { prefixXOR ^= arr [ i ] ; if ( mp . has ( prefixXOR ^ K ) ) { maxLen = Math . max ( maxLen , ( i - mp . get ( prefixXOR ^ K ) ) ) ; } if ( ! mp . has ( prefixXOR ) ) { mp . set ( prefixXOR , i ) ; } } return maxLen ; } var arr = [ 1 , 2 , 4 , 7 , 2 ] ; var N = arr . length ; var K = 1 ; document . write ( LongestLenXORK ( arr , N , K ) ) ;
Minimum value to be added to maximize Bitwise XOR of the given array | Function to find complement of an integer ; Count the number of bits of maxElement ; Return 1 's complement ; Function to find the value required to be added to maximize XOR of the given array ; Stores the required value to be added to the array ; Stores the maximum array element ; Traverse the array ; Update XOR of the array ; Find maximum element in array ; Calculate 1 s ' complement ; Return the answer ; Driver Code
function onesComplement ( n , maxElement ) { let bits = Math . floor ( ( Math . log ( maxElement ) / Math . log ( 2 ) ) ) + 1 ; return ( ( 1 << bits ) - 1 ) ^ n ; } function findNumber ( arr , n ) { let res = 0 ; let maxElement = 0 ; for ( let i = 0 ; i < n ; i ++ ) { res = res ^ arr [ i ] ; if ( maxElement < arr [ i ] ) maxElement = arr [ i ] ; } res = onesComplement ( res , maxElement ) ; return ( res ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let N = arr . length ; document . write ( findNumber ( arr , N ) ) ;
Count ways to select K array elements lying in a given range | Function to calculate factorial of all the numbers up to N ; Factorial of 0 is 1 ; Calculate factorial of all the numbers upto N ; Calculate factorial of i ; Function to find the count of ways to select at least K elements whose values in range [ L , R ] ; Stores count of ways to select at least K elements whose values in range [ L , R ] ; Stores count of numbers having value lies in the range [ L , R ] ; Traverse the array ; Checks if the array elements lie in the given range ; Update cntNum ; Stores factorial of numbers upto N ; Calculate total ways to select at least K elements whose values lies in [ L , R ] ; Update cntWays ; Driver Code
function calculateFactorial ( N ) { var fact = Array ( N + 1 ) ; fact [ 0 ] = 1 ; for ( var i = 1 ; i <= N ; i ++ ) { fact [ i ] = fact [ i - 1 ] * i ; } return fact ; } function cntWaysSelection ( arr , N , K , L , R ) { var cntWays = 0 ; var cntNum = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= L && arr [ i ] <= R ) { cntNum ++ ; } } var fact = calculateFactorial ( cntNum ) ; for ( var i = K ; i <= cntNum ; i ++ ) { cntWays += fact [ cntNum ] / ( fact [ i ] * fact [ cntNum - i ] ) ; } return cntWays ; } var arr = [ 12 , 4 , 6 , 13 , 5 , 10 ] ; var N = arr . length ; var K = 3 ; var L = 4 ; var R = 10 ; document . write ( cntWaysSelection ( arr , N , K , L , R ) ) ;
Bitwise AND of all unordered pairs from a given array | Function to calculate bitwise AND of all pairs from the given array ; Stores bitwise AND of all possible pairs ; Generate all possible pairs ; Calculate bitwise AND of each pair ; Driver Code
function TotalAndPair ( arr , N ) { let totalAND = ( 1 << 30 ) - 1 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { totalAND &= arr [ i ] & arr [ j ] ; } } return totalAND ; } let arr = [ 4 , 5 , 12 , 15 ] ; let N = arr . length ; document . write ( TotalAndPair ( arr , N ) ) ;
Modify N by adding its smallest positive divisor exactly K times | Function to find the smallest divisor of N greater than 1 ; If i is a divisor of N ; If N is a prime number ; Function to find the value of N by performing the operations K times ; Iterate over the range [ 1 , K ] ; Update N ; Driver Code
function smallestDivisorGr1 ( N ) { for ( let i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { return i ; } } return N ; } function findValOfNWithOperat ( N , K ) { for ( let i = 1 ; i <= K ; i ++ ) { N += smallestDivisorGr1 ( N ) ; } return N ; } let N = 6 , K = 4 ; document . write ( findValOfNWithOperat ( N , K ) ) ;
Sum of all possible triplet products from given ranges | JavaScript program to implement the above approach ; Function to find the sum of all possible triplet products ( i * j * k ) ; Stores sum required sum ; Iterate over all possible values of i ; Iterate over all possible values of j ; Iterate over all possible values of k ; Stores the product of ( i * j * k ) ; Update sum ; Driver Code
let M = 1000000007 ; function findTripleSum ( A , B , C ) { let sum = 0 ; for ( let i = 1 ; i <= A ; i ++ ) { for ( let j = 1 ; j <= B ; j ++ ) { for ( let k = 1 ; k <= C ; k ++ ) { let prod = ( ( ( i % M ) * ( j % M ) ) % M * ( k % M ) ) % M ; sum = ( sum + prod ) % M ; } } } return sum ; } let A = 10 ; let B = 100 ; let C = 1000 ; document . write ( findTripleSum ( A , B , C ) ) ;
Maximize minimum of array generated by maximums of same indexed elements of two rows of a given Matrix | Function to find the maximum of minimum of array constructed from any two rows of the given matrix ; Initialize global max as let_MIN ; Iterate through the rows ; Iterate through remaining rows ; Initialize row_min as let_MAX ; Iterate through the column values of two rows ; Find max of two elements ; Update the row_min ; Update the global_max ; Prlet the global max ; Given matrix mat [ ] [ ] ; Given number of rows and columns ; Function Call
function getMaximum ( N , M , mat ) { let global_max = Number . MIN_VALUE ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { let row_min = Number . MAX_VALUE ; for ( let k = 0 ; k < M ; k ++ ) { let m = Math . max ( mat [ i ] [ k ] , mat [ j ] [ k ] ) ; row_min = Math . min ( row_min , m ) ; } global_max = Math . max ( global_max , row_min ) ; } } return global_max ; } let mat = [ [ 5 , 0 , 3 , 1 , 2 ] , [ 1 , 8 , 9 , 1 , 3 ] , [ 1 , 2 , 3 , 4 , 5 ] , [ 9 , 1 , 0 , 3 , 7 ] , [ 2 , 3 , 0 , 6 , 3 ] , [ 6 , 4 , 1 , 7 , 0 ] ] ; let N = 6 , M = 5 ; document . write ( getMaximum ( N , M , mat ) ) ;
Maximize sum of MEX values of each node in an N | Function to create an N - ary Tree ; Traverse the edges ; Add edges ; Function to get the maximum sum of MEX values of tree rooted at 1 ; Initialize mex ; Iterate through all children of node ; Recursively find maximum sum of MEX values of each node in tree rooted at u ; Store the maximum sum of MEX of among all subtrees ; Increase the size of tree rooted at current node ; Resulting MEX for the current node of the recursive call ; Given N nodes ; Given N - 1 edges ; Stores the tree ; Generates the tree ; Returns maximum sum of MEX values of each node
function makeTree ( tree , edges , N ) { for ( var i = 0 ; i < N - 1 ; i ++ ) { var u = edges [ i ] [ 0 ] ; var v = edges [ i ] [ 1 ] ; tree [ u ] . push ( v ) ; } } function dfs ( node , tree ) { var mex = 0 ; var size = 1 ; tree [ node ] . forEach ( u => { var temp = dfs ( u , tree ) ; mex = Math . max ( mex , temp [ 0 ] ) ; size += temp [ 1 ] ; } ) ; return [ mex + size , size ] ; } var N = 7 ; var edges = [ [ 1 , 4 ] , [ 1 , 5 ] , [ 5 , 2 ] , [ 5 , 3 ] , [ 4 , 7 ] , [ 7 , 6 ] ] ; var tree = Array . from ( Array ( N + 1 ) , ( ) => Array ( ) ) ; makeTree ( tree , edges , N ) ; document . write ( dfs ( 1 , tree ) [ 0 ] ) ;
Minimum value exceeding X whose count of divisors has different parity with count of divisors of X | Function to count divisors of n ; Function to find the minimum value exceeding x whose count of divisors has different parity with count of divisors of X ; Divisor count of x ; Iterate from x + 1 and check for each element ; Given X ; Function call
function divisorCount ( n ) { let x = 0 ; for ( let i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == n / i ) x ++ ; else x += 2 ; } } return x ; } function minvalue_y ( x ) { let a = divisorCount ( x ) ; let y = x + 1 ; while ( ( a & 1 ) == ( divisorCount ( y ) & 1 ) ) y ++ ; return y ; } let x = 5 ; document . write ( minvalue_y ( x ) ) ;
Minimum value exceeding X whose count of divisors has different parity with count of divisors of X | Function to find the minimum value exceeding x whose count of divisors has different parity with count of divisors of X ; Check if x is perfect square ; Driver code
function minvalue_y ( x ) { let n = Math . floor ( Math . sqrt ( x ) ) ; if ( n * n == x ) return x + 1 ; return Math . floor ( Math . pow ( n + 1 , 2 ) ) ; } let x = 5 ; document . write ( minvalue_y ( x ) ) ;
Sum of first N natural numbers with alternate signs | Function to find the sum of first N natural numbers with alternate signs ; Stores sum of alternate sign of first N natural numbers ; If N is an even number ; Update alternateSum ; If N is an odd number ; Update alternateSum ; Driver Code
function alternatingSumOfFirst_N ( N ) { var alternateSum = 0 ; if ( N % 2 == 0 ) { alternateSum = ( - N ) / 2 ; } else { alternateSum = ( N + 1 ) / 2 ; } return alternateSum ; } var N = 6 ; document . write ( alternatingSumOfFirst_N ( N ) ) ;
Minimum value to be added to the prefix sums at each array indices to make them positive | Function to find minimum startValue for positive prefix sum at each index ; Store the minimum prefix sum ; Stores prefix sum at each index ; Traverse over the array ; Update the prefix sum ; Update the minValue ; Return the positive start value ; Driver Code ; Function Call
function minStartValue ( nums ) { let minValue = 0 ; let sum = 0 ; for ( let n = 0 ; n < nums . length ; n ++ ) { sum += nums [ n ] ; minValue = Math . min ( minValue , sum ) ; } let startValue = 1 - minValue ; return startValue ; } let nums = [ - 3 , 2 , - 3 , 4 , 2 ] ; document . write ( minStartValue ( nums ) ) ;
Count ways to split array into two equal sum subarrays by replacing each array element to 0 once | Function to find number of ways to split array into 2 subarrays having equal sum by changing element to 0 once ; Stores the count of elements in prefix and suffix of array elements ; Stores the sum of array ; Traverse the array ; Increase the frequency of current element in suffix ; Stores prefix sum upto index i ; Stores sum of suffix of index i ; Stores the desired result ; Traverse the array ; Modify prefix sum ; Add arr [ i ] to prefix map ; Calculate suffix sum by subtracting prefix sum from total sum of elements ; Remove arr [ i ] from suffix map ; Store the difference between the subarrays ; Count number of ways to split the array at index i such that subarray sums are equal ; Update the final result ; Return the result ; Driver Code ; Function Call
function countSubArrayRemove ( arr , N ) { let prefix_element_count = new Map ( ) ; let suffix_element_count = new Map ( ) ; let total_sum_of_elements = 0 ; for ( let i = N - 1 ; i >= 0 ; i -- ) { total_sum_of_elements += arr [ i ] ; if ( ! suffix_element_count . has ( arr [ i ] ) ) suffix_element_count . set ( arr [ i ] , 1 ) ; else suffix_element_count . set ( arr [ i ] , suffix_element_count . get ( arr [ i ] ) + 1 ) ; } let prefix_sum = 0 ; let suffix_sum = 0 ; let count_subarray_equal_sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { prefix_sum += arr [ i ] ; if ( ! prefix_element_count . has ( arr [ i ] ) ) prefix_element_count . set ( arr [ i ] , 1 ) ; else prefix_element_count . set ( arr [ i ] , prefix_element_count . get ( arr [ i ] ) + 1 ) ; suffix_sum = total_sum_of_elements - prefix_sum ; if ( ! suffix_element_count . has ( arr [ i ] ) ) suffix_element_count . set ( arr [ i ] , 0 ) ; else suffix_element_count . set ( arr [ i ] , suffix_element_count . get ( arr [ i ] ) - 1 ) ; let difference = prefix_sum - suffix_sum ; let number_of_subarray_at_i_split = 0 ; if ( prefix_element_count . has ( difference ) ) number_of_subarray_at_i_split = prefix_element_count . get ( difference ) ; if ( suffix_element_count . has ( - difference ) ) number_of_subarray_at_i_split += suffix_element_count . get ( - difference ) ; count_subarray_equal_sum += number_of_subarray_at_i_split ; } return count_subarray_equal_sum ; } let arr = [ 1 , 2 , 1 , 1 , 3 , 1 ] ; let N = arr . length ; document . write ( countSubArrayRemove ( arr , N ) ) ;
Count set bits in Bitwise XOR of all adjacent elements upto N | Function to count of set bits in Bitwise XOR of adjacent elements up to N ; Stores count of set bits by Bitwise XOR on adjacent elements of [ 0 , N ] ; Stores all possible values on right most set bit over [ 0 , N ] ; Iterate over the range [ 0 , N ] ; Update N ; Update bit_Position ; Driver Code
function countXORSetBitsAdjElemRange1_N ( N ) { let total_set_bits = 0 ; let bit_Position = 1 ; while ( N ) { total_set_bits += ( Math . floor ( ( N + 1 ) / 2 ) * bit_Position ) ; N -= Math . floor ( ( N + 1 ) / 2 ) ; bit_Position ++ ; } return total_set_bits ; } let N = 4 ; document . write ( countXORSetBitsAdjElemRange1_N ( N ) ) ;