codeparrot/xlcost-text-to-code huggingface dataset:
Minimum window size containing atleast P primes in every window of given range | Function to check that a number is a prime or not in O ( Math . sqrt ( N ) ) ; Loop to check if any number number is divisible by any other number or not ; Function to check whether window size satisfies condition or not ; Loop to check each window of size have atleast P primes ; Checking condition using prefix sum ; Function to find the minimum window size possible for the given range in X and Y ; Prefix array ; Mark those numbers which are primes as 1 ; Convert to prefix sum ; Applying binary search over window size ; Check whether mid satisfies the condition or not ; If satisfies search in first half ; Else search in second half ; Driver Code
function isPrime ( N ) { if ( N < 2 ) return false ; if ( N < 4 ) return true ; if ( ( N & 1 ) == 0 ) return false ; if ( N % 3 == 0 ) return false ; let curr = 5 , s = Math . floor ( Math . sqrt ( N ) ) ; while ( curr <= s ) { if ( N % curr == 0 ) return false ; curr += 2 ; if ( N % curr == 0 ) return false ; curr += 4 ; } return true ; } function check ( s , p , prefix_sum , n ) { let satisfies = true ; for ( let i = 0 ; i < n ; i ++ ) { if ( i + s - 1 >= n ) break ; if ( prefix_sum [ i + s - 1 ] - ( i - 1 >= 0 ? prefix_sum [ i - 1 ] : 0 ) < p ) satisfies = false ; } return satisfies ; } function minimumWindowSize ( x , y , p ) { let prefix_sum = new Array ( y - x + 1 ) . fill ( 0 ) ; for ( let i = x ; i <= y ; i ++ ) { if ( isPrime ( i ) ) prefix_sum [ i - x ] = 1 ; } for ( let i = 1 ; i < y - x + 1 ; i ++ ) prefix_sum [ i ] += prefix_sum [ i - 1 ] ; let low = 1 , high = y - x + 1 ; let mid ; while ( high - low > 1 ) { mid = Math . floor ( ( low + high ) / 2 ) ; if ( check ( mid , p , prefix_sum , y - x + 1 ) ) { high = mid ; } else low = mid ; } if ( check ( low , p , prefix_sum , y - x + 1 ) ) return low ; return high ; } let x = 12 ; let y = 42 ; let p = 3 ; document . write ( minimumWindowSize ( x , y , p ) ) ;
XOR of pairwise sum of every unordered pairs in an array | Function to find XOR of pairwise sum of every unordered pairs ; Loop to choose every possible pairs in the array ; Driver Code
function xorOfSum ( a , n ) { let answer = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) answer ^= ( a [ i ] + a [ j ] ) ; } return answer ; } let n = 3 ; let A = [ 1 , 2 , 3 ] ; document . write ( xorOfSum ( A , n ) ) ;
Maximum size of square such that all submatrices of that size have sum less than K | Size of matrix ; Function to preprocess the matrix for computing the sum of every possible matrix of the given size ; Loop to copy the first row of the matrix into the aux matrix ; Computing the sum column - wise ; Computing row wise sum ; Function to find the sum of a submatrix with the given indices ; Overall sum from the top to right corner of matrix ; Removing the sum from the top corer of the matrix ; Remove the overlapping sum ; Add the sum of top corner which is subtracted twice ; Function to find the maximum square size possible with the such that every submatrix have sum less than the given sum ; Loop to choose the size of matrix ; Loop to find the sum of the matrix of every possible submatrix ; Driver Code
let N = 4 ; let M = 5 ; function preProcess ( mat , aux ) { for ( let i = 0 ; i < M ; i ++ ) aux [ 0 ] [ i ] = mat [ 0 ] [ i ] ; for ( let i = 1 ; i < N ; i ++ ) for ( let j = 0 ; j < M ; j ++ ) aux [ i ] [ j ] = mat [ i ] [ j ] + aux [ i - 1 ] [ j ] ; for ( let i = 0 ; i < N ; i ++ ) for ( let j = 1 ; j < M ; j ++ ) aux [ i ] [ j ] += aux [ i ] [ j - 1 ] ; } function sumQuery ( aux , tli , tlj , rbi , rbj ) { let res = aux [ rbi ] [ rbj ] ; if ( tli > 0 ) res = res - aux [ tli - 1 ] [ rbj ] ; if ( tlj > 0 ) res = res - aux [ rbi ] [ tlj - 1 ] ; if ( tli > 0 && tlj > 0 ) res = res + aux [ tli - 1 ] [ tlj - 1 ] ; return res ; } function maximumSquareSize ( mat , k ) { let aux = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { aux [ i ] = new Array ( M ) ; } preProcess ( mat , aux ) ; for ( let i = Math . min ( N , M ) ; i >= 1 ; i -- ) { let satisfies = true ; for ( let x = 0 ; x < N ; x ++ ) { for ( let y = 0 ; y < M ; y ++ ) { if ( x + i - 1 <= N - 1 && y + i - 1 <= M - 1 ) { if ( sumQuery ( aux , x , y , x + i - 1 , y + i - 1 ) > K ) satisfies = false ; } } } if ( satisfies == true ) return ( i ) ; } return 0 ; } let mat = [ [ 1 , 2 , 3 , 4 , 6 ] , [ 5 , 3 , 8 , 1 , 2 ] , [ 4 , 6 , 7 , 5 , 5 ] , [ 2 , 4 , 8 , 9 , 4 ] ] ; let K = 30 ; document . write ( maximumSquareSize ( mat , K ) ) ;
Find two Fibonacci numbers whose sum can be represented as N | Function to create hash table to check Fibonacci numbers ; Storing the first two numbers in the hash ; Finding Fibonacci numbers up to N and storing them in the hash ; Function to find the Fibonacci pair with the given sum ; creating a set containing all fibonacci numbers ; Traversing all numbers to find first pair ; If both i and ( N - i ) are Fibonacci ; Prleting the pair because i + ( N - i ) = N ; If no fibonacci pair is found whose sum is equal to n ; Driver code
function createHash ( hash , maxElement ) { let prev = 0 , curr = 1 ; hash . add ( prev ) ; hash . add ( curr ) ; while ( curr < maxElement ) { let temp = curr + prev ; hash . add ( temp ) ; prev = curr ; curr = temp ; } } function findFibonacciPair ( n ) { let hash = new Set ( ) ; createHash ( hash , n ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( hash . has ( i ) && hash . has ( n - i ) ) { document . write ( i + " " + ( n - i ) + " " ) ; return ; } } document . write ( " " + " " ) ; } let N = 90 ; findFibonacciPair ( N ) ;
Count of multiples in an Array before every element | Function to find all factors of N and keep their count in map ; Traverse from 1 to sqrt ( N ) if i divides N , increment i and N / i in map ; Function to count of multiples in an Array before every element ; To store factors all of all numbers ; Traverse for all possible i 's ; Printing value of a [ i ] in map ; Now updating the factors of a [ i ] in the map ; Driver code ; Function call
function add_factors ( n , mp ) { for ( var i = 1 ; i <= parseInt ( Math . sqrt ( n ) ) ; i ++ ) { if ( n % i == 0 ) { if ( parseInt ( n / i ) == i ) { if ( mp . has ( i ) ) mp . set ( i , mp . get ( i ) + 1 ) else mp . set ( i , 1 ) } else { if ( mp . has ( i ) ) mp . set ( i , mp . get ( i ) + 1 ) else mp . set ( i , 1 ) if ( mp . has ( parseInt ( n / i ) ) ) mp . set ( parseInt ( n / i ) , mp . get ( parseInt ( n / i ) ) + 1 ) else mp . set ( parseInt ( n / i ) , 1 ) } } } return mp ; } function count_divisors ( a , n ) { var mp = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { document . write ( ( mp . has ( a [ i ] ) ? mp . get ( a [ i ] ) : 0 ) + " " ) ; mp = add_factors ( a [ i ] , mp ) ; } } var arr = [ 8 , 1 , 28 , 4 , 2 , 6 , 7 ] ; var n = arr . length ; count_divisors ( arr , n ) ;
Reduce the array such that each element appears at most 2 times | Function to remove duplicates ; Initialise 2 nd pointer ; Iterate over the array ; Updating the 2 nd pointer ; Driver code ; Function call
function removeDuplicates ( arr , n ) { let st = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( i < n - 2 && arr [ i ] == arr [ i + 1 ] && arr [ i ] == arr [ i + 2 ] ) continue ; else { arr [ st ] = arr [ i ] ; st ++ ; } } document . write ( " " ) ; for ( let i = 0 ; i < st ; i ++ ) { document . write ( arr [ i ] ) ; if ( i != st - 1 ) document . write ( " " ) ; } document . write ( " " ) ; } let arr = [ 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 3 , 3 , 4 , 5 ] ; let n = arr . length ; removeDuplicates ( arr , n ) ;
Check if an Array is a permutation of numbers from 1 to N | Function to check if an array represents a permutation or not ; Set to check the count of non - repeating elements ; Insert all elements in the set ; Calculating the max element ; Check if set size is equal to n ; Driver Code
function permutation ( arr , n ) { let hash = new Set ( ) ; let maxEle = 0 ; for ( let i = 0 ; i < n ; i ++ ) { hash . add ( arr [ i ] ) ; maxEle = Math . max ( maxEle , arr [ i ] ) ; } if ( maxEle != n ) return false ; if ( hash . length == n ) return true ; return false ; } let arr = [ 1 , 2 , 5 , 3 , 2 ] ; let n = arr . length ; if ( permutation ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count of Subsets of a given Set with element X present in it | Javascript code to implement the above approach ; N stores total number of subsets ; Generate each subset one by one ; Check every bit of i ; if j 'th bit of i is set,  check arr[j] with X ; Driver code
function CountSubSet ( arr , n , X ) { let N = Math . pow ( 2 , n ) ; let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( i & ( 1 << j ) ) if ( arr [ j ] == X ) count += 1 ; } } return count ; } let arr = [ 4 , 5 , 6 , 7 ] ; let X = 5 ; let n = arr . length ; document . write ( CountSubSet ( arr , n , X ) ) ;
Count of Subsets of a given Set with element X present in it | Function to calculate ( 2 ^ ( n - 1 ) ) ; Initially initialize answer to 1 ; If e is odd , multiply b with answer ; Function to count subsets in which X element is present ; Check if X is present in given subset or not ; If X is present in set then calculate 2 ^ ( n - 1 ) as count ; if X is not present in a given set ; Driver code
function calculatePower ( b , e ) { let ans = 1 ; while ( e > 0 ) { if ( e % 2 == 1 ) ans = ans * b ; e = e / 2 ; b = b * b ; } return ans ; } function CountSubSet ( arr , n , X ) { let count = 0 , checkX = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == X ) { checkX = 1 ; break ; } } if ( checkX == 1 ) count = calculatePower ( 2 , n - 1 ) ; else count = 0 ; return count ; } let arr = [ 4 , 5 , 6 , 7 ] ; let X = 5 ; let n = arr . length ; document . write ( CountSubSet ( arr , n , X ) ) ;
Value to be subtracted from array elements to make sum of all elements equals K | Function to return the amount of wood collected if the cut is made at height m ; Function that returns Height at which cut should be made ; Sort the heights of the trees ; The minimum and the maximum cut that can be made ; Binary search to find the answer ; The amount of wood collected when cut is made at the mid ; If the current collected wood is equal to the required amount ; If it is more than the required amount then the cut needs to be made at a height higher than the current height ; Else made the cut at a lower height ; Driver code
function woodCollected ( height , n , m ) { let sum = 0 ; for ( let i = n - 1 ; i >= 0 ; i -- ) { if ( height [ i ] - m <= 0 ) break ; sum += ( height [ i ] - m ) ; } return sum ; } function collectKWood ( height , n , k ) { height . sort ( ( a , b ) => a - b ) ; let low = 0 , high = height [ n - 1 ] ; while ( low <= high ) { let mid = low + ( ( high - low ) / 2 ) ; let collected = woodCollected ( height , n , mid ) ; if ( collected == k ) return mid ; if ( collected > k ) low = mid + 1 ; else high = mid - 1 ; } return - 1 ; } let height = [ 1 , 2 , 1 , 2 ] ; let n = height . length ; let k = 2 ; document . write ( collectKWood ( height , n , k ) ) ;
Check if given string contains all the digits | JavaScript implementation of the approach ; Function that returns true if ch is a digit ; Function that returns true if str contains all the digits from 0 to 9 ; To mark the present digits ; For every character of the String ; If the current character is a digit ; Mark the current digit as present ; For every digit from 0 to 9 ; If the current digit is not present in str ; Driver code
let MAX = 10 ; function isDigit ( ch ) { if ( ch >= ' ' && ch <= ' ' ) return true ; return false ; } function allDigits ( str , len ) { let present = Array . from ( { length : MAX } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < len ; i ++ ) { if ( isDigit ( str [ i ] ) ) { let digit = str [ i ] - ' ' ; present [ digit ] = true ; } } for ( let i = 0 ; i < MAX ; i ++ ) { if ( ! present [ i ] ) return false ; } return true ; } let str = " " ; let len = str . length ; if ( allDigits ( str , len ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if a symmetric plus is possible from the elements of the given array | Function that return true if a symmetric is possible with the elements of the array ; Map to store the frequency of the array elements ; Traverse through array elements and count frequencies ; For every unique element ; Element has already been found ; The frequency of the element something other than 0 and 1 ; Driver code
function isPlusPossible ( arr , n ) { var mp = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) else mp . set ( arr [ i ] , 1 ) } var foundModOne = false ; var ans = true ; mp . forEach ( ( value , key ) => { var element = key ; var frequency = value ; if ( frequency % 4 != 0 ) { if ( frequency % 4 == 1 ) { if ( foundModOne ) ans = false foundModOne = true ; } else ans = false } } ) ; return ans ; } var arr = [ 1 , 1 , 1 , 1 , 2 , 2 , 2 , 3 , 2 ] ; var n = arr . length ; if ( isPlusPossible ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Queries for elements greater than K in the given index range using Segment Tree | Merge procedure to merge two vectors into a single vector ; Final vector to return after merging ; Loop continues until it reaches the end of one of the vectors ; Here , simply add the remaining elements to the vector v ; Procedure to build the segment tree ; Reached the leaf node of the segment tree ; Recursively call the buildTree on both the nodes of the tree ; Storing the final vector after merging the two of its sorted child vector ; Query procedure to get the answer for each query l and r are query range ; out of bound or no overlap ; Complete overlap Query range completely lies in the segment tree node range ; binary search to find index of k ; Partially overlap Query range partially lies in the segment tree node range ; Function to perform the queries ; Driver code ; 1 - based indexing ; Number of queries
function merge ( v1 , v2 ) { let i = 0 , j = 0 ; let v = new Array ( ) ; while ( i < v1 . length && j < v2 . length ) { if ( v1 [ i ] <= v2 [ j ] ) { v . push ( v1 [ i ] ) ; i ++ ; } else { v . push ( v2 [ j ] ) ; j ++ ; } } for ( let k = i ; k < v1 . length ; k ++ ) v . push ( v1 [ k ] ) ; for ( let k = j ; k < v2 . length ; k ++ ) v . push ( v2 [ k ] ) ; return v ; } function buildTree ( tree , arr , index , s , e ) { if ( s == e ) { tree [ index ] . push ( arr [ s ] ) ; return ; } let mid = Math . floor ( ( s + e ) / 2 ) ; buildTree ( tree , arr , 2 * index , s , mid ) ; buildTree ( tree , arr , 2 * index + 1 , mid + 1 , e ) ; tree [ index ] = merge ( tree [ 2 * index ] , tree [ 2 * index + 1 ] ) ; } function query ( tree , index , s , e , l , r , k ) { if ( r < s l > e ) return 0 ; if ( s >= l && e <= r ) { return ( tree [ index ] . length - ( lowerBound ( tree [ index ] , tree [ index ] . length , k ) ) ) ; } let mid = Math . floor ( ( s + e ) / 2 ) ; return ( query ( tree , 2 * index , s , mid , l , r , k ) + query ( tree , 2 * index + 1 , mid + 1 , e , l , r , k ) ) ; } function lowerBound ( array , length , value ) { let low = 0 ; let high = length ; while ( low < high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; if ( value <= array [ mid ] ) { high = mid ; } else { low = mid + 1 ; } } return low ; } function performQueries ( L , R , K , n , q , tree ) { for ( let i = 0 ; i < q ; i ++ ) { document . write ( query ( tree , 1 , 0 , n - 1 , L [ i ] - 1 , R [ i ] - 1 , K [ i ] ) + " " ) ; } } let arr = [ 7 , 3 , 9 , 13 , 5 , 4 ] ; let n = arr . length ; let tree = new Array ( ) ; for ( let i = 0 ; i < 4 * n + 1 ; i ++ ) { tree . push ( [ ] ) } buildTree ( tree , arr , 1 , 0 , n - 1 ) ; let L = [ 1 , 2 ] ; let R = [ 4 , 6 ] ; let K = [ 6 , 8 ] ; let q = L . length ; performQueries ( L , R , K , n , q , tree ) ;
Calculate the Sum of GCD over all subarrays | Utility function to calculate sum of gcd of all sub - arrays . ; Fixing the starting index of a subarray ; Fixing the ending index of a subarray ; Finding the GCD of this subarray ; Adding this GCD in our sum ; Driver Code
function findGCDSum ( n , a ) { var GCDSum = 0 ; var tempGCD = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i ; j < n ; j ++ ) { tempGCD = 0 ; for ( k = i ; k <= j ; k ++ ) { tempGCD = __gcd ( tempGCD , a [ k ] ) ; } GCDSum += tempGCD ; } } return GCDSum ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } var n = 5 ; var a = [ 1 , 2 , 3 , 4 , 5 ] ; var totalSum = findGCDSum ( n , a ) ; document . write ( totalSum + " " ) ;
Check if the array has an element which is equal to XOR of remaining elements | Function that returns true if the array contains an element which is equal to the XOR of the remaining elements ; To store the XOR of all the array elements ; For every element of the array ; Take the XOR after excluding the current element ; If the XOR of the remaining elements is equal to the current element ; If no such element is found ; Driver code
function containsElement ( arr , n ) { let xorArr = 0 ; for ( let i = 0 ; i < n ; ++ i ) xorArr ^= arr [ i ] ; for ( let i = 0 ; i < n ; ++ i ) { let x = xorArr ^ arr [ i ] ; if ( arr [ i ] == x ) return true ; } return false ; } let arr = [ 8 , 2 , 4 , 15 , 1 ] ; let n = arr . length ; if ( containsElement ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Square root of a number without using sqrt ( ) function | Recursive function that returns square root of a number with precision upto 5 decimal places ; If mid itself is the square root , return mid ; If mul is less than n , recur second half ; Else recur first half ; Function to find the square root of n ; While the square root is not found ; If n is a perfect square ; Square root will lie in the interval i - 1 and i ; Driver code
function Square ( n , i , j ) { var mid = ( ( i + j ) / 2 ) ; var mul = mid * mid ; if ( ( mul == n ) || ( Math . abs ( mul - n ) < 0.00001 ) ) return mid ; else if ( mul < n ) return Square ( n , mid , j ) ; else return Square ( n , i , mid ) ; } function findSqrt ( n ) { var i = 1 ; var found = false ; while ( ! found ) { if ( i * i == n ) { document . write ( i ) ; found = true ; } else if ( i * i > n ) { var res = Square ( n , i - 1 , i ) ; document . write ( res . toFixed ( 5 ) ) ; found = true ; } i ++ ; } } var n = 3 ; findSqrt ( n ) ;
Pair with largest sum which is less than K in the array | Function to find max sum less than k ; Sort the array ; While l is less than r ; Driver Code ; Function Call
function maxSum ( arr , k ) { arr . sort ( ( a , b ) => a - b ) ; var n = arr . length , l = 0 , r = n - 1 , ans = 0 ; while ( l < r ) { if ( arr [ l ] + arr [ r ] < k ) { ans = Math . max ( ans , arr [ l ] + arr [ r ] ) ; l ++ ; } else { r -- ; } } return ans ; } var A = [ 20 , 10 , 30 , 100 , 110 ] ; var k = 85 ; document . write ( maxSum ( A , k ) ) ;
Length of longest substring having all characters as K | Function to find the length of longest sub - string having all characters same as character K ; Initialize variables ; Iterate till size of string ; Check if current character is K ; Assingning the max value to max_len ; Driver code ; Function call
function length_substring ( S , K ) { let curr_cnt = 0 , prev_cnt = 0 , max_len ; for ( let i = 0 ; i < S . length ; i ++ ) { if ( S [ i ] == K ) { curr_cnt += 1 ; } else { prev_cnt = Math . max ( prev_cnt , curr_cnt ) ; curr_cnt = 0 ; } } prev_cnt = Math . max ( prev_cnt , curr_cnt ) ; max_len = prev_cnt ; return max_len ; } let S = " " ; let K = ' ' ; document . write ( length_substring ( S , K ) ) ;
Find a partition point in array to maximize its xor sum | Function to find partition point in array to maximize xor sum ; Traverse through the array ; Calculate xor of elements left of index i including ith element ; Calculate xor of the elements right of index i ; Keep the maximum possible xor sum ; Return the 1 based index of the array ; Driver code ; Function call
function Xor_Sum ( arr , n ) { let sum = 0 , index , left_xor = 0 , right_xor = 0 ; for ( let i = 0 ; i < n ; i ++ ) { left_xor = left_xor ^ arr [ i ] ; right_xor = 0 ; for ( let j = i + 1 ; j < n ; j ++ ) { right_xor = right_xor ^ arr [ j ] ; } if ( left_xor + right_xor > sum ) { sum = left_xor + right_xor ; index = i ; } } return index + 1 ; } let arr = [ 1 , 4 , 6 , 3 , 8 , 13 , 34 , 2 , 21 , 10 ] ; let n = arr . length ; document . write ( Xor_Sum ( arr , n ) ) ;
Find a partition point in array to maximize its xor sum | Function to calculate Prefix Xor array ; Calculating prefix xor ; Function to find partition point in array to maximize xor sum ; To store prefix xor ; Compute the prefix xor ; To store sum and index ; Calculate the maximum sum that can be obtained splitting the array at some index i ; PrefixXor [ i ] = Xor of all arr elements till i ' th ▁ index ▁ PrefixXor [ n - 1 ] ▁ ▁ ^ ▁ PrefixXor [ i ] ▁ = ▁ Xor ▁ of ▁ all ▁ elements ▁ ▁ from ▁ i + 1' th index to n - 1 'th index ; Return the index ; Driver code ; Function call
function ComputePrefixXor ( arr , PrefixXor , n ) { PrefixXor [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) PrefixXor [ i ] = PrefixXor [ i - 1 ] ^ arr [ i ] ; } function Xor_Sum ( arr , n ) { let PrefixXor = new Array ( n ) ; ComputePrefixXor ( arr , PrefixXor , n ) ; let sum = 0 , index ; for ( let i = 0 ; i < n ; i ++ ) { if ( PrefixXor [ i ] + ( PrefixXor [ n - 1 ] ^ PrefixXor [ i ] ) > sum ) { sum = PrefixXor [ i ] + ( PrefixXor [ n - 1 ] ^ PrefixXor [ i ] ) ; index = i ; } } return index + 1 ; } let arr = [ 1 , 4 , 6 , 3 , 8 , 13 , 34 , 2 , 21 , 10 ] ; let n = arr . length ; document . write ( Xor_Sum ( arr , n ) ) ;
Find the color of given node in an infinite binary tree | Function to find color of the node ; Maximum is to store maximum color ; Loop to check all the parent values to get maximum color ; Find the number into map and get maximum color ; Take the maximum color and assign into maximum variable ; Find parent index ; Return maximum color ; Function to build hash map with color ; To store color of each node ; For each number add a color number ; Assigning color ; Return hash map ; Driver code ; Build mapWithColor ; Print the maximum color
function findColor ( mapWithColor , query ) { let maximum = 0 ; while ( query >= 1 ) { if ( mapWithColor . has ( query ) ) { maximum = Math . max ( maximum , mapWithColor . get ( query ) ) ; } if ( query % 2 == 1 ) query = ( query - 1 ) / 2 ; else query = query / 2 ; } return maximum ; } function buildMapWithColor ( arr , n ) { let mapWithColor = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { mapWithColor . set ( arr [ i ] , i + 1 ) ; } return mapWithColor ; } let arr = [ 3 , 2 , 1 , 3 ] ; let n = arr . length ; let k = 7 ; let mapWithColor = buildMapWithColor ( arr , n ) ; document . write ( findColor ( mapWithColor , k ) ) ;