codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of days to debug all programs | C # program for the above approach ; Function to calculate the minimum work sessions ; Break condition ; All bits are set ; Check if already calculated ; Store the answer ; Check if ith bit is set or unset ; Including in current work session ; Including in next work session ; Resultant answer will be minimum of both ; Function to initialize DP array and solve the problem ; Initialize dp table with - 1 ; Resultant mask ; no . of minimum work sessions is even ; no . of minimum work sessions is odd ; Driver code
using System ; public class GFG { public static int minSessions ( int [ ] codeTime , int [ , ] dp , int ones , int n , int mask , int currTime , int WorkingSessionTime ) { if ( currTime > WorkingSessionTime ) return Int32 . MaxValue ; if ( mask == ones ) return 1 ; if ( dp [ mask , currTime ] != - 1 ) return dp [ mask , currTime ] ; int ans = Int32 . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( mask & ( 1 << i ) ) == 0 ) { int inc = minSessions ( codeTime , dp , ones , n , mask | ( 1 << i ) , currTime + codeTime [ i ] , WorkingSessionTime ) ; int inc_next = 1 + minSessions ( codeTime , dp , ones , n , mask | ( 1 << i ) , codeTime [ i ] , WorkingSessionTime ) ; ans = Math . Min ( ans , Math . Min ( inc , inc_next ) ) ; } } return dp [ mask , currTime ] = ans ; } public static int solve ( int [ ] codeTime , int n , int WorkingSessionTime ) { int [ , ] dp = new int [ ( 1 << 14 ) , 15 ] ; for ( int i = 0 ; i < 1 << 14 ; i ++ ) { for ( int j = 0 ; j < 15 ; j ++ ) { dp [ i , j ] = - 1 ; } } int ones = ( 1 << n ) - 1 ; int ans = minSessions ( codeTime , dp , ones , n , 0 , 0 , WorkingSessionTime ) ; if ( WorkingSessionTime < 6 ) { if ( ans % 2 == 0 ) ans = ans / 2 ; else ans = ( ans / 2 ) + 1 ; } return ans ; } static public void Main ( ) { int [ ] codeTime = { 1 , 2 , 3 , 1 , 1 , 3 } ; int n = codeTime . Length ; int WorkingSessionTime = 4 ; Console . WriteLine ( solve ( codeTime , n , WorkingSessionTime ) ) ; } }
Count of non | C # program for the above approach ; Define the dp table globally ; Recursive function to calculate total number of valid non - decreasing strings ; If already calculated state ; Base Case ; Stores the total count of strings formed ; Fill the value in dp matrix ; Function to find the total number of non - decreasing string formed by replacing the ' ? ' ; Initialize all value of dp table with - 1 ; Left and Right limits ; Iterate through all the characters of the string S ; Change R to the current character ; Call the recursive function ; Change L to R and R to 9 ; Reinitialize the length of ? to 0 ; Increment the length of the segment ; Update the ans ; Return the total count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int MAXN = 100005 ; static int [ , ] dp = new int [ MAXN , 10 ] ; static int solve ( int len , int gap ) { if ( dp [ len , gap ] != - 1 ) { return dp [ len , gap ] ; } if ( len == 0 gap == 0 ) { return 1 ; } if ( gap < 0 ) { return 0 ; } int ans = 0 ; for ( int i = 0 ; i <= gap ; i ++ ) { ans += solve ( len - 1 , gap - i ) ; } return dp [ len , gap ] = ans ; } static int countValidStrings ( string S ) { for ( int i = 0 ; i < MAXN ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { dp [ i , j ] = - 1 ; } } int N = S . Length ; int L = 1 , R = 9 ; int cnt = 0 ; int ans = 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] != ' ? ' ) { R = ( int ) S [ i ] - 48 ; ans *= solve ( cnt , R - L ) ; L = R ; R = 9 ; cnt = 0 ; } else { cnt ++ ; } } ans *= solve ( cnt , R - L ) ; return ans ; } public static void Main ( ) { string S = "1 ? ? ?2" ; Console . Write ( countValidStrings ( S ) ) ; } }
K | C # program for the above approach ; Recursive function to find the Kth smallest binary string ; Base Case ; Return string of all 1 's  of length B ; Return string of all 0 's  of length A ; Function to find the Kth lexicographically smallest binary string with exactly A zeroes and B ones ; Stores the recurring states ; Calculate the dp values iteratively ; The last character was '0' ; The last character was '1' ; Print the binary string obtained ; Driver Code
using System ; class GFG { static string KthString ( int A , int B , long K , int [ , ] dp ) { if ( A == 0 ) { string ans = " " ; for ( int i = 0 ; i < B ; i ++ ) { ans += '1' ; } return ans ; } if ( B == 0 ) { string ans = " " ; for ( int i = 0 ; i < A ; i ++ ) { ans += '0' ; } return ans ; } if ( K <= dp [ A - 1 , B ] ) { return "0" + KthString ( A - 1 , B , K , dp ) ; } else { return "1" + KthString ( A , B - 1 , K - dp [ A - 1 , B ] , dp ) ; } } static int KthStringUtil ( int A , int B , int K ) { int [ , ] dp = new int [ A + 1 , B + 1 ] ; dp [ 0 , 0 ] = 1 ; for ( int i = 0 ; i <= A ; ++ i ) { for ( int j = 0 ; j <= B ; ++ j ) { if ( i > 0 ) { dp [ i , j ] += dp [ i - 1 , j ] ; } if ( j > 0 ) { dp [ i , j ] += dp [ i , j - 1 ] ; } } } Console . WriteLine ( KthString ( A , B , K , dp ) ) ; return 0 ; } public static void Main ( string [ ] args ) { int A = 3 , B = 3 , K = 7 ; KthStringUtil ( A , B , K ) ; } }
Count of binary arrays of size N with sum of product of adjacent pairs equal to K | C # program for the above approach ; Function to return the number of total possible combinations of 0 and 1 to form an array of size N having sum of product of consecutive elements K ; If value is greater than K , then return 0 as no combination is possible to have sum K ; Check if the result of this recursive call is memoised already , if it is then just return the previously calculated result ; Check if the value is equal to K at N , if it is then return 1 as this combination is possible . Otherwise return 0. ; If previous element is 1 ; If current element is 1 as well , then add 1 to value ; If current element is 0 , then value will remain same ; If previous element is 0 , then value will remain same irrespective of the current element ; Memoise and return the ans ; Driver Code ; As the array can be started by 0 or 1 , so take both cases while calculating the total possible combinations
using System ; class GFG { static int combinationsPossible ( int N , int idx , int prev , int val , int K , int [ , , ] dp ) { if ( val > K ) { return 0 ; } if ( dp [ val , idx , prev ] != - 1 ) { return dp [ val , idx , prev ] ; } if ( idx == N - 1 ) { if ( val == K ) { return 1 ; } return 0 ; } int ans = 0 ; if ( prev == 1 ) { ans += combinationsPossible ( N , idx + 1 , 1 , val + 1 , K , dp ) ; ans += combinationsPossible ( N , idx + 1 , 0 , val , K , dp ) ; } else { ans += combinationsPossible ( N , idx + 1 , 1 , val , K , dp ) ; ans += combinationsPossible ( N , idx + 1 , 0 , val , K , dp ) ; } return dp [ val , idx , prev ] = ans ; } public static void Main ( ) { int N = 5 ; int K = 3 ; int [ , , ] dp = new int [ K + 1 , N + 1 , 2 ] ; for ( int i = 0 ; i < K + 1 ; i ++ ) for ( int j = 0 ; j < N + 1 ; j ++ ) for ( int l = 0 ; l < 2 ; l ++ ) dp [ i , j , l ] = - 1 ; Console . WriteLine ( combinationsPossible ( N , 0 , 0 , 0 , K , dp ) + combinationsPossible ( N , 0 , 1 , 0 , K , dp ) ) ; } }
Count ways to split an array into subarrays such that sum of the i | C # program for the above approach ; Function to count ways to split an array into subarrays such that sum of the i - th subarray is divisible by i ; Stores the prefix sum of array ; Find the prefix sum ; Initialize dp [ ] [ ] array ; Stores the count of splitting ; Iterate over the range [ 0 , N ] ; Update the dp table ; If the last index is reached , then add it to the variable ans ; Return the possible count of splitting of array into subarrays ; Driver Code
using System ; public class GFG { static int countOfWays ( int [ ] arr , int N ) { int [ ] pre = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { pre [ i + 1 ] = pre [ i ] + arr [ i ] ; } int [ , ] dp = new int [ N + 2 , N + 2 ] ; dp [ 1 , 0 ] ++ ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = N ; j >= 1 ; j -- ) { dp [ j + 1 , pre [ i + 1 ] % ( j + 1 ) ] += dp [ j , pre [ i + 1 ] % j ] ; if ( i == N - 1 ) { ans += dp [ j , pre [ i + 1 ] % j ] ; } } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; Console . WriteLine ( countOfWays ( arr , N ) ) ; } }
Queries to find minimum absolute difference between adjacent array elements in given ranges | C # program for the above approach ; Stores the index for the minimum value in the subarray arr [ i , j ] ; Structure for query range ; Function to fill the lookup array lookup [ , ] in the bottom up manner ; Initialize M for the intervals with length 1 ; Find the values from smaller to bigger intervals ; Compute minimum value for all intervals with size 2 ^ j ; For arr [ 2 , 10 ] , compare arr [ lookup [ 0 , 3 ] ] and arr [ lookup [ 3 , 3 ] ] ; Otherwise ; Function find minimum of absolute difference of all adjacent element in subarray arr [ L . . R ] ; For [ 2 , 10 ] , j = 3 ; For [ 2 , 10 ] , compare arr [ lookup [ 0 , 3 ] ] and arr [ lookup [ 3 , 3 ] ] , ; Function to find the minimum of the ranges for M queries ; Fills table lookup [ n , Log n ] ; Compute sum of all queries ; Left and right boundaries of current range ; Print sum of current query range ; Function to find the minimum absolute difference in a range ; diff [ ] is to stores the absolute difference of adjacent elements ; Call Min_difference to get minimum difference of adjacent elements ; Driver Code
using System ; public class GFG { static readonly int MAX = 500 ; static int [ , ] lookup = new int [ MAX , MAX ] ; class Query { public int L , R ; public Query ( int l , int r ) { L = l ; R = r ; } } ; static void preprocess ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) lookup [ i , 0 ] = i ; for ( int j = 1 ; ( 1 << j ) <= n ; j ++ ) { for ( int i = 0 ; ( i + ( 1 << j ) - 1 ) < n ; i ++ ) { if ( arr [ lookup [ i , j - 1 ] ] < arr [ lookup [ i + ( 1 << ( j - 1 ) ) , j - 1 ] ] ) lookup [ i , j ] = lookup [ i , j - 1 ] ; else lookup [ i , j ] = lookup [ i + ( 1 << ( j - 1 ) ) , j - 1 ] ; } } } static int query ( int [ ] arr , int L , int R ) { int j = ( int ) Math . Log ( R - L + 1 ) ; if ( arr [ lookup [ L , j ] ] <= arr [ lookup [ R - ( 1 << j ) + 1 , j ] ] ) return arr [ lookup [ L , j ] ] ; else return arr [ lookup [ R - ( 1 << j ) + 1 , j ] ] ; } static void Min_difference ( int [ ] arr , int n , Query [ ] q , int m ) { preprocess ( arr , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] . L , R = q [ i ] . R ; Console . WriteLine ( query ( arr , L , R - 1 ) ) ; } } static void minimumDifference ( int [ ] arr , Query [ ] q , int N , int m ) { int [ ] diff = new int [ N ] ; for ( int i = 0 ; i < N - 1 ; i ++ ) diff [ i ] = Math . Abs ( arr [ i ] - arr [ i + 1 ] ) ; Min_difference ( diff , N - 1 , q , m ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 6 , 1 , 8 , 3 , 4 } ; int N = arr . Length ; Query [ ] Q = { new Query ( 0 , 3 ) , new Query ( 1 , 5 ) , new Query ( 4 , 5 ) } ; int M = Q . Length ; minimumDifference ( arr , Q , N , M ) ; } }
Domino and Tromino tiling problem | C # program for the above approach ; Function to find the total number of ways to tile a 2 * N board using the given types of tile ; If N is less than 3 ; Store all dp - states ; Base Case ; Traverse the range [ 2 , N ] ; Update the value of dp [ i , 0 ] ; Update the value of dp [ i , 1 ] ; Update the value of dp [ i , 2 ] ; Return the number of ways as the value of dp [ N , 0 ] ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int MOD = 1000000007 ; static int numTilings ( int N ) { if ( N < 3 ) { return N ; } int [ , ] dp = new int [ N + 1 , 3 ] ; dp [ 0 , 0 ] = dp [ 1 , 0 ] = 1 ; dp [ 1 , 1 ] = dp [ 1 , 2 ] = 1 ; for ( int i = 2 ; i <= N ; i ++ ) { dp [ i , 0 ] = ( dp [ i - 1 , 0 ] + dp [ i - 2 , 0 ] + dp [ i - 2 , 1 ] + dp [ i - 2 , 2 ] ) % MOD ; dp [ i , 1 ] = ( dp [ i - 1 , 0 ] + dp [ i - 1 , 2 ] ) % MOD ; dp [ i , 2 ] = ( dp [ i - 1 , 0 ] + dp [ i - 1 , 1 ] ) % MOD ; } return dp [ N , 0 ] ; } public static void Main ( ) { int N = 3 ; Console . Write ( numTilings ( N ) ) ; } }
Count of N | C # program for the above approach ; Store the recurring recursive states ; Function to find the number of strings of length N such that it is a concatenation it substrings ; Single character cant be repeated ; Check if this state has been already calculated ; Stores the resultant count for the current recursive calls ; Iterate over all divisors ; Non - Repeated = Total - Repeated ; Non - Repeated = Total - Repeated ; Store the result for the further calculation ; Return resultant count ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static Dictionary < int , int > dp = new Dictionary < int , int > ( ) ; static int countStrings ( int N ) { if ( N == 1 ) return 0 ; if ( dp . ContainsKey ( N ) ) return dp [ N ] ; int ret = 0 ; for ( int div = 1 ; div <= Math . Sqrt ( N ) ; div ++ ) { if ( N % div == 0 ) { ret += ( 1 << div ) - countStrings ( div ) ; int div2 = N / div ; if ( div2 != div && div != 1 ) ret += ( 1 << div2 ) - countStrings ( div2 ) ; } } dp [ N ] = ret ; return ret ; } public static void Main ( ) { int N = 6 ; Console . Write ( countStrings ( N ) ) ; } }
Count N | C # program for the above approach ; Function to count N - digit numbers such that each position is divisible by the digit occurring at that position ; Stores the answer . ; Iterate from indices 1 to N ; Stores count of digits that can be placed at the current index ; Iterate from digit 1 to 9 ; If index is divisible by digit ; Multiply answer with possible choices ; Driver Code ; Given Input ; Function call
using System ; using System . Collections . Generic ; class GFG { static int mod = 1000000007 ; static void countOfNumbers ( int N ) { int ans = 1 ; for ( int index = 1 ; index <= N ; ++ index ) { int choices = 0 ; for ( int digit = 1 ; digit <= 9 ; ++ digit ) { if ( index % digit == 0 ) { ++ choices ; } } ans = ( ans * choices ) % mod ; } Console . Write ( ans ) ; } public static void Main ( ) { int N = 5 ; countOfNumbers ( N ) ; } }
Maximum number of groups that can receive fresh donuts distributed in batches of size K | C # program for the above approach ; Recursive function to find the maximum number of groups that will receive fresh donuts ; Store the result for the current state ; Check if the leftover donuts from the previous batch is 0 ; If true , then one by one give the fresh donuts to each group ; Decrement arr [ i ] ; Update the maximum number of groups ; Increment arr [ i ] ; Otherwise , traverse the given array , arr [ ] ; Decrement arr [ i ] ; Update the maximum number of groups ; Increment arr [ i ] ; Return the value of q ; Function to find the maximum number of groups that will receive fresh donuts ; Stores count of remainder by K ; Traverse the array arr [ ] ; Stores maximum number of groups ; Return the answer ; Driver code
using System ; class GFG { public static int dfs ( int [ ] arr , int left , int K ) { int q = 0 ; if ( left == 0 ) { for ( int i = 1 ; i < K ; ++ i ) { if ( arr [ i ] > 0 ) { arr [ i ] -- ; q = Math . Max ( q , 1 + dfs ( arr , K - i , K ) ) ; arr [ i ] ++ ; } } } else { for ( int i = 1 ; i < K ; ++ i ) { if ( arr [ i ] > 0 ) { arr [ i ] -- ; int nleft = ( i <= left ? left - i : K + left - i ) ; q = Math . Max ( q , dfs ( arr , nleft , K ) ) ; arr [ i ] ++ ; } } } return q ; } public static int maxGroups ( int K , int [ ] arr ) { int [ ] V = new int [ K ] ; foreach ( int x in arr ) V [ x % K ] ++ ; int ans = V [ 0 ] + dfs ( V , 0 , K ) ; return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int K = 3 ; Console . WriteLine ( maxGroups ( K , arr ) ) ; } }
Maximum jumps to reach end of Array with condition that index i can make arr [ i ] jumps | C # code for the above approach ; Function to find the maximum jumps to reach end of array ; Stores the jumps needed to reach end from each index ; Traverse the array ; Check if j is less than N ; Add dp [ j ] to the value of dp [ i ] ; Update the value of ans ; Print the value of ans ; Driver Code
using System ; class GFG { static void findMaxJumps ( int [ ] arr , int N ) { int [ ] dp = new int [ N ] ; int ans = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { dp [ i ] = arr [ i ] ; int j = i + arr [ i ] ; if ( j < N ) { dp [ i ] = dp [ i ] + dp [ j ] ; } ans = Math . Max ( ans , dp [ i ] ) ; } Console . Write ( ans ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 7 , 9 } ; int N = arr . Length ; findMaxJumps ( arr , N ) ; } }
Longest subsequence with non negative prefix sum at each position | C # program for the above approach ; Function to find the length of the longest subsequence with non negative prefix sum at each position ; Stores the maximum sum possible if we include j elements till the position i ; Initialize dp array with - 1 ; Maximum subsequence sum by including no elements till position ' i ' ; Maximum subsequence sum by including first element at first position ; Iterate over all the remaining positions ; If the current element is excluded ; If current element is included and if total sum is positive or not ; Select the maximum j by which a non negative prefix sum subsequence can be obtained ; Print the answer ; Driver code
using System ; class GFG { static void longestSubsequence ( int [ ] arr , int N ) { int [ , ] dp = new int [ N , N + 1 ] ; for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < N + 1 ; ++ j ) { dp [ i , j ] = - 1 ; } } for ( int i = 0 ; i < N ; ++ i ) { dp [ i , 0 ] = 0 ; } dp [ 0 , 1 ] = ( arr [ 0 ] >= 0 ? arr [ 0 ] : - 1 ) ; for ( int i = 1 ; i < N ; ++ i ) { for ( int j = 1 ; j <= ( i + 1 ) ; ++ j ) { if ( dp [ i - 1 , j ] != - 1 ) { dp [ i , j ] = Math . Max ( dp [ i , j ] , dp [ i - 1 , j ] ) ; } if ( dp [ i - 1 , j - 1 ] >= 0 && dp [ i - 1 , j - 1 ] + arr [ i ] >= 0 ) { dp [ i , j ] = Math . Max ( dp [ i , j ] , dp [ i - 1 , j - 1 ] + arr [ i ] ) ; } } } int ans = 0 ; for ( int j = 0 ; j <= N ; ++ j ) { if ( dp [ N - 1 , j ] >= 0 ) { ans = j ; } } Console . Write ( ans ) ; } public static void Main ( ) { int [ ] arr = { 4 , - 4 , 1 , - 3 , 1 , - 3 } ; int N = arr . Length ; longestSubsequence ( arr , N ) ; } }
Count of strictly increasing N | C # program for the above approach ; Declaration of dp table ; Function to find the count of all N digit numbers such that all the digit is less than its adjacent digits ; Base Case : If i = n , then return 1 as valid number has been formed ; If the state has already been computed , then return it ; Stores the total count of ways for the current recursive call ; If i = 0 , any digit from [ 1 - 9 ] can be placed and also if N = 1 , then 0 can also be placed ; If i = 1 , any digit from [ 0 - 9 ] can be placed such that digit is not equal to previous digit ; If the current digit is not same as the prev ; Place the current digit such that it is less than the previous digit ; Place current digit such that it is more than the previous digit ; Return the resultant total count ; Function to find all N - digit numbers satisfying the given criteria ; Initialize an array [ ] dp with all elements as - 1 ; Function call to count all possible ways ; Driver Code
using System ; using System . Collections . Generic ; public class MyClass { static int [ , , ] dp = new int [ 100 , 10 , 2 ] ; static int solve ( int i , int n , int prev , int sign ) { if ( i == n ) { return 1 ; } int val = dp [ i , prev , sign ] ; if ( val != - 1 ) return val ; val = 0 ; if ( i == 0 ) { for ( int digit = ( n == 1 ? 0 : 1 ) ; digit <= 9 ; ++ digit ) { val += solve ( i + 1 , n , digit , sign ) ; } } else if ( i == 1 ) { for ( int digit = 0 ; digit <= 9 ; ++ digit ) { if ( digit != prev ) { val += solve ( i + 1 , n , digit , ( ( digit > prev ) ? 1 : 0 ) ) ; } } } else { if ( sign == 1 ) { for ( int digit = prev - 1 ; digit >= 0 ; -- digit ) { val += solve ( i + 1 , n , digit , 0 ) ; } } else { for ( int digit = prev + 1 ; digit <= 9 ; ++ digit ) { val += solve ( i + 1 , n , digit , 1 ) ; } } } return val ; } static void countNdigitNumber ( int N ) { for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) dp [ i , j , k ] = - 1 ; } } Console . WriteLine ( solve ( 0 , N , 0 , 0 ) ) ; } public static void Main ( String [ ] args ) { int N = 3 ; countNdigitNumber ( N ) ; } }
Total count of sorted numbers upto N digits in range [ L , R ] ( Magnificent necklace combinatorics problem ) | C # program for the above approach ; Function to count total number of ways ; Stores all DP - states ; Stores the result ; Traverse the range [ 0 , N ] ; Traverse the range [ 1 , R - L ] ; Update dp [ i ] [ j ] ; Assign dp [ 0 ] [ R - L ] to ans ; Traverse the range [ 1 , N ] ; Traverse the range [ 1 , R - L ] ; Update dp [ i ] [ j ] ; Increment ans by dp [ i - 1 ] [ j ] ; Return ans ; Driver Code ; Input ; Function call
using System ; class GFG { static int Count ( int N , int L , int R ) { int [ , ] dp = new int [ N , R - L + 1 ] ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { dp [ i , 0 ] = 1 ; } for ( int i = 1 ; i < dp . GetLength ( 1 ) ; i ++ ) { dp [ 0 , i ] = dp [ 0 , i - 1 ] + 1 ; } ans = dp [ 0 , R - L ] ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < dp . GetLength ( 1 ) ; j ++ ) { dp [ i , j ] = dp [ i - 1 , j ] + dp [ i , j - 1 ] ; } ans += dp [ i , R - L ] ; } return ans ; } public static void Main ( ) { int N = 3 ; int L = 6 ; int R = 9 ; Console . Write ( Count ( N , L , R ) ) ; } }
Length of largest common subarray in all the rows of given Matrix | C # program for the above approach ; Function to find longest common subarray in all the rows of the matrix ; Array to store the position of element in every row ; Traverse the matrix ; Store the position of every element in every row ; Variable to store length of largest common Subarray ; Traverse through the matrix column ; Variable to check if every row has arr [ i ] [ j ] next to arr [ i - 1 ] [ j ] or not ; Traverse through the matrix rows ; Check if arr [ i ] [ j ] is next to arr [ i ] [ j - 1 ] in every row or not ; If every row has arr [ 0 ] [ j ] next to arr [ 0 ] [ j - 1 ] increment len by 1 and update the value of ans ; Driver code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int largestCommonSubarray ( int [ , ] arr , int n , int m ) { int [ , ] dp = new int [ n , m + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { dp [ i , arr [ i , j ] ] = j ; } } int ans = 1 ; int len = 1 ; for ( int i = 1 ; i < m ; i ++ ) { bool check = true ; for ( int j = 1 ; j < n ; j ++ ) { if ( dp [ j , arr [ 0 , i - 1 ] ] + 1 != dp [ j , arr [ 0 , i ] ] ) { check = false ; break ; } } if ( check == true ) { len ++ ; ans = Math . Max ( ans , len ) ; } else { len = 1 ; } } return ans ; } public static void Main ( ) { int [ , ] arr = { { 4 , 5 , 1 , 2 , 3 , 6 , 7 } , { 1 , 2 , 4 , 5 , 7 , 6 , 3 } , { 2 , 7 , 3 , 4 , 5 , 1 , 6 } } ; int N = 3 ; int M = 7 ; Console . Write ( largestCommonSubarray ( arr , N , M ) ) ; } }