codeparrot/xlcost-text-to-code huggingface dataset:
Split N as the sum of K numbers satisfying the given conditions | Java Program to implement the above approach ; Vector to store prime numbers ; Function to generate prime numbers using SieveOfEratosthenes ; Boolean array to store primes ; If p is a prime ; Mark all its multiples as non - prime ; Print all prime numbers ; Function to generate n as the sum of k numbers where atleast K - 1 are distinct and are product of 2 primes ; Stores the product of every pair of prime number ; Sort the products ; If sum exceeds n ; Otherwise , print the k required numbers ; Driver Code
import java . util . * ; class GFG { static Vector < Integer > primes = new Vector < > ( ) ; static void SieveOfEratosthenes ( ) { boolean [ ] prime = new boolean [ 10005 ] ; Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p <= 1000 ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= 1000 ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= 1000 ; p ++ ) if ( prime [ p ] ) primes . add ( p ) ; } static void generate ( int n , int k ) { Vector < Integer > prod = new Vector < > ( ) ; SieveOfEratosthenes ( ) ; int l = primes . size ( ) ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = i + 1 ; j < l ; j ++ ) { if ( primes . get ( i ) * primes . get ( j ) > 0 ) prod . add ( primes . get ( i ) * primes . get ( j ) ) ; } } Collections . sort ( prod ) ; int sum = 0 ; for ( int i = 0 ; i < k - 1 ; i ++ ) sum += prod . get ( i ) ; if ( sum > n ) System . out . print ( " - 1" ) ; else { for ( int i = 0 ; i < k - 1 ; i ++ ) { System . out . print ( prod . get ( i ) + " , ▁ " ) ; } System . out . print ( n - sum + "NEW_LINE"); } } public static void main ( String [ ] args ) { int n = 52 , k = 5 ; generate ( n , k ) ; } }
Maximum modified Array sum possible by choosing elements as per the given conditions | Java program for the above approach ; Function that finds the maximum sum of the array elements according to the given condition ; Sort the array ; Take the max value from the array ; Iterate from the end of the array ; Check for the number had come before or not ; Take the value which is not occured already ; Change max to new value ; Return the maximum sum ; Driver Code ; Given array arr [ ] ; Function Call
import java . util . * ; class GFG { static int findMaxValue ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int ans = arr [ n - 1 ] ; int maxPossible = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; -- i ) { if ( maxPossible > 0 ) { if ( arr [ i ] >= maxPossible ) { ans += ( maxPossible - 1 ) ; maxPossible = maxPossible - 1 ; } else { maxPossible = arr [ i ] ; ans += maxPossible ; } } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 4 , 1 , 5 } ; int n = arr . length ; System . out . print ( findMaxValue ( arr , n ) ) ; } }
Minimum cost to sort an Array such that swapping X and Y costs XY | Java program to implement the above approach ; Function returns the minimum cost to sort the given array ; Create array of pairs in which 1 st element is the array element and 2 nd element is index of first ; Initialize the total cost ; Sort the array with respect to array value ; Initialize the overall minimum which is the 1 st element ; To keep track of visited elements create a visited array & initialize all elements as not visited ; Iterate over every element of the array ; If the element is visited or in the sorted position , and check for next element ; Create a vector which stores all elements of a cycle ; It covers all the elements of a cycle ; If cycle is found then the swapping is required ; Initialize local minimum with 1 st element of the vector as it contains the smallest element in the beginning ; Stores the cost with using only local minimum value . ; Stores the cost of using both local minimum and overall minimum ; Update the result2 ; Store the minimum of the two result to total cost ; Return the minimum cost ; Driver code ; Given array arr [ ] ; Function call
import java . util . * ; class GFG { static int minCost ( int arr [ ] , int n ) { int [ ] [ ] sorted = new int [ n ] [ 2 ] ; int total_cost = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sorted [ i ] [ 0 ] = arr [ i ] ; sorted [ i ] [ 1 ] = i ; } Arrays . sort ( sorted , ( a , b ) -> a [ 0 ] - b [ 0 ] ) ; int overall_minimum = sorted [ 0 ] [ 0 ] ; boolean [ ] vis = new boolean [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( vis [ i ] && sorted [ i ] [ 1 ] == i ) continue ; ArrayList < Integer > v = new ArrayList < > ( ) ; int j = i ; while ( ! vis [ j ] ) { vis [ j ] = true ; v . add ( sorted [ j ] [ 0 ] ) ; j = sorted [ j ] [ 1 ] ; } if ( v . size ( ) > 0 ) { int local_minimum = v . get ( 0 ) , result1 = 0 , result2 = 0 ; for ( int k = 1 ; k < v . size ( ) ; k ++ ) result1 += ( local_minimum * v . get ( k ) ) ; for ( int k = 0 ; k < v . size ( ) ; k ++ ) result2 += ( overall_minimum * v . get ( k ) ) ; result2 += ( overall_minimum * local_minimum ) ; total_cost += Math . min ( result1 , result2 ) ; } } return total_cost ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 8 , 9 , 7 , 6 } ; int n = arr . length ; System . out . print ( minCost ( arr , n ) ) ; } }
Maximize the last Array element as per the given conditions | Java program to implement the above approach ; Function to find the maximum possible value that can be placed at the last index ; Sort the array elements in ascending order ; If the first element is is not equal to 1 ; Traverse the array to make difference between adjacent elements <= 1 ; Driver Code
import java . io . * ; import java . util . * ; class GFG { public static int maximizeFinalElement ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; if ( arr [ 0 ] != 1 ) arr [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] > 1 ) { arr [ i ] = arr [ i - 1 ] + 1 ; } } return arr [ n - 1 ] ; } public static void main ( String [ ] args ) { int n = 4 ; int arr [ ] = { 3 , 1 , 3 , 4 } ; int max = maximizeFinalElement ( arr , n ) ; System . out . print ( max ) ; } }
Find the point on X | Java program to implement the above approach ; Function to find median of the array ; Sort the given array ; If number of elements are even ; Return the first median ; Otherwise ; Driver Code
import java . util . * ; class GFG { static int findLeastDist ( int A [ ] , int N ) { Arrays . sort ( A ) ; if ( N % 2 == 0 ) { return A [ ( N - 1 ) / 2 ] ; } else { return A [ N / 2 ] ; } } public static void main ( String [ ] args ) { int A [ ] = { 4 , 1 , 5 , 10 , 2 } ; int N = A . length ; System . out . print ( " ( " + findLeastDist ( A , N ) + " , ▁ " + 0 + " ) " ) ; } }
Maximum sum of any submatrix of a Matrix which is sorted row | Java program for the above approach ; Function that finds the maximum Sub - Matrix Sum ; Number of rows in the matrix ; Number of columns in the matrix ; dp [ ] [ ] matrix to store the results of each iteration ; Base Case - The largest element in the matrix ; To stores the final result ; Find the max sub matrix sum for the last row ; Check whether the current sub - array yields maximum sum ; Calculate the max sub matrix sum for the last column ; Check whether the current sub - array yields maximum sum ; Build the dp [ ] [ ] matrix from bottom to the top row ; Update sum at each cell in dp [ ] [ ] ; Update the maximum sum ; Return the maximum sum ; Driver Code ; Given matrix mat [ ] [ ] ; Function Call
class GFG { static int maxSubMatSum ( int [ ] [ ] mat ) { int n = mat . length ; int m = mat [ 0 ] . length ; int i , j ; int [ ] [ ] dp = new int [ n ] [ m ] ; dp [ n - 1 ] [ m - 1 ] = mat [ n - 1 ] [ m - 1 ] ; int res = dp [ n - 1 ] [ m - 1 ] ; for ( i = m - 2 ; i >= 0 ; i -- ) { dp [ n - 1 ] [ i ] = mat [ n - 1 ] [ i ] + dp [ n - 1 ] [ i + 1 ] ; res = Math . max ( res , dp [ n - 1 ] [ i ] ) ; } for ( i = n - 2 ; i >= 0 ; i -- ) { dp [ i ] [ m - 1 ] = mat [ i ] [ m - 1 ] + dp [ i + 1 ] [ m - 1 ] ; res = Math . max ( res , dp [ i ] [ m - 1 ] ) ; } for ( i = n - 2 ; i >= 0 ; i -- ) { for ( j = m - 2 ; j >= 0 ; j -- ) { dp [ i ] [ j ] = mat [ i ] [ j ] + dp [ i ] [ j + 1 ] + dp [ i + 1 ] [ j ] - dp [ i + 1 ] [ j + 1 ] ; res = Math . max ( res , dp [ i ] [ j ] ) ; } } return res ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { - 6 , - 4 , - 1 } , { - 3 , 2 , 4 } , { 2 , 5 , 8 } } ; System . out . print ( maxSubMatSum ( mat ) ) ; } }
Arrange the numbers in the Array as per given inequalities | Java program for the above approach ; Function to place the integers in between the inequality signs ; Sort the integers array and set the index of smallest and largest element ; Iterate over the inequalities ; Append the necessary integers per symbol ; Add the final integer ; Return the answer ; Driver Code ; Given List of Integers ; Given list of inequalities ; Function Call ; Print the output
import java . util . Arrays ; public class PlacingNumbers { static String formAnInequality ( int [ ] integers , char [ ] inequalities ) { Arrays . sort ( integers ) ; int lowerIndex = 0 ; int higherIndex = integers . length - 1 ; StringBuilder sb = new StringBuilder ( ) ; for ( char ch : inequalities ) { if ( ch == ' < ' ) { sb . append ( " ▁ " + integers [ lowerIndex ++ ] + " ▁ " + ch ) ; } else { sb . append ( " ▁ " + integers [ higherIndex -- ] + " ▁ " + ch ) ; } } sb . append ( " ▁ " + integers [ lowerIndex ] ) ; return sb . toString ( ) ; } public static void main ( String [ ] args ) { int [ ] integers = { 2 , 5 , 1 , 0 } ; char [ ] inequalities = { ' < ' , ' > ' , ' < ' } ; String output = formAnInequality ( integers , inequalities ) ; System . out . println ( output ) ; } }
Check if two arrays can be made equal by reversing subarrays multiple times | Java implementation to check if two arrays can be made equal ; Function to check if array B can be made equal to array A ; Sort both the arrays ; Check if both the arrays are equal or not ; Driver code
import java . util . * ; class GFG { public static boolean canMadeEqual ( int [ ] A , int [ ] B , int n ) { Arrays . sort ( A ) ; Arrays . sort ( B ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] != B [ i ] ) { return false ; } } return true ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 } ; int B [ ] = { 1 , 3 , 2 } ; int n = A . length ; if ( canMadeEqual ( A , B , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Minimize difference between maximum and minimum of Array by at most K replacements | Java program of the approach ; Function to find minimum difference between the maximum and the minimum elements arr [ ] by at most K replacements ; Check if turns are more than or equal to n - 1 then simply return zero ; Sort the array ; Set difference as the maximum possible difference ; Iterate over the array to track the minimum difference in k turns ; Return the answer ; Driver Code ; Given array arr [ ] ; Given K replacements ; Function Call
import java . io . * ; import java . util . Arrays ; class GFG { static int maxMinDifference ( int arr [ ] , int n , int k ) { if ( k >= n - 1 ) return 0 ; Arrays . sort ( arr ) ; int ans = arr [ n - 1 ] - arr [ 0 ] ; for ( int i = k , j = n - 1 ; i >= 0 ; -- i , -- j ) { ans = Math . min ( arr [ j ] - arr [ i ] , ans ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 6 , 11 , 15 } ; int N = arr . length ; int K = 3 ; System . out . print ( maxMinDifference ( arr , N , K ) ) ; } }
Maximum possible Array sum after performing given operations | Java implementation to find the maximum possible sum of array after performing given operations ; Function to get maximum sum after q operations ; priority queue to get maximum sum ; Push pair , value and 1 in the priority queue ; Push pair , value ( to be replaced ) and number of elements ( to be replaced ) ; Add top n elements from the priority queue to get max sum ; pr is the pair pr . first is the value and pr . second is the occurrence ; pop from the priority queue ; Add value to answer ; Update n ; Driver Code
import java . util . * ; import java . lang . * ; class GFG { static class pair { int first , second ; pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void max_sum ( int ar [ ] , int n , int q , int x [ ] , int y [ ] ) { int ans = 0 , i ; PriorityQueue < pair > pq = new PriorityQueue < > ( ( a , b ) -> Integer . compare ( a . second , b . second ) ) ; for ( i = 0 ; i < n ; i ++ ) pq . add ( new pair ( ar [ i ] , 1 ) ) ; for ( i = 0 ; i < q ; i ++ ) pq . add ( new pair ( y [ i ] , x [ i ] ) ) ; while ( n > 0 ) { pair pr = pq . peek ( ) ; pq . poll ( ) ; ans += pr . first * Math . min ( n , pr . second ) ; n -= pr . second ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int ar [ ] = { 200 , 100 , 200 , 300 } ; int n = ar . length ; int q = 2 ; int x [ ] = { 2 , 3 } ; int y [ ] = { 100 , 90 } ; max_sum ( ar , n , q , x , y ) ; } }
Check if a decreasing Array can be sorted using Triple cyclic shift | Java program for the above approach ; If array is 3 2 1 can ' t ▁ ▁ be ▁ sorted ▁ because ▁ 2 ▁ is ▁ in ▁ ▁ its ▁ correct ▁ position , ▁ 1 ▁ ▁ and ▁ 3 ▁ can ' t shift right because cyclic right shift takes place between 3 elements ; Check if its possible to sort ; Number of swap is N / 2 always for this approach ; Printing index of the cyclic right shift ; Driver code
class GFG { static void sortarray ( int arr [ ] , int N ) { if ( N == 3 ) System . out . println ( " NO " ) ; else if ( N % 4 == 0 N % 4 == 1 ) { System . out . println ( " YES " ) ; System . out . println ( N / 2 ) ; int k = 1 , l ; for ( l = 0 ; l < ( N / 4 ) ; l ++ ) { System . out . println ( k + " ▁ " + ( k + 1 ) + " ▁ " + N ) ; System . out . println ( k + 1 + " ▁ " + N + " ▁ " + ( N - 1 ) ) ; k = k + 2 ; N = N - 2 ; } } else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int N = 5 ; int arr [ ] = { 5 , 4 , 3 , 2 , 1 } ; sortarray ( arr , N ) ; } }
Find least non | Java program to find the least non - overlapping number from a given set intervals ; function to find the smallest non - overlapping number ; create a visited array ; find the first missing value ; Driver function
class GFG { static int MAX = ( int ) ( 1e5 + 5 ) ; static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void find_missing ( pair [ ] interval ) { int [ ] vis = new int [ MAX ] ; for ( int i = 0 ; i < interval . length ; ++ i ) { int start = interval [ i ] . first ; int end = interval [ i ] . second ; vis [ start ] ++ ; vis [ end + 1 ] -- ; } for ( int i = 1 ; i < MAX ; i ++ ) { vis [ i ] += vis [ i - 1 ] ; if ( vis [ i ] == 0 ) { System . out . print ( i + "NEW_LINE"); return ; } } } public static void main ( String [ ] args ) { pair [ ] interval = { new pair ( 0 , 14 ) , new pair ( 86 , 108 ) , new pair ( 22 , 30 ) , new pair ( 5 , 17 ) } ; find_missing ( interval ) ; } }
Find least non | Java program to find the least non - overlapping number from a given set intervals ; Function to find the smallest non - overlapping number ; Sort the intervals based on their starting value ; Check if any missing value exist ; Finally print the missing value ; Driver code
import java . util . * ; import java . io . * ; class GFG { static class Pair implements Comparable < Pair > { int start , end ; Pair ( int s , int e ) { start = s ; end = e ; } public int compareTo ( Pair p ) { return this . start - p . start ; } } static void findMissing ( ArrayList < Pair > interval ) { Collections . sort ( interval ) ; int mx = 0 ; for ( int i = 0 ; i < interval . size ( ) ; ++ i ) { if ( interval . get ( i ) . start > mx ) { System . out . println ( mx ) ; return ; } else mx = Math . max ( mx , interval . get ( i ) . end + 1 ) ; } System . out . println ( mx ) ; } public static void main ( String [ ] args ) { ArrayList < Pair > interval = new ArrayList < > ( ) ; interval . add ( new Pair ( 0 , 14 ) ) ; interval . add ( new Pair ( 86 , 108 ) ) ; interval . add ( new Pair ( 22 , 30 ) ) ; interval . add ( new Pair ( 5 , 17 ) ) ; findMissing ( interval ) ; } }
Maximum bitwise OR value of subsequence of length K | Java program for the above approach ; Function to convert bit array to decimal number ; Return the final result ; Function to find the maximum Bitwise OR value of subsequence of length K ; Initialize bit array of size 32 with all value as 0 ; Iterate for each index of bit [ ] array from 31 to 0 , and check if the ith value of bit array is 0 ; Check for maximum contributing element ; Update the bit array if max_contributing element is found ; Decrement the value of K ; Return the result ; Driver Code ; Given array arr [ ] ; Length of subsequence ; Function call
class GFG { static int build_num ( int [ ] bit ) { int ans = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) if ( bit [ i ] == 1 ) ans += ( 1 << i ) ; ans += 32 ; return ans ; } static int maximumOR ( int [ ] arr , int n , int k ) { int bit [ ] = new int [ 32 ] ; for ( int i = 31 ; i >= 0 ; i -- ) { if ( bit [ i ] == 0 && k > 0 ) { int temp = build_num ( bit ) ; int temp1 = temp ; int val = - 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( temp1 < ( temp arr [ j ] ) ) { temp1 = temp | arr [ j ] ; val = arr [ j ] ; } } if ( val != - 1 ) { k -- ; for ( int j = 0 ; j < 32 ; j ++ ) { bit [ j ] ++ ; } } } } return build_num ( bit ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 9 , 7 , 19 } ; int k = 3 ; int n = arr . length ; System . out . println ( maximumOR ( arr , n , k ) ) ; } }
Lexicographically smallest string after M operations | Java implementation to find the lexicographical smallest String after performing M operations ; Function to find the lexicographical smallest String after performing M operations ; Size of the given String ; Declare an array a ; For each i , a [ i ] contain number of operations to update s [ i ] to ' a ' ; Check if m >= ar [ i ] , then update s [ i ] to ' a ' decrement k by a [ i ] ; Form a cycle of 26 ; update last element of String with the value s [ i ] + ( k % 26 ) ; Return the answer ; Driver code
class GFG { static void smallest_String ( char [ ] s , int m ) { int n = s . length ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int distance = s [ i ] - ' a ' ; if ( distance == 0 ) a [ i ] = 0 ; else a [ i ] = 26 - distance ; } for ( int i = 0 ; i < n ; i ++ ) { if ( m >= a [ i ] ) { s [ i ] = ' a ' ; m = m - a [ i ] ; } } m = m % 26 ; s [ n - 1 ] = ( char ) ( s [ n - 1 ] + m ) ; System . out . print ( String . valueOf ( s ) ) ; } public static void main ( String [ ] args ) { String str = " aazzx " ; int m = 6 ; smallest_String ( str . toCharArray ( ) , m ) ; } }
Maximize jobs that can be completed under given constraint | Java Program to implement the above approach ; Function to find maxiumum number of jobs ; Min Heap ; Sort ranges by start day ; Stores the minimum and maximum day in the ranges ; Iterating from min_day to max_day ; Insert the end day of the jobs which can be completed on i - th day in a priority queue ; Pop all jobs whose end day is less than current day ; If queue is empty , no job can be completed on the i - th day ; Increment the count of jobs completed ; Pop the job with least end day ; Return the jobs on the last day ; Driver code
import java . io . * ; import java . util . * ; class GFG { static int find_maximum_jobs ( int N , ArrayList < ArrayList < Integer > > ranges ) { ArrayList < Integer > queue = new ArrayList < Integer > ( ) ; Collections . sort ( ranges , new Comparator < ArrayList < Integer > > ( ) { @ Override public int compare ( ArrayList < Integer > o1 , ArrayList < Integer > o2 ) { return o1 . get ( 0 ) . compareTo ( o2 . get ( 0 ) ) ; } } ) ; int min_day = ranges . get ( 0 ) . get ( 0 ) ; int max_day = 0 ; for ( int i = 0 ; i < N ; i ++ ) max_day = Math . max ( max_day , ranges . get ( i ) . get ( 1 ) ) ; int index = 0 , count_jobs = 0 ; for ( int i = min_day ; i <= max_day ; i ++ ) { while ( index < ranges . size ( ) && ranges . get ( index ) . get ( 0 ) <= i ) { queue . add ( ranges . get ( index ) . get ( 1 ) ) ; index ++ ; } Collections . sort ( queue ) ; while ( queue . size ( ) > 0 && queue . get ( 0 ) < i ) queue . remove ( 0 ) ; if ( queue . size ( ) == 0 ) continue ; count_jobs ++ ; queue . remove ( 0 ) ; } return count_jobs ; } public static void main ( String [ ] args ) { int N = 5 ; ArrayList < ArrayList < Integer > > ranges = new ArrayList < ArrayList < Integer > > ( ) ; ranges . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 5 ) ) ) ; ranges . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 5 ) ) ) ; ranges . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 5 ) ) ) ; ranges . add ( new ArrayList < Integer > ( Arrays . asList ( 2 , 3 ) ) ) ; ranges . add ( new ArrayList < Integer > ( Arrays . asList ( 2 , 3 ) ) ) ; System . out . println ( find_maximum_jobs ( N , ranges ) ) ; } }
Minimize number of boxes by putting small box inside bigger one | Java implementation to minimize the number of the box by putting small box inside the bigger one ; Function to minimize the count ; Initial number of box ; Sort array of box size in increasing order ; Check is current box size is smaller than next box size ; Decrement box count Increment current box count Increment next box count ; Check if both box have same size ; Print the result ; Driver code
import java . util . Arrays ; class GFG { public static void minBox ( int arr [ ] , int n ) { int box = n ; Arrays . sort ( arr ) ; int curr_box = 0 , next_box = 1 ; while ( curr_box < n && next_box < n ) { if ( arr [ curr_box ] < arr [ next_box ] ) { box -- ; curr_box ++ ; next_box ++ ; } else if ( arr [ curr_box ] == arr [ next_box ] ) next_box ++ ; } System . out . println ( box ) ; } public static void main ( String args [ ] ) { int [ ] size = { 1 , 2 , 3 } ; int n = size . length ; minBox ( size , n ) ; } }
Count of minimum reductions required to get the required sum K | Java program to find the count of minimum reductions required to get the required sum K ; Function to return the count of minimum reductions ; If the sum is already less than K ; Sort in non - increasing order of difference ; Driver code ; Function Call
import java . util . * ; class GFG { static int countReductions ( ArrayList < int [ ] > v , int K ) { int sum = 0 ; for ( int [ ] i : v ) { sum += i [ 0 ] ; } if ( sum <= K ) { return 0 ; } Collections . sort ( v , ( a , b ) -> Math . abs ( b [ 0 ] - b [ 1 ] ) - Math . abs ( a [ 0 ] - a [ 1 ] ) ) ; int i = 0 ; while ( sum > K && i < v . size ( ) ) { sum -= ( v . get ( i ) [ 0 ] - v . get ( i ) [ 1 ] ) ; i ++ ; } if ( sum <= K ) return i ; return - 1 ; } public static void main ( String [ ] args ) { int N = 4 , K = 25 ; ArrayList < int [ ] > v = new ArrayList < > ( ) ; v . add ( new int [ ] { 10 , 5 } ) ; v . add ( new int [ ] { 20 , 9 } ) ; v . add ( new int [ ] { 12 , 10 } ) ; v . add ( new int [ ] { 4 , 2 } ) ; System . out . println ( countReductions ( v , K ) ) ; } }