codeparrot/xlcost-text-to-code huggingface dataset:
Minimum operations required to make every element greater than or equal to K | Java implementation of above approach ; Function to calculate gcd of two numbers ; Java function to get minimum operation needed ; The priority queue holds a minimum element in the top position ; push value one by one from the given array ; store count of minimum operation needed ; All elements are now >= k ; It is impossible to make as there are no sufficient elements available ; Take two smallest elements and replace them by their LCM first smallest element ; Second smallest element ; Increment the count ; Driver code
import java . util . PriorityQueue ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int FindMinOperation ( int [ ] a , int n , int k ) { PriorityQueue < Integer > Q = new PriorityQueue < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) Q . add ( a [ i ] ) ; int ans = 0 ; while ( true ) { if ( Q . peek ( ) >= k ) break ; if ( Q . size ( ) < 2 ) return - 1 ; int x = Q . peek ( ) ; Q . poll ( ) ; int y = Q . peek ( ) ; Q . poll ( ) ; int z = ( x * y ) / gcd ( x , y ) ; Q . add ( z ) ; ans ++ ; } return ans ; } public static void main ( String [ ] args ) { int [ ] a = { 3 , 5 , 7 , 6 , 8 } ; int k = 8 ; int n = a . length ; System . out . println ( FindMinOperation ( a , n , k ) ) ; } }
Check if it is possible to make two matrices strictly increasing by swapping corresponding values only | Java implementation of the above approach ; Function to check whether the matrices can be made strictly increasing with the given operation ; Swap only when a [ i ] [ j ] > b [ i ] [ j ] ; Check if rows are strictly increasing ; Check if columns are strictly increasing ; Driver code
using System ; using System . Collections ; class GfG { class GFG { public static String Check ( int a [ ] [ ] , int b [ ] [ ] , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( a [ i ] [ j ] > b [ i ] [ j ] ) { int temp = a [ i ] [ j ] ; a [ i ] [ j ] = b [ i ] [ j ] ; b [ i ] [ j ] = temp ; } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m - 1 ; j ++ ) if ( a [ i ] [ j ] >= a [ i ] [ j + 1 ] b [ i ] [ j ] >= b [ i ] [ j + 1 ] ) return " No " ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( a [ i ] [ j ] >= a [ i + 1 ] [ j ] b [ i ] [ j ] >= b [ i + 1 ] [ j ] ) return " No " ; return " Yes " ; } public static void main ( String [ ] args ) { int n = 2 , m = 2 ; int a [ ] [ ] = { { 2 , 10 } , { 11 , 5 } } ; int b [ ] [ ] = { { 9 , 4 } , { 3 , 12 } } ; System . out . print ( Check ( a , b , n , m ) ) ; } }
Find the lexicographically smallest string which satisfies the given condition | Java implementation of the above approach ; Function to return the required string ; First character will always be ' a ' ; To store the resultant string ; Since length of the string should be greater than 0 and first element of array should be 1 ; Check one by one all element of given prefix array ; If the difference between any two consecutive elements of the prefix array is greater than 1 then there will be no such string possible that satisfies the given array Also , string cannot have more than 26 distinct characters ; If difference is 0 then the ( i + 1 ) th character will be same as the ith character ; If difference is 1 then the ( i + 1 ) th character will be different from the ith character ; Return the resultant string ; Driver code
class GFG { static String smallestString ( int N , int [ ] A ) { char ch = ' a ' ; String S = " " ; if ( N < 1 A [ 0 ] != 1 ) { S = " - 1" ; return S ; } S += ch ; ch ++ ; for ( int i = 1 ; i < N ; i ++ ) { int diff = A [ i ] - A [ i - 1 ] ; if ( diff > 1 diff < 0 A [ i ] > 26 ) { S = " - 1" ; return S ; } else if ( diff == 0 ) S += ' a ' ; else { S += ch ; ch ++ ; } } return S ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 1 , 2 , 3 , 3 } ; int n = arr . length ; System . out . println ( smallestString ( n , arr ) ) ; } }
Count of alphabets whose ASCII values can be formed with the digits of N | Java implementation of the approach ; Function that returns true if num can be formed with the digits in digits [ ] array ; Copy of the digits array ; Get last digit ; If digit array doesn 't contain current digit ; One occurrence is used ; Remove the last digit ; Function to return the count of required alphabets ; To store the occurrences of digits ( 0 - 9 ) ; Get last digit ; Update the occurrence of the digit ; Remove the last digit ; If any lowercase character can be picked from the current digits ; If any uppercase character can be picked from the current digits ; Return the required count of alphabets ; Driver code
class GFG { static boolean canBePicked ( int digits [ ] , int num ) { int copyDigits [ ] = digits . clone ( ) ; while ( num > 0 ) { int digit = num % 10 ; if ( copyDigits [ digit ] == 0 ) return false ; else copyDigits [ digit ] -- ; num /= 10 ; } return true ; } static int countAlphabets ( int n ) { int i , count = 0 ; int digits [ ] = new int [ 10 ] ; while ( n > 0 ) { int digit = n % 10 ; digits [ digit ] ++ ; n /= 10 ; } for ( i = ' a ' ; i <= ' z ' ; i ++ ) if ( canBePicked ( digits , i ) ) count ++ ; for ( i = ' A ' ; i <= ' Z ' ; i ++ ) if ( canBePicked ( digits , i ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { int n = 1623455078 ; System . out . println ( countAlphabets ( n ) ) ; } }
Largest number less than X having at most K set bits | Java implementation of the approach ; Function to return the greatest number <= X having at most K set bits . ; Remove rightmost set bits one by one until we count becomes k ; Return the required number ; Driver code
import java . io . * ; class GFG { int greatestKBits ( int X , int K ) { int set_bit_count = Integer . bitCount ( X ) ; if ( set_bit_count <= K ) return X ; int diff = set_bit_count - K ; for ( int i = 0 ; i < diff ; i ++ ) X &= ( X - 1 ) ; return X ; } public static void main ( String [ ] args ) { int X = 21 , K = 2 ; GFG g = new GFG ( ) ; System . out . print ( g . greatestKBits ( X , K ) ) ; } }
Find two numbers whose sum and GCD are given | Java program to find two numbers whose sum and GCD is given ; function to find gcd of two numbers ; Function to find two numbers whose sum and gcd is given ; sum != gcd checks that both the numbers are positive or not ; Driver code
import java . util . * ; class Solution { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static void findTwoNumbers ( int sum , int gcd ) { if ( __gcd ( gcd , sum - gcd ) == gcd && sum != gcd ) System . out . println ( " a ▁ = ▁ " + Math . min ( gcd , sum - gcd ) + " , ▁ b ▁ = ▁ " + ( int ) ( sum - Math . min ( gcd , sum - gcd ) ) ) ; else System . out . println ( - 1 ) ; } public static void main ( String args [ ] ) { int sum = 8 ; int gcd = 2 ; findTwoNumbers ( sum , gcd ) ; } }
Find maximum distance between any city and station | Java program to calculate the maximum distance between any city and its nearest station ; Function to calculate the maximum distance between any city and its nearest station ; Initialize boolean list ; Assign True to cities containing station ; Driver code
import java . util . * ; class GFG { static int findMaxDistance ( int numOfCities , int station [ ] , int n ) { boolean hasStation [ ] = new boolean [ numOfCities + 1 ] ; for ( int city = 0 ; city < n ; city ++ ) { hasStation [ station [ city ] ] = true ; } int dist = 0 ; int maxDist = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { maxDist = Math . min ( station [ i ] , maxDist ) ; } for ( int city = 0 ; city < numOfCities ; city ++ ) { if ( hasStation [ city ] == true ) { maxDist = Math . max ( ( dist + 1 ) / 2 , maxDist ) ; dist = 0 ; } else dist += 1 ; } return Math . max ( maxDist , dist ) ; } public static void main ( String args [ ] ) { int numOfCities = 6 ; int station [ ] = { 3 , 1 } ; int n = station . length ; System . out . println ( " Max ▁ Distance : " + findMaxDistance ( numOfCities , station , n ) ) ; } }
Split the number into N parts such that difference between the smallest and the largest part is minimum | Java implementation of the approach ; Function that prints the required sequence ; If we cannot split the number into exactly ' N ' parts ; If x % n == 0 then the minimum difference is 0 and all numbers are x / n ; upto n - ( x % n ) the values will be x / n after that the values will be x / n + 1 ; Driver code
class GFG { static void split ( int x , int n ) { if ( x < n ) System . out . print ( " - 1 ▁ " ) ; else if ( x % n == 0 ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( ( x / n ) + " ▁ " ) ; } else { int zp = n - ( x % n ) ; int pp = x / n ; for ( int i = 0 ; i < n ; i ++ ) { if ( i >= zp ) System . out . print ( ( pp + 1 ) + " ▁ " ) ; else System . out . print ( pp + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int x = 5 ; int n = 3 ; split ( x , n ) ; } }
Minimum time to reach a point with + t and | Java implementation of the above approach ; returns the minimum time required to reach ' X ' ; Stores the minimum time ; increment ' t ' by 1 ; update the sum ; Driver code
class GFG { static long cal_minimum_time ( long X ) { long t = 0 ; long sum = 0 ; while ( sum < X ) { t ++ ; sum = sum + t ; } return t ; } public static void main ( String [ ] args ) { long n = 6 ; long ans = cal_minimum_time ( n ) ; System . out . println ( " The ▁ minimum ▁ time ▁ required ▁ is ▁ : ▁ " + ans ) ; } }
Check if a string can be rearranged to form special palindrome | Java implementation of the above approach ; Driver code ; creating a array which stores the frequency of each character ; Checking if a character is uppercase or not ; Increasing by 1 if uppercase ; Decreasing by 1 if lower case ; Storing the sum of positive numbers in the frequency array ; Storing the sum of negative numbers in the frequency array ; If all character balances out then its Yes ; If there is only 1 character which does not balances then also it is Yes
public class Improve { public static void main ( String args [ ] ) { String s = " ABCdcba " ; int u [ ] = new int [ 26 ] ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( Character . isUpperCase ( s . charAt ( i ) ) ) { u [ s . charAt ( i ) - 65 ] += 1 ; } else { u [ s . charAt ( i ) - 97 ] -= 1 ; } } boolean f1 = true ; int po = 0 ; int ne = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( u [ i ] > 0 ) po += u [ i ] ; if ( u [ i ] < 0 ) ne += u [ i ] ; } if ( po == 0 && ne == 0 ) System . out . println ( " YES " ) ; else if ( po == 1 && ne == 0 ) System . out . println ( " YES " ) ; else if ( po == 0 && ne == - 1 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
Modify the string such that it contains all vowels at least once | Java implementation of the above approach ; All vowels ; List to store distinct vowels ; if length of String is less than 5 then always Impossible ; Storing the distinct vowels in the String by checking if it in the list of String and not in the list of distinct vowels ; Storing the vowels which are not present in the String ; No replacement needed condition ; Replacing the characters to get all Vowels ; copy th rest of the string ; Driver Code
import java . util . * ; class GFG { static boolean find ( char x [ ] , char c ) { for ( int i = 0 ; i < x . length ; i ++ ) if ( x [ i ] == c ) return true ; return false ; } static boolean find ( Vector < Character > v , char c ) { for ( int i = 0 ; i < v . size ( ) ; i ++ ) if ( v . get ( i ) == c ) return true ; return false ; } static void addAllVowel ( String str ) { char x [ ] = { ' A ' , ' E ' , ' I ' , ' O ' , ' U ' } ; Vector < Character > y = new Vector < > ( ) ; int length = str . length ( ) ; if ( length < 5 ) System . out . println ( " Impossible " ) ; else { for ( int i = 0 ; i < length ; i ++ ) { if ( find ( x , str . charAt ( i ) ) && ! find ( y , str . charAt ( i ) ) ) y . add ( str . charAt ( i ) ) ; } Vector < Character > z = new Vector < > ( ) ; for ( int i = 0 ; i < 5 ; i ++ ) if ( ! find ( y , x [ i ] ) ) z . add ( x [ i ] ) ; if ( z . size ( ) == 0 ) System . out . println ( str ) ; else { int cc = 0 ; Vector < Character > y1 = new Vector < > ( ) ; String ans = " " ; for ( int i = 0 ; i < length ; i ++ ) { if ( find ( x , str . charAt ( i ) ) && ! find ( y1 , str . charAt ( i ) ) ) { ans += str . charAt ( i ) ; y1 . add ( str . charAt ( i ) ) ; } else { ans += z . get ( cc ) ; cc ++ ; } if ( cc == z . size ( ) ) { for ( int j = i + 1 ; j < length ; j ++ ) ans += str . charAt ( j ) ; break ; } } System . out . println ( ans ) ; } } } public static void main ( String args [ ] ) { String str = " ABCDEFGHI " ; addAllVowel ( str ) ; } }
Minimum number of days required to complete the work | Java program to find the minimum number days required ; pair class for number of days ; Function to find the minimum number days required ; initialising ans to least value possible ; sort by first i . e D ( i ) ; Calculate the minimum possible days ; return the answer ; Driver Code ; Number of works ; D1 [ i ] ; D2 [ i ]
import java . util . * ; import java . lang . * ; import java . io . * ; class Pair { int x , y ; Pair ( int a , int b ) { this . x = a ; this . y = b ; } } class GFG { static int inf = Integer . MIN_VALUE ; public static int minimumDays ( int N , int D1 [ ] , int D2 [ ] ) { int ans = - inf ; ArrayList < Pair > list = new ArrayList < Pair > ( ) ; for ( int i = 0 ; i < N ; i ++ ) list . add ( new Pair ( D1 [ i ] , D2 [ i ] ) ) ; Collections . sort ( list , new Comparator < Pair > ( ) { @ Override public int compare ( Pair p1 , Pair p2 ) { return p1 . x - p2 . x ; } } ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( list . get ( i ) . y >= ans ) ans = list . get ( i ) . y ; else ans = list . get ( i ) . x ; } return ans ; } public static void main ( String [ ] args ) { int N = 3 ; int D1 [ ] = new int [ ] { 6 , 5 , 4 } ; int D2 [ ] = new int [ ] { 1 , 2 , 3 } ; System . out . print ( minimumDays ( N , D1 , D2 ) ) ; } }
Sum of minimum difference between consecutive elements of an array | Java program for finding the minimum sum of difference between consecutive elements ; function to find minimum sum of difference of consecutive element ; ul to store upper limit ll to store lower limit ; storethe lower range in ll and upper range in ul ; initialize the answer with 0 ; iterate for all ranges ; case 1 , in this case the difference will be 0 ; change upper limit and lower limit ; case 2 ; store the difference ; case 3 ; store the difference ; Driver code ; array of range
import java . io . * ; class GFG { static int solve ( int [ ] [ ] v , int n ) { int ans , ul , ll ; int first = 0 ; int second = 1 ; ll = v [ 0 ] [ first ] ; ul = v [ 0 ] [ second ] ; ans = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( ( v [ i ] [ first ] <= ul && v [ i ] [ first ] >= ll ) || ( v [ i ] [ second ] >= ll && v [ i ] [ second ] <= ul ) ) { if ( v [ i ] [ first ] > ll ) { ll = v [ i ] [ first ] ; } if ( v [ i ] [ second ] < ul ) { ul = v [ i ] [ second ] ; } } else if ( v [ i ] [ first ] > ul ) { ans += Math . abs ( ul - v [ i ] [ first ] ) ; ul = v [ i ] [ first ] ; ll = v [ i ] [ first ] ; } else if ( v [ i ] [ second ] < ll ) { ans += Math . abs ( ll - v [ i ] [ second ] ) ; ul = v [ i ] [ second ] ; ll = v [ i ] [ second ] ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] [ ] v = { { 1 , 3 } , { 2 , 5 } , { 6 , 8 } , { 1 , 2 } , { 2 , 3 } } ; int n = 5 ; System . out . println ( solve ( v , n ) ) ; } }
Find the Largest Cube formed by Deleting minimum Digits from a number | Java code to implement maximum perfect cube formed after deleting minimum digits ; Returns vector of Pre Processed perfect cubes ; convert the cube to String and push into preProcessedCubes vector ; Utility function for findLargestCube ( ) . Returns the Largest cube number that can be formed ; reverse the preProcessed cubes so that we have the largest cube in the beginning of the vector ; iterate over all cubes ; check if the current digit of the cube matches with that of the number num ; if control reaches here , the its not possible to form a perfect cube ; wrapper for findLargestCubeUtil ( ) ; pre process perfect cubes ; convert number n to String ; Driver Code
import java . util . * ; class GFG { static Vector < String > preProcess ( int n ) { Vector < String > preProcessedCubes = new Vector < > ( ) ; for ( int i = 1 ; i * i * i <= n ; i ++ ) { int iThCube = i * i * i ; String cubeString = String . valueOf ( iThCube ) ; preProcessedCubes . add ( cubeString ) ; } return preProcessedCubes ; } static String findLargestCubeUtil ( String num , Vector < String > preProcessedCubes ) { Collections . reverse ( preProcessedCubes ) ; int totalCubes = preProcessedCubes . size ( ) ; for ( int i = 0 ; i < totalCubes ; i ++ ) { String currCube = preProcessedCubes . get ( i ) ; int digitsInCube = currCube . length ( ) ; int index = 0 ; int digitsInNumber = num . length ( ) ; for ( int j = 0 ; j < digitsInNumber ; j ++ ) { if ( num . charAt ( j ) == currCube . charAt ( index ) ) { index ++ ; } if ( digitsInCube == index ) { return currCube ; } } } return " Not ▁ Possible " ; } static void findLargestCube ( int n ) { Vector < String > preProcessedCubes = preProcess ( n ) ; String num = String . valueOf ( n ) ; String ans = findLargestCubeUtil ( num , preProcessedCubes ) ; System . out . println ( " Largest ▁ Cube ▁ that ▁ can ▁ be ▁ formed ▁ from ▁ " + n + " ▁ is ▁ " + ans ) ; } public static void main ( String [ ] args ) { int n ; n = 4125 ; findLargestCube ( n ) ; n = 876 ; findLargestCube ( n ) ; } }
Water Connection Problem | Java program to find efficient solution for the network ; number of houses and number of pipes ; Array rd stores the ending vertex of pipe ; Array wd stores the value of diameters between two pipes ; Array cd stores the starting end of pipe ; arraylist a , b , c are used to store the final output ; Function to perform calculations . ; If a pipe has no ending vertex but has starting vertex i . e is an outgoing pipe then we need to start DFS with this vertex . ; We put the details of component in final output array ; main function ; set the value of the araray to zero
import java . util . * ; class GFG { static int n , p ; static int rd [ ] = new int [ 1100 ] ; static int wt [ ] = new int [ 1100 ] ; static int cd [ ] = new int [ 1100 ] ; static List < Integer > a = new ArrayList < Integer > ( ) ; static List < Integer > b = new ArrayList < Integer > ( ) ; static List < Integer > c = new ArrayList < Integer > ( ) ; static int ans ; static int dfs ( int w ) { if ( cd [ w ] == 0 ) return w ; if ( wt [ w ] < ans ) ans = wt [ w ] ; return dfs ( cd [ w ] ) ; } static void solve ( int arr [ ] [ ] ) { int i = 0 ; while ( i < p ) { int q = arr [ i ] [ 0 ] ; int h = arr [ i ] [ 1 ] ; int t = arr [ i ] [ 2 ] ; cd [ q ] = h ; wt [ q ] = t ; rd [ h ] = q ; i ++ ; } a = new ArrayList < Integer > ( ) ; b = new ArrayList < Integer > ( ) ; c = new ArrayList < Integer > ( ) ; for ( int j = 1 ; j <= n ; ++ j ) if ( rd [ j ] == 0 && cd [ j ] > 0 ) { ans = 1000000000 ; int w = dfs ( j ) ; a . add ( j ) ; b . add ( w ) ; c . add ( ans ) ; } System . out . println ( a . size ( ) ) ; for ( int j = 0 ; j < a . size ( ) ; ++ j ) System . out . println ( a . get ( j ) + " ▁ " + b . get ( j ) + " ▁ " + c . get ( j ) ) ; } public static void main ( String args [ ] ) { n = 9 ; p = 6 ; for ( int i = 0 ; i < 1100 ; i ++ ) rd [ i ] = cd [ i ] = wt [ i ] = 0 ; int arr [ ] [ ] = { { 7 , 4 , 98 } , { 5 , 9 , 72 } , { 4 , 6 , 10 } , { 2 , 8 , 22 } , { 9 , 7 , 17 } , { 3 , 1 , 66 } } ; solve ( arr ) ; } }
Print a closest string that does not contain adjacent duplicates | Java program to print a string with no adjacent duplicates by doing minimum changes to original string ; Function to print simple string ; If any two adjacent characters are equal ; Initialize it to ' a ' ; Traverse the loop until it is different from the left and right letter . ; Driver function
import java . util . * ; import java . lang . * ; public class GfG { public static String noAdjacentDup ( String s1 ) { int n = s1 . length ( ) ; char [ ] s = s1 . toCharArray ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] ) { s [ i ] = ' a ' ; while ( s [ i ] == s [ i - 1 ] || ( i + 1 < n && s [ i ] == s [ i + 1 ] ) ) s [ i ] ++ ; i ++ ; } } return ( new String ( s ) ) ; } public static void main ( String argc [ ] ) { String s = " geeksforgeeks " ; System . out . println ( noAdjacentDup ( s ) ) ; } }
Array element moved by k using single moves | Java program to find winner of game ; if the number of steps is more then n - 1 , ; initially the best is 0 and no of wins is 0. ; traverse through all the numbers ; if the value of array is more then that of previous best ; best is replaced by a [ i ] ; if not the first index ; no of wins is 1 now ; if it wins ; if any position has more then k wins then return ; Maximum element will be winner because we move smaller element at end and repeat the process . ; driver program to test the above function
import java . io . * ; class GFG { static int winner ( int a [ ] , int n , int k ) { if ( k >= n - 1 ) return n ; int best = 0 , times = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > best ) { best = a [ i ] ; if ( i == 1 ) times = 1 ; } else times += 1 ; if ( times >= k ) return best ; } return best ; } public static void main ( String args [ ] ) { int a [ ] = { 2 , 1 , 3 , 4 , 5 } ; int n = a . length ; int k = 2 ; System . out . println ( winner ( a , n , k ) ) ; } }
Paper Cut into Minimum Number of Squares | Java program to find minimum number of squares to cut a paper . ; To swap two numbers ; Returns min number of squares needed ; swap if a is small size side . ; Iterate until small size side is greater then 0 ; Update result ; Driver code
class GFG { static void swap ( int a , int b ) { int temp = a ; a = b ; b = temp ; } static int minimumSquare ( int a , int b ) { int result = 0 , rem = 0 ; if ( a < b ) swap ( a , b ) ; while ( b > 0 ) { result += a / b ; rem = a % b ; a = b ; b = rem ; } return result ; } public static void main ( String [ ] args ) { int n = 13 , m = 29 ; System . out . println ( minimumSquare ( n , m ) ) ; } }
Maximize array sum after K negations | Set 2 | A PriorityQueue based Java program to maximize array sum after k negations . ; Function to find Maximum sum after K negations ; Create a priority queue and insert all array elements int ; Do k negations by removing a minimum element k times ; Retrieve and remove min element ; Modify the minimum element and add back to priority queue ; Compute sum of all elements in priority queue . ; Driver code
import java . util . * ; class maximizeSum { public static int maxSum ( int [ ] a , int k ) { PriorityQueue < Integer > pq = new PriorityQueue < > ( ) ; for ( int x : a ) pq . add ( x ) ; while ( k -- > 0 ) { int temp = pq . poll ( ) ; temp *= - 1 ; pq . add ( temp ) ; } int sum = 0 ; for ( int x : pq ) sum += x ; return sum ; } public static void main ( String [ ] args ) { int [ ] arr = { - 2 , 0 , 5 , - 1 , 2 } ; int k = 4 ; System . out . println ( maxSum ( arr , k ) ) ; } }