codeparrot/xlcost-text-to-code huggingface dataset:
Longest Common Substring ( Space optimized DP solution ) | Space optimized C # implementation of longest common substring . ; Function to find longest common substring . ; Find length of both the strings . ; Variable to store length of longest common substring . ; Matrix to store result of two consecutive rows at a time . ; Variable to represent which row of matrix is current row . ; For a particular value of i and j , len [ currRow ] [ j ] stores length of longest common substring in string X [ 0. . i ] and Y [ 0. . j ] . ; Make current row as previous row and previous row as new current row . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int LCSubStr ( string X , string Y ) { int m = X . Length ; int n = Y . Length ; int result = 0 ; int [ , ] len = new int [ 2 , n ] ; int currRow = 0 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == 0 j == 0 ) { len [ currRow , j ] = 0 ; } else if ( X [ i - 1 ] == Y [ j - 1 ] ) { len [ currRow , j ] = len [ ( 1 - currRow ) , ( j - 1 ) ] + 1 ; result = Math . Max ( result , len [ currRow , j ] ) ; } else { len [ currRow , j ] = 0 ; } } currRow = 1 - currRow ; } return result ; } public static void Main ( ) { string X = " GeeksforGeeks " ; string Y = " GeeksQuiz " ; Console . Write ( LCSubStr ( X , Y ) ) ; } }
Minimal moves to form a string by adding characters or appending string itself | C # program to print the Minimal moves to form a string by appending string and adding characters ; function to return the minimal number of moves ; initializing dp [ i ] to INT_MAX ; initialize both strings to null ; base case ; check if it can be appended ; addition of character takes one step ; appending takes 1 step , and we directly reach index i * 2 + 1 after appending so the number of steps is stord in i * 2 + 1 ; Driver Code ; function call to return minimal number of moves
using System ; class GFG { static int minimalSteps ( String s , int n ) { int [ ] dp = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = int . MaxValue ; String s1 = " " , s2 = " " ; dp [ 0 ] = 1 ; s1 += s [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { s1 += s [ i ] ; s2 = s . Substring ( i , 1 ) ; dp [ i ] = Math . Min ( dp [ i ] , dp [ i - 1 ] + 1 ) ; if ( s1 == s2 ) dp [ i * 2 + 1 ] = Math . Min ( dp [ i ] + 1 , dp [ i * 2 + 1 ] ) ; } return dp [ n - 1 ] ; } public static void Main ( String [ ] args ) { String s = " aaaaaaaa " ; int n = s . Length ; Console . Write ( minimalSteps ( s , n ) / 2 ) ; } }
Check if any valid sequence is divisible by M | C # program for the above approach ; Function to check if any valid sequence is divisible by M ; Declare mod array ; Calculate the mod array ; Check if sum is divisible by M ; Check if sum is not divisible by 2 ; Remove the first element from the ModArray since it is not possible to place minus on the first element ; Decrease the size of array ; Sort the array ; Loop until the pointer cross each other ; Check if sum becomes equal ; Increase and decrease the pointer accordingly ; Driver code ; Function call
using System . Collections . Generic ; using System ; class GFG { static void func ( int n , int m , int [ ] A ) { List < int > ModArray = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) ModArray . Add ( 0 ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ModArray [ i ] = ( A [ i ] % m ) ; sum += ( ( int ) ModArray [ i ] ) ; } sum = sum % m ; if ( sum % m == 0 ) { Console . WriteLine ( " True " ) ; return ; } if ( sum % 2 != 0 ) { Console . WriteLine ( " False " ) ; } else { ModArray . Remove ( 0 ) ; int i = 0 ; int j = ModArray . Count - 1 ; ModArray . Sort ( ) ; sum = sum / 2 ; int i1 , i2 ; while ( i <= j ) { int s = ( int ) ModArray [ i ] + ( int ) ModArray [ j ] ; if ( s == sum ) { i1 = i ; i2 = j ; Console . WriteLine ( " True " ) ; break ; } else if ( s > sum ) j -- ; else i ++ ; } } } public static void Main ( ) { int m = 2 ; int [ ] a = { 1 , 3 , 9 } ; int n = a . Length ; func ( n , m , a ) ; } }
Golomb sequence | C # Program to find first n terms of Golomb sequence . ; Print the first n term of Golomb Sequence ; base cases ; Finding and printing first n terms of Golomb Sequence . ; Driver Code
using System ; class GFG { static void printGolomb ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 1 ] = 1 ; Console . Write ( dp [ 1 ] + " ▁ " ) ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i ] = 1 + dp [ i - dp [ dp [ i - 1 ] ] ] ; Console . Write ( dp [ i ] + " ▁ " ) ; } } public static void Main ( ) { int n = 9 ; printGolomb ( n ) ; } }
Balanced expressions such that given positions have opening brackets | C # code to find number of ways of arranging bracket with proper expressions ; function to calculate the number of proper bracket sequence ; hash array to mark the positions of opening brackets ; dp 2d array ; mark positions in hash array ; first position marked as 1 ; iterate and formulate the recurrences ; if position has a opening bracket ; return answer ; driver code ; positions where opening braces will be placed
using System ; class GFG { static int N = 1000 ; public static long arrangeBraces ( int n , int [ ] pos , int k ) { bool [ ] h = new bool [ N ] ; int [ , ] dp = new int [ N , N ] ; for ( int i = 0 ; i < N ; i ++ ) h [ i ] = false ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) dp [ i , j ] = 0 ; for ( int i = 0 ; i < k ; i ++ ) h [ pos [ i ] ] = true ; dp [ 0 , 0 ] = 1 ; for ( int i = 1 ; i <= 2 * n ; i ++ ) { for ( int j = 0 ; j <= 2 * n ; j ++ ) { if ( h [ i ] ) { if ( j != 0 ) dp [ i , j ] = dp [ i - 1 , j - 1 ] ; else dp [ i , j ] = 0 ; } else { if ( j != 0 ) dp [ i , j ] = dp [ i - 1 , j - 1 ] + dp [ i - 1 , j + 1 ] ; else dp [ i , j ] = dp [ i - 1 , j + 1 ] ; } } } return dp [ 2 * n , 0 ] ; } static void Main ( ) { int n = 3 ; int [ ] pos = new int [ ] { 2 } ; int k = pos . Length ; Console . Write ( arrangeBraces ( n , pos , k ) ) ; } }
Maximum difference of zeros and ones in binary string | Set 2 ( O ( n ) time ) | C # Program to find the length of substring with maximum difference of zeroes and ones in binary string . ; Find the length of substring with maximum difference of zeros and ones in binary string ; traverse a binary string from left to right ; add current value to the current_sum according to the Character if it ' s ▁ ' 0 ' add 1 else -1 ; update maximum sum ; return - 1 if string does not contain any zero that means string contains all ones otherwise max_sum ; Driver Code
using System ; class GFG { public static int findLength ( string str , int n ) { int current_sum = 0 ; int max_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { current_sum += ( str [ i ] == '0' ? 1 : - 1 ) ; if ( current_sum < 0 ) { current_sum = 0 ; } max_sum = Math . Max ( current_sum , max_sum ) ; } return max_sum == 0 ? - 1 : max_sum ; } public static void Main ( string [ ] args ) { string str = "11000010001" ; int n = str . Length ; Console . WriteLine ( findLength ( str , n ) ) ; } }
Number of decimal numbers of length k , that are strict monotone | C # program to count numbers of k digits that are strictly monotone . ; DP [ i ] [ j ] is going to store monotone numbers of length i + 1 considering j + 1 digits ( 1 , 2 , 3 , . .9 ) ; Unit length numbers ; Building dp [ ] in bottom up ; Driver code
using System ; class GFG { static int DP_s = 9 ; static int getNumStrictMonotone ( int len ) { int [ , ] DP = new int [ len , DP_s ] ; for ( int i = 0 ; i < DP_s ; ++ i ) DP [ 0 , i ] = i + 1 ; for ( int i = 1 ; i < len ; ++ i ) for ( int j = 1 ; j < DP_s ; ++ j ) DP [ i , j ] = DP [ i - 1 , j - 1 ] + DP [ i , j - 1 ] ; return DP [ len - 1 , DP_s - 1 ] ; } public static void Main ( ) { int n = 2 ; Console . WriteLine ( getNumStrictMonotone ( n ) ) ; } }
Count ways to divide circle using N non | C # code to count ways to divide circle using N non - intersecting chords . ; n = no of points required ; dp array containing the sum ; returning the required number ; Driver code
using System ; class GFG { static int chordCnt ( int A ) { int n = 2 * A ; int [ ] dpArray = new int [ n + 1 ] ; dpArray [ 0 ] = 1 ; dpArray [ 2 ] = 1 ; for ( int i = 4 ; i <= n ; i += 2 ) { for ( int j = 0 ; j < i - 1 ; j += 2 ) { dpArray [ i ] += ( dpArray [ j ] * dpArray [ i - 2 - j ] ) ; } } return dpArray [ n ] ; } public static void Main ( ) { int N ; N = 2 ; Console . WriteLine ( chordCnt ( N ) ) ; N = 1 ; Console . WriteLine ( chordCnt ( N ) ) ; N = 4 ; Console . WriteLine ( chordCnt ( N ) ) ; } }
Check for possible path in 2D matrix | C # program to find if there is path from top left to right bottom ; to find the path from top left to bottom right ; set arr [ 0 ] [ 0 ] = 1 ; Mark reachable ( from top left ) nodes in first row and first column . ; Mark reachable nodes in remaining matrix . ; return yes if right bottom index is 1 ; Driver code ; Given array ; path from arr [ 0 ] [ 0 ] to arr [ row ] [ col ]
using System ; class GFG { static bool isPath ( int [ , ] arr ) { arr [ 0 , 0 ] = 1 ; for ( int i = 1 ; i < 5 ; i ++ ) if ( arr [ i , 0 ] != - 1 ) arr [ i , 0 ] = arr [ i - 1 , 0 ] ; for ( int j = 1 ; j < 5 ; j ++ ) if ( arr [ 0 , j ] != - 1 ) arr [ 0 , j ] = arr [ 0 , j - 1 ] ; for ( int i = 1 ; i < 5 ; i ++ ) for ( int j = 1 ; j < 5 ; j ++ ) if ( arr [ i , j ] != - 1 ) arr [ i , j ] = Math . Max ( arr [ i , j - 1 ] , arr [ i - 1 , j ] ) ; return ( arr [ 5 - 1 , 5 - 1 ] == 1 ) ; } public static void Main ( ) { int [ , ] arr = { { 0 , 0 , 0 , - 1 , 0 } , { - 1 , 0 , 0 , - 1 , - 1 } , { 0 , 0 , 0 , - 1 , 0 } , { - 1 , 0 , - 1 , 0 , - 1 } , { 0 , 0 , - 1 , 0 , 0 } } ; if ( isPath ( arr ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Newmanâ €“ Shanksâ €“ Williams prime | C # Program to find Newman - Shanks - Williams prime ; return nth Newman - Shanks - Williams prime ; Base case ; Recursive step ; Driver code
using System ; class GFG { static int nswp ( int n ) { if ( n == 0 n == 1 ) return 1 ; return 2 * nswp ( n - 1 ) + nswp ( n - 2 ) ; } public static void Main ( ) { int n = 3 ; Console . WriteLine ( nswp ( n ) ) ; } }
Newman Shanks Williams prime | C # Program to find Newman Shanks Williams prime ; return nth Newman Shanks Williams prime ; Base case ; Finding nth Newman Shanks Williams prime ; Driver Program
using System ; class GFG { static int nswp ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = 2 * dp [ i - 1 ] + dp [ i - 2 ] ; return dp [ n ] ; } public static void Main ( ) { int n = 3 ; Console . WriteLine ( nswp ( n ) ) ; } }
Number of ways to insert a character to increase the LCS by one | C # Program for Number of ways to insert a character to increase LCS by one ; Return the Number of ways to insert a character to increase the longest Common Subsequence by one ; Insert all positions of all characters in string B . ; longest Common Subsequence ; longest Common Subsequence from reverse ; inserting character between position i and i + 1 ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static readonly int MAX = 256 ; static int numberofways ( String A , String B , int N , int M ) { List < int > [ ] pos = new List < int > [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) pos [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < M ; i ++ ) pos [ B [ i ] ] . Add ( i + 1 ) ; int [ , ] dpl = new int [ N + 2 , M + 2 ] ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= M ; j ++ ) { if ( A [ i - 1 ] == B [ j - 1 ] ) dpl [ i , j ] = dpl [ i - 1 , j - 1 ] + 1 ; else dpl [ i , j ] = Math . Max ( dpl [ i - 1 , j ] , dpl [ i , j - 1 ] ) ; } } int LCS = dpl [ N , M ] ; int [ , ] dpr = new int [ N + 2 , M + 2 ] ; for ( int i = N ; i >= 1 ; i -- ) { for ( int j = M ; j >= 1 ; j -- ) { if ( A [ i - 1 ] == B [ j - 1 ] ) dpr [ i , j ] = dpr [ i + 1 , j + 1 ] + 1 ; else dpr [ i , j ] = Math . Max ( dpr [ i + 1 , j ] , dpr [ i , j + 1 ] ) ; } } int ans = 0 ; for ( int i = 0 ; i <= N ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { foreach ( int x in pos [ j ] ) { if ( dpl [ i , x - 1 ] + dpr [ i + 1 , x + 1 ] == LCS ) { ans ++ ; break ; } } } } return ans ; } public static void Main ( String [ ] args ) { String A = " aa " , B = " baaa " ; int N = A . Length , M = B . Length ; Console . WriteLine ( numberofways ( A , B , N , M ) ) ; } }
Minimum cost to make two strings identical by deleting the digits | C # code to find minimum cost to make two strings identical ; Function to returns cost of removing the identical characters in LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains cost of removing identical characters in LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; If both characters are same , add both of them ; Otherwise find the maximum cost among them ; Returns cost of making X [ ] and Y [ ] identical ; Find LCS of X [ ] and Y [ ] ; Initialize the cost variable ; Find cost of all characters in both strings ; Driver function
using System ; public class GfG { static int lcs ( string X , string Y , int m , int n ) { int [ , ] L = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i <= m ; ++ i ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i , j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) L [ i , j ] = L [ i - 1 , j - 1 ] + 2 * ( X [ i - 1 ] - '0' ) ; else L [ i , j ] = L [ i - 1 , j ] > L [ i , j - 1 ] ? L [ i - 1 , j ] : L [ i , j - 1 ] ; } } return L [ m , n ] ; } static int findMinCost ( string X , string Y ) { int m = X . Length , n = Y . Length ; int cost = 0 ; for ( int i = 0 ; i < m ; ++ i ) cost += X [ i ] - '0' ; for ( int i = 0 ; i < n ; ++ i ) cost += Y [ i ] - '0' ; return cost - lcs ( X , Y , m , n ) ; } public static void Main ( ) { string X = "3759" ; string Y = "9350" ; Console . WriteLine ( " Minimum ▁ Cost ▁ to ▁ make ▁ two ▁ strings " + " ▁ identical ▁ is ▁ = ▁ " + findMinCost ( X , Y ) ) ; } }
Given a large number , check if a subsequence of digits is divisible by 8 | C # program to check if a subsequence of digits is divisible by 8. ; Function to calculate any permutation divisible by 8. If such permutation exists , the function will return that permutation else it will return - 1 ; Converting string to integer array for ease of computations ( Indexing in arr [ ] is considered to be starting from 1 ) ; Generating all possible permutations and checking if any such permutation is divisible by 8 ; Driver function
using System ; class GFG { static bool isSubSeqDivisible ( string str ) { int i , j , k , l = str . Length ; int [ ] arr = new int [ l ] ; for ( i = 0 ; i < n ; i ++ ) arr [ i ] = str [ i ] - '0' ; for ( i = 0 ; i < l ; i ++ ) { for ( j = i ; j < l ; j ++ ) { for ( k = j ; k < l ; k ++ ) { if ( arr [ i ] % 8 == 0 ) return true ; else if ( ( arr [ i ] * 10 + arr [ j ] ) % 8 == 0 && i != j ) return true ; else if ( ( arr [ i ] * 100 + arr [ j ] * 10 + arr [ k ] ) % 8 == 0 && i != j && j != k && i != k ) return true ; } } } return false ; } public static void Main ( ) { string str = "3144" ; if ( isSubSeqDivisible ( str ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Given a large number , check if a subsequence of digits is divisible by 8 | C # program to find if there is a subsequence of digits divisible by 8. ; Function takes in an array of numbers , dynamically goes on the location and makes combination of numbers . ; Converting string to integer array for ease of computations ( Indexing in arr [ ] is considered to be starting from 1 ) ; If we consider the number in our combination , we add it to the previous combination ; If we exclude the number from our combination ; If at dp [ i ] [ 0 ] , we find value 1 / true , it shows that the number exists at the value of ' i ' ; Driver function
using System ; class GFG { static bool isSubSeqDivisible ( String str ) { int n = str . Length ; int [ , ] dp = new int [ n + 1 , 10 ] ; int [ ] arr = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) arr [ i ] = ( int ) ( str [ i - 1 ] - '0' ) ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ i , arr [ i ] % 8 ] = 1 ; for ( int j = 0 ; j < 8 ; j ++ ) { if ( dp [ i - 1 , j ] > dp [ i , ( j * 10 + arr [ i ] ) % 8 ] ) dp [ i , ( j * 10 + arr [ i ] ) % 8 ] = dp [ i - 1 , j ] ; if ( dp [ i - 1 , j ] > dp [ i , j ] ) dp [ i , j ] = dp [ i - 1 , j ] ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( dp [ i , 0 ] == 1 ) return true ; } return false ; } public static void Main ( ) { string str = "3144" ; if ( isSubSeqDivisible ( str ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Given a large number , check if a subsequence of digits is divisible by 8 | C # program to check if given String has a subsequence divisible by 8 ; Driver code ; Map key will be tens place digit of number that is divisible by 8 and value will be units place digit ; For filling the map let start with initial value 8 ; Key is digit at tens place and value is digit at units place mp . Add ( { key , value } ) ; Create a hash to check if we visited a number ; Iterate from last index to 0 th index ; If 8 is present in String then 8 divided 8 hence print yes ; Considering present character as the second digit of two digits no we check if the value of this key is marked in hash or not If marked then we a have a number divisible by 8 ; If no subsequence divisible by 8
using System ; using System . Collections . Generic ; class GFG { public static void Main ( String [ ] args ) { String str = "129365" ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int no = 8 ; while ( no < 100 ) { no = no + 8 ; if ( mp . ContainsKey ( ( no / 10 ) % 10 ) ) mp [ ( no / 10 ) % 10 ] = no % 10 ; else mp . Add ( ( no / 10 ) % 10 , no % 10 ) ; } bool [ ] visited = new bool [ 10 ] ; int i ; for ( i = str . Length - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == '8' ) { Console . Write ( " Yes " ) ; break ; } if ( visited [ mp [ str [ i ] - '0' ] ] ) { Console . Write ( " Yes " ) ; break ; } visited [ str [ i ] - '0' ] = true ; } if ( i == - 1 ) Console . Write ( " No " ) ; } }
Length of Longest Balanced Subsequence | C # program to find length of the longest balanced subsequence . ; Considering all balanced substrings of length 2 ; Considering all other substrings ; Driver Code
using System ; class GFG { static int maxLength ( String s , int n ) { int [ , ] dp = new int [ n , n ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( s [ i ] == ' ( ' && s [ i + 1 ] == ' ) ' ) dp [ i , i + 1 ] = 2 ; for ( int l = 2 ; l < n ; l ++ ) { for ( int i = 0 , j = l ; j < n ; i ++ , j ++ ) { if ( s [ i ] == ' ( ' && s [ j ] == ' ) ' ) dp [ i , j ] = 2 + dp [ i + 1 , j - 1 ] ; for ( int k = i ; k < j ; k ++ ) dp [ i , j ] = Math . Max ( dp [ i , j ] , dp [ i , k ] + dp [ k + 1 , j ] ) ; } } return dp [ 0 , n - 1 ] ; } public static void Main ( ) { string s = " ( ) ( ( ( ( ( ( ) " ; int n = s . Length ; Console . WriteLine ( maxLength ( s , n ) ) ; } }
Maximum sum bitonic subarray | C # implementation to find the maximum sum bitonic subarray ; Function to find the maximum sum bitonic subarray . ; To store the maximum sum bitonic subarray ; Find the longest increasing subarray starting at i . ; Now we know that a [ i . . j ] is an increasing subarray . Remove non - positive elements from the left side as much as possible . ; Find the longest decreasing subarray starting at j . ; Now we know that a [ j . . k ] is a decreasing subarray . Remove non - positive elements from the right side as much as possible . last is needed to keep the last seen element . ; Compute the max sum of the increasing part . ; Compute the max sum of the decreasing part . ; The overall max sum is the sum of both parts minus the peak element , because it was counted twice . ; If the next element is equal to the current , i . e . arr [ i + 1 ] == arr [ i ] , last == i . To ensure the algorithm has progress , get the max of last and i + 1. ; Required maximum sum ; Driver code ; The example from the article , the answer is 19. ; Always increasing , the answer is 15. ; Always decreasing , the answer is 15. ; All are equal , the answer is 5. ; The whole array is bitonic , but the answer is 7. ; The answer is 4 ( the tail ) .
using System ; class GFG { static int find_partial_sum ( int [ ] arr , int start , int end ) { int sum = 0 ; for ( int i = start ; i < end ; i ++ ) sum += arr [ i ] ; return sum ; } static int maxSumBitonicSubArr ( int [ ] arr , int n ) { int max_sum = - 1000000 ; int i = 0 ; while ( i < n ) { int j = i ; while ( j + 1 < n && arr [ j ] < arr [ j + 1 ] ) j ++ ; while ( i < j && arr [ i ] <= 0 ) i ++ ; int k = j ; while ( k + 1 < n && arr [ k ] > arr [ k + 1 ] ) k ++ ; int last = k ; while ( k > j && arr [ k ] <= 0 ) k -- ; int sum_inc = find_partial_sum ( arr , i , j + 1 ) ; int sum_dec = find_partial_sum ( arr , j , k + 1 ) ; int sum_all = sum_inc + sum_dec - arr [ j ] ; max_sum = Math . Max ( Math . Max ( max_sum , sum_inc ) , Math . Max ( sum_dec , sum_all ) ) ; i = Math . Max ( last , i + 1 ) ; } return max_sum ; } public static void Main ( ) { int [ ] arr = { 5 , 3 , 9 , 2 , 7 , 6 , 4 } ; int n = arr . Length ; Console . WriteLine ( " Maximum ▁ sum ▁ = ▁ " + maxSumBitonicSubArr ( arr , n ) ) ; int [ ] arr2 = { 1 , 2 , 3 , 4 , 5 } ; int n2 = arr2 . Length ; Console . WriteLine ( " Maximum ▁ sum ▁ = ▁ " + maxSumBitonicSubArr ( arr2 , n2 ) ) ; int [ ] arr3 = { 5 , 4 , 3 , 2 , 1 } ; int n3 = arr3 . Length ; Console . WriteLine ( " Maximum ▁ sum ▁ = ▁ " + maxSumBitonicSubArr ( arr3 , n3 ) ) ; int [ ] arr4 = { 5 , 5 , 5 , 5 } ; int n4 = arr4 . Length ; Console . WriteLine ( " Maximum ▁ sum ▁ = ▁ " + maxSumBitonicSubArr ( arr4 , n4 ) ) ; int [ ] arr5 = { - 1 , 0 , 1 , 2 , 3 , 1 , 0 , - 1 , - 10 } ; int n5 = arr5 . Length ; Console . WriteLine ( " Maximum ▁ sum ▁ = ▁ " + maxSumBitonicSubArr ( arr5 , n5 ) ) ; int [ ] arr6 = { - 1 , 0 , 1 , 2 , 0 , - 1 , - 2 , 0 , 1 , 3 } ; int n6 = arr6 . Length ; Console . WriteLine ( " Maximum ▁ sum ▁ = ▁ " + maxSumBitonicSubArr ( arr6 , n6 ) ) ; } }
Smallest sum contiguous subarray | C # implementation to find the smallest sum contiguous subarray ; function to find the smallest sum contiguous subarray ; to store the minimum value that is ending up to the current index ; to store the minimum value encountered so far ; traverse the array elements ; if min_ending_here > 0 , then it could not possibly contribute to the minimum sum further ; else add the value arr [ i ] to min_ending_here ; update min_so_far ; required smallest sum contiguous subarray value ; Driver method
using System ; class GFG { static int smallestSumSubarr ( int [ ] arr , int n ) { int min_ending_here = 2147483647 ; int min_so_far = 2147483647 ; for ( int i = 0 ; i < n ; i ++ ) { if ( min_ending_here > 0 ) min_ending_here = arr [ i ] ; else min_ending_here += arr [ i ] ; min_so_far = Math . Min ( min_so_far , min_ending_here ) ; } return min_so_far ; } public static void Main ( ) { int [ ] arr = { 3 , - 4 , 2 , - 3 , - 1 , 7 , - 5 } ; int n = arr . Length ; Console . Write ( " Smallest ▁ sum : ▁ " + smallestSumSubarr ( arr , n ) ) ; } }
n | C # code to find nth number with digits 0 , 1 , 2 , 3 , 4 , 5 ; If the Number is less than 6 return the number as it is . ; Call the function again and again the get the desired result . And convert the number to base 6. ; Decrease the Number by 1 and Call ans function to convert N to base 6 ; Driver code
using System ; public class GFG { static int ans ( int n ) { if ( n < 6 ) { return n ; } return n % 6 + 10 * ( ans ( n / 6 ) ) ; } static int getSpecialNumber ( int N ) { return ans ( -- N ) ; } public static void Main ( String [ ] args ) { int N = 17 ; int answer = getSpecialNumber ( N ) ; Console . WriteLine ( answer ) ; } }