codeparrot/xlcost-text-to-code huggingface dataset:
Queries to find sum of distance of a given node to every leaf node in a Weighted Tree | C # 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 ) ;
using System ; using System . Collections . Generic ; class GFG { static int N = 100005 ; static List < List < Tuple < int , int > > > v = new List < List < Tuple < int , int > > > ( ) ; static int [ ] dp = new int [ N ] ; static int [ ] leaves = new int [ N ] ; static void dfs ( int a , int par ) { bool leaf = true ; foreach ( Tuple < int , int > i in v [ a ] ) { if ( i . Item1 == par ) continue ; leaf = false ; dfs ( i . Item1 , a ) ; } if ( leaf == true ) { leaves [ a ] += 1 ; } else { foreach ( Tuple < int , int > i in v [ a ] ) { if ( i . Item1 == par ) continue ; leaves [ a ] += leaves [ i . Item1 ] ; dp [ a ] = ( dp [ a ] + dp [ i . Item1 ] + leaves [ i . Item1 ] * i . Item2 ) ; } } } static void dfs2 ( int a , int par ) { foreach ( Tuple < int , int > i in v [ a ] ) { if ( i . Item1 == par ) continue ; int leafOutside = leaves [ a ] - leaves [ i . Item1 ] ; dp [ i . Item1 ] += ( dp [ a ] - dp [ i . Item1 ] ) ; dp [ i . Item1 ] += i . Item2 * ( leafOutside - leaves [ i . Item1 ] ) ; leaves [ i . Item1 ] += leafOutside ; dfs2 ( i . Item1 , a ) ; } } static void answerQueries ( List < int > queries ) { dfs ( 1 , 0 ) ; dfs2 ( 1 , 0 ) ; for ( int i = 0 ; i < queries . Count ; i ++ ) Console . WriteLine ( dp [ queries [ i ] ] ) ; } static void Main ( ) { for ( int i = 0 ; i < N ; i ++ ) { v . Add ( new List < Tuple < int , int > > ( ) ) ; } v [ 1 ] . Add ( new Tuple < int , int > ( 4 , 4 ) ) ; v [ 4 ] . Add ( new Tuple < int , int > ( 1 , 4 ) ) ; v [ 1 ] . Add ( new Tuple < int , int > ( 2 , 2 ) ) ; v [ 2 ] . Add ( new Tuple < int , int > ( 1 , 2 ) ) ; v [ 2 ] . Add ( new Tuple < int , int > ( 3 , 3 ) ) ; v [ 3 ] . Add ( new Tuple < int , int > ( 2 , 3 ) ) ; v [ 2 ] . Add ( new Tuple < int , int > ( 5 , 5 ) ) ; v [ 5 ] . Add ( new Tuple < int , int > ( 2 , 5 ) ) ; List < int > queries = new List < int > { 1 , 3 , 5 } ; answerQueries ( queries ) ; } }
Minimum changes required to make each path in a matrix palindrome | C # program to implement the above approach ; 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
using System ; class GFG { static int countChanges ( int [ , ] matrix , int n , int m ) { int i , j , dist = n + m - 1 ; int Max_element = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { Max_element = Math . Max ( Max_element , matrix [ i , j ] ) ; } } int [ , ] freq = new int [ dist , Max_element + 1 ] ; 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 ] ] ++ ; } } int min_changes_sum = 0 ; for ( i = 0 ; i < dist / 2 ; i ++ ) { int maximum = 0 ; int 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 ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 7 , 0 , 3 , 1 , 8 , 1 , 3 } , { 0 , 4 , 0 , 1 , 0 , 4 , 0 } , { 3 , 1 , 8 , 3 , 1 , 0 , 7 } } ; int minChanges = countChanges ( mat , 3 , 7 ) ; Console . Write ( minChanges ) ; } }
Count of numbers upto N having absolute difference of at most K between any two adjacent digits | C # program to get the count of numbers upto N having absolute difference 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
using System ; class GFG { static int [ , , , ] dp = new int [ 1002 , 10 , 2 , 2 ] ; static int possibleNumbers ( int pos , int previous , int tight , int start , String N , int K ) { if ( pos == N . Length ) return 1 ; if ( dp [ pos , previous , tight , start ] != - 1 ) return dp [ pos , previous , tight , start ] ; int res = 0 ; int upper_limit = ( tight == 1 ) ? ( N [ pos ] - '0' ) : 9 ; int new_tight ; if ( start == 0 ) { res = possibleNumbers ( pos + 1 , previous , 0 , 0 , N , K ) ; for ( int 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 ( int 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 ] ; } public static void Main ( String [ ] args ) { String N = "20" ; int K = 2 ; for ( int i = 0 ; i < 1002 ; i ++ ) for ( int j = 0 ; j < 10 ; j ++ ) for ( int k = 0 ; k < 2 ; k ++ ) for ( int l = 0 ; l < 2 ; l ++ ) dp [ i , j , k , l ] = - 1 ; Console . Write ( possibleNumbers ( 0 , 0 , 1 , 0 , N , K ) + " STRNEWLINE " ) ; } }
Minimum cost of reducing Array by merging any adjacent elements repetitively | C # program for the above approach ; 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 ; Iterate for length >= 1 ; Find sum in range [ i . . j ] ; Initialise dp [ i , j ] to int . MaxValue ; Iterate for all possible K to find the minimum cost ; Update the minimum sum ; Return the readonly minimum cost ; Driver Code ; Given set of numbers ; Function Call
using System ; class GFG { static int mergeTwoNumbers ( int [ ] numbers ) { int len , i , j , k ; int n = numbers . Length ; if ( numbers . Length == 0 ) { return 0 ; } int [ ] prefixSum = new int [ n + 1 ] ; for ( i = 1 ; i <= n ; i ++ ) { prefixSum [ i ] = prefixSum [ i - 1 ] + numbers [ i - 1 ] ; } int [ , ] dp = new int [ n + 1 , n + 1 ] ; for ( len = 2 ; len <= n ; len ++ ) { for ( i = 1 ; i <= n - len + 1 ; i ++ ) { j = i + len - 1 ; int sum = prefixSum [ j ] - prefixSum [ i - 1 ] ; dp [ i , j ] = int . MaxValue ; 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 ] ; } public static void Main ( String [ ] args ) { int [ ] arr1 = { 6 , 4 , 4 , 6 } ; Console . Write ( mergeTwoNumbers ( arr1 ) + " STRNEWLINE " ) ; } }
Maximum sum possible for every node by including it in a segment of N | C # program to calculate the maximum sum possible for every node by including it in a segment of the N - Ary Tree ; 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
using System ; using System . Collections . Generic ; class GFG { static int [ ] dp1 = new int [ 100005 ] ; static int [ ] dp2 = new int [ 100005 ] ; static void dfs1 ( int u , int par , List < List < int > > g , int [ ] weight ) { dp1 [ u ] = weight [ u ] ; for ( int c = 0 ; c < g [ u ] . Count ; c ++ ) { if ( g [ u ] != par ) { dfs1 ( g [ u ] , u , g , weight ) ; dp1 [ u ] += Math . Max ( 0 , dp1 [ g [ u ] ] ) ; } } } static void dfs2 ( int u , int par , List < List < int > > g , int [ ] weight ) { if ( par != 0 ) { int maxSumAncestors = dp2 [ par ] - Math . Max ( 0 , dp1 [ u ] ) ; dp2 [ u ] = dp1 [ u ] + Math . Max ( 0 , maxSumAncestors ) ; } for ( int c = 0 ; c < g [ u ] . Count ; c ++ ) { if ( g [ u ] != par ) { dfs2 ( g [ u ] , u , g , weight ) ; } } } static void addEdge ( int u , int v , List < List < int > > g ) { g [ u ] . Add ( v ) ; g [ v ] . Add ( u ) ; } static void maxSumSegments ( List < List < int > > g , int [ ] weight , int n ) { dfs1 ( 1 , 0 , g , weight ) ; for ( int i = 1 ; i < n + 1 ; i ++ ) dp2 [ i ] = dp1 [ i ] ; dfs2 ( 1 , 0 , g , weight ) ; } static void printAns ( int n ) { for ( int i = 1 ; i < n ; i ++ ) Console . Write ( dp2 [ i ] + " ▁ " ) ; } static void Main ( ) { int n = 7 ; List < List < int > > g = new List < List < int > > ( ) ; for ( int i = 0 ; i < 100005 ; i ++ ) { g . Add ( new List < int > ( ) ) ; } addEdge ( 1 , 2 , g ) ; addEdge ( 1 , 3 , g ) ; addEdge ( 2 , 4 , g ) ; addEdge ( 2 , 5 , g ) ; addEdge ( 3 , 6 , g ) ; addEdge ( 4 , 7 , g ) ; int [ ] weight = new int [ n + 1 ] ; 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 | C # program to count possible number of pairs of elements 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
using System ; using System . Collections ; using System . Collections . Generic ; using System . Text ; class GFG { static int countPairs ( int [ ] arr , int N ) { int maxm = - int . MaxValue ; for ( int j = 0 ; j < N ; j ++ ) { if ( maxm < arr [ j ] ) { maxm = arr [ j ] ; } } int i , k = 0 ; int [ ] bitscount = new int [ maxm + 1 ] ; Array . Fill ( bitscount , 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 ] ; } } Dictionary < int , int > setbits = new Dictionary < int , int > ( ) ; for ( int j = 0 ; j < N ; j ++ ) { if ( setbits . ContainsKey ( bitscount [ arr [ j ] ] ) ) { setbits [ bitscount [ arr [ j ] ] ] ++ ; } else { setbits [ bitscount [ arr [ j ] ] ] = 1 ; } } int ans = 0 ; foreach ( KeyValuePair < int , int > it in setbits ) { ans += it . Value * ( it . Value - 1 ) / 2 ; } return ans ; } public static void Main ( string [ ] args ) { int N = 12 ; int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 } ; Console . Write ( countPairs ( arr , N ) ) ; } }
Count of Binary strings of length N having atmost M consecutive 1 s or 0 s alternatively exactly K times | C # program to find the count of binary strings of length N having atmost M consecutive 1 s or 0 s alternatively exactly K times ; Array to contain the readonly result ; Function to get the number of desirable binary strings ; If we reach end of string and groups are exhausted , return 1 ; If length is exhausted but groups are still to be made , return 0 ; If length is not exhausted but groups are exhausted , return 0 ; If both are negative just return 0 ; If already calculated , return it ; Initialise answer for each state ; Loop through every possible m ; Driver code
using System ; class GFG { static int [ , ] dp = new int [ 1000 , 1000 ] ; static int solve ( int n , int k , int m ) { if ( n == 0 && k == 0 ) return 1 ; if ( n == 0 && k != 0 ) return 0 ; if ( n != 0 && k == 0 ) return 0 ; if ( n < 0 k < 0 ) return 0 ; if ( dp [ n , k ] > 0 ) return dp [ n , k ] ; int ans = 0 ; for ( int j = 1 ; j <= m ; j ++ ) { ans += solve ( n - j , k - 1 , m ) ; } return dp [ n , k ] = ans ; } public static void Main ( String [ ] args ) { int N = 7 , K = 4 , M = 3 ; Console . Write ( solve ( N , K , M ) ) ; } }
Maximum neighbor element in a matrix within distance K | C # implementation to find the maximum neighbor element within the distance of less than K ; Function to print the matrix ; Loop to iterate over the matrix ; Function to find the maximum neighbor within the distance of less than equal to K ; Loop to find the maximum element within the distance of less than K ; Driver Code
using System ; class GFG { static void printMatrix ( int [ , ] A ) { for ( int i = 0 ; i < A . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < A . GetLength ( 1 ) ; j ++ ) Console . Write ( A [ i , j ] + " ▁ " ) ; Console . Write ( ' STRNEWLINE ' ) ; } } static int [ , ] getMaxNeighbour ( int [ , ] A , int K ) { int [ , ] ans = A ; for ( int q = 1 ; q <= K ; q ++ ) { for ( int i = 0 ; i < A . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < A . GetLength ( 1 ) ; j ++ ) { int maxi = ans [ i , j ] ; if ( i > 0 ) maxi = Math . Max ( maxi , ans [ i - 1 , j ] ) ; if ( j > 0 ) maxi = Math . Max ( maxi , ans [ i , j - 1 ] ) ; if ( i < A . GetLength ( 0 ) - 1 ) maxi = Math . Max ( maxi , ans [ i + 1 , j ] ) ; if ( j < A . GetLength ( 0 ) - 1 ) maxi = Math . Max ( maxi , ans [ i , j + 1 ] ) ; A [ i , j ] = maxi ; } } ans = A ; } return ans ; } public static void Main ( String [ ] args ) { int [ , ] B = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; printMatrix ( getMaxNeighbour ( B , 2 ) ) ; } }
Number of distinct ways to represent a number as sum of K unique primes | C # program to count the number of distinct ways to represent a number as K different primes ; Prime vector ; Sieve array of prime ; DP array ; Initialise all numbers as prime ; Sieve of Eratosthenes . ; Push all the primes into prime vector ; Function to get the number of distinct ways to get sum as K different primes ; If index went out of prime array size or the sum became larger than n return 0 ; If sum becomes equal to n and j becomes exactly equal to k . Return 1 , else if j is still not equal to k , return 0 ; If sum != n and still j as exceeded , return 0 ; If that state is already calculated , return directly the ans ; Include the current prime ; Exclude the current prime ; Return by memoizing the ans ; Driver code ; Precompute primes by sieve
using System ; using System . Collections . Generic ; class GFG { static List < int > prime = new List < int > ( ) ; static bool [ ] isprime = new bool [ 1000 ] ; static int [ , , ] dp = new int [ 200 , 20 , 1000 ] ; static void sieve ( ) { for ( int i = 0 ; i < 1000 ; i ++ ) isprime [ i ] = true ; for ( int i = 2 ; i * i < 1000 ; i ++ ) { if ( isprime [ i ] ) { for ( int j = i * i ; j < 1000 ; j += i ) { isprime [ j ] = false ; } } } for ( int i = 2 ; i < 1000 ; i ++ ) { if ( isprime [ i ] ) { prime . Add ( i ) ; } } } static int CountWays ( int i , int j , int sum , int n , int k ) { if ( i >= prime . Count - 1 sum > n ) { return 0 ; } if ( sum == n ) { if ( j == k ) { return 1 ; } return 0 ; } if ( j == k ) return 0 ; if ( dp [ i , j , sum ] != 0 ) return dp [ i , j , sum ] ; int inc = 0 , exc = 0 ; inc = CountWays ( i + 1 , j + 1 , sum + prime [ i ] , n , k ) ; exc = CountWays ( i + 1 , j , sum , n , k ) ; return dp [ i , j , sum ] = inc + exc ; } static public void Main ( ) { sieve ( ) ; int N = 100 , K = 5 ; Console . WriteLine ( CountWays ( 0 , 0 , 0 , N , K ) ) ; } }
Count minimum factor jumps required to reach the end of an Array | C # program for bottom up approach ; Vector to store factors of each integer ; Initialize the dp array ; Precompute all the factors of every integer ; Function to count the minimum factor jump ; Initialise minimum jumps to reach each cell as Integer . MAX_VALUE ; 0 jumps required to reach the first cell ; Iterate over all cells ; Calculating for each jump ; If a cell is in bound ; Return minimum jumps to reach last cell ; Driver code ; Pre - calculating the factors ; Function call
using System ; using System . Collections . Generic ; class GFG { static List < List < int > > factors = new List < List < int > > ( ) ; static int [ ] dp ; static void precompute ( ) { for ( int i = 1 ; i <= 100000 ; i ++ ) { for ( int j = i ; j <= 100000 ; j += i ) factors [ j ] . Add ( i ) ; } } static int solve ( int [ ] arr , int n ) { for ( int i = 0 ; i < 100005 ; i ++ ) { dp [ i ] = int . MaxValue ; } dp [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { foreach ( int j in factors [ arr [ i ] ] ) { if ( i + j < n ) dp [ i + j ] = Math . Min ( dp [ i + j ] , 1 + dp [ i ] ) ; } } return dp [ n - 1 ] ; } static public void Main ( ) { for ( int i = 0 ; i < 100005 ; i ++ ) factors . Add ( new List < int > ( ) ) ; dp = new int [ 100005 ] ; precompute ( ) ; int [ ] arr = { 2 , 8 , 16 , 55 , 99 , 100 } ; int n = arr . Length ; Console . Write ( solve ( arr , n ) ) ; } }
Find the length of the Largest subset such that all elements are Pairwise Coprime | C # implementation to find the length of the largest subset such that all elements are Pairwise Coprime ; Dynamic programming table ; Function to obtain the mask for any integer ; List of prime numbers till 50 ; Iterate through all prime numbers to obtain the mask ; Set this prime 's bit ON in the mask ; Return the mask value ; Function to count the number of ways ; Check if subproblem has been solved ; Excluding current element in the subset ; Check if there are no common prime factors then only this element can be included ; Calculate the new mask if this element is included ; Store and return the answer ; Function to find the count of subarray with all digits unique ; Driver code
using System ; class GFG { static int [ , ] dp = new int [ 5000 , 1029 ] ; static int getmask ( int val ) { int mask = 0 ; int [ ] prime = { 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 } ; for ( int i = 0 ; i < 15 ; i ++ ) { if ( val % prime [ i ] == 0 ) { mask = mask | ( 1 << i ) ; } } return mask ; } static int calculate ( int pos , int mask , int [ ] a , int n ) { if ( pos == n || mask == ( int ) ( 1 << n - 1 ) ) return 0 ; if ( dp [ pos , mask ] != - 1 ) return dp [ pos , mask ] ; int size = 0 ; size = Math . Max ( size , calculate ( pos + 1 , mask , a , n ) ) ; if ( ( getmask ( a [ pos ] ) & mask ) == 0 ) { int new_mask = ( mask | ( getmask ( a [ pos ] ) ) ) ; size = Math . Max ( size , 1 + calculate ( pos + 1 , new_mask , a , n ) ) ; } return dp [ pos , mask ] = size ; } static int largestSubset ( int [ ] a , int n ) { for ( int i = 0 ; i < 5000 ; i ++ ) { for ( int j = 0 ; j < 1029 ; j ++ ) dp [ i , j ] = - 1 ; } return calculate ( 0 , 0 , a , n ) ; } public static void Main ( ) { int [ ] A = { 2 , 3 , 13 , 5 , 14 , 6 , 7 , 11 } ; int N = A . Length ; Console . WriteLine ( largestSubset ( A , N ) ) ; } }
Maximum sum such that exactly half of the elements are selected and no two adjacent | C # program to find maximum sum possible such that exactly Math . Floor ( N / 2 ) elements are selected and no two selected elements are adjacent to each other ; Function return the maximum sum possible under given condition ; Base case ; When i is odd ; When i is even ; Maximum of if we pick last element or not ; Driver code
using System ; class GFG { static int MaximumSum ( int [ ] a , int n ) { int [ , ] dp = new int [ n + 1 , 2 ] ; dp [ 2 , 1 ] = a [ 1 ] ; dp [ 2 , 0 ] = a [ 0 ] ; for ( int i = 3 ; i < n + 1 ; i ++ ) { if ( i % 2 == 1 ) { int temp = Math . Max ( ( Math . Max ( dp [ i - 3 , 1 ] , dp [ i - 3 , 0 ] ) ) , Math . Max ( dp [ i - 2 , 1 ] , dp [ i - 2 , 0 ] ) ) ; dp [ i , 1 ] = a [ i - 1 ] + temp ; dp [ i , 0 ] = Math . Max ( ( Math . Max ( a [ i - 2 ] + dp [ i - 2 , 0 ] , a [ i - 2 ] + dp [ i - 3 , 1 ] ) ) , Math . Max ( a [ i - 2 ] + dp [ i - 3 , 0 ] , a [ i - 3 ] + dp [ i - 3 , 0 ] ) ) ; } else { dp [ i , 1 ] = a [ i - 1 ] + ( Math . Max ( ( Math . Max ( dp [ i - 2 , 1 ] , dp [ i - 2 , 0 ] ) ) , dp [ i - 1 , 0 ] ) ) ; dp [ i , 0 ] = a [ i - 2 ] + dp [ i - 2 , 0 ] ; } } return Math . Max ( dp [ n , 1 ] , dp [ n , 0 ] ) ; } static int max ( int [ ] arr ) { return 1 ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = A . Length ; Console . Write ( MaximumSum ( A , N ) ) ; } }
Optimal Strategy for the Divisor game using Dynamic Programming | C # program for implementation of optimal strategy for the divisor game using dynamic programming ; Recursive function to find the winner ; Check if N = 1 or N = 3 then player B wins ; Check if N = 2 then player A wins ; Check if current state already visited then return the previously obtained ans ; Check if currently it is player A 's turn  then initialise the ans to 0 ; Traversing across all the divisors of N which are less than N ; Check if current value of i is a divisor of N ; Check if it is player A 's turn  then we need at least one true ; Else if it is player B 's turn  then we need at least one false ; Return the current ans ; Driver code ; Initialise N
using System ; class GFG { static int divisorGame ( int N , int A , int [ , ] dp ) { if ( N == 1 N == 3 ) return 0 ; if ( N == 2 ) return 1 ; if ( dp [ N , A ] != - 1 ) return dp [ N , A ] ; int ans = ( A == 1 ) ? 0 : 1 ; for ( int i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { if ( A == 1 ) ans |= divisorGame ( N - i , 0 , dp ) ; else ans &= divisorGame ( N - i , 1 , dp ) ; } } return dp [ N , A ] = ans ; } public static void Main ( String [ ] args ) { int N = 3 ; int [ , ] dp = new int [ N + 1 , 2 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { dp [ i , j ] = - 1 ; } } if ( divisorGame ( N , 1 , dp ) == 1 ) { Console . Write ( " Player ▁ A ▁ wins " ) ; } else { Console . Write ( " Player ▁ B ▁ wins " ) ; } } }