codeparrot/xlcost-text-to-code huggingface dataset:
Maximize the subarray sum after multiplying all elements of any subarray with X | JavaScript implementation of the approach ; Function to return the maximum sum ; Base case ; If already calculated ; If no elements have been chosen ; Do not choose any element and use Kadane 's algorithm by taking max ; Choose the sub - array and multiply x ; Choose the sub - array and multiply x ; End the sub - array multiplication ; } else No more multiplication ; Memoize and return the answer ; Function to get the maximum sum ; Initialize dp with - 1 ; Iterate from every position and find the maximum sum which is possible ; Driver code
var N = 5 ; function func ( idx , cur , a , dp , n , x ) { if ( idx == n ) { return 0 ; } if ( dp [ idx ] [ cur ] != - 1 ) { return dp [ idx ] [ cur ] ; } var ans = 0 ; if ( cur == 0 ) { ans = Math . max ( ans , a [ idx ] + func ( idx + 1 , 0 , a , dp , n , x ) ) ; ans = Math . max ( ans , x * a [ idx ] + func ( idx + 1 , 1 , a , dp , n , x ) ) ; } else if ( cur == 1 ) { ans = Math . max ( ans , x * a [ idx ] + func ( idx + 1 , 1 , a , dp , n , x ) ) ; ans = Math . max ( ans , a [ idx ] + func ( idx + 1 , 2 , a , dp , n , x ) ) ; { ans = Math . max ( ans , a [ idx ] + func ( idx + 1 , 2 , a , dp , n , x ) ) ; } return dp [ idx ] [ cur ] = ans ; } function getMaximumSum ( a , n , x ) { var dp = Array ( n ) . fill ( ) . map ( ( ) => Array ( 3 ) . fill ( 0 ) ) ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < 3 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } var maxi = 0 ; for ( i = 0 ; i < n ; i ++ ) { maxi = Math . max ( maxi , func ( i , 0 , a , dp , n , x ) ) ; } return maxi ; } var a = [ - 3 , 8 , - 2 , 1 , - 6 ] ; var n = a . length ; var x = - 1 ; document . write ( getMaximumSum ( a , n , x ) ) ;
Count pairs of non | javascript implementation of the approach ; Pre - processing function ; Get the size of the string ; Initially mark every position as false ; For the length ; Iterate for every index with length j ; If the length is less than 2 ; If characters are equal ; Check for equal ; Function to return the number of pairs ; Create the dp table initially ; Declare the left array ; Declare the right array ; Initially left [ 0 ] is 1 ; Count the number of palindrome pairs to the left ; Initially right most as 1 ; Count the number of palindrome pairs to the right ; Count the number of pairs ; Driver code
var N = 100 ; function pre_process ( dp , s ) { var n = s . length ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] = false ; } } for ( j = 1 ; j <= n ; j ++ ) { for ( i = 0 ; i <= n - j ; i ++ ) { if ( j <= 2 ) { if ( s [ i ] == s [ i + j - 1 ] ) { dp [ i ] [ i + j - 1 ] = true ; } } else if ( s [ i ] == s [ i + j - 1 ] ) { dp [ i ] [ i + j - 1 ] = dp [ i + 1 ] [ i + j - 2 ] ; } } } } function countPairs ( s ) { var dp = Array ( N ) . fill ( ) . map ( ( ) => Array ( N ) . fill ( false ) ) ; pre_process ( dp , s ) ; var n = s . length ; var left = Array ( n ) . fill ( 0 ) ; var right = Array ( n ) . fill ( 0 ) ; left [ 0 ] = 1 ; for ( i = 1 ; i < n ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) { if ( dp [ j ] [ i ] == true ) { left [ i ] ++ ; } } } right [ n - 1 ] = 1 ; for ( i = n - 2 ; i >= 0 ; i -- ) { right [ i ] = right [ i + 1 ] ; for ( j = n - 1 ; j >= i ; j -- ) { if ( dp [ i ] [ j ] == true ) { right [ i ] ++ ; } } } var ans = 0 ; for ( i = 0 ; i < n - 1 ; i ++ ) { ans += left [ i ] * right [ i + 1 ] ; } return ans ; } var s = " " ; document . write ( countPairs ( s ) ) ;
Queries to check if substring [ L ... R ] is palindrome or not | javascript implementation of the approach ; Pre - processing function ; Get the size of the string ; Initially mark every position as false ; For the length ; Iterate for every index with length j ; If the length is less than 2 ; If characters are equal ; Check for equal ; Function to answer every query in O ( 1 ) ; Driver code
var N = 100 ; function pre_process ( dp , s ) { var n = s . length ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] = false ; } } for ( j = 1 ; j <= n ; j ++ ) { for ( i = 0 ; i <= n - j ; i ++ ) { if ( j <= 2 ) { if ( s [ i ] == s [ i + j - 1 ] ) { dp [ i ] [ i + j - 1 ] = true ; } } else if ( s [ i ] == s [ i + j - 1 ] ) { dp [ i ] [ i + j - 1 ] = dp [ i + 1 ] [ i + j - 2 ] ; } } } } function answerQuery ( l , r , dp ) { if ( dp [ l ] [ r ] ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var s = " " ; var dp = Array ( N ) . fill ( ) . map ( ( ) => Array ( N ) . fill ( ) ) ; pre_process ( dp , s ) ; var queries = [ [ 0 , 1 ] , [ 1 , 5 ] ] ; var q = queries . length ; for ( i = 0 ; i < q ; i ++ ) { answerQuery ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , dp ) ; }
Length of the longest increasing subsequence such that no two adjacent elements are coprime | Javascript program to find the length of the longest increasing sub sequence from the given array such that no two adjacent elements are co prime ; Function to find the length of the longest increasing sub sequence from the given array such that no two adjacent elements are co prime ; To store dp and d value ; To store required answer ; For all elements in the array ; Initially answer is one ; For all it 's divisors ; Update the dp value ; Update the divisor value ; Check for required answer ; Update divisor of a [ i ] ; Return required answer ; Driver code
let N = 100005 ; function LIS ( a , n ) { let dp = new Array ( ) ; let d = new Array ( ) ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { dp [ a [ i ] ] = 1 ; for ( j = 2 ; j * j <= a [ i ] ; j ++ ) { if ( a [ i ] % j == 0 ) { dp [ a [ i ] ] = Math . max ( dp [ a [ i ] ] , dp [ d [ j ] ] + 1 ) ; dp [ a [ i ] ] = Math . max ( dp [ a [ i ] ] , dp [ d [ a [ i ] / j ] ] + 1 ) ; d [ j ] = a [ i ] ; d [ a [ i ] / j ] = a [ i ] ; } } ans = Math . max ( ans , dp [ a [ i ] ] ) ; d [ a [ i ] ] = a [ i ] ; } return ans ; } let a = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; let n = a . length ; document . write ( LIS ( a , n ) ) ;
Find the number of binary strings of length N with at least 3 consecutive 1 s | Function that returns true if s contains three consecutive 1 's ; Function to return the count of required strings ; Driver code
function check ( s ) { let n = s . length ; for ( let i = 2 ; i < n ; i ++ ) { if ( s [ i ] == ' ' && s [ i - 1 ] == ' ' && s [ i - 2 ] == ' ' ) return true ; } return false ; } function countStr ( i , s ) { if ( i < 0 ) { if ( check ( s ) ) return 1 ; return 0 ; } let myNameChars = s . split ( " " ) ; myNameChars [ i ] = ' ' ; s = ( myNameChars ) . join ( " " ) ; let ans = countStr ( i - 1 , s ) ; let myChar = s . split ( " " ) ; myChar [ i ] = ' ' ; s = ( myChar ) . join ( " " ) ; ans += countStr ( i - 1 , s ) ; let myChar1 = s . split ( " " ) ; myChar1 [ i ] = ' ' ; s = ( myChar1 ) . join ( " " ) ; return ans ; } let N = 4 ; let s = " " ; document . write ( countStr ( N - 1 , s ) ) ;
Find the number of binary strings of length N with at least 3 consecutive 1 s | Javascript implementation of the above approach ; Function to return the count of required strings ; '0' at ith position ; '1' at ith position ; Driver code ; Base condition : 2 ^ ( i + 1 ) because of 0 indexing
var n ; function solve ( i , x , dp ) { if ( i < 0 ) { return x == 3 ? 1 : 0 ; } if ( dp [ i ] [ x ] != - 1 ) { return dp [ i ] [ x ] ; } dp [ i ] [ x ] = solve ( i - 1 , 0 , dp ) ; dp [ i ] [ x ] += solve ( i - 1 , x + 1 , dp ) ; return dp [ i ] [ x ] ; } n = 4 ; var dp = Array ( n ) . fill ( ) . map ( ( ) => Array ( 4 ) . fill ( 0 ) ) ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < 4 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } for ( i = 0 ; i < n ; i ++ ) { dp [ i ] [ 3 ] = ( 1 << ( i + 1 ) ) ; } document . write ( solve ( n - 1 , 0 , dp ) ) ;
Find the sum of the diagonal elements of the given N X N spiral matrix | Function to return the sum of both the diagonal elements of the required matrix ; Array to store sum of diagonal elements ; Base cases ; Computing the value of dp ; Driver code
let findSum ( n ) { let dp = new Array ( n + 1 ) ; dp [ 1 ] = 1 ; dp [ 0 ] = 0 ; for ( let i = 2 ; i <= n ; i ++ ) { dp [ i ] = ( 4 * ( i * i ) ) - 6 * ( i - 1 ) + dp [ i - 2 ] ; } return dp [ n ] ; } let n = 4 ; document . write ( findSum ( n ) ) ;
Count number of ways to reach a given score in a Matrix | Javascript implementation of the approach ; To store the states of dp ; To check whether a particular state of dp has been solved ; Function to find the ways using memoization ; Base cases ; If required score becomes negative ; If current state has been reached before ; Set current state to visited ; Driver code
var n = 3 ; var MAX = 30 ; var dp = Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { dp [ i ] = Array ( n ) ; for ( var j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] = Array ( MAX ) ; } } var v = Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { v [ i ] = Array ( n ) ; for ( var j = 0 ; j < n ; j ++ ) { v [ i ] [ j ] = Array ( MAX ) ; } } function findCount ( mat , i , j , m ) { if ( i == 0 && j == 0 ) { if ( m == mat [ 0 ] [ 0 ] ) return 1 ; else return 0 ; } if ( m < 0 ) return 0 ; if ( i < 0 j < 0 ) return 0 ; if ( v [ i ] [ j ] [ m ] ) return dp [ i ] [ j ] [ m ] ; v [ i ] [ j ] [ m ] = true ; dp [ i ] [ j ] [ m ] = findCount ( mat , i - 1 , j , m - mat [ i ] [ j ] ) + findCount ( mat , i , j - 1 , m - mat [ i ] [ j ] ) ; return dp [ i ] [ j ] [ m ] ; } var mat = [ [ 1 , 1 , 1 ] , [ 1 , 1 , 1 ] , [ 1 , 1 , 1 ] ] ; var m = 5 ; document . write ( findCount ( mat , n - 1 , n - 1 , m ) ) ;
Number of ways of scoring R runs in B balls with at most W wickets | javascript implementation of the approach ; Function to return the number of ways to score R runs in B balls with at most W wickets ; If the wickets lost are more ; If runs scored are more ; If condition is met ; If no run got scored ; Already visited state ; If scored 0 run ; If scored 1 run ; If scored 2 runs ; If scored 3 runs ; If scored 4 runs ; If scored 6 runs ; If scored no run and lost a wicket ; Memoize and return ; Driver code
var mod = 1000000007 ; var RUNMAX = 300 ; var BALLMAX = 50 ; var WICKETMAX = 10 ; function CountWays ( r , b , l , R , B , W , dp ) { if ( l > W ) return 0 ; if ( r > R ) return 0 ; if ( b == B && r == R ) return 1 ; if ( b == B ) return 0 ; if ( dp [ r ] [ b ] [ l ] != - 1 ) return dp [ r ] [ b ] [ l ] ; var ans = 0 ; ans += CountWays ( r , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 1 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 2 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 3 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 4 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 6 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r , b + 1 , l + 1 , R , B , W , dp ) ; ans = ans % mod ; return dp [ r ] [ b ] [ l ] = ans ; } var R = 40 , B = 10 , W = 4 ; var dp = Array ( RUNMAX ) . fill ( ) . map ( ( ) => Array ( BALLMAX ) . fill ( ) . map ( ( ) => Array ( WICKETMAX ) . fill ( 0 ) ) ) ; for ( i = 0 ; i < RUNMAX ; i ++ ) for ( j = 0 ; j < BALLMAX ; j ++ ) for ( k = 0 ; k < WICKETMAX ; k ++ ) dp [ i ] [ j ] [ k ] = - 1 ; document . write ( CountWays ( 0 , 0 , 0 , R , B , W , dp ) ) ;
Minimum steps to delete a string by deleting substring comprising of same characters | JavaScript implementation of the approach ; Function to return the minimum number of delete operations ; When a single character is deleted ; When a group of consecutive characters are deleted if any of them matches ; When both the characters are same then delete the letters in between them ; Memoize ; Driver code
var N = 10 ; function findMinimumDeletion ( l , r , dp , s ) { if ( l > r ) return 0 ; if ( l == r ) return 1 ; if ( dp [ l ] [ r ] != - 1 ) return dp [ l ] [ r ] ; var res = 1 + findMinimumDeletion ( l + 1 , r , dp , s ) ; for ( var i = l + 1 ; i <= r ; ++ i ) { if ( s [ l ] == s [ i ] ) res = Math . min ( res , findMinimumDeletion ( l + 1 , i - 1 , dp , s ) + findMinimumDeletion ( i , r , dp , s ) ) ; } return dp [ l ] [ r ] = res ; } var s = " " ; var n = s . length ; var dp = Array . from ( Array ( N ) , ( ) => Array ( N ) . fill ( - 1 ) ) ; document . write ( findMinimumDeletion ( 0 , n - 1 , dp , s ) ) ;
Ways to form a group from three groups with given constraints | javascript program to find the number of ways to form the group of peopls ; Function to pre - compute the Combination using DP ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; return C [ n ] [ k ] ; ; Function to find the number of ways ; Function to pre - compute ; Sum the Zci ; Iterate for second position ; Iterate for first position ; Multiply the common Combination value ; Driver Code
var C = Array ( 1000 ) . fill ( ) . map ( ( ) => Array ( 1000 ) . fill ( 0 ) ) ; function binomialCoeff ( n ) { var i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } } function numberOfWays ( x , y , z ) { binomialCoeff ( Math . max ( x , Math . max ( y , z ) ) ) ; var sum = 0 ; for ( i = 1 ; i <= z ; i ++ ) { sum = ( sum + C [ z ] [ i ] ) ; } var sum1 = 0 ; for ( i = 1 ; i <= y ; i ++ ) { for ( j = i + 1 ; j <= x ; j ++ ) { sum1 = ( sum1 + ( C [ y ] [ i ] * C [ x ] [ j ] ) ) ; } } sum1 = ( sum * sum1 ) ; return sum1 ; } var x = 3 ; var y = 2 ; var z = 1 ; document . write ( numberOfWays ( x , y , z ) ) ;
Minimum cost to make a string free of a subsequence | Function to return the Math . minimum cost ; Traverse the string ; Math . min Cost to remove ' c ' ; Math . min Cost to remove subsequence " co " ; Math . min Cost to remove subsequence " cod " ; Math . min Cost to remove subsequence " code " ; Return the Math . minimum cost ; Driver program
function findCost ( str , arr , n ) { var costofC = 0 , costofO = 0 , costofD = 0 , costofE = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) costofC += arr [ i ] ; else if ( str [ i ] == ' ' ) costofO = Math . min ( costofC , costofO + arr [ i ] ) ; else if ( str [ i ] == ' ' ) costofD = Math . min ( costofO , costofD + arr [ i ] ) ; else if ( str [ i ] == ' ' ) costofE = Math . min ( costofD , costofE + arr [ i ] ) ; } return costofE ; } var str = " " ; var arr = [ 1 , 2 , 1 , 3 , 4 , 2 , 6 , 4 , 6 , 2 , 3 , 3 , 3 , 2 ] ; var n = arr . length ; document . write ( findCost ( str , arr , n ) ) ;
Longest subsequence such that adjacent elements have at least one common digit | Returns length of maximum length subsequence ; To store the length of the maximum length subsequence ; To store current element arr [ i ] ; To store the length of the sub - sequence ending at index i and having common digit d ; To store digits present in current element ; To store length of maximum length subsequence ending at index i ; For first element maximum length is 1 for each digit ; Find digits of each element , then find length of subsequence for each digit and then find local maximum ; Find digits in current element ; For each digit present find length of subsequence and find local maximum ; Update value of dp [ i ] [ d ] for each digit present in current element to local maximum found . ; Update maximum length with local maximum ; Driver code
function findSubsequence ( arr , n ) { let len = 1 ; let tmp ; let i , j , d ; let dp = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { dp [ i ] = new Array ( 10 ) ; for ( let j = 0 ; j < 10 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } let cnt = new Array ( 10 ) ; let locMax ; tmp = arr [ 0 ] ; while ( tmp > 0 ) { dp [ 0 ] [ tmp % 10 ] = 1 ; tmp = Math . floor ( tmp / 10 ) ; } for ( i = 1 ; i < n ; i ++ ) { tmp = arr [ i ] ; locMax = 1 ; for ( let x = 0 ; x < 10 ; x ++ ) cnt [ x ] = 0 ; while ( tmp > 0 ) { cnt [ tmp % 10 ] = 1 ; tmp = Math . floor ( tmp / 10 ) ; } for ( d = 0 ; d <= 9 ; d ++ ) { if ( cnt [ d ] > 0 ) { dp [ i ] [ d ] = 1 ; for ( j = 0 ; j < i ; j ++ ) { dp [ i ] [ d ] = Math . max ( dp [ i ] [ d ] , dp [ j ] [ d ] + 1 ) ; locMax = Math . max ( dp [ i ] [ d ] , locMax ) ; } } } for ( d = 0 ; d <= 9 ; d ++ ) { if ( cnt [ d ] > 0 ) { dp [ i ] [ d ] = locMax ; } } len = Math . max ( len , locMax ) ; } return len ; } let arr = [ 1 , 12 , 44 , 29 , 33 , 96 , 89 ] ; let n = arr . length ; document . write ( findSubsequence ( arr , n ) ) ;
Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime | JavaScript implementation of the above approach ; Prime numbers upto 100 ; Function to return the count of required numbers from 0 to num ; Base Case ; check if the difference is equal to any prime number ; If this result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is 1 , means number has already become smaller so we can place any digit , otherwise num [ pos ] ; If the current position is odd add it to currOdd , otherwise to currEven ; Function to convert x into its digit vector and uses count ( ) function to return the required count ; Initialize dp ; Driver Code
var M = 18 ; var a , b ; var dp = Array . from ( Array ( M ) , ( ) => Array ( 90 ) ) ; var prime = [ 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97 ] ; function count ( pos , even , odd , tight , num ) { if ( pos == num . length ) { if ( num . length & 1 ) [ odd , even ] = [ even , odd ] var d = even - odd ; for ( var i = 0 ; i < 24 ; i ++ ) if ( d == prime [ i ] ) return 1 ; return 0 ; } if ( dp [ pos ] [ even ] [ odd ] [ tight ] != - 1 ) return dp [ pos ] [ even ] [ odd ] [ tight ] ; var ans = 0 ; var limit = ( tight ? 9 : num [ pos ] ) ; for ( var d = 0 ; d <= limit ; d ++ ) { var currF = tight , currEven = even ; var currOdd = odd ; if ( d < num [ pos ] ) currF = 1 ; if ( pos & 1 ) currOdd += d ; else currEven += d ; ans += count ( pos + 1 , currEven , currOdd , currF , num ) ; } return dp [ pos ] [ even ] [ odd ] [ tight ] = ans ; } function solve ( x ) { var num = [ ] ; while ( x ) { num . push ( x % 10 ) ; x = parseInt ( x / 10 ) ; } num . reverse ( ) ; for ( var i = 0 ; i < M ; i ++ ) for ( var j = 0 ; j < 90 ; j ++ ) dp [ i ] [ j ] = Array . from ( Array ( 90 ) , ( ) => Array ( 2 ) . fill ( - 1 ) ) return count ( 0 , 0 , 0 , 0 , num ) ; } var L = 1 , R = 50 ; document . write ( solve ( R ) - solve ( L - 1 ) + " " ) ; L = 50 , R = 100 ; document . write ( solve ( R ) - solve ( L - 1 ) ) ;
Find the number of distinct pairs of vertices which have a distance of exactly k in a tree | Javascript implementation of the approach ; To store vertices and value of k ; To store number vertices at a level i ; To store the final answer ; Function to add an edge between two nodes ; Function to find the number of distinct pairs of the vertices which have a distance of exactly k in a tree ; At level zero vertex itself is counted ; Count the pair of vertices at distance k ; For all levels count vertices ; Driver code ; Add edges ; Function call ; Required answer
let N = 5005 ; let n , k ; let gr = new Array ( N ) ; let d = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { d [ i ] = new Array ( 505 ) ; for ( let j = 0 ; j < 505 ; j ++ ) { d [ i ] [ j ] = 0 ; } } let ans = 0 ; function Add_edge ( x , y ) { gr [ x ] . push ( y ) ; gr [ y ] . push ( x ) ; } function dfs ( v , par ) { d [ v ] [ 0 ] = 1 ; for ( let i = 0 ; i < gr [ v ] . length ; i ++ ) { if ( gr [ v ] [ i ] != par ) { dfs ( gr [ v ] [ i ] , v ) ; for ( let j = 1 ; j <= k ; j ++ ) ans += d [ gr [ v ] [ i ] ] [ j - 1 ] * d [ v ] [ k - j ] ; for ( let j = 1 ; j <= k ; j ++ ) d [ v ] [ j ] += d [ gr [ v ] [ i ] ] [ j - 1 ] ; } } } n = 5 ; k = 2 ; for ( let i = 0 ; i < N ; i ++ ) gr [ i ] = [ ] ; Add_edge ( 1 , 2 ) ; Add_edge ( 2 , 3 ) ; Add_edge ( 3 , 4 ) ; Add_edge ( 2 , 5 ) ; dfs ( 1 , 0 ) ; document . write ( ans ) ;
Sum of XOR of all subarrays | Function to calculate the sum of XOR of all subarrays ; variable to store the final sum ; multiplier ; variable to store number of sub - arrays with odd number of elements with ith bits starting from the first element to the end of the array ; variable to check the status of the odd - even count while calculating c_odd ; loop to calculate initial value of c_odd ; loop to iterate through all the elements of the array and update sum ; updating the multiplier ; returning the sum ; Driver Code
function findXorSum ( arr , n ) { let sum = 0 ; let mul = 1 ; for ( let i = 0 ; i < 30 ; i ++ ) { let c_odd = 0 ; let odd = 0 ; for ( let j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) > 0 ) odd = ( ! odd ) ; if ( odd ) c_odd ++ ; } for ( let j = 0 ; j < n ; j ++ ) { sum += ( mul * c_odd ) ; if ( ( arr [ j ] & ( 1 << i ) ) > 0 ) c_odd = ( n - j - c_odd ) ; } mul *= 2 ; } return sum ; } let arr = [ 3 , 8 , 13 ] ; let n = arr . length ; document . write ( findXorSum ( arr , n ) ) ;
Minimum replacements to make adjacent characters unequal in a ternary string | Set | function to return integer value of i - th character in the string ; Function to count the number of minimal replacements ; If the string has reached the end ; If the state has been visited previously ; Get the current value of character ; If it is equal then change it ; All possible changes ; Change done ; } else If same no change ; Driver Code ; Length of string ; Create a DP array ; First character ; Function to find minimal replacements
function charVal ( s , i ) { if ( s . charAt ( i ) == ' ' ) { return 0 ; } else if ( s . charAt ( i ) == ' ' ) { return 1 ; } else { return 2 ; } } function countMinimalReplacements ( s , i , prev , dp , n ) { if ( i == n ) { return 0 ; } if ( dp [ i ] [ prev ] != - 1 ) { return dp [ i ] [ prev ] ; } var val = charVal ( s , i ) ; var ans = Number . MAX_VALUE ; if ( val == prev ) { val = 0 ; for ( var cur = 0 ; cur <= 2 ; cur ++ ) { if ( cur == prev ) { continue ; } val = 1 + countMinimalReplacements ( s , i + 1 , cur , dp , n ) ; ans = Math . min ( ans , val ) ; } { ans = countMinimalReplacements ( s , i + 1 , val , dp , n ) ; } return dp [ i ] [ val ] = ans ; } var s = " " ; var n = s . length ; var dp = Array ( n ) . fill ( ) . map ( ( ) => Array ( 3 ) . fill ( 0 ) ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < 3 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } var val = charVal ( s , 0 ) ; document . write ( countMinimalReplacements ( s , 1 , val , dp , n ) ) ;