codeparrot/xlcost-text-to-code huggingface dataset:
Sum of products of all possible K size subsets of the given array | C # implementation of the approach ; Function to return the sum of products of all the possible k size subsets ; Initialising all the values to 0 ; To store the answer for current value of k ; For k = 1 , the answer will simply be the sum of all the elements ; Filling the table in bottom up manner ; To store the elements of the current row so that we will be able to use this sum for subsequent values of k ; We will subtract previously computed value so as to get the sum of elements from j + 1 to n in the ( i - 1 ) th row ; Driver code
using System . Collections . Generic ; using System ; class GFG { static int sumOfProduct ( int [ ] arr , int n , int k ) { int [ , ] dp = new int [ n + 1 , n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) dp [ i , j ] = 0 ; int cur_sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ 1 , i ] = arr [ i - 1 ] ; cur_sum += arr [ i - 1 ] ; } for ( int i = 2 ; i <= k ; i ++ ) { int temp_sum = 0 ; for ( int j = 1 ; j <= n ; j ++ ) { cur_sum -= dp [ i - 1 , j ] ; dp [ i , j ] = arr [ j - 1 ] * cur_sum ; temp_sum += dp [ i , j ] ; } cur_sum = temp_sum ; } return cur_sum ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( sumOfProduct ( arr , n , k ) ) ; } }
Find the equal pairs of subsequence of S and subsequence of T | C # implementation of the approach ; Function to return the pairs of subsequences from S [ ] and subsequences from T [ ] such that both have the same content ; Create dp array ; Base values ; Base values ; Keep previous dp value ; If both elements are same ; Return the required answer ; Driver code
using System ; class GFG { static int subsequence ( int [ ] S , int [ ] T , int n , int m ) { int [ , ] dp = new int [ n + 1 , m + 1 ] ; int mod = 1000000007 ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i , 0 ] = 1 ; for ( int j = 0 ; j <= m ; j ++ ) dp [ 0 , j ] = 1 ; 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 ] ; if ( S [ i - 1 ] == T [ j - 1 ] ) dp [ i , j ] += dp [ i - 1 , j - 1 ] ; dp [ i , j ] += mod ; dp [ i , j ] %= mod ; } } return dp [ n , m ] ; } public static void Main ( ) { int [ ] S = { 1 , 1 } ; int n = S . Length ; int [ ] T = { 1 , 1 } ; int m = T . Length ; Console . WriteLine ( subsequence ( S , T , n , m ) ) ; } }
Maximum count of elements divisible on the left for any element | C # implementation of the above approach ; Function to return the maximum count of required elements ; For every element in the array starting from the second element ; Check all the elements on the left of current element which are divisible by the current element ; Driver Code
using System ; class GFG { static int findMax ( int [ ] arr , int n ) { int res = 0 ; int i , j ; for ( i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( j = 0 ; j < i ; j ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) count += 1 ; } res = Math . Max ( count , res ) ; } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 1 , 28 , 4 , 2 , 6 , 7 } ; int n = arr . Length ; Console . WriteLine ( findMax ( arr , n ) ) ; } }
Maximum count of elements divisible on the left for any element | C # implementation of the approach ; Function to return the maximum count of required elements ; divisible [ i ] will store true if arr [ i ] is divisible by any element on its right ; To store the maximum required count ; For every element of the array ; If the current element is divisible by any element on its right ; Find the count of element on the left which are divisible by the current element ; If arr [ j ] is divisible then set divisible [ j ] to true ; Update the maximum required count ; Driver code
using System ; class GFG { static int findMax ( int [ ] arr , int n ) { bool [ ] divisible = new bool [ n ] ; int res = 0 ; for ( int i = n - 1 ; i > 0 ; i -- ) { if ( divisible [ i ] ) continue ; int cnt = 0 ; for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] % arr [ i ] ) == 0 ) { divisible [ j ] = true ; cnt ++ ; } } res = Math . Max ( res , cnt ) ; } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 1 , 28 , 4 , 2 , 6 , 7 } ; int n = arr . Length ; Console . WriteLine ( findMax ( arr , n ) ) ; } }
Number of subsets with sum divisible by M | Set 2 | C # implementation of the approach ; To store the states of DP ; Function to find the required count ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code
using System ; class GFG { static int maxN = 20 ; static int maxM = 10 ; static int [ , ] dp = new int [ maxN , maxM ] ; static bool [ , ] v = new bool [ maxN , maxM ] ; static int findCnt ( int [ ] arr , int i , int curr , int n , int m ) { if ( i == n ) { if ( curr == 0 ) return 1 ; else return 0 ; } if ( v [ i , curr ] ) return dp [ i , curr ] ; v [ i , curr ] = true ; return dp [ i , curr ] = findCnt ( arr , i + 1 , curr , n , m ) + findCnt ( arr , i + 1 , ( curr + arr [ i ] ) % m , n , m ) ; } public static void Main ( ) { int [ ] arr = { 3 , 3 , 3 , 3 } ; int n = arr . Length ; int m = 6 ; Console . WriteLine ( findCnt ( arr , 0 , 0 , n , m ) - 1 ) ; } }
Longest subsequence with a given OR value : Dynamic Programming Approach | C # implementation of the approach ; To store the states of DP ; Function to return the required length ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code
using System ; class GFG { static int maxN = 20 ; static int maxM = 64 ; static int [ , ] dp = new int [ maxN , maxM ] ; static bool [ , ] v = new bool [ maxN , maxM ] ; static int findLen ( int [ ] arr , int i , int curr , int n , int m ) { if ( i == n ) { if ( curr == m ) return 0 ; else return - 1 ; } if ( v [ i , curr ] ) return dp [ i , curr ] ; v [ i , curr ] = true ; int l = findLen ( arr , i + 1 , curr , n , m ) ; int r = findLen ( arr , i + 1 , curr arr [ i ] , n , m ) ; dp [ i , curr ] = l ; if ( r != - 1 ) dp [ i , curr ] = Math . Max ( dp [ i , curr ] , r + 1 ) ; return dp [ i , curr ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 7 , 2 , 3 } ; int n = arr . Length ; int m = 3 ; int ans = findLen ( arr , 0 , 0 , n , m ) ; if ( ans == - 1 ) Console . WriteLine ( 0 ) ; else Console . WriteLine ( ans ) ; } }
Longest subsequence with a given AND value | C # implementation of the approach ; To store the states of DP ; Function to return the required length ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code
using System ; class GFG { static int maxN = 300 ; static int maxM = 300 ; static int [ , ] dp = new int [ maxN , maxM ] ; static bool [ , ] v = new bool [ maxN , maxM ] ; static int findLen ( int [ ] arr , int i , int curr , int n , int m ) { if ( i == n ) { if ( curr == m ) return 0 ; else return - 1 ; } if ( v [ i , curr ] ) return dp [ i , curr ] ; v [ i , curr ] = true ; int l = findLen ( arr , i + 1 , curr , n , m ) ; int r = findLen ( arr , i + 1 , curr & arr [ i ] , n , m ) ; dp [ i , curr ] = l ; if ( r != - 1 ) dp [ i , curr ] = Math . Max ( dp [ i , curr ] , r + 1 ) ; return dp [ i , curr ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 7 , 2 , 3 } ; int n = arr . Length ; int m = 3 ; int ans = findLen ( arr , 0 , ( ( 1 << 8 ) - 1 ) , n , m ) ; if ( ans == - 1 ) Console . WriteLine ( 0 ) ; else Console . WriteLine ( ans ) ; } }
Longest subsequence whose sum is divisible by a given number | C # implementation of the approach ; To store the states of DP ; Function to return the length of the longest subsequence whose sum is divisible by m ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code
using System ; class GFG { static int maxN = 20 ; static int maxM = 64 ; static int [ , ] dp = new int [ maxN , maxM ] ; static Boolean [ , ] v = new Boolean [ maxN , maxM ] ; static int findLen ( int [ ] arr , int i , int curr , int n , int m ) { if ( i == n ) { if ( curr == 0 ) return 0 ; else return - 1 ; } if ( v [ i , curr ] ) return dp [ i , curr ] ; v [ i , curr ] = true ; int l = findLen ( arr , i + 1 , curr , n , m ) ; int r = findLen ( arr , i + 1 , ( curr + arr [ i ] ) % m , n , m ) ; dp [ i , curr ] = l ; if ( r != - 1 ) dp [ i , curr ] = Math . Max ( dp [ i , curr ] , r + 1 ) ; return dp [ i , curr ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 2 , 1 } ; int n = arr . Length ; int m = 3 ; Console . WriteLine ( findLen ( arr , 0 , 0 , n , m ) ) ; } }
Count of subsets with sum equal to X | C # implementation of the approach ; To store the states of DP ; Function to return the required count ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code
using System ; class GFG { static int maxN = 20 ; static int maxSum = 50 ; static int minSum = 50 ; static int Base = 50 ; static int [ , ] dp = new int [ maxN , maxSum + minSum ] ; static Boolean [ , ] v = new Boolean [ maxN , maxSum + minSum ] ; static int findCnt ( int [ ] arr , int i , int required_sum , int n ) { if ( i == n ) { if ( required_sum == 0 ) return 1 ; else return 0 ; } if ( v [ i , required_sum + Base ] ) return dp [ i , required_sum + Base ] ; v [ i , required_sum + Base ] = true ; dp [ i , required_sum + Base ] = findCnt ( arr , i + 1 , required_sum , n ) + findCnt ( arr , i + 1 , required_sum - arr [ i ] , n ) ; return dp [ i , required_sum + Base ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 3 , 3 , 3 } ; int n = arr . Length ; int x = 6 ; Console . WriteLine ( findCnt ( arr , 0 , x , n ) ) ; } }
Number of subsets with a given OR value | C # implementation of the approach ; To store states of DP ; Function to return the required count ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code
using System ; class GFG { static int maxN = 20 ; static int maxM = 64 ; static int [ , ] dp = new int [ maxN , maxM ] ; static Boolean [ , ] v = new Boolean [ maxN , maxM ] ; static int findCnt ( int [ ] arr , int i , int curr , int n , int m ) { if ( i == n ) { return ( curr == m ? 1 : 0 ) ; } if ( v [ i , curr ] ) return dp [ i , curr ] ; v [ i , curr ] = true ; dp [ i , curr ] = findCnt ( arr , i + 1 , curr , n , m ) + findCnt ( arr , i + 1 , ( curr arr [ i ] ) , n , m ) ; return dp [ i , curr ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 2 } ; int n = arr . Length ; int m = 3 ; Console . WriteLine ( findCnt ( arr , 0 , 0 , n , m ) ) ; } }
Number of subsets with a given AND value | C # implementation of the approach ; To store states of DP ; Function to return the required count ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code
using System ; class GFG { static int maxN = 20 ; static int maxM = 64 ; static int [ , ] dp1 = new int [ maxN , maxM ] ; static bool [ , ] v1 = new bool [ maxN , maxM ] ; static int findCnt ( int [ ] arr , int i , int curr , int n , int m ) { if ( i == n ) { return ( curr == m ? 1 : 0 ) ; } if ( v1 [ i , curr ] ) return dp1 [ i , curr ] ; v1 [ i , curr ] = true ; dp1 [ i , curr ] = findCnt ( arr , i + 1 , curr , n , m ) + findCnt ( arr , i + 1 , ( curr & arr [ i ] ) , n , m ) ; return dp1 [ i , curr ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 0 , 0 } ; int n = arr . Length ; int m = 0 ; Console . WriteLine ( findCnt ( arr , 0 , ( ( 1 << 6 ) - 1 ) , n , m ) ) ; } }
Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13 | C # implementation of the approach ; Function to find the count of integers obtained by replacing ' ? ' in a given string such that formed integer gives remainder 5 when it is divided by 13 ; Initialise ; Place digit j at ? position ; Get the remainder ; Return the required answer ; Driver code
using System ; class GFG { static int MOD = ( int ) ( 1e9 + 7 ) ; static int modulo_13 ( String s , int n ) { long [ , ] dp = new long [ n + 1 , 13 ] ; dp [ 0 , 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { int nxt = s [ i ] - '0' ; if ( s [ i ] == ' ? ' ) nxt = j ; for ( int k = 0 ; k < 13 ; k ++ ) { int rem = ( 10 * k + nxt ) % 13 ; dp [ i + 1 , rem ] += dp [ i , k ] ; dp [ i + 1 , rem ] %= MOD ; } if ( s [ i ] != ' ? ' ) break ; } } return ( int ) dp [ n , 5 ] ; } public static void Main ( String [ ] args ) { String s = " ? 44" ; int n = s . Length ; Console . WriteLine ( modulo_13 ( s , n ) ) ; } }
Longest Increasing Subsequence using Longest Common Subsequence Algorithm | C # implementation of above approach ; Function to return the size of the longest increasing subsequence ; Create an 2D array of integer for tabulation ; Take the second sequence as the sorted sequence of the given sequence ; Classical Dynamic Programming algorithm for longest Common Subsequence ; Return the ans ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int LISusingLCS ( List < int > seq ) { int n = seq . Count ; int [ , ] L = new int [ n + 1 , n + 1 ] ; List < int > sortedseq = new List < int > ( seq ) ; sortedseq . Sort ( ) ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i , j ] = 0 ; else if ( seq [ i - 1 ] == sortedseq [ j - 1 ] ) L [ i , j ] = L [ i - 1 , j - 1 ] + 1 ; else L [ i , j ] = Math . Max ( L [ i - 1 , j ] , L [ i , j - 1 ] ) ; } } return L [ n , n ] ; } public static void Main ( String [ ] args ) { List < int > sequence = new List < int > ( ) ; sequence . Add ( 12 ) ; sequence . Add ( 34 ) ; sequence . Add ( 1 ) ; sequence . Add ( 5 ) ; sequence . Add ( 40 ) ; sequence . Add ( 80 ) ; Console . WriteLine ( LISusingLCS ( sequence ) ) ; } }
Count of N | C # implementation of the approach ; Function to return the count of n - digit numbers that satisfy the given conditions ; Base case ; If 0 wasn 't chosen previously ; If 0 wasn 't chosen previously ; Driver code
using System ; class GFG { static int count_numbers ( int k , int n , bool flag ) { if ( n == 1 ) { if ( flag ) { return ( k - 1 ) ; } else { return 1 ; } } if ( flag ) return ( k - 1 ) * ( count_numbers ( k , n - 1 , false ) + count_numbers ( k , n - 1 , true ) ) ; else return count_numbers ( k , n - 1 , true ) ; } public static void Main ( String [ ] args ) { int n = 3 ; int k = 10 ; Console . Write ( count_numbers ( k , n , true ) ) ; } }
Count of N | C # implementation of the approach ; Function to return the count of n - digit numbers that satisfy the given conditions ; DP array to store the pre - caluclated states ; Base cases ; i - digit numbers ending with 0 can be formed by concatenating 0 in the end of all the ( i - 1 ) - digit number ending at a non - zero digit ; i - digit numbers ending with non - zero can be formed by concatenating any non - zero digit in the end of all the ( i - 1 ) - digit number ending with any digit ; n - digit number ending with and ending with non - zero ; Driver code
using System ; class GFG { static int count_numbers ( int k , int n ) { int [ , ] dp = new int [ n + 1 , 2 ] ; dp [ 1 , 0 ] = 0 ; dp [ 1 , 1 ] = k - 1 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i , 0 ] = dp [ i - 1 , 1 ] ; dp [ i , 1 ] = ( dp [ i - 1 , 0 ] + dp [ i - 1 , 1 ] ) * ( k - 1 ) ; } return dp [ n , 0 ] + dp [ n , 1 ] ; } public static void Main ( String [ ] args ) { int k = 10 ; int n = 3 ; Console . WriteLine ( count_numbers ( k , n ) ) ; } }
Divide an array into K subarray with the given condition | C # implementation of above approach ; Function to divide an array into k parts such that the sum of difference of every element with the maximum element of that part is minimum ; Dp to store the values ; Fill up the dp table ; Intitilize maximum value ; Max element and the sum ; Run a loop from i to n ; Find the maximum number from i to l and the sum from i to l ; Find the sum of difference of every element with the maximum element ; If the array can be divided ; Returns the minimum sum in K parts ; Driver code
using System ; class GFG { static int divideArray ( int [ ] arr , int n , int k ) { int [ , ] dp = new int [ 500 , 500 ] ; int i , j ; for ( i = 0 ; i < 500 ; i ++ ) for ( j = 0 ; j < 500 ; j ++ ) dp [ i , j ] = 0 ; k -= 1 ; for ( i = n - 1 ; i >= 0 ; i -- ) { for ( j = 0 ; j <= k ; j ++ ) { dp [ i , j ] = int . MaxValue ; int max_ = - 1 , sum = 0 ; for ( int l = i ; l < n ; l ++ ) { max_ = Math . Max ( max_ , arr [ l ] ) ; sum += arr [ l ] ; int diff = ( l - i + 1 ) * max_ - sum ; if ( j > 0 ) dp [ i , j ] = Math . Min ( dp [ i , j ] , diff + dp [ l + 1 , j - 1 ] ) ; else dp [ i , j ] = diff ; } } } return dp [ 0 , k ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 9 , 5 , 4 , 8 , 3 , 6 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( divideArray ( arr , n , k ) ) ; } }
Count of subsets not containing adjacent elements | C # implementation of the approach ; Function to return the count of possible subsets ; Total possible subsets of n sized array is ( 2 ^ n - 1 ) ; To store the required count of subsets ; Run from i 000. .0 to 111. .1 ; If current subset has consecutive ; Driver code
using System ; class GFG { static int cntSubsets ( int [ ] arr , int n ) { int max = ( int ) Math . Pow ( 2 , n ) ; int result = 0 ; for ( int i = 0 ; i < max ; i ++ ) { int counter = i ; if ( ( counter & ( counter >> 1 ) ) > 0 ) continue ; result ++ ; } return result ; } static public void Main ( String [ ] arg ) { int [ ] arr = { 3 , 5 , 7 } ; int n = arr . Length ; Console . WriteLine ( cntSubsets ( arr , n ) ) ; } }
Count of subsets not containing adjacent elements | C # implementation of the approach ; Function to return the count of possible subsets ; If previous element was 0 then 0 as well as 1 can be appended ; If previous element was 1 then only 0 can be appended ; Store the count of all possible subsets ; Driver code
using System ; class GFG { static int cntSubsets ( int [ ] arr , int n ) { int [ ] a = new int [ n ] ; int [ ] b = new int [ n ] ; a [ 0 ] = b [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; b [ i ] = a [ i - 1 ] ; } int result = a [ n - 1 ] + b [ n - 1 ] ; return result ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 7 } ; int n = arr . Length ; Console . WriteLine ( cntSubsets ( arr , n ) ) ; } }
Maximum perimeter of a square in a 2D grid | C # implementation of the approach ; Function to calculate the perfix sum of the rows and the columns of the given matrix ; Number of rows and cols ; First column of the row prefix array ; Update the prefix sum for the rows ; First row of the column prefix array ; Update the prefix sum for the columns ; Function to return the perimeter of the square having top - left corner at ( i , j ) and size k ; i and j represent the top left corner of the square and k is the size ; Get the upper row sum ; Get the left column sum ; At the distance of k in both direction ; The perimeter will be sum of all the values ; Since all the corners are included twice , they need to be subtract from the sum ; Function to return the maximum perimeter of a square in the given matrix ; Number of rows and cols ; Function call to calculate the prefix sum of rows and cols ; To store the maximum perimeter ; Nested loops to choose the top - left corner of the square ; Loop for the size of the square ; Get the perimeter of the current square ; Update the maximum perimeter so far ; Driver code
using System ; class GFG { static void perfix_calculate ( int [ , ] A , int [ , ] row , int [ , ] col ) { int n = ( int ) A . GetLength ( 0 ) ; int m = ( int ) A . GetLength ( 1 ) ; for ( int i = 0 ; i < n ; ++ i ) { row [ i , 0 ] = A [ i , 0 ] ; } for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 1 ; j < m ; ++ j ) { row [ i , j ] = row [ i , j - 1 ] + A [ i , j ] ; } } for ( int i = 0 ; i < m ; ++ i ) { col [ 0 , i ] = A [ 0 , i ] ; } for ( int i = 0 ; i < m ; ++ i ) { for ( int j = 1 ; j < n ; ++ j ) { col [ j , i ] = A [ j , i ] + col [ j - 1 , i ] ; } } } static int perimeter ( int i , int j , int k , int [ , ] row , int [ , ] col , int [ , ] A ) { int row_s , col_s ; if ( j == 0 ) row_s = 0 ; else row_s = row [ i , j - 1 ] ; if ( i == 0 ) col_s = 0 ; else col_s = col [ i - 1 , j ] ; int upper_row = row [ i , j + k ] - row_s ; int left_col = col [ i + k , j ] - col_s ; if ( j == 0 ) row_s = 0 ; else row_s = row [ i + k , j - 1 ] ; if ( i == 0 ) col_s = 0 ; else col_s = col [ i - 1 , j + k ] ; int lower_row = row [ i + k , j + k ] - row_s ; int right_col = col [ i + k , j + k ] - col_s ; int sum = upper_row + lower_row + left_col + right_col ; sum -= ( A [ i , j ] + A [ i + k , j ] + A [ i , j + k ] + A [ i + k , j + k ] ) ; return sum ; } static int maxPerimeter ( int [ , ] A ) { int n = ( int ) A . GetLength ( 0 ) ; int m = ( int ) A . GetLength ( 1 ) ; int [ , ] row = new int [ n , m ] ; int [ , ] col = new int [ n , m ] ; perfix_calculate ( A , row , col ) ; int maxPer = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { for ( int k = 0 ; k < Math . Min ( n - i , m - j ) ; ++ k ) { int perimtr = perimeter ( i , j , k , row , col , A ) ; maxPer = Math . Max ( maxPer , perimtr ) ; } } } return maxPer ; } public static void Main ( String [ ] args ) { int [ , ] A = { { 1 , 1 , 0 } , { 1 , 1 , 1 } , { 0 , 1 , 1 } } ; Console . Write ( maxPerimeter ( A ) ) ; } }
Minimum number of Fibonacci jumps to reach end | A Dynamic Programming based C # program to find minimum number of jumps to reach Destination ; Function that returns the min number of jump to reach the destination ; We consider only those Fibonacci numbers which are less than n , where we can consider fib [ 30 ] to be the upper bound as this will cross 10 ^ 5 ; DP [ i ] will be storing the minimum number of jumps required for the position i . So DP [ N + 1 ] will have the result we consider 0 as source and N + 1 as the destination ; Base case ( Steps to reach source is ) ; Initialize all table values as Infinite ; Go through each positions till destination . ; Calculate the minimum of that position if all the Fibonacci numbers are considered ; If the position is safe or the position is the destination then only we calculate the minimum otherwise the cost is MAX as default ; - 1 denotes if there is no path possible ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static int minJumps ( int [ ] arr , int N ) { int MAX = 1000000 ; int [ ] fib = new int [ 30 ] ; fib [ 0 ] = 0 ; fib [ 1 ] = 1 ; for ( int i = 2 ; i < 30 ; i ++ ) fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; int [ ] DP = new int [ N + 2 ] ; DP [ 0 ] = 0 ; for ( int i = 1 ; i <= N + 1 ; i ++ ) DP [ i ] = MAX ; for ( int i = 1 ; i <= N + 1 ; i ++ ) { for ( int j = 1 ; j < 30 ; j ++ ) { if ( ( i == N + 1 arr [ i - 1 ] == 1 ) && i - fib [ j ] >= 0 ) DP [ i ] = Math . Min ( DP [ i ] , 1 + DP [ i - fib [ j ] ] ) ; } } if ( DP [ N + 1 ] != MAX ) return DP [ N + 1 ] ; else return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = new int [ ] { 0 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 , 0 , 0 } ; int n = 11 ; int ans = minJumps ( arr , n ) ; Console . WriteLine ( ans ) ; } }