codeparrot/xlcost-text-to-code huggingface dataset:
Find the smallest positive number missing from an unsorted array | Set 3 | Java program to find the smallest positive missing number ; Function to find the smallest positive missing number ; Default smallest Positive Integer ; Store values in set which are greater than variable m ; Store value when m is less than current index of given array ; Increment m when it is equal to current element ; Increment m when it is one of the element of the set ; Return the required answer ; Driver code ; Function call
import java . util . * ; class GFG { static int findMissingPositive ( int arr [ ] , int n ) { int m = 1 ; HashSet < Integer > x = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m < arr [ i ] ) { x . add ( arr [ i ] ) ; } else if ( m == arr [ i ] ) { m = m + 1 ; while ( x . contains ( m ) ) { x . remove ( m ) ; m = m + 1 ; } } } return m ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , - 7 , 6 , 8 , 1 , - 10 , 15 } ; int n = arr . length ; System . out . println ( findMissingPositive ( arr , n ) ) ; } }
Sum of all second largest divisors after splitting a number into one or more parts | Java program to Sum of all second largest divisors after splitting a number into one or more parts ; Function to find a number is prime or not ; If there is any divisor ; Function to find the sum of all second largest divisor after splitting a number into one or more parts ; If number is prime ; If n is even ; If the number is odd ; If N - 2 is prime ; There exists 3 primes x1 , x2 , x3 such that x1 + x2 + x3 = n ; Driver code ; Function call
import java . io . * ; class GFG { static boolean prime ( int n ) { if ( n == 1 ) return false ; for ( int i = 2 ; i * i <= n ; ++ i ) if ( n % i == 0 ) return false ; return true ; } static int Min_Sum ( int n ) { if ( prime ( n ) ) return 1 ; if ( n % 2 == 0 ) return 2 ; else { if ( prime ( n - 2 ) ) return 2 ; else return 3 ; } } public static void main ( String [ ] args ) { int n = 27 ; System . out . println ( Min_Sum ( n ) ) ; } }
Find maximum value of the last element after reducing the array with given operations | Java implementation of the approach ; Function to return the maximized value ; Overall minimum absolute value of some element from the array ; Add all absolute values ; Count positive and negative elements ; Both positive and negative values are present ; Only positive or negative values are present ; Driver code
import java . io . * ; class GFG { static int find_maximum_value ( int a [ ] , int n ) { int sum = 0 ; int minimum = Integer . MAX_VALUE ; int pos = 0 , neg = 0 ; for ( int i = 0 ; i < n ; i ++ ) { minimum = Math . min ( minimum , Math . abs ( a [ i ] ) ) ; sum += Math . abs ( a [ i ] ) ; if ( a [ i ] >= 0 ) pos += 1 ; else neg += 1 ; } if ( pos > 0 && neg > 0 ) return sum ; return ( sum - 2 * minimum ) ; } public static void main ( String [ ] args ) { int [ ] a = { 5 , 4 , 6 , 2 } ; int n = a . length ; System . out . println ( find_maximum_value ( a , n ) ) ; } }
Find Nth smallest number that is divisible by 100 exactly K times | Java implementation of above approach ; Function to find the Nth smallest number ; If N is divisible by 100 then we multiply N + 1 otherwise , it will be divisible by 100 more than K times ; convert integer to string ; if N is not divisible by 100 ; convert integer to string ; add 2 * K 0 's at the end to be divisible  by 100 exactly K times ; Driver Code
import java . util . * ; class GFG { static String find_number ( int N , int K ) { String r ; if ( N % 100 == 0 ) { N += 1 ; r = String . valueOf ( N ) ; } else { r = String . valueOf ( N ) ; } for ( int i = 1 ; i <= K ; i ++ ) r += "00" ; return r ; } public static void main ( String [ ] args ) { int N = 1000 , K = 2 ; String ans = find_number ( N , K ) ; System . out . println ( ans ) ; } }
Count of adjacent Vowel Consonant Pairs | Java Program to implement the above approach ; Function to count the adjacent pairs of consonant and vowels in the String ; Using a set to store the vowels so that checking each character becomes easier ; Variable to store number of consonant - vowel pairs ; If the ith character is not found in the set , means it is a consonant And if the ( i + 1 ) th character is found in the set , means it is a vowel We increment the count of such pairs ; Driver Code
import java . util . * ; class Sol { static int countPairs ( String s ) { Set < Character > st = new HashSet < Character > ( ) ; st . add ( ' a ' ) ; st . add ( ' e ' ) ; st . add ( ' i ' ) ; st . add ( ' o ' ) ; st . add ( ' u ' ) ; int count = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( st . contains ( s . charAt ( i ) ) && ! st . contains ( s . charAt ( i + 1 ) ) ) count ++ ; } return count ; } public static void main ( String args [ ] ) { String s = " geeksforgeeks " ; System . out . println ( countPairs ( s ) ) ; } }
Find the original coordinates whose Manhattan distances are given | Java implementation of the approach ; Function to find the original coordinated ; Maximum of the given distances ; Sum of the given distances ; Conditions when the solution doesn 't exist ; First coordinate ; Second coordinate ; Third coordinate ; Driver code
import java . io . * ; class GFG { static void solve ( int d1 , int d2 , int d3 ) { int maxx = Math . max ( d1 , Math . max ( d2 , d3 ) ) ; int sum = ( d1 + d2 + d3 ) ; if ( 2 * maxx > sum sum % 2 == 1 ) { System . out . print ( " - 1" ) ; return ; } int x1 = 0 , y1 = 0 ; int x2 = d1 , y2 = 0 ; int x3 = ( d1 + d2 - d3 ) / 2 ; int y3 = ( d2 + d3 - d1 ) / 2 ; System . out . print ( " ( " + x1 + " , ▁ " + y1 + " ) , ▁ ( " + x2 + " , ▁ " + y2 + " ) ▁ and ▁ ( " + x3 + " , ▁ " + y3 + " ) " ) ; } public static void main ( String [ ] args ) { int d1 = 3 , d2 = 4 , d3 = 5 ; solve ( d1 , d2 , d3 ) ; } }
Find the largest interval that contains exactly one of the given N integers . | Java implementation of the approach ; Function to return the maximum size of the required interval ; Insert the borders for array ; Sort the elements in ascending order ; To store the maximum size ; To store the range [ L , R ] such that only v [ i ] lies within the range ; Total integers in the range ; Driver code
import static java . lang . Integer . max ; import java . util . * ; class GFG { static int maxSize ( Vector < Integer > v , int n ) { v . add ( 0 ) ; v . add ( 100001 ) ; n += 2 ; Collections . sort ( v ) ; int mx = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { int L = v . get ( i - 1 ) + 1 ; int R = v . get ( i + 1 ) - 1 ; int cnt = R - L + 1 ; mx = max ( mx , cnt ) ; } return mx ; } public static void main ( String [ ] args ) { Integer arr [ ] = { 200 , 10 , 5 } ; Vector v = new Vector ( Arrays . asList ( arr ) ) ; int n = v . size ( ) ; System . out . println ( maxSize ( v , n ) ) ; } }
Maximum length of subarray such that sum of the subarray is even | Java implementation of the approach ; Function to find length of the longest subarray such that sum of the subarray is even ; Check if sum of complete array is even ; if ( sum % 2 == 0 ) total sum is already even ; Find an index i such the a [ i ] is odd and compare length of both halfs excluding a [ i ] to find max length subarray ; Driver Code
class GFG { static int maxLength ( int a [ ] , int n ) { int sum = 0 , len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } { return n ; } for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) { len = Math . max ( len , Math . max ( n - i - 1 , i ) ) ; } } return len ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 2 } ; int n = a . length ; System . out . println ( maxLength ( a , n ) ) ; } }
Find a Symmetric matrix of order N that contain integers from 0 to N | Java implementation of the approach ; Function to generate the required matrix ; Form cyclic array of elements 1 to n - 1 ; Store initial array into final array ; Fill the last row and column with 0 's ; Swap 0 and the number present at the current indexed row ; Also make changes in the last row with the number we swapped ; Print the final array ; Driver code
class GFG { static void solve ( long n ) { long initial_array [ ] [ ] = new long [ ( int ) n - 1 ] [ ( int ) n - 1 ] , final_array [ ] [ ] = new long [ ( int ) n ] [ ( int ) n ] ; for ( long i = 0 ; i < n - 1 ; ++ i ) initial_array [ 0 ] [ ( int ) i ] = i + 1 ; for ( long i = 1 ; i < n - 1 ; ++ i ) for ( long j = 0 ; j < n - 1 ; ++ j ) initial_array [ ( int ) i ] [ ( int ) j ] = initial_array [ ( int ) i - 1 ] [ ( int ) ( ( int ) j + 1 ) % ( ( int ) n - 1 ) ] ; for ( long i = 0 ; i < n - 1 ; ++ i ) for ( long j = 0 ; j < n - 1 ; ++ j ) final_array [ ( int ) i ] [ ( int ) j ] = initial_array [ ( int ) i ] [ ( int ) j ] ; for ( long i = 0 ; i < n ; ++ i ) final_array [ ( int ) i ] [ ( int ) n - 1 ] = final_array [ ( int ) n - 1 ] [ ( int ) i ] = 0 ; for ( long i = 0 ; i < n ; ++ i ) { long t0 = final_array [ ( int ) i ] [ ( int ) i ] ; long t1 = final_array [ ( int ) i ] [ ( int ) n - 1 ] ; long s = final_array [ ( int ) i ] [ ( int ) i ] ; final_array [ ( int ) i ] [ ( int ) i ] = final_array [ ( int ) i ] [ ( int ) n - 1 ] ; final_array [ ( int ) i ] [ ( int ) n - 1 ] = s ; final_array [ ( int ) n - 1 ] [ ( int ) i ] = t0 ; } for ( long i = 0 ; i < n ; ++ i ) { for ( long j = 0 ; j < n ; ++ j ) System . out . print ( final_array [ ( int ) i ] [ ( int ) j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String args [ ] ) { long n = 5 ; solve ( n ) ; } }
Generate original array from difference between every two consecutive elements | Java program to implement the above approach ; Function to print the required permutation ; Take x = 0 for simplicity ; Calculate aint the differences and store it in a vector ; Preserve the original array ; Check if aint the consecutive elements have difference = 1 ; If consecutive elements don 't have  difference 1 at any position then  the answer is impossible ; Else store the indices and values at those indices in a map and finainty print them ; Driver code
import java . util . * ; class GFG { static void findPerm ( int n , Vector < Integer > differences ) { Vector < Integer > ans = new Vector < Integer > ( ) ; ans . clear ( ) ; ans . add ( 0 ) ; int x = 0 ; for ( int i = 0 ; i <= n - 2 ; ++ i ) { int diff = differences . get ( i ) ; x = x + diff ; ans . add ( x ) ; } Vector < Integer > anss = new Vector < Integer > ( ) ; for ( Integer obj : ans ) anss . add ( obj ) ; Collections . sort ( ans ) ; int flag = - 1 ; for ( int i = 1 ; i <= n - 1 ; ++ i ) { int res = ans . get ( i ) - ans . get ( i - 1 ) ; if ( res != 1 ) { flag = 0 ; } } if ( flag == 0 ) { System . out . print ( - 1 ) ; return ; } else { Map < Integer , Integer > mpp = new HashMap < > ( ) ; mpp . clear ( ) ; int j = 1 ; Vector < Integer > value_at_index = new Vector < Integer > ( ) ; for ( Integer x1 : ans ) { mpp . put ( x1 , j ) ; ++ j ; } for ( Integer x2 : anss ) { value_at_index . add ( mpp . get ( x2 ) ) ; } for ( Integer x3 : value_at_index ) { System . out . print ( x3 + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { Vector < Integer > differences = new Vector < Integer > ( ) ; differences . add ( 2 ) ; differences . add ( - 3 ) ; differences . add ( 2 ) ; int n = differences . size ( ) + 1 ; findPerm ( n , differences ) ; } }
Find smallest number K such that K % p = 0 and q % K = 0 | Java implementation of the approach ; Function to return the minimum value K such that K % p = 0 and q % k = 0 ; If K is possible ; No such K is possible ; Driver code
import java . io . * ; class GFG { static int getMinVal ( int p , int q ) { if ( q % p == 0 ) return p ; return - 1 ; } public static void main ( String [ ] args ) { int p = 24 , q = 48 ; System . out . println ( getMinVal ( p , q ) ) ; } }
Count all Prime Length Palindromic Substrings | Java implementation of the approach ; Function that returns true if sub - string starting at i and ending at j in str is a palindrome ; Function to count all palindromic substring whose lwngth is a prime number ; 0 and 1 are non - primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; To store the required number of sub - strings ; Starting from the smallest prime till the largest length of the sub - string possible ; If j is prime ; Check all the sub - strings of length j ; If current sub - string is a palindrome ; Driver code
import java . util . Arrays ; class GfG { static boolean isPalindrome ( String str , int i , int j ) { while ( i < j ) { if ( str . charAt ( i ) != str . charAt ( j ) ) return false ; i ++ ; j -- ; } return true ; } static int countPrimePalindrome ( String str , int len ) { boolean [ ] prime = new boolean [ len + 1 ] ; Arrays . fill ( prime , true ) ; prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p * p <= len ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * p ; i <= len ; i += p ) prime [ i ] = false ; } } int count = 0 ; for ( int j = 2 ; j <= len ; j ++ ) { if ( prime [ j ] ) { for ( int i = 0 ; i + j - 1 < len ; i ++ ) { if ( isPalindrome ( str , i , i + j - 1 ) ) count ++ ; } } } return count ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; int len = s . length ( ) ; System . out . println ( countPrimePalindrome ( s , len ) ) ; } }
Minimum operations of the given type required to make a complete graph | Java implementation of the approach ; Function to return the minimum number of steps required ; Driver Code
class GFG { static int minOperations ( int N ) { double x = Math . log ( N ) / Math . log ( 2 ) ; int ans = ( int ) ( Math . ceil ( x ) ) ; return ans ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . println ( minOperations ( N ) ) ; } }
Greatest divisor which divides all natural number in range [ L , R ] | Java implementation of the approach ; Function to return the greatest divisor that divides all the natural numbers in the range [ l , r ] ; Driver Code
class GFG { static int find_greatest_divisor ( int l , int r ) { if ( l == r ) { return l ; } return 1 ; } public static void main ( String [ ] args ) { int l = 2 , r = 12 ; System . out . println ( find_greatest_divisor ( l , r ) ) ; } }
Probability of getting two consecutive heads after choosing a random coin among two different types of coins | Java program to get the probability of getting two consecutive heads ; Function to return the probability of getting two consecutive heads ; Formula derived from Bayes 's theorem ; Driver code ; given the probability of getting a head for both the coins
import java . io . * ; class GFG { static double getProbability ( double p , double q ) { p /= 100 ; q /= 100 ; double probability = ( p * p + q * q ) / ( p + q ) ; return probability ; } public static void main ( String [ ] args ) { double p , q ; p = 80 ; q = 40 ; System . out . println ( getProbability ( p , q ) ) ; } }
Check whether bitwise OR of N numbers is Even or Odd | Java implementation to check whether bitwise OR of n numbers is even or odd ; Function to check if bitwise OR of n numbers is even or odd ; if at least one odd number is found , then the bitwise OR of all numbers will be odd ; Bitwise OR is an odd number ; Driver Code
class GFG { static boolean check ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { return true ; } } return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 9 , 12 , 13 , 15 } ; int n = arr . length ; if ( check ( arr , n ) ) { System . out . println ( " Odd ▁ Bit - wise ▁ OR " ) ; } else { System . out . println ( " Even ▁ Bit - wise ▁ OR " ) ; } } }
Amortized analysis for increment in counter |
import java . util . * ; class GFG { public static void main ( String args [ ] ) { String st = "10010111" ; char [ ] str = st . toCharArray ( ) ; int lengthh = st . length ( ) ; int i = lengthh - 1 ; while ( str [ i ] == '1' ) { str [ i ] = '0' ; i -- ; } if ( i >= 0 ) str [ i ] = '1' ; System . out . print ( str ) ; } }
Time taken by Loop unrolling vs Normal loop | Java program to compare normal loops and loops with unrolling technique ; n is 8 lakhs ; t to note start time ; to store the sum ; Normal loop ; to mark end time ; to mark start time of unrolling ; Unrolling technique ( assuming that n is a multiple of 8 ) . ; to mark the end of loop
class GFG { public static void main ( String [ ] args ) { int n = 800000 ; double t = ( double ) System . nanoTime ( ) ; long sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += i ; t = ( double ) System . nanoTime ( ) - t ; System . out . println ( " sum ▁ is : ▁ " + Double . toString ( sum ) ) ; System . out . println ( " time ▁ taken ▁ by ▁ normal ▁ loops : " + Double . toString ( t / Math . pow ( 10.0 , 9.0 ) ) ) ; t = ( double ) System . nanoTime ( ) ; sum = 0 ; for ( int i = 1 ; i <= n ; i += 8 ) { sum += i ; sum += ( i + 1 ) ; sum += ( i + 2 ) ; sum += ( i + 3 ) ; sum += ( i + 4 ) ; sum += ( i + 5 ) ; sum += ( i + 6 ) ; sum += ( i + 7 ) ; } t = ( double ) System . nanoTime ( ) - t ; System . out . println ( " sum ▁ is : ▁ " + Double . toString ( sum ) ) ; System . out . println ( " time ▁ taken ▁ by ▁ normal ▁ loops : " + Double . toString ( t / Math . pow ( 10.0 , 9.0 ) ) ) ; } }
Iterated Logarithm log * ( n ) | Recursive Java program to find value of Iterated Logarithm ; Driver code
import java . io . * ; class GFG { static int _log ( double x , double base ) { return ( int ) ( Math . log ( x ) / Math . log ( base ) ) ; } static double recursiveLogStar ( double n , double b ) { if ( n > 1.0 ) return 1.0 + recursiveLogStar ( _log ( n , b ) , b ) ; else return 0 ; } public static void main ( String [ ] args ) { int n = 100 , base = 5 ; System . out . println ( " Log * ( " + n + " ) ▁ = ▁ " + recursiveLogStar ( n , base ) ) ; } }
Iterated Logarithm log * ( n ) | Iterative Java function to find value of Iterated Logarithm
public static int iterativeLogStar ( double n , double b ) { int count = 0 ; while ( n >= 1 ) { n = _log ( n , b ) ; count ++ ; } return count ; }
Tail Recursion | An example of tail recursive function ; The last executed statement is recursive call
static void print ( int n ) { if ( n < 0 ) return ; System . out . print ( " ▁ " + n ) ; print ( n - 1 ) ; }
Find the maximum among the count of positive or negative integers in the array | Java program for the above approach ; Function to find the maximum of the count of positive or negative elements ; Initialize the pointers ; Find the value of mid ; If element is negative then ignore the left half ; If element is positive then ignore the right half ; Return maximum among the count of positive & negative element ; Driver Code
import java . io . * ; public class GFG { static int findMaximum ( int arr [ ] , int size ) { int i = 0 , j = size - 1 , mid ; while ( i <= j ) { mid = i + ( j - i ) / 2 ; if ( arr [ mid ] < 0 ) i = mid + 1 ; else if ( arr [ mid ] > 0 ) j = mid - 1 ; } return Math . max ( i , size - i ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 9 , - 7 , - 4 , 1 , 5 , 8 , 9 } ; int N = arr . length ; System . out . println ( findMaximum ( arr , N ) ) ; } }
Maximize Kth largest element after splitting the given Array at most C times | Java program for the above approach ; Function to find the K - th maximum element after upto C operations ; Check for the base case ; Stores the count iterations of BS ; Create the left and right bounds of binary search ; Perform binary search ; Find the value of mid ; Traverse the array ; Update the ranges ; Return the maximum value obtained ; Driver Code
import java . io . * ; class GFG { static double maxKth ( int arr [ ] , int N , int C , int K ) { if ( N + C < K ) { return - 1 ; } int iter = 300 ; double l = 0 , r = 1000000000.0 ; while ( iter -- > 0 ) { double mid = ( l + r ) * 0.5 ; double a = 0 ; double b = 0 ; for ( int i = 0 ; i < N ; i ++ ) { a += ( int ) ( ( double ) arr [ i ] / mid ) ; if ( ( double ) arr [ i ] >= mid ) { b ++ ; } } if ( a >= K && b + C >= K ) { l = mid ; } else { r = mid ; } } return l ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 8 } ; int K = 1 , C = 1 ; int N = arr . length ; System . out . println ( maxKth ( arr , N , C , K ) ) ; } }
Maximize the minimum difference between any element pair by selecting K elements from given Array | Java implementation for the above approach ; To check if selection of K elements is possible such that difference between them is greater than dif ; Selecting first element in the sorted array ; prev is the previously selected element initially at index 0 as first element is already selected ; Check if selection of K - 1 elements from array with a minimum difference of dif is possible ; If the current element is atleast dif difference apart from the previously selected element then select the current element and increase the count ; If selection of K elements with a min difference of dif is possible then return true ; Prev will become current element for the next iteration ; If selection of K elements with minimum difference of dif is not possible then return false ; Minimum largest difference possible is 1 ; Check if selection of K elements is possible with a minimum difference of dif ; If dif is greater than previous ans we update ans ; Continue to search for better answer . Try finding greater dif ; K elements cannot be selected ; Driver code ; arr should be in a sorted order
import java . io . * ; import java . util . Arrays ; class GFG { static boolean isPossibleToSelect ( int [ ] arr , int N , int dif , int K ) { int count = 1 ; int prev = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] >= ( prev + dif ) ) { count ++ ; if ( count == K ) return true ; prev = arr [ i ] ; } } return false ; } static int binarySearch ( int [ ] arr , int left , int right , int K , int N ) { int ans = 1 ; while ( left <= right ) { int dif = left + ( right - left ) / 2 ; if ( isPossibleToSelect ( arr , N , dif , K ) ) { ans = Math . max ( ans , dif ) ; left = dif + 1 ; } else right = dif - 1 ; } return ans ; } public static void main ( String [ ] args ) { int N , K ; N = 7 ; K = 4 ; int [ ] arr = { 1 , 4 , 9 , 0 , 2 , 13 , 3 } ; Arrays . sort ( arr ) ; System . out . println ( binarySearch ( arr , 0 , arr [ N - 1 ] , K , N ) ) ; } }
Find maximum height to cut all chocolates horizontally such that at least K amount remains | Java program for the above approach ; Function to find the sum of remaining chocolate after making the horizontal cut at height mid ; Stores the sum of chocolates ; Traverse the array arr [ ] ; If the height is at least mid ; Return the possible sum ; Function to find the maximum horizontal cut made to all the chocolates such that the sum of the remaining element is at least K ; Ranges of Binary Search ; Perform the Binary Search ; Find the sum of removed after making cut at height mid ; If the chocolate removed is same as the chocolate needed then return the height ; If the chocolate removed is less than chocolate needed then shift to the left range ; Otherwise , shift to the right range ; Return the possible cut ; Driver Code
import java . util . * ; import java . util . Arrays ; class GFG { static int cal ( int arr [ ] , int mid ) { int chocolate = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] >= mid ) chocolate += arr [ i ] - mid ; } return chocolate ; } static int maximumCut ( int arr [ ] , int K ) { int low = 0 ; int high = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int chocolate = cal ( arr , mid ) ; if ( chocolate == K ) return mid ; else if ( chocolate < K ) high = mid - 1 ; else { low = mid + 1 ; if ( mid > high ) high = mid ; } } return high ; } public static void main ( String [ ] args ) { int K = 7 ; int arr [ ] = { 15 , 20 , 8 , 17 } ; System . out . println ( maximumCut ( arr , K ) ) ; } }