codeparrot/xlcost-text-to-code huggingface dataset:
Number of subsequences with negative product | C # implementation of the approach ; Function to return the count of all the subsequences with negative product ; To store the count of positive elements in the array ; To store the count of negative elements in the array ; If the current element is positive ; If the current element is negative ; For all the positive elements of the array ; For all the negative elements of the array ; Driver code
using System ; class GFG { static int cntSubSeq ( int [ ] arr , int n ) { int pos_count = 0 ; int neg_count = 0 ; int result ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) pos_count ++ ; if ( arr [ i ] < 0 ) neg_count ++ ; } result = ( int ) Math . Pow ( 2 , pos_count ) ; if ( neg_count > 0 ) result *= ( int ) Math . Pow ( 2 , neg_count - 1 ) ; else result = 0 ; return result ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , - 4 , - 1 , 6 } ; int n = arr . Length ; Console . Write ( cntSubSeq ( arr , n ) ) ; } }
Find minimum number of steps to reach the end of String | C # implementation of the approach ; Function to return the minimum number of steps to reach the end ; If the end can 't be reached ; Already at the end ; If the length is 2 or 3 then the end can be reached in a single step ; For the other cases , solve the problem using dynamic programming ; It requires no move from the end to reach the end ; From the 2 nd last and the 3 rd last index , only a single move is required ; Update the answer for every index ; If the current index is not reachable ; To store the minimum steps required from the current index ; If it is a valiINT_MAXd move then update the minimum steps required ; Update the minimum steps required starting from the current index ; Cannot reach the end starting from str [ 0 ] ; Return the minimum steps required ; Driver code
using System ; class GFG { static int INT_MAX = int . MaxValue ; static int minSteps ( string str , int n , int k ) { if ( str [ n - 1 ] == '0' ) return - 1 ; if ( n == 1 ) return 0 ; if ( n < 4 ) return 1 ; int [ ] dp = new int [ n ] ; dp [ n - 1 ] = 0 ; dp [ n - 2 ] = 1 ; dp [ n - 3 ] = 1 ; for ( int i = n - 4 ; i >= 0 ; i -- ) { if ( str [ i ] == '0' ) continue ; int steps = INT_MAX ; if ( i + k < n && str [ i + k ] == '1' ) steps = Math . Min ( steps , dp [ i + k ] ) ; if ( str [ i + 1 ] == '1' ) steps = Math . Min ( steps , dp [ i + 1 ] ) ; if ( str [ i + 2 ] == '1' ) steps = Math . Min ( steps , dp [ i + 2 ] ) ; dp [ i ] = ( steps == INT_MAX ) ? steps : 1 + steps ; } if ( dp [ 0 ] == INT_MAX ) return - 1 ; return dp [ 0 ] ; } public static void Main ( ) { string str = "101000011" ; int n = str . Length ; int k = 5 ; Console . WriteLine ( minSteps ( str , n , k ) ) ; } }
Length of longest Palindromic Subsequence of even length with no two adjacent characters same | C # implementation of the above approach ; To store the values of subproblems ; Function to find the Longest Palindromic subsequence of even length with no two adjacent characters same ; Base cases If the String length is 1 return 0 ; If the String length is 2 ; Check if the characters match ; If the value with given parameters is previously calculated ; If the first and last character of the String matches with the given character ; Remove the first and last character and call the function for all characters ; If it does not match ; Then find the first and last index of given character in the given String ; Take the subString from i to j and call the function with subString and the given character ; Store the answer for future use ; Driver Code ; Check for all starting characters
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static Dictionary < string , int > dp = new Dictionary < string , int > ( ) ; static int solve ( char [ ] s , char c ) { if ( s . Length == 1 ) return 0 ; if ( s . Length == 2 ) { if ( s [ 0 ] == s [ 1 ] && s [ 0 ] == c ) return 1 ; else return 0 ; } if ( dp . ContainsKey ( new string ( s ) + " ▁ " + c ) ) return dp [ new string ( s ) + " ▁ " + c ] ; int ans = 0 ; if ( s [ 0 ] == s [ s . Length - 1 ] && s [ 0 ] == c ) { for ( char c1 = ' a ' ; c1 <= ' z ' ; c1 ++ ) if ( c1 != c ) { int len = s . Length - 2 ; char [ ] tmp = new char [ len ] ; Array . Copy ( s , 1 , tmp , 0 , len ) ; ans = Math . Max ( ans , 1 + solve ( tmp , c1 ) ) ; } } else { for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == c ) { for ( int j = s . Length - 1 ; j > i ; j -- ) if ( s [ j ] == c ) { if ( j == i ) break ; int len = j + 1 - i ; char [ ] tmp = new char [ len ] ; Array . Copy ( s , i , tmp , 0 , len ) ; ans = solve ( tmp , c ) ; break ; } break ; } } } dp [ new string ( s ) + " ▁ " + c ] = ans ; return dp [ new string ( s ) + " ▁ " + c ] ; } public static void Main ( string [ ] args ) { string s = " abscrcdba " ; int ma = 0 ; for ( char c1 = ' a ' ; c1 <= ' z ' ; c1 ++ ) ma = Math . Max ( ma , solve ( s . ToCharArray ( ) , c1 ) * 2 ) ; Console . Write ( ma + " STRNEWLINE " ) ; } }
Eggs dropping puzzle | Set 2 | C # implementation of the approach ; Function to return the minimum number of trials needed in the worst case with n eggs and k floors ; Fill all the entries in table using optimal substructure property ; Return the minimum number of moves ; Driver code
using System ; class GFG { static int eggDrop ( int n , int k ) { int [ , ] dp = new int [ k + 1 , n + 1 ] ; int x = 0 ; while ( dp [ x , n ] < k ) { x ++ ; for ( int i = 1 ; i <= n ; i ++ ) dp [ x , i ] = dp [ x - 1 , i - 1 ] + dp [ x - 1 , i ] + 1 ; } return x ; } public static void Main ( String [ ] args ) { int n = 2 , k = 36 ; Console . WriteLine ( eggDrop ( n , k ) ) ; } }
Maximum length of Strictly Increasing Sub | C # implementation of the approach ; Function to return the maximum length of strictly increasing subarray after removing atmost one element ; Create two arrays pre and pos ; Find out the contribution of the current element in array [ 0 , i ] and update pre [ i ] ; Find out the contribution of the current element in array [ N - 1 , i ] and update pos [ i ] ; Calculate the maximum length of the stricly increasing subarray without removing any element ; Calculate the maximum length of the strictly increasing subarray after removing the current element ; Driver code
using System ; class GFG { static int maxIncSubarr ( int [ ] a , int n ) { int [ ] pre = new int [ n ] ; int [ ] pos = new int [ n ] ; pre [ 0 ] = 1 ; pos [ n - 1 ] = 1 ; int l = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] > a [ i - 1 ] ) pre [ i ] = pre [ i - 1 ] + 1 ; else pre [ i ] = 1 ; } l = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( a [ i ] < a [ i + 1 ] ) pos [ i ] = pos [ i + 1 ] + 1 ; else pos [ i ] = 1 ; } int ans = 0 ; l = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] > a [ i - 1 ] ) l ++ ; else l = 1 ; ans = Math . Max ( ans , l ) ; } for ( int i = 1 ; i <= n - 2 ; i ++ ) { if ( a [ i - 1 ] < a [ i + 1 ] ) ans = Math . Max ( pre [ i - 1 ] + pos [ i + 1 ] , ans ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 2 } ; int n = arr . Length ; Console . WriteLine ( maxIncSubarr ( arr , n ) ) ; } }
Number of square matrices with all 1 s | C # program to return the number of square submatrices with all 1 s ; Function to return the number of square submatrices with all 1 s ; Initialize count variable ; If a [ i ] [ j ] is equal to 0 ; Calculate number of square submatrices ending at ( i , j ) ; Calculate the sum of the array ; Driver code
using System ; class GFG { static int n = 3 ; static int m = 3 ; static int countSquareMatrices ( int [ , ] a , int N , int M ) { int count = 0 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { if ( a [ i , j ] == 0 ) continue ; a [ i , j ] = Math . Min ( Math . Min ( a [ i - 1 , j ] , a [ i , j - 1 ] ) , a [ i - 1 , j - 1 ] ) + 1 ; } } for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) count += a [ i , j ] ; return count ; } public static void Main ( ) { int [ , ] arr = { { 1 , 0 , 1 } , { 1 , 1 , 0 } , { 1 , 1 , 0 } } ; Console . WriteLine ( countSquareMatrices ( arr , n , m ) ) ; } }
Count number of increasing sub | C # implementation of the approach ; Segment tree array ; Function for point update In segment tree ; Base case ; If l == r == up ; Mid element ; Updating the segment tree ; Function for the range sum - query ; Base case ; Mid element ; Calling for the left and the right subtree ; Function to return the count ; Copying array arr to sort it ; Sorting array brr ; Map to store the rank of each element ; dp array ; To store the readonly answer ; Updating the dp array ; Rank of the element ; Solving the dp - states using segment tree ; Updating the readonly answer ; Updating the segment tree ; Returning the readonly answer ; Driver code
using System ; using System . Collections . Generic ; class GFG { static readonly int N = 10000 ; static int [ ] seg = new int [ 3 * N ] ; static int update ( int In , int l , int r , int up_in , int val ) { if ( r < up_in l > up_in ) return seg [ In ] ; if ( l == up_in && r == up_in ) return seg [ In ] = val ; int m = ( l + r ) / 2 ; return seg [ In ] = update ( 2 * In + 1 , l , m , up_in , val ) + update ( 2 * In + 2 , m + 1 , r , up_in , val ) ; } static int query ( int In , int l , int r , int l1 , int r1 ) { if ( l > r ) return 0 ; if ( r < l1 l > r1 ) return 0 ; if ( l1 <= l && r <= r1 ) return seg [ In ] ; int m = ( l + r ) / 2 ; return query ( 2 * In + 1 , l , m , l1 , r1 ) + query ( 2 * In + 2 , m + 1 , r , l1 , r1 ) ; } static int findCnt ( int [ ] arr , int n ) { int [ ] brr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; Array . Sort ( brr ) ; Dictionary < int , int > r = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) r . Add ( brr [ i ] , i + 1 ) ; int [ ] dp = new int [ n ] ; int ans = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int rank = r [ arr [ i ] ] ; dp [ i ] = 1 + query ( 0 , 0 , n - 1 , rank , n - 1 ) ; ans += dp [ i ] ; update ( 0 , 0 , n - 1 , rank - 1 , dp [ i ] + query ( 0 , 0 , n - 1 , rank - 1 , rank - 1 ) ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 10 , 9 } ; int n = arr . Length ; Console . Write ( findCnt ( arr , n ) ) ; } }
Minimum cost to build N blocks from one block | C # program to Minimum cost to build N blocks from one block ; Function to calculate min cost to build N blocks ; Initialize base case ; Recurence when i is odd ; Recurence when i is even ; Driver code
using System ; class GFG { static int minCost ( int n , int x , int y , int z ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 1 ) { dp [ i ] = Math . Min ( dp [ ( i + 1 ) / 2 ] + x + z , dp [ i - 1 ] + y ) ; } else { dp [ i ] = Math . Min ( dp [ i / 2 ] + x , dp [ i - 1 ] + y ) ; } } return dp [ n ] ; } public static void Main ( String [ ] args ) { int n = 5 , x = 2 , y = 1 , z = 3 ; Console . Write ( minCost ( n , x , y , z ) + " STRNEWLINE " ) ; } }
Size of the largest divisible subset in an Array | C # implementation of the above approach ; Function to find the size of the largest divisible subarray ; Mark all elements of the array ; Traverse reverse ; For all multiples of i ; Return the required answer ; Driver code ; Function call
using System ; class GFG { static int N = 1000005 ; static int maximum_set ( int [ ] a , int n ) { int [ ] dp = new int [ N ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ a [ i ] ] = 1 ; int ans = 1 ; for ( int i = N - 1 ; i >= 1 ; i -- ) { if ( dp [ i ] != 0 ) { for ( int j = 2 * i ; j < N ; j += i ) { dp [ i ] = Math . Max ( dp [ i ] , 1 + dp [ j ] ) ; ans = Math . Max ( ans , dp [ i ] ) ; } } } return ans ; } public static void Main ( ) { int [ ] arr = { 3 , 4 , 6 , 8 , 10 , 18 , 21 , 24 } ; int n = arr . Length ; Console . WriteLine ( maximum_set ( arr , n ) ) ; } }
Longest sub | C # implementation of th approach ; Function to return the length of the largest sub - string divisible by 3 ; Base - case ; If the state has been solved before then return its value ; Marking the state as solved ; Recurrence relation ; Driver code
using System ; using System . Collections . Generic ; class GFG { readonly static int N = 100 ; readonly static int INT_MIN = int . MinValue ; static int [ , ] dp = new int [ N , 3 ] ; static int [ , ] v = new int [ N , 3 ] ; static int findLargestString ( String s , int i , int r ) { if ( i == s . Length ) { if ( r == 0 ) return 0 ; else return INT_MIN ; } if ( v [ i , r ] == 1 ) return dp [ i , r ] ; v [ i , r ] = 1 ; dp [ i , r ] = Math . Max ( 1 + findLargestString ( s , i + 1 , ( r * 2 + ( s [ i ] - '0' ) ) % 3 ) , findLargestString ( s , i + 1 , r ) ) ; return dp [ i , r ] ; } public static void Main ( String [ ] args ) { String s = "101" ; Console . Write ( findLargestString ( s , 0 , 0 ) ) ; } }
Number of sub | C # implementation of th approach ; Function to return the number of sub - sequences divisible by 3 ; Base - cases ; If the state has been solved before then return its value ; Marking the state as solved ; Recurrence relation ; Driver code
using System ; class GFG { static readonly int N = 100 ; static int [ , ] dp = new int [ N , 3 ] ; static int [ , ] v = new int [ N , 3 ] ; static int findCnt ( String s , int i , int r ) { if ( i == s . Length ) { if ( r == 0 ) return 1 ; else return 0 ; } if ( v [ i , r ] == 1 ) return dp [ i , r ] ; v [ i , r ] = 1 ; dp [ i , r ] = findCnt ( s , i + 1 , ( r * 2 + ( s [ i ] - '0' ) ) % 3 ) + findCnt ( s , i + 1 , r ) ; return dp [ i , r ] ; } public static void Main ( String [ ] args ) { String s = "11" ; Console . Write ( findCnt ( s , 0 , 0 ) - 1 ) ; } }
Remove minimum elements from ends of array so that sum decreases by at least K | O ( N ) | C # implementation of the approach ; Function to return the count of minimum elements to be removed from the ends of the array such that the sum of the array decrease by at least K ; To store the final answer ; Maximum possible sum required ; Left point ; Right pointer ; Total current sum ; Two pointer loop ; If the sum fits ; Update the answer ; Update the total sum ; Increment the left pointer ; Driver code
using System ; class GFG { static int minCount ( int [ ] arr , int n , int k ) { int ans = 0 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; sum -= k ; int l = 0 ; int r = 0 ; int tot = 0 ; while ( l < n ) { if ( tot <= sum ) { ans = Math . Max ( ans , r - l ) ; if ( r == n ) break ; tot += arr [ r ++ ] ; } else { tot -= arr [ l ++ ] ; } } return ( n - ans ) ; } public static void Main ( ) { int [ ] arr = { 1 , 11 , 5 , 5 } ; int n = arr . Length ; int k = 11 ; Console . WriteLine ( minCount ( arr , n , k ) ) ; } }
Minimum cost to merge all elements of List | C # implementation of the approach ; To store the states of DP ; Function to return the minimum merge cost ; Base case ; If the state has been solved before ; Marking the state as solved ; Reference to dp [ i , j ] ; To store the sum of all the elements in the subarray arr [ i ... j ] ; Loop to iterate the recurrence ; Returning the solved value ; Driver code
using System ; class GFG { static readonly int N = 401 ; static int [ , ] dp = new int [ N , N ] ; static bool [ , ] v = new bool [ N , N ] ; static int minMergeCost ( int i , int j , int [ ] arr ) { if ( i == j ) return 0 ; if ( v [ i , j ] ) return dp [ i , j ] ; v [ i , j ] = true ; int x = dp [ i , j ] ; x = int . MaxValue ; int tot = 0 ; for ( int k = i ; k <= j ; k ++ ) tot += arr [ k ] ; for ( int k = i + 1 ; k <= j ; k ++ ) { x = Math . Min ( x , tot + minMergeCost ( i , k - 1 , arr ) + minMergeCost ( k , j , arr ) ) ; } return x ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 7 } ; int n = arr . Length ; Console . Write ( minMergeCost ( 0 , n - 1 , arr ) ) ; } }
Expected number of moves to reach the end of a board | Dynamic programming | C # implementation of the approach ; To store the states of dp ; To determine whether a state has been solved before ; Function to return the count ; Base cases ; If a state has been solved before it won 't be evaluated again ; Recurrence relation ; Driver code
using System ; class GFG { static int maxSize = 50 ; static double [ ] dp = new double [ maxSize ] ; static int [ ] v = new int [ maxSize ] ; static double expectedSteps ( int x ) { if ( x == 0 ) return 0 ; if ( x <= 5 ) return 6 ; if ( v [ x ] == 1 ) return dp [ x ] ; v [ x ] = 1 ; dp [ x ] = 1 + ( expectedSteps ( x - 1 ) + expectedSteps ( x - 2 ) + expectedSteps ( x - 3 ) + expectedSteps ( x - 4 ) + expectedSteps ( x - 5 ) + expectedSteps ( x - 6 ) ) / 6 ; return dp [ x ] ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( expectedSteps ( n - 1 ) ) ; } }
Number of subsequences in a given binary string divisible by 2 | C # implementation of the approach ; Function to return the count of the required subsequences ; To store the final answer ; Multiplier ; Loop to find the answer ; Condition to update the answer ; updating multiplier ; Driver code
using System ; class GFG { static int countSubSeq ( string str , int len ) { int ans = 0 ; int mul = 1 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == '0' ) ans += mul ; mul *= 2 ; } return ans ; } static public void Main ( ) { string str = "10010" ; int len = str . Length ; Console . WriteLine ( countSubSeq ( str , len ) ) ; } }
Knapsack with large Weights | C # implementation of the approach ; To store the states of DP ; Function to solve the recurrence relation ; Base cases ; Marking state as solved ; Recurrence relation ; Function to return the maximum weight ; Iterating through all possible values to find the the largest value that can be represented by the given weights ; Driver code
using System ; class GFG { static readonly int V_SUM_MAX = 1000 ; static readonly int N_MAX = 100 ; static readonly int W_MAX = 10000000 ; static int [ , ] dp = new int [ V_SUM_MAX + 1 , N_MAX ] ; static bool [ , ] v = new bool [ V_SUM_MAX + 1 , N_MAX ] ; static int solveDp ( int r , int i , int [ ] w , int [ ] val , int n ) { if ( r <= 0 ) return 0 ; if ( i == n ) return W_MAX ; if ( v [ r , i ] ) return dp [ r , i ] ; v [ r , i ] = true ; dp [ r , i ] = Math . Min ( solveDp ( r , i + 1 , w , val , n ) , w [ i ] + solveDp ( r - val [ i ] , i + 1 , w , val , n ) ) ; return dp [ r , i ] ; } static int maxWeight ( int [ ] w , int [ ] val , int n , int c ) { for ( int i = V_SUM_MAX ; i >= 0 ; i -- ) { if ( solveDp ( i , 0 , w , val , n ) <= c ) { return i ; } } return 0 ; } public static void Main ( String [ ] args ) { int [ ] w = { 3 , 4 , 5 } ; int [ ] val = { 30 , 50 , 60 } ; int n = w . Length ; int C = 8 ; Console . WriteLine ( maxWeight ( w , val , n , C ) ) ; } }
Sum of lengths of all paths possible in a given tree | C # implementation of the approach ; Number of vertices ; Adjacency list representation of the tree ; Array that stores the subtree size ; Array to mark all the vertices which are visited ; Utility function to create an edge between two vertices ; Add a to b 's list ; Add b to a 's list ; Function to calculate the subtree size ; Mark visited ; For every adjacent node ; If not already visited ; Recursive call for the child ; Function to calculate the contribution of each edge ; Mark current node as visited ; For every adjacent node ; If it is not already visisted ; Function to return the required sum ; First pass of the dfs ; Second pass ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int sz = 100005 ; static int n ; static ArrayList [ ] tree = new ArrayList [ sz ] ; static int [ ] subtree_size = new int [ sz ] ; static int [ ] vis = new int [ sz ] ; static void addEdge ( int a , int b ) { tree [ a ] . Add ( b ) ; tree [ b ] . Add ( a ) ; } static int dfs ( int node ) { vis [ node ] = 1 ; subtree_size [ node ] = 1 ; foreach ( int child in tree [ node ] ) { if ( vis [ child ] == 0 ) { subtree_size [ node ] += dfs ( child ) ; } } return subtree_size [ node ] ; } static void contribution ( int node , ref int ans ) { vis [ node ] = 1 ; foreach ( int child in tree [ node ] ) { if ( vis [ child ] == 0 ) { ans += ( subtree_size [ child ] * ( n - subtree_size [ child ] ) ) ; contribution ( child , ref ans ) ; } } } static int getSum ( ) { Array . Fill ( vis , 0 ) ; dfs ( 0 ) ; int ans = 0 ; Array . Fill ( vis , 0 ) ; contribution ( 0 , ref ans ) ; return ans ; } public static void Main ( ) { n = 5 ; for ( int i = 0 ; i < sz ; i ++ ) { tree [ i ] = new ArrayList ( ) ; } addEdge ( 0 , 1 ) ; addEdge ( 0 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 1 , 4 ) ; Console . Write ( getSum ( ) ) ; } }
Number of cells in a matrix that satisfy the given condition | C # implementation of the approach ; Function to return the number of cells in which mirror can be placed ; Update the row array where row [ i , j ] will store whether the current row i contains all 1 s in the columns starting from j ; Update the column array where col [ i , j ] will store whether the current column j contains all 1 s in the rows starting from i ; To store the required result ; For every cell except the last row and the last column ; If the current cell is not blocked and the light can travel from the next row and the next column then the current cell is valid ; For the last column ; For the last row , note that the last column is not taken into consideration as the bottom right element has already been considered in the last column previously ; Driver code
using System ; class GFG { static int N = 3 ; static int numberOfCells ( int [ , ] mat ) { bool [ , ] row = new bool [ N , N ] ; bool [ , ] col = new bool [ N , N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = N - 1 ; j >= 0 ; j -- ) { if ( mat [ i , j ] == 1 ) { row [ i , j ] = ( j + 1 < N ) ? row [ i , j + 1 ] : true ; } else { row [ i , j ] = false ; } } } for ( int j = 0 ; j < N ; j ++ ) { for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( mat [ i , j ] == 1 ) { col [ i , j ] = ( i + 1 < N ) ? col [ i + 1 , j ] : true ; } else { col [ i , j ] = false ; } } } int cnt = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = 0 ; j < N - 1 ; j ++ ) { if ( row [ i , j ] && col [ i , j ] ) { cnt ++ ; } } } for ( int i = 0 ; i < N ; i ++ ) { if ( col [ i , N - 1 ] ) cnt ++ ; } for ( int j = 0 ; j < N - 1 ; j ++ ) { if ( row [ N - 1 , j ] ) cnt ++ ; } return cnt ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 0 , 1 , 1 } , { 0 , 1 , 1 } , { 0 , 1 , 1 } } ; Console . Write ( numberOfCells ( mat ) ) ; } }
Maximum possible GCD after replacing at most one element in the given array | C # implementation of the approach ; Function to return the maximum possible gcd after replacing a single element ; Prefix and Suffix arrays ; Single state dynamic programming relation for storing gcd of first i elements from the left in Prefix [ i ] ; Initializing Suffix array ; Single state dynamic programming relation for storing gcd of all the elements having index greater than or equal to i in Suffix [ i ] ; If first or last element of the array has to be replaced ; If any other element is replaced ; Return the maximized gcd ; Driver code
using System ; class GFG { static int MaxGCD ( int [ ] a , int n ) { int [ ] Prefix = new int [ n + 2 ] ; int [ ] Suffix = new int [ n + 2 ] ; Prefix [ 1 ] = a [ 0 ] ; for ( int i = 2 ; i <= n ; i += 1 ) { Prefix [ i ] = __gcd ( Prefix [ i - 1 ] , a [ i - 1 ] ) ; } Suffix [ n ] = a [ n - 1 ] ; for ( int i = n - 1 ; i >= 1 ; i -= 1 ) { Suffix [ i ] = __gcd ( Suffix [ i + 1 ] , a [ i - 1 ] ) ; } int ans = Math . Max ( Suffix [ 2 ] , Prefix [ n - 1 ] ) ; for ( int i = 2 ; i < n ; i += 1 ) { ans = Math . Max ( ans , __gcd ( Prefix [ i - 1 ] , Suffix [ i + 1 ] ) ) ; } return ans ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 6 , 7 , 8 } ; int n = a . Length ; Console . WriteLine ( MaxGCD ( a , n ) ) ; } }