codeparrot/xlcost-text-to-code huggingface dataset:
Count triplets from an array which can form quadratic equations with real roots | Function to count the number of triplets ( a , b , c ) such that the equation ax ^ 2 + bx + c = 0 has real roots ; Sort the array in ascending order ; Stores count of triplets ( a , b , c ) such that ax ^ 2 + bx + c = 0 has real roots ; Base case ; Traverse the given array ; If values of a and c are equal to b ; Increment a ; Decrement c ; Condition for having real roots for a quadratic equation ; If b lies in between a and c ; Update count ; Update count ; Increment a ; Decrement c ; For each pair two values are possible of a and c ; Driver Code ; Function Call
function getCount ( arr , N ) { arr . sort ( ) ; var count = 0 ; if ( N < 3 ) return 0 ; for ( var b = 0 ; b < N ; b ++ ) { var a = 0 , c = N - 1 ; var d = arr [ b ] * arr [ b ] / 4 ; while ( a < c ) { if ( a == b ) { a ++ ; continue ; } if ( c == b ) { c -- ; continue ; } if ( arr [ a ] * arr <= d ) { if ( a < b && b < c ) { count += c - a - 1 ; } else { count += c - a ; } a ++ ; } else { c -- ; } } } return count * 2 ; } var arr = [ 3 , 6 , 10 , 13 , 21 ] ; var N = arr . length ; document . write ( getCount ( arr , N ) ) ;
Maximize difference between the sum of absolute differences of each element with the remaining array | Function to maximize difference of the sum of absolute difference of an element with the rest of the elements in the array ; Sort the array in ascending order ; Stores prefix sum at any instant ; Store the total array sum ; Initialize minimum and maximum absolute difference ; Traverse the array to find the total array sum ; Traverse the array arr [ ] ; Store the number of elements to its left ; Store the number of elements to its right ; Update the sum of elements on its left ; Store the absolute difference sum ; Update the Minimum ; Update the Maximum ; Update sum of elements on its left ; Prlet the result ; Driver Code
function findMaxDifference ( arr , n ) { arr . sort ( ) ; let Leftsum = 0 ; let Totalsum = 0 ; let Min = Number . MAX_VALUE , Max = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) Totalsum += arr [ i ] ; for ( let i = 0 ; i < n ; i ++ ) { let leftNumbers = i ; let rightNumbers = n - i - 1 ; Totalsum = Totalsum - arr [ i ] ; let sum = ( leftNumbers * arr [ i ] ) - Leftsum + Totalsum - ( rightNumbers * arr [ i ] ) ; Min = Math . min ( Min , sum ) ; Max = Math . max ( Max , sum ) ; Leftsum += arr [ i ] ; } document . write ( Max - Min ) ; } let arr = [ 1 , 2 , 4 , 7 ] ; let N = arr . length ; findMaxDifference ( arr , N ) ;
Minimum pairs required to be removed such that the array does not contain any pair with sum K | Function to find the maximum count of pairs required to be removed such that no pairs exist whose sum equal to K ; Stores maximum count of pairs required to be removed such that no pairs exist whose sum equal to K ; Base Case ; Sort the array ; Stores index of left pointer ; Stores index of right pointer ; Stores sum of left and right pointer ; If s equal to k ; Update cntPairs ; Update left ; Update right ; If s > k ; Update right ; Update left ; Return the cntPairs ; Driver Code ; Function call
function maxcntPairsSumKRemoved ( arr , k ) { var cntPairs = 0 ; if ( arr . length <= 1 ) return cntPairs ; arr . sort ( ) ; var left = 0 ; var right = arr . length - 1 ; while ( left < right ) { var s = arr [ left ] + arr [ right ] ; if ( s == k ) { cntPairs += 1 ; left += 1 ; right -= 1 ; } else if ( s > k ) right -= 1 ; else left += 1 ; } return cntPairs ; } var arr = [ 1 , 2 , 3 , 4 ] ; var K = 5 ; document . write ( maxcntPairsSumKRemoved ( arr , K ) ) ;
Minimize cost to split an array into K subsets such that the cost of each element is its product with its position in the subset | Reverses an array ; Function to find the minimum cost to split array into K subsets ; Sort the array in descending order ; Stores minimum cost to split the array into K subsets ; Stores position of elements of a subset ; Iterate over the range [ 1 , N ] ; Calculate the cost to select X - th element of every subset ; Update min_cost ; Update X ; Driver code ; Function call
function reverse ( a , n ) { var i , k , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } function getMinCost ( arr , n , k ) { arr . sort ( ( a , b ) => b - a ) ; var min_cost = 0 ; var X = 0 ; for ( var i = 0 ; i < n ; i += k ) { for ( var j = i ; j < i + k && j < n ; j ++ ) { min_cost += arr [ j ] * ( X + 1 ) ; } X ++ ; } return min_cost ; } var arr = [ 9 , 20 , 7 , 8 ] ; var K = 2 ; var N = arr . length ; document . write ( getMinCost ( arr , N , K ) ) ;
Minimize difference between the largest and smallest array elements by K replacements | Function to find minimum difference between largest and smallest element after K replacements ; Sort array in ascending order ; Length of array ; Minimum difference ; Check for all K + 1 possibilities ; Return answer ; Given array ; Prints the minimum possible difference
function minDiff ( A , K ) { A . sort ( function ( a , b ) { return a - b } ) ; let n = A . length ; if ( n <= K ) return 0 ; let mindiff = A [ n - 1 ] - A [ 0 ] ; if ( K == 0 ) return mindiff ; for ( let i = 0 , j = n - 1 - K ; j < n ; ) { mindiff = Math . min ( mindiff , A [ j ] - A [ i ] ) ; i ++ ; j ++ ; } return mindiff ; } let A = [ - 1 , 3 , - 1 , 8 , 5 , 4 ] ; let K = 3 ; document . write ( minDiff ( A , K ) ) ;
Minimize difference between the largest and smallest array elements by K replacements | Function to find minimum difference between the largest and smallest element after K replacements ; Create a MaxHeap ; Create a MinHeap ; Update maxHeap and MinHeap with highest and smallest K elements respectively ; Insert current element into the MaxHeap ; If maxHeap size exceeds K + 1 ; Remove top element ; Insert current element into the MaxHeap ; If maxHeap size exceeds K + 1 ; Remove top element ; Store all max element from maxHeap ; Store all min element from minHeap ; Generating all K + 1 possibilities ; Return answer ; Given array ; Function call
function minDiff ( A , K , N ) { if ( N <= K + 1 ) return 0 ; var maxHeap = [ ] ; var minHeap = [ ] ; for ( var i = 0 ; i < N ; i ++ ) { maxHeap . push ( A [ i ] ) ; maxHeap . sort ( ( a , b ) => b - a ) ; if ( maxHeap . length > K + 1 ) maxHeap . pop ( ) ; minHeap . push ( A [ i ] ) ; minHeap . sort ( ( a , b ) => a - b ) ; if ( minHeap . length > K + 1 ) minHeap . pop ( ) ; } var maxList = [ ] ; while ( maxHeap . length > 0 ) { maxList . push ( maxHeap [ maxHeap . length - 1 ] ) ; maxHeap . pop ( ) ; } var minList = [ ] ; while ( minHeap . length > 0 ) { minList . push ( minHeap [ minHeap . length - 1 ] ) ; minHeap . pop ( ) ; } var mindiff = 1000000000 ; for ( var i = 0 ; i <= K ; i ++ ) { mindiff = Math . min ( mindiff , maxList [ i ] - minList [ K - i ] ) ; } return mindiff ; } var A = [ - 1 , 3 , - 1 , 8 , 5 , 4 ] ; var N = A . length ; var K = 3 ; document . write ( minDiff ( A , K , N ) ) ;
Check if all K | Function to check all subset - sums of K - length subsets in A [ ] is greater that that in the array B [ ] or not ; Sort the array in ascending order ; Sort the array in descending order ; Stores sum of first K elements of A [ ] ; Stores sum of first K elements of B [ ] ; Traverse both the arrays ; Update sum1 ; Update sum2 ; If sum1 exceeds sum2 ; Driver Code
function checkSubsetSum ( A , B , N , K ) { A . sort ( ( a , b ) => a - b ) ; B . sort ( ( a , b ) => b - a ) ; var sum1 = 0 ; var sum2 = 0 ; for ( var i = 0 ; i < K ; i ++ ) { sum1 += A [ i ] ; sum2 += B [ i ] ; } if ( sum1 > sum2 ) { return true ; } return false ; } var A = [ 12 , 11 , 10 , 13 ] ; var B = [ 7 , 10 , 6 , 2 ] ; var N = A . length ; var K = 3 ; if ( checkSubsetSum ( A , B , N , K ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Sort given array to descending | Function to sort first K array elements in descending and last N - K in ascending order ; Sort the array in descending order ; Print array elements ; Driver Code
function sortArrayInDescAsc ( arr , N , K ) { arr . sort ( ( b , a ) => a - b ) var temp = 0 ; for ( var i = K ; i < N ; i ++ ) { for ( var j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] > arr [ j ] ) { temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } } arr . forEach ( element => { document . write ( element + " " ) ; } ) ; } var arr = [ 7 , 6 , 8 , 9 , 0 , 1 , 2 , 2 , 1 , 8 , 9 , 6 , 7 ] ; var N = arr . length ; var K = 6 ; sortArrayInDescAsc ( arr , N , K ) ;
Median of all non | Function to calculate the median of all possible subsets by given operations ; Stores sum of elements of arr [ ] ; Traverse the array arr [ ] ; Update sum ; Sort the array ; DP [ i ] [ j ] : Stores total number of ways to form the sum j by either selecting ith element or not selecting ith item . ; Base case ; Fill dp [ i ] [ 0 ] ; Base case ; Fill all the DP states based on the mentioned DP relation ; If j is greater than or equal to arr [ i ] ; Update dp [ i ] [ j ] ; Update dp [ i ] [ j ] ; Stores all possible subset sum ; Traverse all possible subset sum ; Stores count of subsets whose sum is j ; Itearate over the range [ 1 , M ] ; Insert j into sumSub ; Stores middle element of sumSub ; Driver Code
function findMedianOfsubSum ( arr , N ) { var sum = 0 ; for ( var i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } arr . sort ( ( a , b ) => a - b ) var dp = Array . from ( Array ( N ) , ( ) => Array ( sum + 1 ) . fill ( 0 ) ) ; for ( var i = 0 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = 1 ; } dp [ 0 ] [ arr [ 0 ] ] = 1 ; for ( var i = 1 ; i < N ; i ++ ) { for ( var j = 1 ; j <= sum ; j ++ ) { if ( j >= arr [ i ] ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j - arr [ i ] ] ; } else { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } } } var sumSub = [ ] ; for ( var j = 1 ; j <= sum ; j ++ ) { var M = dp [ N - 1 ] [ j ] ; for ( var i = 1 ; i <= M ; i ++ ) { sumSub . push ( j ) ; } } var mid = sumSub [ parseInt ( sumSub . length / 2 ) ] ; return mid ; } var arr = [ 2 , 3 , 3 ] ; var N = arr . length document . write ( findMedianOfsubSum ( arr , N ) ) ;
Reduce array to a single element by repeatedly replacing adjacent unequal pairs with their maximum | Function to prlet the index from where the operation can be started ; Initialize B [ ] ; Initialize save ; Make B [ ] equals to arr [ ] ; Sort the array B [ ] ; Traverse from N - 1 to 1 ; If B [ i ] & B [ i - 1 ] are unequal ; If all elements are same ; If arr [ 1 ] is maximum element ; If arr [ N - 1 ] is maximum element ; Find the maximum element ; Given array arr [ ] ; Length of array ; Function Call
function printIndex ( arr , N ) { let B = [ ] ; let save = - 1 ; for ( let i = 0 ; i < N ; i ++ ) { B [ i ] = arr [ i ] ; } B . sort ( ) ; for ( let i = N - 1 ; i >= 1 ; i -- ) { if ( B [ i ] != B [ i - 1 ] ) { save = B [ i ] ; break ; } } if ( save == - 1 ) { document . write ( - 1 + " " ) ; return ; } if ( save == arr [ 0 ] && save != arr [ 1 ] ) { document . write ( 1 ) ; } else if ( save == arr [ N - 1 ] && save != arr [ N - 2 ] ) { document . write ( N ) ; } for ( let i = 1 ; i < N - 1 ; i ++ ) { if ( save == arr [ i ] && ( save != arr [ i - 1 ] save != arr [ i + 1 ] ) ) { document . write ( i + 1 ) ; break ; } } } let arr = [ 5 , 3 , 4 , 4 , 5 ] ; let N = arr . length ; printIndex ( arr , N ) ;
Lexicographically smallest subsequence possible by removing a character from given string | Function to find the lexicographically smallest subsequence of length N - 1 ; Generate all subsequence of length N - 1 ; Store main value of String str ; Sort the vector ; Print first element of vector ; Given String S ; Function Call
function firstSubsequence ( s ) { let allsubseq = [ ] ; for ( let i = 0 ; i < s . length ; i ++ ) { let k = " " ; for ( let j = 0 ; j < s . length ; j ++ ) { if ( i != j ) { k += s [ j ] ; } } allsubseq . push ( k ) ; } ( allsubseq ) . sort ( ) ; document . write ( allsubseq [ 0 ] ) ; } let S = " " ; firstSubsequence ( S ) ;
Maximum even sum subsequence of length K | Function to find the maximum even sum of any subsequence of length K ; If count of elements is less than K ; Stores maximum even subsequence sum ; Stores Even numbers ; Stores Odd numbers ; Traverse the array ; If current element is an odd number ; Insert odd number ; Insert even numbers ; Sort Odd [ ] array ; Sort Even [ ] array ; Stores current index Of Even [ ] array ; Stores current index Of Odd [ ] array ; If K is odd ; If count of elements in Even [ ] >= 1 ; Update maxSum ; Update i ; If count of elements in Even [ ] array is 0. ; Update K ; If count of elements in Even [ ] and odd [ ] >= 2 ; Update maxSum ; Update j . ; Update maxSum ; Update i ; Update K ; If count of elements in Even [ ] array >= 2. ; Update maxSum ; Update i . ; Update K . ; If count of elements in Odd [ ] array >= 1 ; Update maxSum ; Update i . ; Update K . ; Driver Code
function evenSumK ( arr , N , K ) { if ( K > N ) { return - 1 ; } var maxSum = 0 ; var Even = [ ] ; var Odd = [ ] ; for ( var i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 ) { Odd . push ( arr [ i ] ) ; } else { Even . push ( arr [ i ] ) ; } } Odd . sort ( ( a , b ) => a - b ) ; Even . sort ( ( a , b ) => a - b ) ; var i = Even . length - 1 ; var j = Odd . length - 1 ; while ( K > 0 ) { if ( K % 2 == 1 ) { if ( i >= 0 ) { maxSum += Even [ i ] ; i -- ; } else { return - 1 ; } K -- ; } else if ( i >= 1 && j >= 1 ) { if ( Even [ i ] + Even [ i - 1 ] <= Odd [ j ] + Odd [ j - 1 ] ) { maxSum += Odd [ j ] + Odd [ j - 1 ] ; j -= 2 ; } else { maxSum += Even [ i ] + Even [ i - 1 ] ; i -= 2 ; } K -= 2 ; } else if ( i >= 1 ) { maxSum += Even [ i ] + Even [ i - 1 ] ; i -= 2 ; K -= 2 ; } else if ( j >= 1 ) { maxSum += Odd [ j ] + Odd [ j - 1 ] ; j -= 2 ; K -= 2 ; } } return maxSum ; } var arr = [ 2 , 4 , 10 , 3 , 5 ] ; var N = arr . length ; var K = 3 ; document . write ( evenSumK ( arr , N , K ) ) ;
Program to find weighted median of a given array | Function to calculate weighted median ; Store pr of arr [ i ] and W [ i ] ; Sort the list of pr w . r . t . to their arr [ ] values ; If N is odd ; Traverse the set pr from left to right ; Update sums ; If sum becomes > 0.5 ; If N is even ; For lower median traverse the set pr from left ; Update sums ; When sum >= 0.5 ; For upper median traverse the set pr from right ; Update sums ; When sum >= 0.5 ; Given array arr [ ] ; Given weights W [ ] ; Function Call
function weightedMedian ( arr , W ) { let pr = [ ] ; for ( let index = 0 ; index < arr . length ; index ++ ) pr . push ( [ arr [ index ] , W [ index ] ] ) ; ( pr ) . sort ( function ( a , b ) { return a [ 1 ] - b [ 1 ] ; } ) ; if ( arr . length % 2 != 0 ) { let sums = 0 ; for ( let element = 0 ; element < pr . length ; element ++ ) { sums += pr [ element ] [ 1 ] ; if ( sums > 0.5 ) document . write ( " " + pr [ element ] [ 0 ] + " " ) ; } } else { let sums = 0 ; for ( let element = 0 ; element < pr . length ; element ++ ) { sums += pr [ element ] [ 1 ] ; if ( sums <= 0.5 ) { document . write ( " " + pr [ element ] [ 0 ] + " " ) ; break ; } } sums = 0 ; for ( let index = pr . length - 1 ; index >= 0 ; index -- ) { let element = pr [ index ] [ 0 ] ; let weight = pr [ index ] [ 1 ] ; sums += weight ; if ( sums >= 0.5 ) { document . write ( " " + element ) ; break ; } } } } let arr = [ ] ; arr . push ( 4 ) ; arr . push ( 1 ) ; arr . push ( 3 ) ; arr . push ( 2 ) ; let W = [ ] ; W . push ( 0.25 ) ; W . push ( 0.49 ) ; W . push ( 0.25 ) ; W . push ( 0.01 ) ; weightedMedian ( arr , W ) ;
Kth smallest element from an array of intervals | Function to get the Kth smallest element from an array of intervals ; Store all the intervals so that it returns the minimum element in O ( 1 ) ; Insert all Intervals into the MinHeap ; Stores the count of popped elements ; Iterate over MinHeap ; Stores minimum element from all remaining intervals ; Remove minimum element ; Check if the minimum of the current interval is less than the maximum of the current interval ; Insert new interval ; Intervals given ; Size of the arr
function KthSmallestNum ( arr , n , k ) { var pq = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { pq . push ( [ arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ] ) ; } var cnt = 1 ; while ( cnt < k ) { pq . sort ( ( a , b ) => { if ( a [ 0 ] == b [ 0 ] ) return a [ 1 ] - b [ 1 ] return a [ 0 ] - b [ 0 ] } ) ; var interval = pq [ 0 ] ; pq . shift ( ) ; if ( interval [ 0 ] < interval [ 1 ] ) { pq . push ( [ interval [ 0 ] + 1 , interval [ 1 ] ] ) ; } cnt += 1 ; } pq . sort ( ( a , b ) => { if ( a [ 0 ] == b [ 0 ] ) return a [ 1 ] - b [ 1 ] return a [ 0 ] - b [ 0 ] } ) ; return ( pq [ 0 ] ) [ 0 ] ; } var arr = [ [ 5 , 11 ] , [ 10 , 15 ] , [ 12 , 20 ] ] ; var n = arr . length ; var k = 12 ; document . write ( KthSmallestNum ( arr , n , k ) ) ;
Longest increasing subsequence which forms a subarray in the sorted representation of the array | Function to find the length of the longest increasing sorted sequence ; Stores the count of all elements ; Store the original array ; Sort the array ; If adjacent element are not same ; Increment count ; Store frequency of each element ; Initialize a DP array ; Iterate over the array ar [ ] ; Length of the longest increasing sorted sequence ; Iterate over the array ; Current element ; If the element has been encountered the first time ; If all the x - 1 previous elements have already appeared ; Otherwise ; If all x - 1 elements have already been encountered ; Increment the count of the current element ; Update maximum subsequence size ; Return the maximum subsequence size ; Driver Code ; Function Call
function LongestSequence ( a , n ) { var m = new Map ( ) ; var ar = Array ( n + 1 ) . fill ( 0 ) , i , j ; for ( i = 1 ; i <= n ; i ++ ) { ar [ i ] = a [ i - 1 ] ; } a . sort ( ( a , b ) => a - b ) ; var c = 1 ; m . set ( a [ 0 ] , c ) ; for ( i = 1 ; i <= n ; i ++ ) { if ( a [ i ] != a [ i - 1 ] ) { c ++ ; m . set ( a [ i ] , c ) ; } } var cnt = new Map ( ) ; var dp = Array . from ( Array ( n + 1 ) , ( ) => Array ( 3 ) . fill ( 0 ) ) ; cnt . set ( 0 , 0 ) ; for ( i = 1 ; i <= n ; i ++ ) { ar [ i ] = m . get ( ar [ i ] ) ; if ( cnt . has ( ar [ i ] ) ) cnt . set ( ar [ i ] , cnt . get ( ar [ i ] ) + 1 ) else cnt . set ( ar [ i ] , 1 ) } var ans = 0 , x ; for ( i = 1 ; i <= n ; i ++ ) { x = ar [ i ] ; if ( dp [ x ] [ 0 ] == 0 ) { if ( dp [ x - 1 ] [ 0 ] == cnt . get ( x - 1 ) ) { dp [ x ] [ 1 ] = dp [ x - 1 ] [ 1 ] ; dp [ x ] [ 2 ] = dp [ x - 1 ] [ 1 ] ; } else { dp [ x ] [ 1 ] = dp [ x - 1 ] [ 0 ] ; } } dp [ x ] [ 2 ] = Math . max ( dp [ x - 1 ] [ 0 ] , dp [ x ] [ 2 ] ) ; if ( dp [ x - 1 ] [ 0 ] == cnt [ x - 1 ] ) { dp [ x ] [ 2 ] = Math . max ( dp [ x ] [ 2 ] , dp [ x - 1 ] [ 1 ] ) ; } for ( j = 0 ; j < 3 ; j ++ ) { dp [ x ] [ j ] ++ ; ans = Math . max ( ans , dp [ x ] [ j ] ) ; } } return ans ; } var arr = [ 2 , 6 , 4 , 8 , 2 , 9 ] ; var N = arr . length ; document . write ( LongestSequence ( arr , N ) ) ;
Maximize the sum of Kth column of a Matrix | Function to maximize the Kth column sum ; Store all the elements of the resultant matrix of size N * N ; Store value of each elements of the matrix ; Fill all the columns < K ; Fill all the columns >= K ; Function to prlet the matrix ; Driver code
function findMatrix ( N , K ) { let mat = new Array ( N ) ; for ( var i = 0 ; i < mat . length ; i ++ ) { mat [ i ] = new Array ( 2 ) ; } let element = 1 ; for ( let i = 0 ; i < N ; ++ i ) { for ( let j = 0 ; j < K - 1 ; ++ j ) { mat [ i ] [ j ] = element ++ ; } } for ( let i = 0 ; i < N ; ++ i ) { for ( let j = K - 1 ; j < N ; ++ j ) { mat [ i ] [ j ] = element ++ ; } } return mat ; } function prletMatrix ( mat , N ) { for ( let i = 0 ; i < N ; ++ i ) { for ( let j = 0 ; j < N ; ++ j ) { document . write ( mat [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let N = 3 , K = 2 ; let mat = findMatrix ( N , K ) ; prletMatrix ( mat , N ) ;
Range sum queries based on given conditions | Function to calculate the sum between the given range as per value of m ; Stores the sum ; Condition for a to print the sum between ranges [ a , b ] ; Return sum ; Function to precalculate the sum of both the vectors ; Make Prefix sum array ; Function to compute the result for each query ; Take a dummy vector and copy the element of arr in brr ; Sort the dummy vector ; Compute prefix sum of both vectors ; Performs operations ; Function call to find sum ; Function call to find sum ; Given [ ] arr ; Number of queries ; Given queries ; Function call
function range_sum ( arr , a , b ) { let sum = 0 ; if ( a - 2 < 0 ) sum = arr [ b - 1 ] ; else sum = arr [ b - 1 ] - arr [ a - 2 ] ; return sum ; } function precompute_sum ( arr , brr ) { let N = arr . length ; for ( let i = 1 ; i < N ; i ++ ) { arr [ i ] = arr [ i ] + arr [ i - 1 ] ; brr [ i ] = brr [ i ] + brr [ i - 1 ] ; } } function find_sum ( arr , q , Queries ) { let brr = [ ... arr ] ; let N = arr . length ; brr . sort ( function ( a , b ) { return a - b ; } ) ; precompute_sum ( arr , brr ) ; for ( let i = 0 ; i < q ; i ++ ) { let m = Queries [ i ] [ 0 ] ; let a = Queries [ i ] [ 1 ] ; let b = Queries [ i ] [ 2 ] ; if ( m == 1 ) { document . write ( range_sum ( arr , a , b ) + " " ) ; } else if ( m == 2 ) { document . write ( range_sum ( brr , a , b ) + " " ) ; } } } let arr = [ 0 , 6 , 4 , 2 , 7 , 2 , 7 ] ; let Q = 1 ; let Queries = [ [ 2 , 3 , 6 ] ] ; find_sum ( arr , Q , Queries ) ;
XOR of all possible pairwise sum from two given Arrays | Lower bound ; Function to calculate the XOR of the sum of every pair ; Stores the maximum bit ; Look for all the k - th bit ; Stores the modulo of elements B [ ] with ( 2 ^ ( k + 1 ) ) ; Calculate modulo of array B [ ] with ( 2 ^ ( k + 1 ) ) ; Sort the array C [ ] ; Stores the total number whose k - th bit is set ; Calculate and store the modulo of array A [ ] with ( 2 ^ ( k + 1 ) ) ; Lower bound to count the number of elements having k - th bit in the range ( 2 ^ k - x , 2 * 2 ^ ( k ) - x ) ; Add total number i . e ( r - l ) whose k - th bit is one ; Lower bound to count the number of elements having k - th bit in range ( 3 * 2 ^ k - x , 4 * 2 ^ ( k ) - x ) ; If count is even , Xor of k - th bit becomes zero , no need to add to the answer . If count is odd , only then , add to the final answer ; Return answer ; Driver code ; Function call
function lower_bound ( a , low , high , element ) { while ( low < high ) { let middle = low + Math . floor ( ( high - low ) / 2 ) ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } function XorSum ( A , B , N ) { let maxBit = 29 ; let ans = 0 ; for ( let k = 0 ; k < maxBit ; k ++ ) { let C = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { C [ i ] = B [ i ] % ( 1 << ( k + 1 ) ) ; } C . sort ( function ( x , y ) { return x - y ; } ) ; let count = 0 ; let l , r ; for ( let i = 0 ; i < N ; i ++ ) { let x = A [ i ] % ( 1 << ( k + 1 ) ) ; l = lower_bound ( C , 0 , N , ( 1 << k ) - x ) ; r = lower_bound ( C , 0 , N , ( 1 << k ) * 2 - x ) ; count += ( r - l ) ; l = lower_bound ( C , 0 , N , ( 1 << k ) * 3 - x ) ; r = lower_bound ( C , 0 , N , ( 1 << k ) * 4 - x ) ; count += ( r - l ) ; } if ( ( count & 1 ) != 0 ) ans += ( 1 << k ) ; } return ans ; } let A = [ 4 , 6 , 0 , 0 , 3 , 3 ] ; let B = [ 0 , 5 , 6 , 5 , 0 , 3 ] ; let N = A . length ; document . write ( XorSum ( A , B , N ) + " " ) ;
Minimize count of Subsets with difference between maximum and minimum element not exceeding K | Function to find the minimum count of subsets of required type ; Stores the result ; Store the maximum and minimum element of the current subset ; Update current maximum ; If difference exceeds K ; Update count ; Update maximum and minimum to the current subset ; Driver Code
function findCount ( arr , N , K ) { arr . sort ( ) ; let result = 1 ; let cur_max = arr [ 0 ] ; let cur_min = arr [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { cur_max = arr [ i ] ; if ( cur_max - cur_min > K ) { result ++ ; cur_max = arr [ i ] ; cur_min = arr [ i ] ; } } return result ; } let arr = [ 1 , 10 , 8 , 3 , 9 ] ; let K = 3 ; let N = arr . length ; document . write ( findCount ( arr , N , K ) ) ;