codeparrot/xlcost-text-to-code huggingface dataset:
Count distinct possible Bitwise XOR values of subsets of an array | C # program for the above approach ; Stores the mask of the vector ; Stores the current size of dp [ ] ; Function to store the mask of given integer ; Iterate over the range [ 0 , 20 ] ; If i - th bit 0 ; If dp [ i ] is zero ; Store the position in dp ; Increment the answer ; Return from the loop ; mask = mask XOR dp [ i ] ; Function to find the size of the set having Bitwise XOR of all the subset of the given array ; Traverse the array ; Print the answer ; Driver code ; Function Call
using System ; class GFG { static int size = 20 ; static int [ ] dp = new int [ size ] ; static int ans ; static void insertVector ( int mask ) { for ( int i = 0 ; i < 20 ; i ++ ) { if ( ( mask & 1 << i ) == 0 ) continue ; if ( dp [ i ] == 0 ) { dp [ i ] = mask ; ++ ans ; return ; } mask ^= dp [ i ] ; } } static void maxSizeSet ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { insertVector ( arr [ i ] ) ; } Console . WriteLine ( 1 << ans ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; maxSizeSet ( arr , N ) ; } }
Maximize sum possible from an array by jumps of length i + K * arr [ i ] from any ith index | C # program for the above approach ; Function to find the maximum sum possible by jumps of length i + K * arr [ i ] from any i - th index ; Initialize an array dp [ ] ; Stores the maximum sum ; Iterate over the range [ N - 1 , 0 ] ; If length of the jump exceeds N ; Set dp [ i ] as arr [ i ] ; Otherwise , update dp [ i ] as sum of dp [ i + K * arr [ i ] ] and arr [ i ] ; Update the overall maximum sum ; Print the answer ; Driver Code
using System ; class GFG { static void maxSum ( int [ ] arr , int N , int K ) { int [ ] dp = new int [ N + 2 ] ; for ( int i = 0 ; i < N + 2 ; i ++ ) { dp [ i ] = 0 ; } int maxval = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( ( i + K * arr [ i ] ) >= N ) { dp [ i ] = arr [ i ] ; } else { dp [ i ] = dp [ i + K * arr [ i ] ] + arr [ i ] ; } maxval = Math . Max ( maxval , dp [ i ] ) ; } Console . WriteLine ( maxval ) ; } static public void Main ( ) { int [ ] arr = { 2 , 1 , 3 , 1 , 2 } ; int N = arr . Length ; int K = 3 ; maxSum ( arr , N , K ) ; } }
Maximum sum submatrix | C # program to implement the above approach ; Function to find maximum sum submatrix ; Stores the number of rows and columns in the matrix ; Stores maximum submatrix sum ; Take each row as starting row ; Take each column as the starting column ; Take each row as the ending row ; Take each column as the ending column ; Stores the sum of submatrix having topleft index ( i , j ) and bottom right index ( k , l ) ; Iterate the submatrix row - wise and calculate its sum ; Update the maximum sum ; Print the answer ; Driver Code
using System ; public class GFG { static void maxSubmatrixSum ( int [ , ] matrix ) { int r = matrix . GetLength ( 0 ) ; int c = matrix . GetLength ( 1 ) ; int maxSubmatrix = 0 ; for ( int i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) { for ( int k = i ; k < r ; k ++ ) { for ( int l = j ; l < c ; l ++ ) { int sumSubmatrix = 0 ; for ( int m = i ; m <= k ; m ++ ) { for ( int n = j ; n <= l ; n ++ ) { sumSubmatrix += matrix [ m , n ] ; } } maxSubmatrix = Math . Max ( maxSubmatrix , sumSubmatrix ) ; } } } } Console . WriteLine ( maxSubmatrix ) ; } public static void Main ( String [ ] args ) { int [ , ] matrix = { { 0 , - 2 , - 7 , 0 } , { 9 , 2 , - 6 , 2 } , { - 4 , 1 , - 4 , 1 } , { - 1 , 8 , 0 , - 2 } } ; maxSubmatrixSum ( matrix ) ; } }
Maximize sum of subsets from two arrays having no consecutive values | C # program for the above approach ; Function to calculate maximum subset sum ; Initialize array to store dp states ; Base Cases ; Pre initializing for dp [ 0 ] & dp [ 1 ] ; Calculating dp [ index ] based on above formula ; Print maximum subset sum ; Driver Code ; Given arrays ; Length of the array
using System ; class GFG { static void maximumSubsetSum ( int [ ] arr1 , int [ ] arr2 , int length ) { int [ ] dp = new int [ length + 1 ] ; if ( length == 1 ) { Console . WriteLine ( Math . Max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) ; return ; } if ( length == 2 ) { Console . WriteLine ( Math . Max ( Math . Max ( arr1 [ 1 ] , arr2 [ 1 ] ) , Math . Max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) ) ; return ; } else { dp [ 0 ] = Math . Max ( arr1 [ 0 ] , arr2 [ 0 ] ) ; dp [ 1 ] = Math . Max ( Math . Max ( arr1 [ 1 ] , arr2 [ 1 ] ) , Math . Max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) ; int index = 2 ; while ( index < length ) { dp [ index ] = Math . Max ( Math . Max ( arr1 [ index ] , arr2 [ index ] ) , Math . Max ( Math . Max ( arr1 [ index ] + dp [ index - 2 ] , arr2 [ index ] + dp [ index - 2 ] ) , dp [ index - 1 ] ) ) ; ++ index ; } Console . WriteLine ( dp [ length - 1 ] ) ; } } static public void Main ( ) { int [ ] arr1 = { - 1 , - 2 , 4 , - 4 , 5 } ; int [ ] arr2 = { - 1 , - 2 , - 3 , 4 , 10 } ; int length = arr1 . Length ; maximumSubsetSum ( arr1 , arr2 , length ) ; } }
Modify array by replacing every array element with minimum possible value of arr [ j ] + | j | C # program for the above approach ; Function to find minimum value of arr [ j ] + | j - i | for every array index ; Stores minimum of a [ j ] + | i - j | upto position i ; Stores minimum of a [ j ] + | i - j | upto position i from the end ; Traversing and storing minimum of a [ j ] + | i - j | upto i ; Traversing and storing minimum of a [ j ] + | i - j | upto i from the end ; Traversing from [ 0 , N ] and storing minimum of a [ j ] + | i - j | from starting and end ; Print the required array ; Driver code ; Given array [ ] arr ; Size of the array ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void minAtEachIndex ( int n , int [ ] arr ) { int [ ] dp1 = new int [ n ] ; int [ ] dp2 = new int [ n ] ; int i ; dp1 [ 0 ] = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) dp1 [ i ] = Math . Min ( arr [ i ] , dp1 [ i - 1 ] + 1 ) ; dp2 [ n - 1 ] = arr [ n - 1 ] ; for ( i = n - 2 ; i >= 0 ; i -- ) dp2 [ i ] = Math . Min ( arr [ i ] , dp2 [ i + 1 ] + 1 ) ; List < int > v = new List < int > ( ) ; for ( i = 0 ; i < n ; i ++ ) v . Add ( Math . Min ( dp1 [ i ] , dp2 [ i ] ) ) ; foreach ( int x in v ) Console . Write ( x + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 2 , 5 , 3 } ; int N = arr . Length ; minAtEachIndex ( N , arr ) ; } }
Count all N | C # program for the above approach ; Function to print the count of arrays satisfying given condition ; First element of array is set as 1 ; Since the first element of arr [ ] is 1 , the second element can 't be 1 ; Traverse the remaining indices ; If arr [ i ] = 1 ; If arr [ i ] a 1 ; Since last element needs to be 1 ; Driver code ; Stores the count of arrays where arr [ 0 ] = arr [ N - 1 ] = 1 ; Since arr [ 0 ] and arr [ N - 1 ] can be any number from 1 to M ; Print answer
using System ; using System . Collections . Generic ; class GFG { static int totalArrays ( int N , int M ) { int [ ] end_with_one = new int [ N + 1 ] ; int [ ] end_not_with_one = new int [ N + 1 ] ; end_with_one [ 0 ] = 1 ; end_not_with_one [ 0 ] = 0 ; end_with_one [ 1 ] = 0 ; end_not_with_one [ 1 ] = M - 1 ; for ( int i = 2 ; i < N ; i ++ ) { end_with_one [ i ] = end_not_with_one [ i - 1 ] ; end_not_with_one [ i ] = end_with_one [ i - 1 ] * ( M - 1 ) + end_not_with_one [ i - 1 ] * ( M - 2 ) ; } return end_with_one [ N - 1 ] ; } static void Main ( ) { int N = 3 , M = 3 ; int temp = totalArrays ( N , M ) ; int ans = M * temp ; Console . WriteLine ( ans ) ; } }
Count numbers from a given range whose product of digits is K | C # program to implement the above approach ; Function to find the product of digits of a number ; Stores product of digits of N ; Update res ; Update N ; Function to count numbers in the range [ 0 , X ] whose product of digit is K ; Stores count of numbers in the range [ L , R ] whose product of digit is K ; Iterate over the range [ L , R ] ; If product of digits of i equal to K ; Update cnt ; Driver Code
using System ; class GFG { static int prodOfDigit ( int N ) { int res = 1 ; while ( N > 0 ) { res = res * ( N % 10 ) ; N /= 10 ; } return res ; } static int cntNumRange ( int L , int R , int K ) { int cnt = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( prodOfDigit ( i ) == K ) { cnt ++ ; } } return cnt ; } static void Main ( ) { int L = 20 , R = 10000 , K = 14 ; Console . WriteLine ( cntNumRange ( L , R , K ) ) ; } }
Median of Bitwise XOR of all submatrices starting from the top left corner | C # program to implement the above approach ; Function to find the median of bitwise XOR of all the submatrix whose topmost leftmost corner is ( 0 , 0 ) ; dp [ i , j ] : Stores the bitwise XOR of submatrix having top left corner at ( 0 , 0 ) and bottom right corner at ( i , j ) ; Stores count of submatrix ; Base Case ; Base Case ; Fill [ , ] dp using tabulation ; Fill dp [ i , j ] ; Driver code
using System ; class GFG { static double findMedXOR ( int [ , ] mat , int N , int M ) { int [ , ] dp = new int [ N , M ] ; int [ ] med = new int [ N * M ] ; dp [ 0 , 0 ] = mat [ 0 , 0 ] ; med [ 0 ] = dp [ 0 , 0 ] ; int len = 1 ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i , 0 ] = dp [ i - 1 , 0 ] ^ mat [ i , 0 ] ; med [ len ++ ] = dp [ i , 0 ] ; } for ( int i = 1 ; i < M ; i ++ ) { dp [ 0 , i ] = dp [ 0 , i - 1 ] ^ mat [ 0 , i ] ; med [ len ++ ] = dp [ 0 , i ] ; } for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { dp [ i , j ] = dp [ i - 1 , j ] ^ dp [ i , j - 1 ] ^ dp [ i - 1 , j - 1 ] ^ mat [ i , j ] ; med [ len ++ ] = dp [ i , j ] ; } } Array . Sort ( med ) ; if ( len % 2 == 0 ) { return ( med [ ( len / 2 ) ] + med [ ( len / 2 ) - 1 ] ) / 2.0 ; } return med [ len / 2 ] ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 2 } , { 2 , 3 } } ; int N = mat . GetLength ( 0 ) ; int M = 2 ; Console . WriteLine ( findMedXOR ( mat , N , M ) ) ; } }
Count ways to tile an N | C # program to implement the above approach ; Function to count the ways to tile N * 1 board using 1 * 1 and 2 * 1 tiles ; dp [ i ] : Stores count of ways to tile i * 1 board using given tiles ; Base Case ; Iterate over the range [ 2 , N ] ; Fill dp [ i ] using the recurrence relation ; Driver Code ; Given N ; Function Call
using System ; class GFG { static void countWaysToTileBoard ( int N ) { int [ ] dp = new int [ N + 1 ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 2 ; for ( int i = 2 ; i <= N ; i ++ ) { dp [ i ] = ( 2 * dp [ i - 1 ] + dp [ i - 2 ] ) ; } Console . Write ( dp [ N ] ) ; } public static void Main ( String [ ] args ) { int N = 2 ; countWaysToTileBoard ( N ) ; } }
Subsequences generated by including characters or ASCII value of characters of given string | C # program to implement the above approach ; Function to print subsequences containing ASCII value of the characters or the the characters of the given string ; Base Case ; If length of the subsequence exceeds 0 ; Print the subsequence ; Stores character present at i - th index of str ; If the i - th character is not included in the subsequence ; Including the i - th character in the subsequence ; Include the ASCII value of the ith character in the subsequence ; Driver Code ; Stores length of str
using System ; class GFG { static void FindSub ( string str , string res , int i ) { if ( i == str . Length ) { if ( res . Length > 0 ) { Console . Write ( res + " ▁ " ) ; } return ; } char ch = str [ i ] ; FindSub ( str , res , i + 1 ) ; FindSub ( str , res + ch , i + 1 ) ; FindSub ( str , res + ( int ) ch , i + 1 ) ; } public static void Main ( String [ ] args ) { string str = " ab " ; string res = " " ; int N = str . Length ; FindSub ( str , res , 0 ) ; } }
Minimize given flips required to reduce N to 0 | C # program to implement the above approach ; Function to find the minimum count of operations required to Reduce N to 0 ; Stores count of bits in N ; Recurrence relation ; Driver code
using System ; class GFG { public static int MinOp ( int N ) { if ( N <= 1 ) return N ; int bit = ( int ) ( Math . Log ( N ) / Math . Log ( 2 ) ) + 1 ; return ( ( 1 << bit ) - 1 ) - MinOp ( N - ( 1 << ( bit - 1 ) ) ) ; } public static void Main ( ) { int N = 4 ; Console . WriteLine ( MinOp ( N ) ) ; } }
Maximum non | C # program for the above approach ; Function to find the maximum product from the top left and bottom right cell of the given matrix grid [ ] [ ] ; Store dimension of grid ; Stores maximum product path ; Stores minimum product path ; Traverse the grid and update maxPath and minPath array ; Initialize to inf and - inf ; Base Case ; Calculate for row : ; Update the maximum ; Update the minimum ; Calculate for column ; Update the maximum ; Update the minimum ; Update maxPath and minPath ; If negative product ; Otherwise ; Driver Code ; Given matrix mat [ ] [ ] ; Function Call
using System ; class GFG { static int maxProductPath ( int [ , ] grid ) { int n = grid . GetLength ( 0 ) ; int m = grid . GetLength ( 1 ) ; int [ , ] maxPath = new int [ n , m ] ; int [ , ] minPath = new int [ n , m ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { int mn = Int32 . MaxValue ; int mx = Int32 . MinValue ; if ( i == 0 && j == 0 ) { mx = grid [ i , j ] ; mn = grid [ i , j ] ; } if ( i > 0 ) { int tempmx = Math . Max ( ( maxPath [ i - 1 , j ] * grid [ i , j ] ) , ( minPath [ i - 1 , j ] * grid [ i , j ] ) ) ; mx = Math . Max ( mx , tempmx ) ; int tempmn = Math . Min ( ( maxPath [ i - 1 , j ] * grid [ i , j ] ) , ( minPath [ i - 1 , j ] * grid [ i , j ] ) ) ; mn = Math . Min ( mn , tempmn ) ; } if ( j > 0 ) { int tempmx = Math . Max ( ( maxPath [ i , j - 1 ] * grid [ i , j ] ) , ( minPath [ i , j - 1 ] * grid [ i , j ] ) ) ; mx = Math . Max ( mx , tempmx ) ; int tempmn = Math . Min ( ( maxPath [ i , j - 1 ] * grid [ i , j ] ) , ( minPath [ i , j - 1 ] * grid [ i , j ] ) ) ; mn = Math . Min ( mn , tempmn ) ; } maxPath [ i , j ] = mx ; minPath [ i , j ] = mn ; } } if ( maxPath [ n - 1 , m - 1 ] < 0 ) { return - 1 ; } else { return ( maxPath [ n - 1 , m - 1 ] ) ; } } static public void Main ( ) { int [ , ] mat = { { 1 , - 2 , 1 } , { 1 , - 2 , 1 } , { 3 , - 4 , 1 } } ; Console . WriteLine ( maxProductPath ( mat ) ) ; } }
Count N | C # program to illustrate Count of N - length strings consisting only of vowels sorted lexicographically ; to keep the string in lexicographically sorted order use start index to add the vowels starting the from that index ; base case : if string length is 0 add to the count ; if last character in string is ' e ' add vowels starting from ' e ' i . e ' e ' , ' i ' , ' o ' , ' u ' ; decrease the length of string ; char arr [ 5 ] = { ' a ' , ' e ' , ' i ' , ' o ' , ' u ' } ; starting from index 0 add the vowels to strings
using System ; using System . Collections . Generic ; class GFG { static int countstrings ( int n , int start ) { if ( n == 0 ) { return 1 ; } int cnt = 0 ; for ( int i = start ; i < 5 ; i ++ ) { cnt += countstrings ( n - 1 , i ) ; } return cnt ; } static int countVowelStrings ( int n ) { return countstrings ( n , 0 ) ; } static void Main ( ) { int n = 2 ; Console . Write ( countVowelStrings ( n ) ) ; } }
Count N | C # program to implement the above approach ; Function to count N - length strings consisting of vowels only sorted lexicographically ; Driver Code ; Function Call
using System ; class GFG { static int findNumberOfStrings ( int n ) { return ( n + 1 ) * ( n + 2 ) * ( n + 3 ) * ( n + 4 ) / 24 ; } public static void Main ( string [ ] args ) { int N = 2 ; Console . Write ( findNumberOfStrings ( N ) ) ; } }
Count unique paths with given sum in an N | C # program for the above approach ; Function for counting total no of paths possible with the sum is equal to X ; If the path of the sum from the root to current node is stored in sum ; If already computed ; Count different no of paths using all possible ways ; Return total no of paths ; Driver Code ; Stores the number of ways to obtains sums 0 to X ; Function call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int mod = ( int ) 1e9 + 7 ; static int findTotalPath ( int X , int n , int [ ] dp ) { if ( X == 0 ) { return 1 ; } int ans = 0 ; if ( dp [ X ] != - 1 ) { return dp [ X ] ; } for ( int i = 1 ; i <= Math . Min ( X , n ) ; ++ i ) { ans += findTotalPath ( X - i , n , dp ) % mod ; ans %= mod ; } dp [ X ] = ans ; return ans ; } public static void Main ( ) { int n = 3 , X = 2 ; int [ ] dp = new int [ X + 1 ] ; Array . Fill ( dp , - 1 ) ; Console . WriteLine ( findTotalPath ( X , n , dp ) ) ; } }
Count sequences of positive integers having product X | C # program for the above approach ; Function to print the total number of possible sequences with product X ; Precomputation of binomial coefficients ; Max length of a subsequence ; Ways dp array ; Fill i slots using all the primes ; Subtract ways for all slots that exactly fill less than i slots ; Total possible sequences ; Print the resultant count ; Driver Code ; Function call
using System ; class GFG { static int [ , ] bin = new int [ 3000 , 3000 ] ; static void countWays ( int [ ] arr ) { int mod = ( int ) 1e9 + 7 ; bin [ 0 , 0 ] = 1 ; for ( int i = 1 ; i < 3000 ; i ++ ) { bin [ i , 0 ] = 1 ; for ( int j = 1 ; j <= i ; j ++ ) { bin [ i , j ] = ( bin [ i - 1 , j ] + bin [ i - 1 , j - 1 ] ) % mod ; } } int n = 0 ; foreach ( int x in arr ) n += x ; int [ ] ways = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { ways [ i ] = 1 ; for ( int j = 0 ; j < arr . Length ; j ++ ) { ways [ i ] = ( ways [ i ] * bin [ arr [ j ] + i - 1 , i - 1 ] ) % mod ; } for ( int j = 1 ; j < i ; j ++ ) { ways [ i ] = ( ( ways [ i ] - bin [ i , j ] * ways [ j ] ) % mod + mod ) % mod ; } } int ans = 0 ; foreach ( int x in ways ) ans = ( ans + x ) % mod ; Console . Write ( ans + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 } ; countWays ( arr ) ; } }
Maximize trailing zeros in product from top left to bottom right of given Matrix | C # program for the above approach ; Create a class pair to store count of 2 ' s ▁ and ▁ 5' s ; Parameterized Constructor ; Function to covert into Strings ; Function to get maximum no of zeros in product of path from topleft to bottom right ; Base Case ; Store the maximum count of zeros till ith and jth index ; Initialize the ( 0 , 0 ) ; Initialize the first row and column explicitly ; Iterate through all the cells ; Get the pair from the top and from left ; If there are more number of 0 s from top or left ; Print the no of zeros min ( no of 2 ' s , ▁ no ▁ of ▁ 5' s ) ; Function to calculate no of zeros ; Function to calculate no of 2 's ; Return the readonly count ; Function to calculate no of 5 's ; Return the readonly count ; Function to add pairs ; Return the resultant pair ; Driver Code ; Given N & M ; Given matrix ; Function Call
using System ; public class pair { public int x , y ; public pair ( int x , int y ) { this . x = x ; this . y = y ; } public String toString ( ) { return " ( " + this . x + " , ▁ " + this . y + " ) " ; } } class GFG { public static void maxZeros ( int [ , ] mat , int n , int m ) { if ( n == 0 m == 0 ) return ; pair [ , ] dp = new pair [ n + 1 , m + 1 ] ; dp [ 0 , 0 ] = new pair ( countTwos ( mat [ 0 , 0 ] ) , countFives ( mat [ 0 , 0 ] ) ) ; for ( int i = 1 ; i < n ; i ++ ) dp [ i , 0 ] = add ( dp [ i - 1 , 0 ] , new pair ( countTwos ( mat [ i , 0 ] ) , countFives ( mat [ i , 0 ] ) ) ) ; for ( int i = 1 ; i < m ; i ++ ) dp [ 0 , i ] = add ( dp [ 0 , i - 1 ] , new pair ( countTwos ( mat [ 0 , i ] ) , countFives ( mat [ 0 , i ] ) ) ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 1 ; j < m ; j ++ ) { pair top = dp [ i - 1 , j ] ; pair left = dp [ i , j - 1 ] ; pair curr = new pair ( countTwos ( mat [ i , j ] ) , countFives ( mat [ i , j ] ) ) ; top = add ( top , curr ) ; left = add ( left , curr ) ; if ( check ( top , left ) ) dp [ i , j ] = top ; else dp [ i , j ] = left ; } } Console . WriteLine ( Math . Min ( dp [ n - 1 , m - 1 ] . x , dp [ n - 1 , m - 1 ] . y ) ) ; } public static bool check ( pair one , pair two ) { int top = Math . Min ( one . x , one . y ) ; int left = Math . Min ( two . x , two . y ) ; if ( top > left ) return true ; else return false ; } public static int countTwos ( int num ) { int count = 0 ; while ( num != 0 && num % 2 == 0 ) { num /= 2 ; count ++ ; } return count ; } public static int countFives ( int num ) { int count = 0 ; while ( num != 0 && num % 5 == 0 ) { num /= 5 ; count ++ ; } return count ; } public static pair add ( pair one , pair two ) { pair np = new pair ( one . x + two . x , one . y + two . y ) ; return np ; } public static void Main ( String [ ] args ) { int N = 3 , M = 4 ; int [ , ] mat = { { 6 , 25 , 4 , 10 } , { 12 , 25 , 1 , 15 } , { 7 , 15 , 15 , 5 } } ; maxZeros ( mat , N , M ) ; } }
Maximum subsequence sum obtained by concatenating disjoint subarrays whose lengths are prime | C # program for the above approach ; Function to return all prime numbers smaller than N ; Create a bool array " prime [ 0 . . n ] " ; Initialize all its entries as true ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it ; Stores all prime numbers smaller than MAX ; Store all prime numbers ; If p is prime ; Function to build the auxiliary DP array from the start ; Base Case ; Stores all prime numbers < N ; Stores prefix sum ; Update prefix sum ; Iterate over range ; Update each state i . e . . when current element is excluded ; Find start & end index of subarrays when prime [ i ] is taken ; Check if starting point lies in the array ; Include the elements al al + 1 . . . ar ; Check if element lies before start of selected subarray ; Update value of dp [ i ] ; Function to find the maximum sum subsequence with prime length ; Auxiliary DP array ; Build DP array ; Print the result ; Driver Code ; Given [ ] arr ; Size of array ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int MAX = 100005 ; static List < int > SieveOfEratosthenes ( ) { bool [ ] seive = new bool [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) seive [ i ] = true ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( seive [ p ] == true ) { for ( int i = p * p ; i < MAX ; i += p ) { seive [ i ] = false ; } } } List < int > v = new List < int > ( ) ; for ( int p = 2 ; p < MAX ; p ++ ) { if ( seive [ p ] ) { v . Add ( p ) ; } } return v ; } static void build ( int [ ] dp , int [ ] arr , int N ) { dp [ 0 ] = 0 ; dp [ 1 ] = 0 ; List < int > prime = SieveOfEratosthenes ( ) ; int [ ] pref = new int [ N + 1 ] ; pref [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { pref [ i ] = pref [ i - 1 ] + arr [ i - 1 ] ; } for ( int i = 2 ; i <= N ; i ++ ) { dp [ i ] = dp [ i - 1 ] ; for ( int j = 0 ; j <= prime . Count ; j ++ ) { int r = i - 1 ; int l = r - prime [ j ] + 1 ; if ( l < 0 ) break ; int temp = 0 ; temp = pref [ r + 1 ] - pref [ l ] ; if ( l - 2 >= 0 ) temp += dp [ l - 2 + 1 ] ; dp [ i ] = Math . Max ( dp [ i ] , temp ) ; } } } static void maxSumSubseq ( int [ ] arr , int N ) { int [ ] dp = new int [ N + 1 ] ; build ( dp , arr , N ) ; Console . Write ( dp [ N ] ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 10 , 7 , 10 , 10 , 10 } ; int N = arr . Length ; maxSumSubseq ( arr , N ) ; } }
Maximum possible score that can be obtained by constructing a Binary Tree based on given conditions | C # program for the above approach ; Function to find the maximum score for one possible tree having N nodes N - 1 Edges ; Number of nodes ; Initialize [ , ] dp ; Score with 0 vertices is 0 ; Traverse the nodes from 1 to N ; Find maximum scores for each sum ; Iterate over degree of new node ; Update the current state ; Return maximum score for N node tree having 2 ( N - 1 ) sum of degree ; Driver Code ; Given array of scores ; Function Call
using System ; class GFG { static int maxScore ( int [ ] arr ) { int N = arr . Length ; N ++ ; int [ , ] dp = new int [ N + 1 , 2 * ( N - 1 ) + 1 ] ; dp [ 0 , 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int s = 1 ; s <= 2 * ( N - 1 ) ; s ++ ) { for ( int j = 1 ; j <= N - 1 && j <= s ; j ++ ) { dp [ i , s ] = Math . Max ( dp [ i , s ] , arr [ j - 1 ] + dp [ i - 1 , s - j ] ) ; } } } return dp [ N , 2 * ( N - 1 ) ] - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 0 } ; Console . Write ( maxScore ( arr ) ) ; } }