codeparrot/xlcost-text-to-code huggingface dataset:
Check if a symmetric plus is possible from the elements of the given array | C # implementation of the approach ; Function that return true if a symmetric is possible with the elements of the array ; Map to store the frequency of the array elements ; Traverse through array elements and count frequencies ; For every unique element ; Element has already been found ; The frequency of the element something other than 0 and 1 ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool isPlusPossible ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } } bool foundModOne = false ; foreach ( KeyValuePair < int , int > x in mp ) { int element = x . Key ; int frequency = x . Value ; if ( frequency % 4 == 0 ) continue ; if ( frequency % 4 == 1 ) { if ( foundModOne ) return false ; foundModOne = true ; } else return false ; } return true ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 1 , 2 , 2 , 2 , 3 , 2 } ; int n = arr . Length ; if ( isPlusPossible ( arr , n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Queries for elements greater than K in the given index range using Segment Tree | C # implementation of the approach ; Merge procedure to merge two vectors into a single vector ; Final vector to return after merging ; Loop continues until it reaches the end of one of the vectors ; Here , simply add the remaining elements to the vector v ; Procedure to build the segment tree ; Reached the leaf node of the segment tree ; Recursively call the buildTree on both the nodes of the tree ; Storing the readonly vector after merging the two of its sorted child vector ; Query procedure to get the answer for each query l and r are query range ; out of bound or no overlap ; Complete overlap Query range completely lies in the segment tree node range ; binary search to find index of k ; Partially overlap Query range partially lies in the segment tree node range ; Function to perform the queries ; Driver Code ; 1 - based indexing ; Number of queries
using System ; using System . Collections . Generic ; class GFG { static List < int > merge ( List < int > v1 , List < int > v2 ) { int i = 0 , j = 0 ; List < int > v = new List < int > ( ) ; while ( i < v1 . Count && j < v2 . Count ) { if ( v1 [ i ] <= v2 [ j ] ) { v . Add ( v1 [ i ] ) ; i ++ ; } else { v . Add ( v2 [ j ] ) ; j ++ ; } } for ( int k = i ; k < v1 . Count ; k ++ ) v . Add ( v1 [ k ] ) ; for ( int k = j ; k < v2 . Count ; k ++ ) v . Add ( v2 [ k ] ) ; return v ; } static void buildTree ( List < int > [ ] tree , int [ ] arr , int index , int s , int e ) { if ( s == e ) { tree [ index ] . Add ( arr [ s ] ) ; return ; } int mid = ( s + e ) / 2 ; buildTree ( tree , arr , 2 * index , s , mid ) ; buildTree ( tree , arr , 2 * index + 1 , mid + 1 , e ) ; tree [ index ] = merge ( tree [ 2 * index ] , tree [ 2 * index + 1 ] ) ; } static int query ( List < int > [ ] tree , int index , int s , int e , int l , int r , int k ) { if ( r < s l > e ) return 0 ; if ( s >= l && e <= r ) { return ( tree [ index ] . Count - lowerBound ( tree [ index ] , tree [ index ] . Count , k ) ) ; } int mid = ( s + e ) / 2 ; return ( query ( tree , 2 * index , s , mid , l , r , k ) + query ( tree , 2 * index + 1 , mid + 1 , e , l , r , k ) ) ; } static void performQueries ( int [ ] L , int [ ] R , int [ ] K , int n , int q , List < int > [ ] tree ) { for ( int i = 0 ; i < q ; i ++ ) { Console . WriteLine ( query ( tree , 1 , 0 , n - 1 , L [ i ] - 1 , R [ i ] - 1 , K [ i ] ) ) ; } } static int lowerBound ( List < int > array , int length , int value ) { int low = 0 ; int high = length ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( value <= array [ mid ] ) { high = mid ; } else { low = mid + 1 ; } } return low ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 3 , 9 , 13 , 5 , 4 } ; int n = arr . Length ; List < int > [ ] tree = new List < int > [ 4 * n + 1 ] ; for ( int i = 0 ; i < ( 4 * n + 1 ) ; i ++ ) { tree [ i ] = new List < int > ( ) ; } buildTree ( tree , arr , 1 , 0 , n - 1 ) ; int [ ] L = { 1 , 2 } ; int [ ] R = { 4 , 6 } ; int [ ] K = { 6 , 8 } ; int q = L . Length ; performQueries ( L , R , K , n , q , tree ) ; } }
Calculate the Sum of GCD over all subarrays | C # program to find Sum of GCD over all subarrays . ; Utility function to calculate sum of gcd of all sub - arrays . ; Fixing the starting index of a subarray ; Fixing the ending index of a subarray ; Finding the GCD of this subarray ; Adding this GCD in our sum ; Driver Code
using System ; class GFG { static int findGCDSum ( int n , int [ ] a ) { int GCDSum = 0 ; int tempGCD = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { tempGCD = 0 ; for ( int k = i ; k <= j ; k ++ ) { tempGCD = __gcd ( tempGCD , a [ k ] ) ; } GCDSum += tempGCD ; } } return GCDSum ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int n = 5 ; int [ ] a = { 1 , 2 , 3 , 4 , 5 } ; int totalSum = findGCDSum ( n , a ) ; Console . Write ( totalSum + " STRNEWLINE " ) ; } }
Check if the array has an element which is equal to XOR of remaining elements | C # implementation of the approach ; Function that returns true if the array contains an element which is equal to the XOR of the remaining elements ; To store the XOR of all the array elements ; For every element of the array ; Take the XOR after excluding the current element ; If the XOR of the remaining elements is equal to the current element ; If no such element is found ; Driver code
using System ; class GFG { static bool containsElement ( int [ ] arr , int n ) { int xorArr = 0 ; for ( int i = 0 ; i < n ; ++ i ) xorArr ^= arr [ i ] ; for ( int i = 0 ; i < n ; ++ i ) { int x = xorArr ^ arr [ i ] ; if ( arr [ i ] == x ) return true ; } return false ; } public static void Main ( ) { int [ ] arr = { 8 , 2 , 4 , 15 , 1 } ; int n = arr . Length ; if ( containsElement ( arr , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Square root of a number without using sqrt ( ) function | C # implementation of the approach ; Recursive function that returns square root of a number with precision upto 5 decimal places ; If mid itself is the square root , return mid ; If mul is less than n , recur second half ; Else recur first half ; Function to find the square root of n ; While the square root is not found ; If n is a perfect square ; Square root will lie in the interval i - 1 and i ; Driver code
using System ; class GFG { static double Square ( double n , double i , double j ) { double mid = ( i + j ) / 2 ; double mul = mid * mid ; if ( ( mul == n ) || ( Math . Abs ( mul - n ) < 0.00001 ) ) return mid ; else if ( mul < n ) return Square ( n , mid , j ) ; else return Square ( n , i , mid ) ; } static void findSqrt ( double n ) { double i = 1 ; Boolean found = false ; while ( ! found ) { if ( i * i == n ) { Console . WriteLine ( i ) ; found = true ; } else if ( i * i > n ) { double res = Square ( n , i - 1 , i ) ; Console . Write ( " { 0 : F5 } " , res ) ; found = true ; } i ++ ; } } public static void Main ( String [ ] args ) { double n = 3 ; findSqrt ( n ) ; } }
Length of longest substring having all characters as K | C # program for the above approach ; Function to find the length of longest sub - string having all characters same as character K ; Initialize variables ; Iterate till size of string ; Check if current character is K ; Assingning the max value to max_len ; Driver code ; Function call
using System ; class GFG { static int length_substring ( string S , char K ) { int curr_cnt = 0 , prev_cnt = 0 , max_len ; for ( int i = 0 ; i < S . Length ; i ++ ) { if ( S [ i ] == K ) { curr_cnt += 1 ; } else { prev_cnt = Math . Max ( prev_cnt , curr_cnt ) ; curr_cnt = 0 ; } } prev_cnt = Math . Max ( prev_cnt , curr_cnt ) ; max_len = prev_cnt ; return max_len ; } static public void Main ( ) { string S = " abcd1111aabc " ; char K = '1' ; Console . WriteLine ( length_substring ( S , K ) ) ; } }
Find a partition point in array to maximize its xor sum | C # program to find partition point in array to maximize xor sum ; Function to find partition point in array to maximize xor sum ; Traverse through the array ; Calculate xor of elements left of index i including ith element ; Calculate xor of the elements right of index i ; Keep the maximum possible xor sum ; Return the 1 based index of the array ; Driver code ; Function call
using System ; class GFG { public static int Xor_Sum ( int [ ] arr , int n ) { int sum = 0 , index = - 1 ; int left_xor = 0 , right_xor = 0 ; for ( int i = 0 ; i < n ; i ++ ) { left_xor = left_xor ^ arr [ i ] ; right_xor = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { right_xor = right_xor ^ arr [ j ] ; } if ( left_xor + right_xor > sum ) { sum = left_xor + right_xor ; index = i ; } } return index + 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 6 , 3 , 8 , 13 , 34 , 2 , 21 , 10 } ; int n = arr . Length ; Console . WriteLine ( Xor_Sum ( arr , n ) ) ; } }
Find a partition point in array to maximize its xor sum | C # program to find partition point in array to maximize xor sum ; Function to calculate Prefix Xor array ; Calculating prefix xor ; Function to find partition point in array to maximize xor sum ; To store prefix xor ; Compute the prefix xor ; To store sum and index ; Calculate the maximum sum that can be obtained splitting the array at some index i ; PrefixXor [ i ] = Xor of all arr elements till i ' th ▁ index ▁ PrefixXor [ n - 1 ] ▁ ▁ ^ ▁ PrefixXor [ i ] ▁ = ▁ Xor ▁ of ▁ all ▁ elements ▁ ▁ from ▁ i + 1' th index to n - 1 'th index ; Return the index ; Driver code ; Function call
using System ; class GFG { static void ComputePrefixXor ( int [ ] arr , int [ ] PrefixXor , int n ) { PrefixXor [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) PrefixXor [ i ] = PrefixXor [ i - 1 ] ^ arr [ i ] ; } static int Xor_Sum ( int [ ] arr , int n ) { int [ ] PrefixXor = new int [ n ] ; ComputePrefixXor ( arr , PrefixXor , n ) ; int sum = 0 , index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( PrefixXor [ i ] + ( PrefixXor [ n - 1 ] ^ PrefixXor [ i ] ) > sum ) { sum = PrefixXor [ i ] + ( PrefixXor [ n - 1 ] ^ PrefixXor [ i ] ) ; index = i ; } } return index + 1 ; } public static void Main ( ) { int [ ] arr = { 1 , 4 , 6 , 3 , 8 , 13 , 34 , 2 , 21 , 10 } ; int n = arr . Length ; Console . WriteLine ( Xor_Sum ( arr , n ) ) ; } }
Find the color of given node in an infinite binary tree | C # program to find color of the node ; Function to find color of the node ; Maximum is to store maximum color ; Loop to check all the parent values to get maximum color ; Find the number into map and get maximum color ; Take the maximum color and assign into maximum variable ; Find parent index ; Return maximum color ; Function to build hash map with color ; To store color of each node ; For each number add a color number ; Assigning color ; Return hash map ; Driver code ; Build mapWithColor ; Print the maximum color
using System ; using System . Collections . Generic ; class GFG { static int findColor ( Dictionary < int , int > mapWithColor , int query ) { int maximum = 0 ; while ( query >= 1 ) { if ( mapWithColor . ContainsKey ( query ) ) { maximum = Math . Max ( maximum , mapWithColor [ query ] ) ; } if ( query % 2 == 1 ) query = ( query - 1 ) / 2 ; else query = query / 2 ; } return maximum ; } static Dictionary < int , int > buildMapWithColor ( int [ ] arr , int n ) { Dictionary < int , int > mapWithColor = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mapWithColor . ContainsKey ( arr [ i ] ) ) mapWithColor [ arr [ i ] ] = i + 1 ; else mapWithColor . Add ( arr [ i ] , i + 1 ) ; } return mapWithColor ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 , 3 } ; int n = arr . Length ; int k = 7 ; Dictionary < int , int > mapWithColor = buildMapWithColor ( arr , n ) ; Console . Write ( findColor ( mapWithColor , k ) ) ; } }
Count of strings in the first array which are smaller than every string in the second array | C # implementation of the approach ; Function to count the number of smaller strings in A [ ] for every String in B [ ] ; Count the frequency of all characters ; Iterate for all possible strings in A [ ] ; Increase the frequency of every character ; Check for the smallest character 's frequency ; Get the smallest character frequency ; Insert it in the vector ; Sort the count of all the frequency of the smallest character in every string ; Iterate for every String in B [ ] ; Hash set every frequency 0 ; Count the frequency of every character ; Find the frequency of the smallest character ; Count the number of strings in A [ ] which has the frequency of the smaller character less than the frequency of the smaller character of the String in B [ ] ; Store the answer ; Function to print the answer ; Get the answer ; Print the number of strings for every answer ; Driver code
using System ; using System . Collections . Generic ; public class GFG { static int MAX = 26 ; static List < int > findCount ( String [ ] a , String [ ] b , int n , int m ) { int [ ] freq = new int [ MAX ] ; List < int > smallestFreq = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { String s = a [ i ] ; for ( int l = 0 ; l < freq . Length ; l ++ ) freq [ l ] = 0 ; for ( int j = 0 ; j < s . Length ; j ++ ) { freq [ s [ j ] - ' a ' ] ++ ; } for ( int j = 0 ; j < MAX ; j ++ ) { if ( freq [ j ] > 0 ) { smallestFreq . Add ( freq [ j ] ) ; break ; } } } smallestFreq . Sort ( ) ; List < int > ans = new List < int > ( ) ; for ( int i = 0 ; i < m ; i ++ ) { String s = b [ i ] ; for ( int l = 0 ; l < freq . Length ; l ++ ) freq [ l ] = 0 ; for ( int j = 0 ; j < s . Length ; j ++ ) { freq [ s [ j ] - ' a ' ] ++ ; } int frequency = 0 ; for ( int j = 0 ; j < MAX ; j ++ ) { if ( freq [ j ] > 0 ) { frequency = freq [ j ] ; break ; } } int [ ] array = new int [ smallestFreq . Count ] ; int k = 0 ; foreach ( int val in smallestFreq ) { array [ k ] = val ; k ++ ; } int ind = lower_bound ( array , 0 , smallestFreq . Count , frequency ) ; ans . Add ( ind ) ; } return ans ; } static int lower_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } static void printAnswer ( String [ ] a , String [ ] b , int n , int m ) { List < int > ans = findCount ( a , b , n , m ) ; foreach ( int it in ans ) { Console . Write ( it + " ▁ " ) ; } } public static void Main ( String [ ] args ) { String [ ] A = { " aaa " , " aa " , " bdc " } ; String [ ] B = { " cccch " , " cccd " } ; int n = A . Length ; int m = B . Length ; printAnswer ( A , B , n , m ) ; } }
Find Leftmost and Rightmost node of BST from its given preorder traversal | C # program to find leftmost and rightmost node from given preorder sequence ; Function to return the leftmost and rightmost nodes of the BST whose preorder traversal is given ; Variables for finding minimum and maximum values of the array ; Update the minimum ; Update the maximum ; Print the values ; Driver Code
using System ; class GFG { static void LeftRightNode ( int [ ] preorder , int n ) { int min = int . MaxValue , max = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( min > preorder [ i ] ) min = preorder [ i ] ; if ( max < preorder [ i ] ) max = preorder [ i ] ; } Console . WriteLine ( " Leftmost ▁ node ▁ is ▁ " + min ) ; Console . WriteLine ( " Rightmost ▁ node ▁ is ▁ " + max ) ; } public static void Main ( String [ ] args ) { int [ ] preorder = { 3 , 2 , 1 , 5 , 4 } ; int n = 5 ; LeftRightNode ( preorder , n ) ; } }
Find the position of the given row in a 2 | C # implementation of the approach ; Function that compares both the arrays and returns - 1 , 0 and 1 accordingly ; Return 1 if mid row is less than arr [ ] ; Return 1 if mid row is greater than arr [ ] ; Both the arrays are equal ; Function to find a row in the given matrix using binary search ; If current row is equal to the given array then return the row number ; If arr [ ] is greater , ignore left half ; If arr [ ] is smaller , ignore right half ; No valid row found ; Driver code
using System ; class GFG { static int m = 6 , n = 4 ; static int compareRow ( int [ ] a1 , int [ ] a2 ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a1 [ i ] < a2 [ i ] ) return 1 ; else if ( a1 [ i ] > a2 [ i ] ) return - 1 ; } return 0 ; } static int binaryCheck ( int [ , ] ar , int [ ] arr ) { int l = 0 , r = m - 1 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; int temp = compareRow ( GetRow ( ar , mid ) , arr ) ; if ( temp == 0 ) return mid + 1 ; else if ( temp == 1 ) l = mid + 1 ; else r = mid - 1 ; } return - 1 ; } public static int [ ] GetRow ( int [ , ] matrix , int row ) { var rowLength = matrix . GetLength ( 1 ) ; var rowVector = new int [ rowLength ] ; for ( var i = 0 ; i < rowLength ; i ++ ) rowVector [ i ] = matrix [ row , i ] ; return rowVector ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 0 , 0 , 1 , 0 } , { 10 , 9 , 22 , 23 } , { 40 , 40 , 40 , 40 } , { 43 , 44 , 55 , 68 } , { 81 , 73 , 100 , 132 } , { 100 , 75 , 125 , 133 } } ; int [ ] row = { 10 , 9 , 22 , 23 } ; Console . WriteLine ( binaryCheck ( mat , row ) ) ; } }
Number of subarrays having absolute sum greater than K | Set | C # implementation of the approach ; Function to find required value ; Variable to store final answer ; Loop to find prefix - sum ; Sorting prefix - sum array ; Loop to find upper_bound for each element ; Returning final answer ; Driver code ; Function to find required value
using System ; class GFG { static int findCnt ( int [ ] arr , int n , int k ) { int ans = 0 ; for ( int i = 1 ; i < n ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; if ( arr [ i ] > k arr [ i ] < - 1 * k ) ans ++ ; } if ( arr [ 0 ] > k arr [ 0 ] < - 1 * k ) ans ++ ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) ans += n - upper_bound ( arr , 0 , n , arr [ i ] + k ) ; return ans ; } static int upper_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } public static void Main ( ) { int [ ] arr = { - 1 , 4 , - 5 , 6 } ; int n = arr . Length ; int k = 0 ; Console . WriteLine ( findCnt ( arr , n , k ) ) ; } }
Number of intersections between two ranges | C # implementation of above approach ; Function to return total number of intersections ; Maximum possible number of intersections ; Store all starting points of type1 ranges ; Store all endpoints of type1 ranges ; Starting point of type2 ranges ; Ending point of type2 ranges ; Subtract those ranges which are starting after R ; Subtract those ranges which are ending before L ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int upperBound ( List < int > a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) { high = middle ; } else { low = middle + 1 ; } } return low ; } static int FindIntersection ( List < List < int > > type1 , int n , List < List < int > > type2 , int m ) { int ans = n * m ; List < int > start = new List < int > ( ) ; List < int > end = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { start . Add ( type1 [ i ] [ 0 ] ) ; end . Add ( type1 [ i ] [ 1 ] ) ; } start . Sort ( ) ; end . Sort ( ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = type2 [ i ] [ 0 ] ; int R = type2 [ i ] [ 1 ] ; ans -= start . Count - upperBound ( start , 0 , start . Count , R ) ; ans -= upperBound ( end , 0 , end . Count , L - 1 ) ; } return ans ; } static public void Main ( ) { List < List < int > > type1 = new List < List < int > > ( ) ; type1 . Add ( new List < int > ( ) { 1 , 2 } ) ; type1 . Add ( new List < int > ( ) { 2 , 3 } ) ; type1 . Add ( new List < int > ( ) { 4 , 5 } ) ; type1 . Add ( new List < int > ( ) { 6 , 7 } ) ; List < List < int > > type2 = new List < List < int > > ( ) ; type2 . Add ( new List < int > ( ) { 1 , 5 } ) ; type2 . Add ( new List < int > ( ) { 2 , 3 } ) ; type2 . Add ( new List < int > ( ) { 4 , 7 } ) ; type2 . Add ( new List < int > ( ) { 5 , 7 } ) ; int n = type1 . Count ; int m = type2 . Count ; Console . WriteLine ( FindIntersection ( type1 , n , type2 , m ) ) ; } }
Find the Nth term divisible by a or b or c | C # implementation of the approach ; Function to return gcd of a and b ; Function to return the lcm of a and b ; Function to return the count of numbers from 1 to num which are divisible by a , b or c ; Calculate number of terms divisible by a and by b and by c then , remove the terms which is are divisible by both a and b , both b and c , both c and a and then add which are divisible by a and b and c ; Function to find the nth term divisible by a , b or c by using binary search ; Set low to 1 and high to max ( a , b , c ) * n ; If the current term is less than n then we need to increase low to mid + 1 ; If current term is greater than equal to n then high = mid ; Driver code
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int lcm ( int a , int b ) { return ( a * b ) / gcd ( a , b ) ; } static int divTermCount ( int a , int b , int c , int num ) { return ( ( num / a ) + ( num / b ) + ( num / c ) - ( num / lcm ( a , b ) ) - ( num / lcm ( b , c ) ) - ( num / lcm ( a , c ) ) + ( num / lcm ( a , lcm ( b , c ) ) ) ) ; } static int findNthTerm ( int a , int b , int c , int n ) { int low = 1 , high = int . MaxValue , mid ; while ( low < high ) { mid = low + ( high - low ) / 2 ; if ( divTermCount ( a , b , c , mid ) < n ) low = mid + 1 ; else high = mid ; } return low ; } public static void Main ( String [ ] args ) { int a = 2 , b = 3 , c = 5 , n = 10 ; Console . WriteLine ( findNthTerm ( a , b , c , n ) ) ; } }
Split the given array into K sub | C # implementation of the above approach ; Function to check if mid can be maximum sub - arrays sum ; If individual element is greater maximum possible sum ; Increase sum of current sub - array ; If the sum is greater than mid increase count ; Check condition ; Function to find maximum subarray sum which is minimum ; start = array [ i ] ; Max subarray sum , considering subarray of length 1 ; end += array [ i ] ; Max subarray sum , considering subarray of length n ; Answer stores possible maximum sub array sum ; If mid is possible solution Put answer = mid ; ; Driver Code
using System ; class GFG { static Boolean check ( int mid , int [ ] array , int n , int K ) { int count = 0 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( array [ i ] > mid ) return false ; sum += array [ i ] ; if ( sum > mid ) { count ++ ; sum = array [ i ] ; } } count ++ ; if ( count <= K ) return true ; return false ; } static int solve ( int [ ] array , int n , int K ) { int start = 1 ; for ( int i = 0 ; i < n ; ++ i ) { if ( array [ i ] > start ) } int end = 0 ; for ( int i = 0 ; i < n ; i ++ ) { } int answer = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( check ( mid , array , n , K ) ) { answer = mid ; end = mid - 1 ; } else { start = mid + 1 ; } } return answer ; } public static void Main ( String [ ] args ) { int [ ] array = { 1 , 2 , 3 , 4 } ; int n = array . Length ; int K = 3 ; Console . WriteLine ( solve ( array , n , K ) ) ; } }