codeparrot/xlcost-text-to-code huggingface dataset:
Minimum cost required to rearrange a given array to make it equal to another given array | Function to find lower_bound ; Function to find length of the longest common subsequence ; Find position where element is to be inserted ; Return the length of LCS ; Function to find the minimum cost required to convert the sequence A exactly same as B ; Auxiliary array ; Stores positions of elements of A ; Initialize index array with - 1 ; Update the index array with index of corresponding elements of B ; Place only A 's array values  with its mapped values  into nums array ; Find LCS ; No of elements to be added in array A ; Stores minimum cost ; Prvar the minimum cost ; Given array A ; Given C ; Size of arr A ; Size of arr B ; Function call
function LowerBound ( a , k , x ) { var l = - 1 ; var r = k ; while ( l + 1 < r ) { var m = ( l + r ) >>> 1 ; if ( a [ m ] >= x ) { r = m ; } else { l = m ; } } return r ; } function findLCS ( nums , N ) { var k = 0 ; for ( i = 0 ; i < N ; i ++ ) { var pos = LowerBound ( nums , k , nums [ i ] ) ; nums [ pos ] = nums [ i ] ; if ( k == pos ) { k = pos + 1 ; } } return k ; } function minimumCost ( A , B , M , N , C ) { var nums = Array ( 100000 ) . fill ( 0 ) ; var index = Array ( 100000 ) . fill ( 0 ) ; for ( i = 0 ; i < 100000 ; i ++ ) index [ i ] = - 1 ; for ( i = 0 ; i < N ; i ++ ) { index [ B [ i ] ] = i ; } var k = 0 ; for ( i = 0 ; i < M ; i ++ ) { if ( index [ A [ i ] ] != - 1 ) { nums [ k ++ ] = index [ A [ i ] ] ; } } var lcs_length = findLCS ( nums , k ) ; var elements_to_be_added = N - lcs_length ; var min_cost = elements_to_be_added * C ; document . write ( min_cost ) ; return 0 ; } var A = [ 1 , 6 , 3 , 5 , 10 ] ; var B = [ 3 , 1 , 5 ] ; var C = 2 ; var M = A . length ; var N = B . length ; minimumCost ( A , B , M , N , C ) ;
Maximize count of array elements required to obtain given sum | Function that count the maximum number of elements to obtain sum V ; Stores the maximum number of elements required to obtain V ; Base Case ; Initialize all table values as Infinite ; Find the max arr required for all values from 1 to V ; Go through all arr smaller than i ; If current coin value is less than i ; Update table [ i ] ; Return the final count ; Given array ; Given sum V ; Function Call
function maxCount ( arr , m , V ) { let table = [ ] ; table [ 0 ] = 0 ; for ( let i = 1 ; i <= V ; i ++ ) table [ i ] = - 1 ; for ( let i = 1 ; i <= V ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { if ( arr [ j ] <= i ) { let sub_res = table [ i - arr [ j ] ] ; if ( sub_res != - 1 && sub_res + 1 > table [ i ] ) table [ i ] = sub_res + 1 ; } } } return table [ V ] ; } let arr = [ 25 , 10 , 5 ] ; let m = arr . length ; let V = 30 ; document . write ( maxCount ( arr , m , V ) ) ;
Maximize path sum from top | Javascript program to implement the above approach ; Function to get the maximum path sum from top - left cell to all other cells of the given matrix ; Store the maximum path sum ; Loop to create 2D array using 1D array ; Base case ; Compute the value of dp [ i ] [ j ] using the recurrence relation ; Prlet maximum path sum from the top - left cell ; Driver Code
let SZ = 100 ; function pathSum ( mat , N , M ) { let dp = new Array ( N ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] ; } for ( let j = 1 ; j < M ; j ++ ) { dp [ 0 ] [ j ] = mat [ 0 ] [ j ] + dp [ 0 ] [ j - 1 ] ; } for ( let i = 1 ; i < N ; i ++ ) { for ( let j = 1 ; j < M ; j ++ ) { dp [ i ] [ j ] = mat [ i ] [ j ] + Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { document . write ( dp [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let mat = [ [ 3 , 2 , 1 ] , [ 6 , 5 , 4 ] , [ 7 , 8 , 9 ] ] ; let N = 3 ; let M = 3 ; pathSum ( mat , N , M ) ;
Minimum repeated addition of even divisors of N required to convert N to M | INF is the maximum value which indicates Impossible state ; Stores the dp states ; Recursive Function that considers all possible even divisors of cur ; Indicates impossible state ; Check dp [ cur ] is already calculated or not ; Initially it is set to INF that meanswe cur can 't be transform to M ; Loop to find even divisors of cur ; if i is divisor of cur ; if i is even ; Find recursively for cur + i ; Check another divisor ; Find recursively for cur + ( cur / i ) ; Finally store the current state result and return the answer ; Function that counts the minimum operation to reduce N to M ; Initialise dp state ; Function Call ; Given N and M ; Function Call ; INF indicates impossible state
var INF = 10000000 ; var max_size = 100007 ; var dp = Array ( max_size ) ; function min_op ( cur , M ) { if ( cur > M ) return INF ; if ( cur == M ) return 0 ; if ( dp [ cur ] != - 1 ) return dp [ cur ] ; var op = INF ; for ( var i = 2 ; i * i <= cur ; i ++ ) { if ( cur % i == 0 ) { if ( i % 2 == 0 ) { op = Math . min ( op , 1 + min_op ( cur + i , M ) ) ; } if ( ( cur / i ) != i && ( cur / i ) % 2 == 0 ) { op = Math . min ( op , 1 + min_op ( cur + ( cur / i ) , M ) ) ; } } } return dp [ cur ] = op ; } function min_operations ( N , M ) { for ( i = N ; i <= M ; i ++ ) { dp [ i ] = - 1 ; } return min_op ( N , M ) ; } var N = 6 , M = 24 ; var op = min_operations ( N , M ) ; if ( op >= INF ) document . write ( " " ) ; else document . write ( op + " " ) ;
Maximize Sum possible from an Array by the given moves | Function to find the maximum sum possible by given moves from the array ; Checking for boundary ; If previously computed subproblem occurs ; If element can be moved left ; Calculate maximum possible sum by moving left from current index ; If element can be moved right ; Calculate maximum possible sum by moving right from current index and update the maximum sum ; Store the maximum sum ; Driver Code ; Function call
function maxValue ( a , n , pos , moves , left , dp ) { if ( moves == 0 || ( pos > n - 1 pos < 0 ) ) return 0 ; if ( dp [ pos ] [ left ] != - 1 ) return dp [ pos ] [ left ] ; let value = 0 ; if ( left > 0 && pos >= 1 ) value = Math . max ( value , a [ pos ] + maxValue ( a , n , pos - 1 , moves - 1 , left - 1 , dp ) ) ; if ( pos <= n - 1 ) value = Math . max ( value , a [ pos ] + maxValue ( a , n , pos + 1 , moves - 1 , left , dp ) ) ; return dp [ pos ] [ left ] = value ; } let n = 5 ; let a = [ 1 , 5 , 4 , 3 , 2 ] ; let k = 1 ; let m = 4 ; let dp = new Array ( n + 1 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < dp . length ; i ++ ) { for ( var j = 0 ; j < dp . length ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } document . write ( ( a [ 0 ] + maxValue ( a , n , 1 , m , k , dp ) ) ) ;
Maximize the Sum of a Subsequence from an Array based on given conditions | JavaScript program to implement the above approach ; Function to select the array elements to maximize the sum of the selected elements ; If the entire array is solved ; Memoized subproblem ; Calculate sum considering the current element in the subsequence ; Calculate sum without considering the current element in the subsequence ; Update the maximum of the above sums in the dp [ ] [ ] table ; Driver Code ; Initialize the dp array ; Function call
var N = 6 ; function maximumSum ( a , count , index , n , dp ) { if ( index == n ) return 0 ; if ( dp [ index ] [ count ] != - 1 ) return dp [ index ] [ count ] ; var take_element = a [ index ] * count + maximumSum ( a , count + 1 , index + 1 , n , dp ) ; var dont_take = maximumSum ( a , count , index + 1 , n , dp ) ; return dp [ index ] [ count ] = Math . max ( take_element , dont_take ) ; } var n = 5 ; var a = [ - 1 , - 9 , 0 , 5 , - 7 ] ; var dp = Array . from ( Array ( N ) , ( ) => Array ( N ) . fill ( - 1 ) ) ; document . write ( maximumSum ( a , 1 , 0 , n , dp ) ) ;
Minimum number of Nodes to be removed such that no subtree has more than K nodes | Variables used to store data globally ; Adjacency list representation of tree ; Function to perform DFS Traversal ; Mark the node as true ; Traverse adjacency list of child node ; If already visited then omit the node ; Add number of nodes in subtree ; Increment the count ; Return the nodes ; Function to add edges in graph ; Function that finds the number of nodes to be removed such that every subtree has size at most K ; Function Call to find the number of nodes to remove ; Print Removed Nodes ; Creating list for all nodes ; Insert of nodes in graph ; Required subtree size ; Function Call
let N = 20 ; let visited = new Array ( N ) ; let K ; let removals = 0 ; let removed_nodes = [ ] ; let adj = [ ] ; function dfs ( s ) { visited [ s ] = true ; let nodes = 1 ; for ( let child = 0 ; child < adj [ s ] . length ; child ++ ) { if ( visited [ adj [ s ] [ child ] ] ) continue ; nodes += dfs ( adj [ s ] [ child ] ) ; } if ( nodes > K ) { removals ++ ; removed_nodes . push ( s ) ; nodes = 0 ; } return nodes ; } function addEdge ( a , b ) { adj [ a ] . push ( b ) ; adj [ b ] . push ( a ) ; } function findRemovedNodes ( K ) { dfs ( 1 ) ; document . write ( " " + " " + removals + " " ) ; document . write ( " " ) ; for ( let node = 0 ; node < removed_nodes . length ; node ++ ) document . write ( removed_nodes [ node ] + " " ) ; } for ( let i = 0 ; i < N ; i ++ ) adj . push ( [ ] ) ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 2 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 3 , 6 ) ; K = 3 ; findRemovedNodes ( K ) ;
Queries to find sum of distance of a given node to every leaf node in a Weighted Tree | JavaScript program for the above problem ; MAX size ; Graph with { destination , weight } ; ; For storing the sum for ith node ; Leaves in subtree of ith . ; dfs to find sum of distance of leaves in the subtree of a node ; Flag is the node is leaf or not ; ; Skipping if parent ; Setting flag to false ; Doing dfs call ; Doing calculation in postorder . ; If the node is leaf then we just increment the no . of leaves under the subtree of a node ; Adding num of leaves ; Calculating answer for the sum in the subtree ; dfs function to find the sum of distance of leaves outside the subtree ; Number of leaves other than the leaves in the subtree of i ; Adding the contribution of leaves outside to the ith node ; Adding the leafs outside to ith node 's leaves. ; Calculating the sum of distance of leaves in the subtree of a node assuming the root of the tree is 1 ; Calculating the sum of distance of leaves outside the subtree of node assuming the root of the tree is 1 ; Answering the queries ; ; 1 ( 4 ) / \ ( 2 ) / \ 4 2 ( 5 ) / \ ( 3 ) / \ 5 3 ; Initialising tree ; System . out . println ( v ) ;
class pair { constructor ( f , s ) { this . first = f ; this . second = s ; } } let N = 1e5 + 5 ; let v = [ ] ; let dp = new Array ( N ) ; let leaves = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { dp [ i ] = 0 ; leaves [ i ] = 0 ; } let n ; function dfs ( a , par ) { let leaf = 1 ; for ( let i = 0 ; i < v [ a ] . length ; i ++ ) { if ( v [ a ] [ i ] . first == par ) continue ; leaf = 0 ; dfs ( v [ a ] [ i ] . first , a ) ; } if ( leaf == 1 ) { leaves [ a ] += 1 ; } else { for ( let i = 0 ; i < v [ a ] . length ; i ++ ) { if ( v [ a ] [ i ] . first == par ) continue ; leaves [ a ] += leaves [ v [ a ] [ i ] . first ] ; dp [ a ] = dp [ a ] + dp [ v [ a ] [ i ] . first ] + leaves [ v [ a ] [ i ] . first ] * v [ a ] [ i ] . second ; } } } function dfs2 ( a , par ) { for ( let i = 0 ; i < v [ a ] . length ; i ++ ) { if ( v [ a ] [ i ] . first == par ) continue ; let leafOutside = leaves [ a ] - leaves [ v [ a ] [ i ] . first ] ; dp [ v [ a ] [ i ] . first ] += ( dp [ a ] - dp [ v [ a ] [ i ] . first ] ) ; dp [ v [ a ] [ i ] . first ] += v [ a ] [ i ] . second * ( leafOutside - leaves [ v [ a ] [ i ] . first ] ) ; leaves [ v [ a ] [ i ] . first ] += leafOutside ; dfs2 ( v [ a ] [ i ] . first , a ) ; } } function answerQueries ( queries ) { dfs ( 1 , 0 ) ; dfs2 ( 1 , 0 ) ; for ( let i = 0 ; i < queries . length ; i ++ ) { document . write ( dp [ queries [ i ] ] + " " ) ; } } n = 5 ; v = [ ] ; for ( let i = 0 ; i <= n ; i ++ ) v . push ( [ ] ) ; v [ 1 ] . push ( new pair ( 4 , 4 ) ) ; v [ 4 ] . push ( new pair ( 1 , 4 ) ) ; v [ 1 ] . push ( new pair ( 2 , 2 ) ) ; v [ 2 ] . push ( new pair ( 1 , 2 ) ) ; v [ 2 ] . push ( new pair ( 3 , 3 ) ) ; v [ 3 ] . push ( new pair ( 2 , 3 ) ) ; v [ 2 ] . push ( new pair ( 5 , 5 ) ) ; v [ 5 ] . push ( new pair ( 2 , 5 ) ) ; let queries = [ 1 , 3 , 5 ] ; answerQueries ( queries ) ;
Minimum changes required to make each path in a matrix palindrome | Function for counting changes ; Maximum distance possible is ( n - 1 + m - 1 ) ; Stores the maximum element ; Update the maximum ; Stores frequencies of values for respective distances ; Initialize frequencies of cells as 0 ; Count frequencies of cell values in the matrix ; Increment frequency of value at distance i + j ; Store the most frequent value at i - th distance from ( 0 , 0 ) and ( N - 1 , M - 1 ) ; Calculate max frequency and total cells at distance i ; Count changes required to convert all cells at i - th distance to most frequent value ; Driver Code
function countChanges ( matrix , n , m ) { let i , j , dist = n + m - 1 ; let Max_element = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { Max_element = Math . max ( Max_element , matrix [ i ] [ j ] ) ; } } let freq = new Array ( dist ) ; for ( i = 0 ; i < freq . length ; i ++ ) { freq [ i ] = new Array ( 2 ) ; } for ( i = 0 ; i < dist ; i ++ ) { for ( j = 0 ; j < Max_element + 1 ; j ++ ) freq [ i ] [ j ] = 0 ; } for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { freq [ i + j ] [ matrix [ i ] [ j ] ] ++ ; } } let min_changes_sum = 0 ; for ( i = 0 ; i < Math . floor ( dist / 2 ) ; i ++ ) { let maximum = 0 ; let total_values = 0 ; for ( j = 0 ; j < Max_element + 1 ; j ++ ) { maximum = Math . max ( maximum , freq [ i ] [ j ] + freq [ n + m - 2 - i ] [ j ] ) ; total_values += freq [ i ] [ j ] + freq [ n + m - 2 - i ] [ j ] ; } min_changes_sum += total_values - maximum ; } return min_changes_sum ; } let mat = [ [ 7 , 0 , 3 , 1 , 8 , 1 , 3 ] , [ 0 , 4 , 0 , 1 , 0 , 4 , 0 ] , [ 3 , 1 , 8 , 3 , 1 , 0 , 7 ] ] ; let minChanges = countChanges ( mat , 3 , 7 ) ; document . write ( minChanges ) ;
Count of numbers upto N having absolute difference of at most K between any two adjacent digits | Table to store solution of each subproblem ; Function to calculate all possible numbers ; Check if position reaches end that is equal to length of N ; Check if the result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is false , means number has already become smaller so we can place any digit , otherwise N [ pos ] ; Check if start is false the number has not started yet ; Check if we do not start the number at pos then recur forward ; If we start the number we can place any digit from 1 to upper_limit ; Finding the new tight ; Condition if the number has already started ; We can place digit upto upperbound & absolute difference with previous digit much be atmost K ; Absolute difference atmost K ; Store the solution to this subproblem ; Driver code ; Initialising the table with - 1 ; Function call
let dp = new Array ( 1002 ) ; function possibleNumbers ( pos , previous , tight , start , N , K ) { if ( pos == N . length ) return 1 ; if ( dp [ pos ] [ previous ] [ tight ] [ start ] != - 1 ) return dp [ pos ] [ previous ] [ tight ] [ start ] ; let res = 0 ; let upper_limit = ( tight == 1 ) ? ( N [ pos ] - ' ' ) : 9 ; let new_tight ; if ( start == 0 ) { res = possibleNumbers ( pos + 1 , previous , 0 , 0 , N , K ) ; for ( let i = 1 ; i <= upper_limit ; i ++ ) { new_tight = ( tight > 0 && i == upper_limit ) ? 1 : 0 ; res += possibleNumbers ( pos + 1 , i , new_tight , 1 , N , K ) ; } } else { for ( let i = 0 ; i <= upper_limit ; i ++ ) { new_tight = ( tight > 0 && i == upper_limit ) ? 1 : 0 ; if ( Math . abs ( i - previous ) <= K ) res += possibleNumbers ( pos + 1 , i , new_tight , 1 , N , K ) ; } } dp [ pos ] [ previous ] [ tight ] [ start ] = res ; return dp [ pos ] [ previous ] [ tight ] [ start ] ; } let N = " " ; let K = 2 ; for ( let i = 0 ; i < 1002 ; i ++ ) { dp [ i ] = new Array ( 10 ) ; for ( let j = 0 ; j < 10 ; j ++ ) { dp [ i ] [ j ] = new Array ( 2 ) ; for ( let k = 0 ; k < 2 ; k ++ ) { dp [ i ] [ j ] [ k ] = new Array ( 2 ) ; for ( let l = 0 ; l < 2 ; l ++ ) { dp [ i ] [ j ] [ k ] [ l ] = - 1 ; } } } } document . write ( possibleNumbers ( 0 , 0 , 1 , 0 , N , K ) + " " ) ;
Minimum cost of reducing Array by merging any adjacent elements repetitively | Function to find the total minimum cost of merging two consecutive numbers ; Find the size of numbers [ ] ; If array is empty , return 0 ; To store the prefix Sum of numbers array numbers [ ] ; Traverse numbers [ ] to find the prefix sum ; dp table to memoised the value ; For single numbers cost is zero ; Iterate for length >= 1 ; Find sum in range [ i . . j ] ; Initialise dp [ i ] [ j ] to INT_MAX ; Iterate for all possible K to find the minimum cost ; Update the minimum sum ; Return the final minimum cost ; Given set of numbers ; Function Call
function mergeTwoNumbers ( numbers ) { var len , i , j , k ; var n = numbers . length ; if ( numbers . length == 0 ) { return 0 ; } var prefixSum = Array ( n + 1 ) . fill ( 0 ) ; for ( var i = 1 ; i <= n ; i ++ ) { prefixSum [ i ] = prefixSum [ i - 1 ] + numbers [ i - 1 ] ; } var dp = Array . from ( Array ( n + 1 ) , ( ) => Array ( n + 1 ) ) ; for ( var i = 1 ; i <= n ; i ++ ) { dp [ i ] [ i ] = 0 ; } for ( len = 2 ; len <= n ; len ++ ) { for ( i = 1 ; i <= n - len + 1 ; i ++ ) { j = i + len - 1 ; var sum = prefixSum [ j ] - prefixSum [ i - 1 ] ; dp [ i ] [ j ] = 1000000000 ; for ( k = i ; k < j ; k ++ ) { dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , dp [ i ] [ k ] + dp [ k + 1 ] [ j ] + sum ) ; } } } return dp [ 1 ] [ n ] ; } var arr1 = [ 6 , 4 , 4 , 6 ] ; document . write ( mergeTwoNumbers ( arr1 ) )
Maximum sum possible for every node by including it in a segment of N | Stores the maximum sum possible for every node by including them in a segment with their successors ; Stores the maximum sum possible for every node by including them in a segment with their ancestors ; Store the maximum sum for every node by including it in a segment with its successors ; Update the maximum sums for each node by including them in a sequence with their ancestors ; Condition to check , if current node is not root ; Add edges ; Function to find the maximum answer for each node ; Compute the maximum sums with successors ; Store the computed maximums ; Update the maximum sums by including their ancestors ; Print the desired result ; Number of nodes ; Graph ; Add edges ; Weight of each node ; Compute the max sum of segments for each node ; Print the answer for every node
let dp1 = [ ] ; let dp2 = [ ] ; for ( let i = 0 ; i < 100005 ; i ++ ) { dp1 . push ( 0 ) ; dp2 . push ( 0 ) ; } function dfs1 ( u , par , g , weight ) { dp1 [ u ] = weight [ u ] ; for ( let c = 0 ; c < g [ u ] . length ; c ++ ) { if ( g [ u ] != par ) { dfs1 ( g [ u ] , u , g , weight ) ; dp1 [ u ] += Math . max ( 0 , dp1 [ g [ u ] ] ) ; } } } function dfs2 ( u , par , g , weight ) { if ( par != 0 ) { maxSumAncestors = dp2 [ par ] - Math . max ( 0 , dp1 [ u ] ) ; dp2 [ u ] = dp1 [ u ] + Math . max ( 0 , maxSumAncestors ) ; } for ( let c = 0 ; c < g [ u ] . length ; c ++ ) { if ( g [ u ] != par ) { dfs2 ( g [ u ] , u , g , weight ) ; } } } function addEdge ( u , v , g ) { g [ u ] . push ( v ) ; g [ v ] . push ( u ) ; } function maxSumSegments ( g , weight , n ) { dfs1 ( 1 , 0 , g , weight ) ; for ( let i = 1 ; i < n + 1 ; i ++ ) dp2 [ i ] = dp1 [ i ] ; dfs2 ( 1 , 0 , g , weight ) ; } function printAns ( n ) { for ( let i = 1 ; i < n ; i ++ ) document . write ( dp2 [ i ] + " " ) ; } let n = 7 , u = 0 , v = 0 ; let g = [ ] ; for ( let i = 0 ; i < 100005 ; i ++ ) { g . push ( [ ] ) ; } addEdge ( 1 , 2 , g ) ; addEdge ( 1 , 3 , g ) ; addEdge ( 2 , 4 , g ) ; addEdge ( 2 , 5 , g ) ; addEdge ( 3 , 6 , g ) ; addEdge ( 4 , 7 , g ) ; let weight = new Array ( n + 1 ) ; weight . fill ( 0 ) ; weight [ 1 ] = - 8 ; weight [ 2 ] = 9 ; weight [ 3 ] = 7 ; weight [ 4 ] = - 4 ; weight [ 5 ] = 5 ; weight [ 6 ] = - 10 ; weight [ 7 ] = - 6 ; maxSumSegments ( g , weight , n ) ; printAns ( n ) ;
Count of pairs in an Array with same number of set bits | Function to return the count of Pairs ; Get the maximum element ; Array to store count of bits of all elements upto maxm ; Store the set bits for powers of 2 ; Compute the set bits for the remaining elements ; Store the frequency of respective counts of set bits ; Driver Code
function countPairs ( arr , N ) { var maxm = arr . reduce ( ( a , b ) => Math . max ( a , b ) ) ; var i , k ; var bitscount = Array ( maxm + 1 ) . fill ( 0 ) ; for ( i = 1 ; i <= maxm ; i *= 2 ) bitscount [ i ] = 1 ; for ( i = 1 ; i <= maxm ; i ++ ) { if ( bitscount [ i ] == 1 ) k = i ; if ( bitscount [ i ] == 0 ) { bitscount [ i ] = bitscount [ k ] + bitscount [ i - k ] ; } } var setbits = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( setbits . has ( bitscount [ arr [ i ] ] ) ) setbits . set ( bitscount [ arr [ i ] ] , setbits . get ( bitscount [ arr [ i ] ] ) + 1 ) else setbits . set ( bitscount [ arr [ i ] ] , 1 ) } var ans = 0 ; setbits . forEach ( ( value , key ) => { ans += value * ( value - 1 ) / 2 ; } ) ; return ans ; } var N = 12 ; var arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ] ; document . write ( countPairs ( arr , N ) ) ;