codeparrot/xlcost-text-to-code huggingface dataset:
Minimum operations required to make all elements in an array of first N odd numbers equal | Function to find the minimum number of operations required to make the array elements equal ; Stores the array elements ; Stores the sum of the array ; Iterate over the range [ 0 , N ] ; Update the value arr [ i ] ; Increment the sum by the value arr [ i ] ; Stores the middle element ; If N is even ; Otherwise ; Stores the result ; Traverse the range [ 0 , N / 2 ] ; Update the value of ans ; Return the result ; Driver Code
function minOperations ( N ) { let arr = Array . from ( { length : N } , ( _ , i ) => 0 ) ; let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { arr [ i ] = ( 2 * i ) + 1 ; sum = sum + arr [ i ] ; } let mid = 0 ; if ( N % 2 == 0 ) { mid = sum / N ; } else { mid = arr [ N / 2 ] ; } let ans = 0 ; for ( let i = 0 ; i < N / 2 ; i ++ ) { ans += mid - arr [ i ] ; } return ans ; } let N = 6 ; document . write ( minOperations ( N ) ) ;
Minimum operations required to make all elements in an array of first N odd numbers equal | Function to find the minimum number of operations required to make the array elements equal ; If the value of N is even ; Return the value ; Otherwise , N is odd ; Return the value ; Driver Code
function minOperation ( N ) { if ( N % 2 == 0 ) { return ( N / 2 ) * ( N / 2 ) ; } let k = ( N - 1 ) / 2 ; return k * ( k + 1 ) ; } let N = 6 ; document . write ( minOperation ( N ) ) ;
Bitwise XOR of Bitwise AND of all pairs from two given arrays | Function to find the Bitwise XOR of Bitwise AND of all pairs from the arrays arr1 [ ] and arr2 [ ] ; Stores the result ; Iterate over the range [ 0 , N - 1 ] ; Iterate over the range [ 0 , M - 1 ] ; Stores Bitwise AND of the pair { arr1 [ i ] , arr2 [ j ] } ; Update res ; Return the res ; Input
function findXORS ( arr1 , arr2 , N , M ) { let res = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { let temp = arr1 [ i ] & arr2 [ j ] ; res ^= temp ; } } return res ; } let arr1 = [ 1 , 2 , 3 ] ; let arr2 = [ 6 , 5 ] ; let N = arr1 . length ; let M = arr2 . length ; document . write ( findXORS ( arr1 , arr2 , N , M ) ) ;
Bitwise XOR of Bitwise AND of all pairs from two given arrays | Function to find the Bitwise XOR of Bitwise AND of all pairs from the arrays arr1 [ ] and arr2 [ ] ; Stores XOR of array arr1 [ ] ; Stores XOR of array arr2 [ ] ; Traverse the array arr1 [ ] ; Traverse the array arr2 [ ] ; Return the result ; Input
function findXORS ( arr1 , arr2 , N , M ) { let XORS1 = 0 ; let XORS2 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { XORS1 ^= arr1 [ i ] ; } for ( let i = 0 ; i < M ; i ++ ) { XORS2 ^= arr2 [ i ] ; } return XORS1 && XORS2 ; } let arr1 = [ 1 , 2 , 3 ] ; let arr2 = [ 6 , 5 ] ; let N = arr1 . length ; let M = arr2 . length ; document . write ( findXORS ( arr1 , arr2 , N , M ) ) ;
Count numbers up to N that cannot be expressed as sum of at least two consecutive positive integers | Function to check if a number can be expressed as a power of 2 ; f N is power of two ; Function to count numbers that cannot be expressed as sum of two or more consecutive + ve integers ; Stores the resultant count of letegers ; Iterate over the range [ 1 , N ] ; Check if i is power of 2 ; Increment the count if i is not power of 2 ; Print the value of count ; Driver code
function isPowerof2 ( n ) { return ( ( n & ( n - 1 ) ) && n ) ; } function countNum ( N ) { let count = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { let flag = isPowerof2 ( i ) ; if ( ! flag ) { count ++ ; } } document . write ( count + " " ) ; } let N = 100 ; countNum ( N ) ;
Count numbers up to N that cannot be expressed as sum of at least two consecutive positive integers | Function to count numbers that cannot be expressed as sum of two or more consecutive + ve integers ; Stores the count of such numbers ; Driver Code
function countNum ( N ) { var ans = parseInt ( Math . log ( N ) / Math . log ( 2 ) ) + 1 ; document . write ( ans ) ; } var N = 100 ; countNum ( N ) ;
Partition a Linked List into K continuous groups with difference in their sizes at most 1 | Link List Node ; Function to insert a node into the Linked List ; Allocate a new dynamic node ; Update new_node . val ; Stores the head_ref ; Update ( * head_ref ) ; Function to split the linked list in K groups ; Stores the K groups ; If head is null ; Iterate until K is non - zero ; Stores the length of the linked list ; Stores the head node of the linked list ; Iterate over the linked list ; Update p ; Update N ; Iterate over the linked list ; Stores the length of the current group ; Stores the current node ; Stores the previous node ; If rem is greater than 0 ; Update p ; Decrement rem by 1 ; Iterate until x is non - zero ; If the last is equal to p ; Otherwise ; Join the link between last and the current element ; Update the last node ; Update p node ; Assign null to last . next ; Push the current linked list in ans ; Decrement K ; While K greater than 0 ; Update the value of ans ; Increment K ; Print the result ; Print the value ; Update ans [ i ] ; Driver Code
class ListNode { constructor ( ) { this . val = 0 ; this . next = null ; } } ; function pushIn ( head_ref , node_val ) { var new_node = new ListNode ( ) ; new_node . val = node_val ; new_node . next = ( head_ref ) ; head_ref = new_node ; return head_ref ; } function splitListInParts ( head , K ) { var ans = [ ] ; if ( ! head ) { while ( K -- ) ans . push ( null ) ; } var N = 0 ; var p = head ; while ( p ) { p = p . next ; N ++ ; } var len = parseInt ( N / K ) ; var rem = N % K ; p = head ; while ( K > 0 && p ) { var x = len ; var curr_head = p ; var last = p ; if ( rem > 0 ) { p = p . next ; rem -- ; } while ( x -- ) { if ( last == p ) p = p . next ; else { last . next = p ; last = p ; p = p . next ; } } last . next = null ; ans . push ( curr_head ) ; K -- ; } while ( K > 0 ) { ans . push ( null ) ; K -- ; } document . write ( " " ) ; for ( var i = 0 ; i < ans . length ; i ++ ) { document . write ( " " ) ; while ( ans [ i ] ) { document . write ( ans [ i ] . val + " " ) ; ans [ i ] = ans [ i ] . next ; } document . write ( " " ) ; if ( i != ans . length - 1 ) document . write ( " " ) ; } document . write ( " " ) ; } var root = null ; root = pushIn ( root , 8 ) ; root = pushIn ( root , 7 ) ; root = pushIn ( root , 6 ) ; root = pushIn ( root , 5 ) ; root = pushIn ( root , 4 ) ; root = pushIn ( root , 3 ) ; root = pushIn ( root , 2 ) ; root = pushIn ( root , 1 ) ; var K = 3 ; splitListInParts ( root , K ) ;
Replace array elements that contains K as a digit with the nearest power of K | Function to calculate the power of base nearest to x ; Stores logX to the base K ; Function to replace array elements with nearest power of K ; Traverse the array ; Convert integer into a string ; If K is found , then replace with the nearest power of K ; Print the array ; Given array ; Given value of K ; Function call to replace array elements with nearest power of K
function nearestPow ( x , base ) { let k = Math . floor ( Math . log ( x ) / Math . log ( base ) ) ; if ( Math . abs ( Math . pow ( base , k ) - x ) < Math . abs ( Math . pow ( base , ( k + 1 ) ) - x ) ) return Math . pow ( base , k ) ; else return Math . pow ( base , ( k + 1 ) ) ; } function replaceWithNearestPowerOfK ( arr , K , n ) { for ( let i = 0 ; i < n ; i ++ ) { let strEle = arr [ i ] . toString ( ) ; for ( let c = 0 ; c < strEle . length ; c ++ ) { if ( ( strEle - ' ' ) == K ) { arr [ i ] = nearestPow ( arr [ i ] , K ) ; break ; } } } for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) } let arr = [ 432 , 953 , 232 , 333 ] ; let n = arr . length ; let K = 3 ; replaceWithNearestPowerOfK ( arr , K , n )
Difference between ceil of array sum divided by K and sum of ceil of array elements divided by K | Function to find absolute difference between array sum divided by x and sum of ceil of array elements divided by x ; Stores the total sum ; Stores the sum of ceil of array elements divided by x ; Traverse the array ; Adding each array element ; Add the value ceil of arr [ i ] / x ; Find the ceil of the total sum divided by x ; Return absolute difference ; Driver Code
function ceilDifference ( arr , n , x ) { var totalSum = 0 ; var perElementSum = 0 ; for ( var i = 0 ; i < n ; i ++ ) { totalSum += arr [ i ] ; perElementSum += parseInt ( Math . ceil ( ( arr [ i ] ) / ( x ) ) ) ; } var totalCeilSum = parseInt ( Math . ceil ( ( totalSum ) / ( x ) ) ) ; return Math . abs ( perElementSum - totalCeilSum ) ; } var arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; var K = 4 ; var N = arr . length ; document . write ( ceilDifference ( arr , N , K ) ) ;
Remove trailing zeros from the sum of two numbers ( Using Stack ) | Function to remove trailing zeros from the sum of two numbers ; Stores the sum of A and B ; Stores the digits ; Stores the equivalent string of integer N ; Traverse the string ; Push the digit at i in the stack ; While top element is '0' ; Pop the top element ; Stores the resultant number without tailing 0 's ; While s is not empty ; Append top element of S in res ; Pop the top element of S ; Reverse the string res ; Input ; Function Call
function removeTailing ( A , B ) { let N = A + B ; let s = new Array ( ) ; let strsum = N . toString ( ) ; for ( let i = 0 ; i < strsum . length ; i ++ ) { s . push ( strsum . charAt ( i ) ) ; } while ( s [ s . length - 1 ] === ' ' ) { s . pop ( ) ; } let res = " " ; while ( s . length != 0 ) { res = res . concat ( s [ s . length - 1 ] ) ; s . pop ( ) ; } let str = " " ; str = str . concat ( res ) str = str . split ( " " ) . reverse ( ) . join ( " " ) ; return str . toString ( ) ; } let A = 130246 , B = 450164 ; document . write ( removeTailing ( A , B ) ) ;
Count number of triplets with product not exceeding a given number | Function to count the number of triplets whose product is at most N ; Stores the count of triplets ; Iterate over the range [ 0 , N ] ; Iterate over the range [ 0 , N ] ; If the product of pairs exceeds N ; Increment the count of possible triplets ; Return the total count ; Driver Code
function countTriplets ( N ) { let ans = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = 1 ; j <= N ; j ++ ) { if ( i * j > N ) break ; ans += Math . floor ( N / ( i * j ) ) ; } } return ans ; } let N = 10 ; document . write ( countTriplets ( N ) ) ;
Sum of an Infinite Geometric Progression ( GP ) | Function to calculate the sum of an infinite Geometric Progression ; Case for Infinite Sum ; Store the sum of GP Series ; Print the value of sum ; Driver Code
function findSumOfGP ( a , r ) { if ( Math . abs ( r ) >= 1 ) { document . write ( " " ) ; return ; } let sum = a / ( 1 - r ) ; document . write ( sum ) ; } let A = 1 , R = 0.5 ; findSumOfGP ( A , R ) ;
Number of Relations that are both Irreflexive and Antisymmetric on a Set | JavaScript program for the above approach ; Function to calculate x ^ y % mod in O ( log y ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the value of x ^ y ; Function to count relations that are irreflexive and antisymmetric in a set consisting of N elements ; Return the resultant count ; Driver Code
let mod = 1000000007 ; function power ( x , y ) { let res = 1 ; x = x % mod ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } function numberOfRelations ( N ) { return power ( 3 , ( N * N - N ) / 2 ) ; } let N = 2 ; document . write ( numberOfRelations ( N ) ) ;
Count of numbers up to N having at least one prime factor common with N | Function to count all the numbers in the range [ 1 , N ] having common factor with N other than 1 ; Stores the count of numbers having more than 1 factor with N ; Iterate over the range [ 1 , N ] ; If gcd is not 1 then increment the count ; Prvar the resultant count ; Driver Code
function countNumbers ( N ) { var count = 0 ; for ( var i = 1 ; i <= N ; i ++ ) { if ( __gcd ( i , N ) != 1 ) count ++ ; } document . write ( count ) ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } var N = 5 ; countNumbers ( N ) ;
Count of numbers up to N having at least one prime factor common with N | Function to calculate the value of Euler 's totient function ; Initialize result with N ; Find all prime factors of N and subtract their multiples ; Check if p is a prime factor ; If found to be true , then update N and result ; If N has a prime factor greater than sqrt ( N ) , then there can be at - most one such prime factor ; Function to count all the numbers in the range [ 1 , N ] having common factor with N other than 1 ; Stores the resultant count ; Prlet the count ; Driver Code
function phi ( N ) { let result = N ; for ( let p = 2 ; p * p <= N ; ++ p ) { if ( N % p == 0 ) { while ( N % p == 0 ) N /= p ; result -= result / p ; } } if ( N > 1 ) result -= result / N ; return result ; } function countNumbers ( N ) { let count = N - phi ( N ) ; document . write ( count ) ; } let N = 5 ; countNumbers ( N ) ;
Queries to update array by adding or multiplying array elements and print the element present at specified index | Function to modify the array by performing given queries ; Stores the multiplication of all letegers of type 1 ; Stores the value obtained after performing queries of type 1 & 2 ; Iterate over the queries ; Query of type 0 ; Update the value of add ; Query of type 1 ; Update the value of mul ; Update the value of add ; Otherwise ; Store the element at index Q [ i ] [ 1 ] ; Prlet the result for the current query ; Driver Code
function Query ( arr , N , Q ) { let mul = 1 ; let add = 0 ; for ( let i = 0 ; i < Q . length ; i ++ ) { if ( Q [ i ] [ 0 ] == 0 ) { add = add + Q [ i ] [ 1 ] ; } else if ( Q [ i ] [ 0 ] == 1 ) { mul = mul * Q [ i ] [ 1 ] ; add = add * Q [ i ] [ 1 ] ; } else { let ans = arr [ Q [ i ] [ 1 ] ] * mul + add ; document . write ( ans + " " ) ; } } } let arr = [ 3 , 1 , 23 , 45 , 100 ] ; let N = arr . length ; let Q = [ [ 1 , 2 ] , [ 0 , 10 ] , [ 2 , 3 ] , [ 1 , 5 ] , [ 2 , 4 ] ] ; Query ( arr , N , Q ) ;
Minimize cost of converting all array elements to Fibonacci Numbers | Function to find the N - th Fibonacci Number ; Find the value of a , b , and r ; Find the N - th Fibonacci ; Return the result ; Function to find the Fibonacci number which is nearest to X ; Calculate the value of n for X ; Return the nearest Fibonacci Number ; Function to find the minimum cost to convert all array elements to Fibonacci Numbers ; Stores the total minimum cost ; Traverse the given array arr [ ] ; Find the nearest Fibonacci Number ; Add the cost ; Return the final cost ; Driver Code
function nthFibo ( n ) { let a = ( Math . pow ( 5 , 0.5 ) + 1 ) / 2 ; let b = ( - 1 * ( Math . pow ( 5 , 0.5 ) ) + 1 ) / 2 ; let r = Math . pow ( 5 , 0.5 ) ; let ans = ( Math . pow ( a , n ) - Math . pow ( b , n ) ) / r ; return Math . floor ( ans ) ; } function nearFibo ( X ) { let a = ( Math . pow ( 5 , 0.5 ) + 1 ) / 2 ; let n = Math . floor ( Math . log ( ( Math . pow ( 5 , 0.5 ) ) * X ) / Math . log ( a ) ) ; let nth = nthFibo ( n ) ; let nplus = nthFibo ( n + 1 ) ; if ( Math . abs ( X - nth ) < Math . abs ( X - nplus ) ) return nth ; else return nplus ; } function getCost ( arr , n ) { let cost = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let fibo = nearFibo ( arr [ i ] ) ; cost += Math . abs ( arr [ i ] - fibo ) ; } return cost ; } let arr = [ 56 , 34 , 23 , 98 , 7 ] ; let n = arr . length ; document . write ( getCost ( arr , n ) ) ;
Maximum sum of pairs that are at least K distance apart in an array | Function to find the largest sum pair that are K distant apart ; Stores the prefix maximum array ; Base Case ; Traverse the array and update the maximum value upto index i ; Stores the maximum sum of pairs ; Iterate over the range [ K , N ] ; Find the maximum value of the sum of valid pairs ; Return the resultant sum ; Driver Code
function getMaxPairSum ( arr , N , K ) { var preMax = Array ( N ) ; preMax [ 0 ] = arr [ 0 ] ; for ( var i = 1 ; i < N ; i ++ ) { preMax [ i ] = Math . max ( preMax [ i - 1 ] , arr [ i ] ) ; } var res = - 1000000000 ; for ( var i = K ; i < N ; i ++ ) { res = Math . max ( res , arr [ i ] + preMax [ i - K ] ) ; } return res ; } var arr = [ 1 , 2 , 4 , 8 , 6 , 3 ] ; var K = 3 ; var N = arr . length ; document . write ( getMaxPairSum ( arr , N , K ) ) ;
Program to calculate sum of an Infinite Arithmetic | Function to find the sum of the infinite AGP ; Stores the sum of infinite AGP ; Print the required sum ; Driver Code
function sumOfInfiniteAGP ( a , d , r ) { let ans = a / ( 1 - r ) + ( d * r ) / ( 1 - r * r ) ; document . write ( ans ) } let a = 0 , d = 1 , r = 0.5 ; sumOfInfiniteAGP ( a , d , r ) ;
Count pairs from an array having GCD equal to the minimum element in the pair | Function to count pairs from an array having GCD equal to minimum element of that pair ; Stores the resultant count ; Iterate over the range [ 0 , N - 2 ] ; Iterate over the range [ i + 1 , N ] ; If arr [ i ] % arr [ j ] is 0 or arr [ j ] % arr [ i ] is 0 ; Increment count by 1 ; Return the resultant count ; Driver Code
function countPairs ( arr , N ) { let count = 0 ; for ( let i = 0 ; i < N - 1 ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] % arr [ j ] == 0 arr [ j ] % arr [ i ] == 0 ) { count ++ ; } } } return count ; } let arr = [ 2 , 3 , 1 , 2 ] ; let N = arr . length ; document . write ( countPairs ( arr , N ) ) ;
Count pairs from an array having GCD equal to the minimum element in the pair | Function to count pairs from an array having GCD equal to minimum element of that pair ; Stores the resultant count ; Stores the frequency of each array element ; Traverse the array arr [ ] ; Iterate over the Map mp ; Stores the array element ; Stores the count of array element x ; If x is 1 ; Increment res by N - 1 ; Increment res by yC2 ; Iterate over the range [ 2 , sqrt ( x ) ] ; If x is divisible by j ; Increment the value of res by mp [ j ] ; If j is not equal to x / j ; Increment res by mp [ x / j ] ; Return the resultant count ; Driver Code
function CountPairs ( arr , N ) { var res = 0 ; 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 ) ; } } mp . forEach ( ( value , key ) => { var x = key ; var y = value ; if ( x == 1 ) { res += N - 1 ; } res += parseInt ( ( y * ( y - 1 ) ) / 2 ) ; for ( var j = 2 ; j <= parseInt ( Math . sqrt ( x ) ) ; j ++ ) { if ( x % j == 0 ) { res += mp . get ( j ) ; if ( j != parseInt ( x / j ) ) res += mp . get ( parseInt ( x / j ) ) ; } } } ) ; return res ; } var arr = [ 2 , 3 , 1 , 2 ] ; var N = arr . length ; document . write ( CountPairs ( arr , N ) ) ;
Product of count of set bits present in binary representations of elements in an array | Function to count the set bits in an integer ; Stores the count of set bits ; Iterate while N is not equal to 0 ; Increment count by 1 ; Divide N by 2 ; Return the total count obtained ; Function to find the product of count of set bits present in each element of an array ; Stores the resultant product ; Traverse the array arr [ ] ; Stores the count of set bits of arr [ i ] ; Update the product ; Return the resultant product ; Driver Code
function countbits ( n ) { let count = 0 ; while ( n != 0 ) { if ( ( n & 1 ) != 0 ) count ++ ; n = Math . floor ( n / 2 ) ; } return count ; } function BitProduct ( arr , N ) { let product = 1 ; for ( let i = 0 ; i < N ; i ++ ) { let bits = countbits ( arr [ i ] ) ; product *= bits ; } return product ; } let arr = [ 3 , 2 , 4 , 1 , 5 ] ; let N = arr . length ; document . write ( BitProduct ( arr , N ) ) ;
Queries to calculate sum of array elements consisting of odd number of divisors | Function to get the middle index from the given ranges ; Recursive function to find the sum of values in the given range of the array ; If segment of this node is a part of given range , then return the sum of the segment ; If segment of this node is outside the given range ; If a part of this segment overlaps the given range ; Function to find the sum of elements in the range from index qs ( query start ) to qe ( query end ) ; Invalid ranges ; Recursive function to construct the Segment Tree for array [ ss . . se ] . si is index of current node in tree st ; If there is one element in the array ; Recur for left and right subtrees and store the sum of values in this node ; Function to construct segment tree from the given array ; Allocate memory for the segment tree Height of segment tree ; Maximum size of segment tree ; Allocate memory ; Fill the allocated memory st ; Return the constructed segment tree ; Function to find the sum of elements having odd number of divisors in index range [ L , R ] for Q queries ; Traverse the array , arr [ ] ; Replace elements that are not perfect squares with 0 ; Build segment tree from the given array ; Iterate through all the queries ; Print sum of values in array from index l to r ; Driver Code
function getMid ( s , e ) { return s + Math . floor ( ( e - s ) / 2 ) ; } function getSumUtil ( st , ss , se , qs , qe , si ) { if ( qs <= ss && qe >= se ) return st [ si ] ; if ( se < qs ss > qe ) return 0 ; let mid = getMid ( ss , se ) ; return getSumUtil ( st , ss , mid , qs , qe , 2 * si + 1 ) + getSumUtil ( st , mid + 1 , se , qs , qe , 2 * si + 2 ) ; } function getSum ( st , n , qs , qe ) { if ( qs < 0 qe > n - 1 qs > qe ) { document . write ( " " ) ; return - 1 ; } return getSumUtil ( st , 0 , n - 1 , qs , qe , 0 ) ; } function constructSTUtil ( arr , ss , se , st , si ) { if ( ss == se ) { st [ si ] = arr [ ss ] ; return arr [ ss ] ; } let mid = getMid ( ss , se ) ; st [ si ] = constructSTUtil ( arr , ss , mid , st , si * 2 + 1 ) + constructSTUtil ( arr , mid + 1 , se , st , si * 2 + 2 ) ; return st [ si ] ; } function constructST ( arr , n ) { let x = ( Math . ceil ( Math . log ( n ) / Math . log ( 2 ) ) ) ; let max_size = 2 * Math . pow ( 2 , x ) - 1 ; let st = new Array ( max_size ) ; constructSTUtil ( arr , 0 , n - 1 , st , 0 ) ; return st ; } function OddDivisorsSum ( n , q , arr , Query ) { for ( let i = 0 ; i < n ; i ++ ) { let sq = Math . sqrt ( arr [ i ] ) ; if ( sq * sq != arr [ i ] ) arr [ i ] = 0 ; } let st = constructST ( arr , n ) ; for ( let i = 0 ; i < q ; i ++ ) { let l = Query [ i ] [ 0 ] ; let r = Query [ i ] [ 1 ] ; document . write ( getSum ( st , n , l , r ) + " " ) ; } } let arr = [ 2 , 4 , 5 , 6 , 9 ] ; let N = arr . length ; let Q = 3 ; let Query = [ [ 0 , 2 ] , [ 1 , 3 ] , [ 1 , 4 ] ] ; OddDivisorsSum ( N , Q , arr , Query ) ;
Queries to calculate sum of array elements consisting of odd number of divisors | Function to find the sum of elements having odd number of divisors in index range [ L , R ] for Q queries ; Initialize the dp [ ] array ; Traverse the array , arr [ ] ; If a [ i ] is a perfect square , then update value of DP [ i ] to a [ i ] ; Find the prefix sum of DP [ ] array ; Iterate through all the queries ; Find the sum for each query ; Driver Code
function OddDivisorsSum ( n , q , a , Query ) { let DP = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { DP [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { let x = Math . floor ( Math . sqrt ( a [ i ] ) ) ; if ( x * x == a [ i ] ) DP [ i ] = a [ i ] ; } for ( let i = 1 ; i < n ; i ++ ) { DP [ i ] = DP [ i - 1 ] + DP [ i ] ; } for ( let i = 0 ; i < q ; i ++ ) { let l = Query [ i ] [ 0 ] ; let r = Query [ i ] [ 1 ] ; if ( l == 0 ) { document . write ( DP [ r ] + " " ) ; } else { document . write ( DP [ r ] - DP [ l - 1 ] + " " ) ; } } } let arr = [ 2 , 4 , 5 , 6 , 9 ] ; let N = arr . length ; let Q = 3 let Query = [ [ 0 , 2 ] , [ 1 , 3 ] , [ 1 , 4 ] ] ; OddDivisorsSum ( N , Q , arr , Query ) ;