codeparrot/xlcost-text-to-code huggingface dataset:
Maximum Subarray Sum Excluding Certain Elements | C # Program to find max subarray sum excluding some elements ; Utility function for findMaxSubarraySum ( ) with the following parameters A = > Array A , B = > Array B , n = > Number of elements in Array A , m = > Number of elements in Array B ; set max_so_far to INT_MIN ; if the element is present in B , set current max to 0 and move to the next element ; Proceed as in Kadane 's Algorithm ; Wrapper for findMaxSubarraySumUtil ( ) ; sort array B to apply Binary Search ; This case will occour when all elements of A are present in B , thus no subarray can be formed ; Driver Code ; Function call
using System ; class GFG { static int findMaxSubarraySumUtil ( int [ ] A , int [ ] B , int n , int m ) { int max_so_far = int . MinValue , curr_max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( Array . BinarySearch ( B , A [ i ] ) >= 0 ) { curr_max = 0 ; continue ; } curr_max = Math . Max ( A [ i ] , curr_max + A [ i ] ) ; max_so_far = Math . Max ( max_so_far , curr_max ) ; } return max_so_far ; } static void findMaxSubarraySum ( int [ ] A , int [ ] B , int n , int m ) { Array . Sort ( B ) ; int maxSubarraySum = findMaxSubarraySumUtil ( A , B , n , m ) ; if ( maxSubarraySum == int . MinValue ) { Console . WriteLine ( " Maximum ▁ subarray ▁ sum ▁ cant ▁ be ▁ found " ) ; } else { Console . WriteLine ( " The ▁ Maximum ▁ subarray ▁ sum ▁ = ▁ " + maxSubarraySum ) ; } } public static void Main ( params string [ ] args ) { int [ ] A = { 3 , 4 , 5 , - 4 , 6 } ; int [ ] B = { 1 , 8 , 5 } ; int n = A . Length ; int m = B . Length ; findMaxSubarraySum ( A , B , n , m ) ; } }
Number of n | C # function to find number of diagonals in n sided convex polygon ; Function that returns count of non - decreasing numbers with n digits ; a [ i ] [ j ] = count of all possible number with i digits having leading digit as j ; Initialization of all 0 - digit number ; Initialization of all i - digit non - decreasing number leading with 9 ; for all digits we should calculate number of ways depending upon leading digits ; driver program
using System ; class GFG { static int nonDecNums ( int n ) { int [ , ] a = new int [ n + 1 , 10 ] ; for ( int i = 0 ; i <= 9 ; i ++ ) a [ 0 , i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) a [ i , 9 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 8 ; j >= 0 ; j -- ) a [ i , j ] = a [ i - 1 , j ] + a [ i , j + 1 ] ; return a [ n , 0 ] ; } public static void Main ( ) { int n = 2 ; Console . WriteLine ( " Non - decreasing ▁ digits ▁ = ▁ " + nonDecNums ( n ) ) ; } }
Count Balanced Binary Trees of Height h | C # program to count number of balanced binary trees of height h . ; base cases ; Driver program
using System ; class GFG { static int MOD = 1000000007 ; public static long countBT ( int h ) { long [ ] dp = new long [ h + 1 ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= h ; ++ i ) dp [ i ] = ( dp [ i - 1 ] * ( ( 2 * dp [ i - 2 ] ) % MOD + dp [ i - 1 ] ) % MOD ) % MOD ; return dp [ h ] ; } static void Main ( ) { int h = 3 ; Console . WriteLine ( " No . ▁ of ▁ balanced ▁ binary ▁ trees ▁ of ▁ height ▁ " + h + " ▁ is : ▁ " + countBT ( h ) ) ; } }
Maximum Subarray Sum Excluding Certain Elements | C # Program implementation of the above idea ; Function to calculate the max sum of contigous subarray of B whose elements are not present in A ; mark all the elements present in B ; initialize max_so_far with INT_MIN ; traverse the array A ; if current max is greater than max so far then update max so far ; Driver code . ; Function call
using System ; using System . Collections . Generic ; class GFG { static int findMaxSubarraySum ( int [ ] A , int [ ] B ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < B . Length ; i ++ ) { m [ B [ i ] ] = 1 ; } int max_so_far = Int32 . MinValue ; int currmax = 0 ; for ( int i = 0 ; i < A . Length ; i ++ ) { if ( currmax < 0 || ( m . ContainsKey ( A [ i ] ) && m [ A [ i ] ] == 1 ) ) { currmax = 0 ; continue ; } currmax = Math . Max ( A [ i ] , A [ i ] + currmax ) ; if ( max_so_far < currmax ) { max_so_far = currmax ; } } return max_so_far ; } static void Main ( ) { int [ ] a = { 3 , 4 , 5 , - 4 , 6 } ; int [ ] b = { 1 , 8 , 5 } ; Console . WriteLine ( findMaxSubarraySum ( a , b ) ) ; } }
Print all k | C # program to print all paths with sum k . ; utility function to print contents of a vector from index i to it 's end  ; binary tree node ; This function prints all paths that have sum k ; empty node ; add current node to the path ; check if there 's any k sum path   in the left sub-tree.  ; check if there 's any k sum path   in the right sub-tree.  ; check if there 's any k sum path that   terminates at this node   Traverse the entire path as   there can be negative elements too  ; If path sum is k , print the path ; Remove the current element from the path ; A wrapper over printKPathUtil ( ) ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void printList ( List < int > v , int i ) { for ( int j = i ; j < v . Count ; j ++ ) Console . Write ( v [ j ] + " ▁ " ) ; Console . WriteLine ( ) ; } public class Node { public int data ; public Node left , right ; public Node ( int x ) { data = x ; left = right = null ; } } ; static List < int > path = new List < int > ( ) ; static void printKPathUtil ( Node root , int k ) { if ( root == null ) return ; path . Add ( root . data ) ; printKPathUtil ( root . left , k ) ; printKPathUtil ( root . right , k ) ; int f = 0 ; for ( int j = path . Count - 1 ; j >= 0 ; j -- ) { f += path [ j ] ; if ( f == k ) printList ( path , j ) ; } path . RemoveAt ( path . Count - 1 ) ; } static void printKPath ( Node root , int k ) { path = new List < int > ( ) ; printKPathUtil ( root , k ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 3 ) ; root . left . left = new Node ( 2 ) ; root . left . right = new Node ( 1 ) ; root . left . right . left = new Node ( 1 ) ; root . right = new Node ( - 1 ) ; root . right . left = new Node ( 4 ) ; root . right . left . left = new Node ( 1 ) ; root . right . left . right = new Node ( 2 ) ; root . right . right = new Node ( 5 ) ; root . right . right . right = new Node ( 2 ) ; int k = 5 ; printKPath ( root , k ) ; } }
Number of substrings divisible by 8 but not by 3 | C # program to count substrings which are divisible by 8 but not by 3 ; Function that returns count of substrings divisible by 8 but not by 3 ; Iterating the string . ; Prefix sum of number of substrings whose sum of digits mudolo 3 is 0 , 1 , 2. ; Iterating the string . ; Since single digit 8 is divisible by 8 and not by 3. ; Taking two digit number . ; 10 th position ; Complete 2 digit number ; Taking 3 digit number . ; 100 th position ; 10 th position ; Complete 3 digit number . ; If number formed is divisible by 8 then last 3 digits are also divisible by 8. Then all the substring ending at this index is divisible . ; But those substring also contain number which are not divisible by 3 so remove them . ; driver program
using System ; class GFG { static int count ( String s , int len ) { int MAX = 1000 ; int cur = 0 , dig = 0 ; int [ ] sum = new int [ MAX ] ; int [ , ] dp = new int [ MAX , 3 ] ; dp [ 0 , 0 ] = 1 ; for ( int i = 1 ; i <= len ; i ++ ) { dig = ( int ) ( s [ i - 1 ] ) - 48 ; cur += dig ; cur %= 3 ; sum [ i ] = cur ; dp [ i , 0 ] = dp [ i - 1 , 0 ] ; dp [ i , 1 ] = dp [ i - 1 , 1 ] ; dp [ i , 2 ] = dp [ i - 1 , 2 ] ; dp [ i , sum [ i ] ] ++ ; } int ans = 0 , dprev = 0 , value = 0 , dprev2 = 0 ; for ( int i = 1 ; i <= len ; i ++ ) { dig = ( int ) ( s [ i - 1 ] ) - 48 ; if ( dig == 8 ) ans ++ ; if ( i - 2 >= 0 ) { dprev = ( int ) ( s [ i - 2 ] ) - 48 ; value = dprev * 10 + dig ; if ( ( value % 8 == 0 ) && ( value % 3 != 0 ) ) ans ++ ; } if ( i - 3 >= 0 ) { dprev2 = ( int ) ( s [ i - 3 ] ) - 48 ; dprev = ( int ) ( s [ i - 2 ] ) - 48 ; value = dprev2 * 100 + dprev * 10 + dig ; if ( value % 8 != 0 ) continue ; ans += ( i - 2 ) ; ans -= ( dp [ i - 3 , sum [ i ] ] ) ; } } return ans ; } public static void Main ( String [ ] args ) { String str = "6564525600" ; int len = str . Length ; Console . Write ( count ( str , len ) ) ; } }
Print all distinct characters of a string in order ( 3 Methods ) | C # program to print distinct characters of a string . ; Function to print distinct characters in given string str [ ] ; count [ x ] is going to store count of character ' x ' in str . If x is not present , then it is going to store 0. ; index [ x ] is going to store index of character ' x ' in str . If x is not present or x is more than once , then it is going to store a value ( for example , length of string ) that cannot be a valid index in str [ ] ; Initialize counts of all characters and indexes of distinct characters . ; A value more than any index in str [ ] ; Traverse the input string ; Find current character and increment its count ; If this is first occurrence , then set value in index as index of it . ; If character repeats , then remove it from index [ ] ; Since size of index is constant , below operations take constant time . ; Driver code
using System ; public class GFG { static int MAX_CHAR = 256 ; static void printDistinct ( string str ) { int n = str . Length ; int [ ] count = new int [ MAX_CHAR ] ; int [ ] index = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { count [ i ] = 0 ; index [ i ] = n ; } for ( int i = 0 ; i < n ; i ++ ) { char x = str [ i ] ; ++ count [ x ] ; if ( count [ x ] == 1 && x != ' ▁ ' ) index [ x ] = i ; if ( count [ x ] == 2 ) index [ x ] = n ; } Array . Sort ( index ) ; for ( int i = 0 ; i < MAX_CHAR && index [ i ] != n ; i ++ ) Console . Write ( str [ index [ i ] ] ) ; } public static void Main ( ) { string str = " GeeksforGeeks " ; printDistinct ( str ) ; } }
Smallest length string with repeated replacement of two distinct adjacent | C # program to find smallest possible length of a string of only three characters ; Program to find length of reduced string in a string made of three characters . ; A memoized function find result recursively . a , b and c are counts of ' a ' s , ' b ' s and ' c ' s in str ; If this subproblem is already evaluated ; If this subproblem is already evaluated ; If there is only one type of character ; If only two types of characters are present ; If all types of characters are present . Try combining all pairs . ; Returns smallest possible length with given operation allowed . ; Counting occurrences of three different characters ' a ' , ' b ' and ' c ' in str ; Initialize DP [ ] [ ] entries as - 1 ; Driver code
using System ; public class GFG { static int MAX_LEN = 110 ; static int [ , , ] DP = new int [ MAX_LEN , MAX_LEN , MAX_LEN ] ; static int length ( int a , int b , int c ) { if ( a < 0 b < 0 c < 0 ) if ( DP [ a , b , c ] != - 1 ) return DP [ a , b , c ] ; if ( a == 0 && b == 0 ) return ( DP [ a , b , c ] = c ) ; if ( a == 0 && c == 0 ) return ( DP [ a , b , c ] = b ) ; if ( b == 0 && c == 0 ) return ( DP [ a , b , c ] = a ) ; if ( a == 0 ) return ( DP [ a , b , c ] = length ( a + 1 , b - 1 , c - 1 ) ) ; if ( b == 0 ) return ( DP [ a , b , c ] = length ( a - 1 , b + 1 , c - 1 ) ) ; if ( c == 0 ) return ( DP [ a , b , c ] = length ( a - 1 , b - 1 , c + 1 ) ) ; DP [ a , b , c ] = Math . Min ( length ( a - 1 , b - 1 , c + 1 ) , Math . Min ( length ( a - 1 , b + 1 , c - 1 ) , length ( a + 1 , b - 1 , c - 1 ) ) ) ; return DP [ a , b , c ] ; } static int stringReduction ( string str ) { int n = str . Length ; int [ ] count = new int [ 3 ] ; for ( int i = 0 ; i < n ; ++ i ) count [ str [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i <= count [ 0 ] ; ++ i ) for ( int j = 0 ; j < count [ 1 ] ; ++ j ) for ( int k = 0 ; k < count [ 2 ] ; ++ k ) DP [ i , j , k ] = - 1 ; return length ( count [ 0 ] , count [ 1 ] , count [ 2 ] ) ; } static public void Main ( ) { string str = " abcbbaacb " ; Console . WriteLine ( stringReduction ( str ) ) ; } }
Equilibrium index of an array | C # program to find equilibrium index of an array ; function to find the equilibrium index ; Check for indexes one by one until an equilibrium index is found ; get left sum ; get right sum ; if leftsum and rightsum are same , then we are done ; return - 1 if no equilibrium index is found ; driver code
using System ; class GFG { static int equilibrium ( int [ ] arr , int n ) { int i , j ; int leftsum , rightsum ; for ( i = 0 ; i < n ; ++ i ) { leftsum = 0 ; rightsum = 0 ; for ( j = 0 ; j < i ; j ++ ) leftsum += arr [ j ] ; for ( j = i + 1 ; j < n ; j ++ ) rightsum += arr [ j ] ; if ( leftsum == rightsum ) return i ; } return - 1 ; } public static void Main ( ) { int [ ] arr = { - 7 , 1 , 5 , 2 , - 4 , 3 , 0 } ; int arr_size = arr . Length ; Console . Write ( equilibrium ( arr , arr_size ) ) ; } }
Find number of endless points | C # program to find count of endless points ; Returns count of endless points ; Fills column matrix . For every column , start from every last row and fill every entry as blockage after a 0 is found . ; flag which will be zero once we get a '0' and it will be 1 otherwise ; encountered a '0' , set the isEndless variable to false ; Similarly , fill row matrix ; Calculate total count of endless points ; If there is NO blockage in row or column after this point , increment result . ; Driver code
using System ; public class GFG { static int countEndless ( bool [ , ] input , int n ) { bool [ , ] row = new bool [ n , n ] ; bool [ , ] col = new bool [ n , n ] ; for ( int j = 0 ; j < n ; j ++ ) { bool isEndless = true ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( input [ i , j ] == false ) isEndless = false ; col [ i , j ] = isEndless ; } } for ( int i = 0 ; i < n ; i ++ ) { bool isEndless = true ; for ( int j = n - 1 ; j >= 0 ; j -- ) { if ( input [ i , j ] == false ) isEndless = false ; row [ i , j ] = isEndless ; } } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 1 ; j < n ; j ++ ) if ( row [ i , j ] && col [ i , j ] ) ans ++ ; return ans ; } public static void Main ( ) { bool [ , ] input = { { true , false , true , true } , { false , true , true , true } , { true , true , true , true } , { false , true , true , false } } ; int n = 4 ; Console . Write ( countEndless ( input , n ) ) ; } }
Equilibrium index of an array | C # program to find the equilibrium index of an array ; function to find the equilibrium index ; initialize sum of whole array ; initialize leftsum ; Find sum of the whole array ; sum is now right sum for index i ; If no equilibrium index found , then return 0 ; Driver code
using System ; class GFG { static int equilibrium ( int [ ] arr , int n ) { int sum = 0 ; int leftsum = 0 ; for ( int i = 0 ; i < n ; ++ i ) sum += arr [ i ] ; for ( int i = 0 ; i < n ; ++ i ) { sum -= arr [ i ] ; if ( leftsum == sum ) return i ; leftsum += arr [ i ] ; } return - 1 ; } public static void Main ( ) { int [ ] arr = { - 7 , 1 , 5 , 2 , - 4 , 3 , 0 } ; int arr_size = arr . Length ; Console . Write ( " First ▁ equilibrium ▁ index ▁ is ▁ " + equilibrium ( arr , arr_size ) ) ; } }
Sum of all substrings of a string representing a number | Set 1 | C # program to print sum of all substring of a number represented as a string ; Returns sum of all substring of num ; allocate memory equal to length of string ; initialize first value with first digit ; loop over all digits of string ; update each sumofdigit from previous value ; add current value to the result ; Driver code
using System ; class GFG { public static int sumOfSubstrings ( String num ) { int n = num . Length ; int [ ] sumofdigit = new int [ n ] ; sumofdigit [ 0 ] = num [ 0 ] - '0' ; int res = sumofdigit [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { int numi = num [ i ] - '0' ; sumofdigit [ i ] = ( i + 1 ) * numi + 10 * sumofdigit [ i - 1 ] ; res += sumofdigit [ i ] ; } return res ; } public static void Main ( ) { String num = "1234" ; Console . Write ( sumOfSubstrings ( num ) ) ; } }
Sum of all substrings of a string representing a number | Set 1 | C # program to print sum of all substring of a number represented as a string ; Utility method to convert character digit to integer digit ; Returns sum of all subString of num ; Storing prev value ; SubStrings sum upto current index loop over all digits of String ; Update each sumofdigit from previous value ; Add current value to the result ; Update previous ; Driver code to test above methods
using System ; class GFG { static int toDigit ( char ch ) { return ( ch - '0' ) ; } static int sumOfSubStrings ( string num ) { int n = num . Length ; int prev = toDigit ( num [ 0 ] ) ; int res = prev ; int current = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int numi = toDigit ( num [ i ] ) ; current = ( i + 1 ) * numi + 10 * prev ; res += current ; prev = current ; } return res ; } static void Main ( ) { string num = "1234" ; Console . WriteLine ( sumOfSubStrings ( num ) ) ; } }
Equilibrium index of an array | C # program to find equilibrium index of an array ; Taking the prefixsum from front end array ; Taking the prefixsum from back end of array ; Checking for equilibrium index by compairing front and back sums ; If no equilibrium index found , then return - 1 ; Driver code
using System ; class GFG { static int equilibrium ( int [ ] a , int n ) { if ( n == 1 ) return ( 0 ) ; int [ ] front = new int [ n ] ; int [ ] back = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( i != 0 ) { front [ i ] = front [ i - 1 ] + a [ i ] ; } else { front [ i ] = a [ i ] ; } } for ( int i = n - 1 ; i > 0 ; i -- ) { if ( i <= n - 2 ) { back [ i ] = back [ i + 1 ] + a [ i ] ; } else { back [ i ] = a [ i ] ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( front [ i ] == back [ i ] ) { return i ; } } return - 1 ; } public static void Main ( string [ ] args ) { int [ ] arr = { - 7 , 1 , 5 , 2 , - 4 , 3 , 0 } ; int arr_size = arr . Length ; Console . WriteLine ( " First ▁ Point ▁ of ▁ equilibrium ▁ " + " is ▁ at ▁ index ▁ " + equilibrium ( arr , arr_size ) ) ; } }
Leaders in an array | C # program to print leaders in array ; the loop didn 't break ; Driver Code
using System ; class GFG { void printLeaders ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) { int j ; for ( j = i + 1 ; j < size ; j ++ ) { if ( arr [ i ] <= arr [ j ] ) break ; } if ( j == size ) Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( ) { GFG lead = new GFG ( ) ; int [ ] arr = new int [ ] { 16 , 17 , 4 , 3 , 5 , 2 } ; int n = arr . Length ; lead . printLeaders ( arr , n ) ; } }
Leaders in an array | C # program to find Leaders in an array ; C # Function to print leaders in an array ; Rightmost element is always leader ; Driver Code
using System ; class LeadersInArray { void printLeaders ( int [ ] arr , int size ) { int max_from_right = arr [ size - 1 ] ; Console . Write ( max_from_right + " ▁ " ) ; for ( int i = size - 2 ; i >= 0 ; i -- ) { if ( max_from_right < arr [ i ] ) { max_from_right = arr [ i ] ; Console . Write ( max_from_right + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { LeadersInArray lead = new LeadersInArray ( ) ; int [ ] arr = new int [ ] { 16 , 17 , 4 , 3 , 5 , 2 } ; int n = arr . Length ; lead . printLeaders ( arr , n ) ; } }
Unbounded Knapsack ( Repetition of items allowed ) | C # program to find maximum achievable value with a knapsack of weight W and multiple instances allowed . ; Returns the maximum value with knapsack of W capacity ; dp [ i ] is going to store maximum value with knapsack capacity i . ; Fill dp [ ] using above recursive formula ; Driver program
using System ; class UboundedKnapsack { private static int max ( int i , int j ) { return ( i > j ) ? i : j ; } private static int unboundedKnapsack ( int W , int n , int [ ] val , int [ ] wt ) { int [ ] dp = new int [ W + 1 ] ; for ( int i = 0 ; i <= W ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( wt [ j ] <= i ) { dp [ i ] = Math . Max ( dp [ i ] , dp [ i - wt [ j ] ] + val [ j ] ) ; } } } return dp [ W ] ; } public static void Main ( ) { int W = 100 ; int [ ] val = { 10 , 30 , 20 } ; int [ ] wt = { 5 , 10 , 15 } ; int n = val . Length ; Console . WriteLine ( unboundedKnapsack ( W , n , val , wt ) ) ; } }
Ceiling in a sorted array | C # program to find celing in a sorted array ; Function to get index of ceiling of x in arr [ low . . high ] ; If x is smaller than or equal to first element , then return the first element ; Otherwise , linearly search for ceil value ; if x lies between arr [ i ] and arr [ i + 1 ] including arr [ i + 1 ] , then return arr [ i + 1 ] ; If we reach here then x is greater than the last element of the array , return - 1 in this case ; Driver code
using System ; class GFG { static int ceilSearch ( int [ ] arr , int low , int high , int x ) { int i ; if ( x <= arr [ low ] ) return low ; for ( i = low ; i < high ; i ++ ) { if ( arr [ i ] == x ) return i ; if ( arr [ i ] < x && arr [ i + 1 ] >= x ) return i + 1 ; } return - 1 ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 8 , 10 , 10 , 12 , 19 } ; int n = arr . Length ; int x = 3 ; int index = ceilSearch ( arr , 0 , n - 1 , x ) ; if ( index == - 1 ) Console . Write ( " Ceiling ▁ of ▁ " + x + " ▁ doesn ' t ▁ exist ▁ in ▁ array " ) ; else Console . Write ( " ceiling ▁ of ▁ " + x + " ▁ is ▁ " + arr [ index ] ) ; } }
Maximum sum subarray removing at most one element | C # program to get maximum sum subarray removing at - most one element ; Method returns maximum sum of all subarray where removing one element is also allowed ; Maximum sum subarrays in forward and backward directions ; Initialize current max and max so far . ; calculating maximum sum subarrays in forward direction ; storing current maximum till ith , in forward array ; calculating maximum sum subarrays in backward direction ; storing current maximum from ith , in backward array ; Initializing final ans by max_so_far so that , case when no element is removed to get max sum subarray is also handled ; choosing maximum ignoring ith element ; Driver code
using System ; class GFG { static int maxSumSubarrayRemovingOneEle ( int [ ] arr , int n ) { int [ ] fw = new int [ n ] ; int [ ] bw = new int [ n ] ; int cur_max = arr [ 0 ] , max_so_far = arr [ 0 ] ; fw [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { cur_max = Math . Max ( arr [ i ] , cur_max + arr [ i ] ) ; max_so_far = Math . Max ( max_so_far , cur_max ) ; fw [ i ] = cur_max ; } cur_max = max_so_far = bw [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { cur_max = Math . Max ( arr [ i ] , cur_max + arr [ i ] ) ; max_so_far = Math . Max ( max_so_far , cur_max ) ; bw [ i ] = cur_max ; } int fans = max_so_far ; for ( int i = 1 ; i < n - 1 ; i ++ ) fans = Math . Max ( fans , fw [ i - 1 ] + bw [ i + 1 ] ) ; return fans ; } public static void Main ( ) { int [ ] arr = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n = arr . Length ; Console . WriteLine ( maxSumSubarrayRemovingOneEle ( arr , n ) ) ; } }
Ceiling in a sorted array | C # program to find celing in a sorted array ; Function to get index of ceiling of x in arr [ low . . high ] ; If x is smaller than or equal to the first element , then return the first element . ; If x is greater than the last element , then return - 1 ; get the index of middle element of arr [ low . . high ] ; If x is same as middle element then return mid ; If x is greater than arr [ mid ] , then either arr [ mid + 1 ] is ceiling of x or ceiling lies in arr [ mid + 1. . . high ] ; If x is smaller than arr [ mid ] , then either arr [ mid ] is ceiling of x or ceiling lies in arr [ low ... mid - 1 ] ; Driver code
using System ; class GFG { static int ceilSearch ( int [ ] arr , int low , int high , int x ) { int mid ; if ( x <= arr [ low ] ) return low ; if ( x > arr [ high ] ) return - 1 ; mid = ( low + high ) / 2 ; if ( arr [ mid ] == x ) return mid ; else if ( arr [ mid ] < x ) { if ( mid + 1 <= high && x <= arr [ mid + 1 ] ) return mid + 1 ; else return ceilSearch ( arr , mid + 1 , high , x ) ; } else { if ( mid - 1 >= low && x > arr [ mid - 1 ] ) return mid ; else return ceilSearch ( arr , low , mid - 1 , x ) ; } } public static void Main ( ) { int [ ] arr = { 1 , 2 , 8 , 10 , 10 , 12 , 19 } ; int n = arr . Length ; int x = 8 ; int index = ceilSearch ( arr , 0 , n - 1 , x ) ; if ( index == - 1 ) Console . Write ( " Ceiling ▁ of ▁ " + x + " ▁ doesn ' t ▁ exist ▁ in ▁ array " ) ; else Console . Write ( " ceiling ▁ of ▁ " + x + " ▁ is ▁ " + arr [ index ] ) ; } }