codeparrot/xlcost-text-to-code huggingface dataset:
Remove minimum characters from string to split it into three substrings under given constraints | Java program for the above approach ; Function that counts minimum character that must be removed ; Length of string ; Create prefix array ; Initialize first position ; Fill prefix array ; Initialise maxi ; Check all the possibilities by putting i and j at different position & find maximum among them ; Print the characters to be removed ; Driver Code ; Given String ; Function call
class GFG { static void min_remove ( String str ) { int N = str . length ( ) ; int [ ] prefix_a = new int [ N + 1 ] ; int [ ] prefix_b = new int [ N + 1 ] ; int [ ] prefix_c = new int [ N + 1 ] ; prefix_a [ 0 ] = 0 ; prefix_b [ 0 ] = 0 ; prefix_c [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { prefix_a [ i ] = prefix_a [ i - 1 ] + ( int ) ( ( str . charAt ( i - 1 ) == ' a ' ) ? 1 : 0 ) ; prefix_b [ i ] = prefix_b [ i - 1 ] + ( int ) ( ( str . charAt ( i - 1 ) == ' b ' ) ? 1 : 0 ) ; prefix_c [ i ] = prefix_c [ i - 1 ] + ( int ) ( ( str . charAt ( i - 1 ) == ' c ' ) ? 1 : 0 ) ; } int maxi = Integer . MIN_VALUE ; for ( int i = 0 ; i <= N ; i ++ ) { for ( int j = i ; j <= N ; j ++ ) { maxi = Math . max ( maxi , ( prefix_a [ i ] + ( prefix_b [ j ] - prefix_b [ i ] ) + ( prefix_c [ N ] - prefix_c [ j ] ) ) ) ; } } System . out . println ( ( N - maxi ) ) ; } public static void main ( String [ ] args ) { String str = " aaaabaaxccac " ; min_remove ( str ) ; } }
Create an array of size N with sum S such that no subarray exists with sum S or S | Java for the above approach ; Function to create an array with N elements with sum as S such that the given conditions satisfy ; Check if the solution exists ; Print the array as print ( n - 1 ) elements of array as 2 ; Print the last element of the array ; Print the value of k ; If solution doesnot exists ; Driver Code ; Given N and sum S ; Function call
class GFG { static void createArray ( int n , int s ) { if ( 2 * n <= s ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { System . out . print ( 2 + " ▁ " ) ; s -= 2 ; } System . out . println ( s ) ; System . out . println ( 1 ) ; } else System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int N = 1 ; int S = 4 ; createArray ( N , S ) ; } }
Maximize partitions such that no two substrings have any common character | Java program to find the maximum number of partitions possible such that no two subStrings have common character ; Function to calculate and return the maximum number of partitions ; r : Stores the maximum number of partitions k : Stores the ending index of the partition ; Stores the last index of every unique character of the String ; Traverse the String and store the last index of every character ; Store the last index of the first character from map ; Update K to find the end of partition ; Otherwise , the end of partition is found ; Increment r ; Update k for the next partition ; Add the last partition ; Driver code
import java . util . * ; class GFG { static int maximum_partition ( String str ) { int i = 0 , j = 0 , k = 0 ; int c = 0 , r = 0 ; HashMap < Character , Integer > m = new HashMap < > ( ) ; for ( i = str . length ( ) - 1 ; i >= 0 ; i -- ) { if ( ! m . containsKey ( str . charAt ( i ) ) ) { m . put ( str . charAt ( i ) , i ) ; } } i = 0 ; k = m . get ( str . charAt ( i ) ) ; for ( i = 0 ; i < str . length ( ) ; i ++ ) { if ( i <= k ) { c = c + 1 ; k = Math . max ( k , m . get ( str . charAt ( i ) ) ) ; } else { r = r + 1 ; c = 1 ; k = Math . max ( k , m . get ( str . charAt ( i ) ) ) ; } } if ( c != 0 ) { r = r + 1 ; } return r ; } public static void main ( String [ ] args ) { String str = " ababcbacadefegdehijhklij " ; System . out . print ( maximum_partition ( str ) ) ; } }
Minimize steps required to move all 1 's in a matrix to a given index | Java program to calculate the minimum steps required to reach a given cell from all cells consisting of 1 's ; Function to calculate and return the minimum number of steps required to move all 1 s to ( X , Y ) ; Iterate the given matrix ; Update the answer with minimum moves required for the given element to reach the given index ; Return the number of steps ; Driver code ; Given matrix ; Given position ; Function Call
import java . util . * ; class GFG { static int findAns ( int [ ] [ ] mat , int x , int y , int n , int m ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { ans += Math . abs ( x - i ) + Math . abs ( y - j ) ; } } } return ans ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 } , { 1 , 0 , 1 , 1 } } ; int x = 0 , y = 2 ; System . out . print ( findAns ( mat , x , y , mat . length , mat [ 0 ] . length ) + "NEW_LINE"); } }
Count of all possible reverse bitonic subarrays | Java program for the above approach ; Function that counts all the reverse bitonic subarray in arr [ ] ; To store the count of reverse bitonic subarray ; Iterate the array and select the starting element ; Iterate for selecting the ending element for subarray ; Subarray arr [ i to j ] ; For 1 length , increment the count and continue ; For decreasing subarray ; Check if only decreasing ; For increasing subarray ; Print the total count of subarrays ; Driver Code ; Given array arr [ ] ; Function Call
import java . util . * ; class GFG { static void countReversebitonic ( int arr [ ] , int n ) { int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int temp = arr [ i ] , f = 0 ; if ( j == i ) { c ++ ; continue ; } int k = i + 1 ; while ( temp > arr [ k ] && k <= j ) { temp = arr [ k ] ; k ++ ; } if ( k > j ) { c ++ ; f = 2 ; } while ( k <= j && temp < arr [ k ] && f != 2 ) { temp = arr [ k ] ; k ++ ; } if ( k > j && f != 2 ) { c ++ ; f = 0 ; } } } System . out . print ( c + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 , 4 } ; int N = arr . length ; countReversebitonic ( arr , N ) ; } }
Longest increasing sequence by the boundary elements of an Array | Java program to print the longest increasing subsequence from the boundary elements of an array ; Function to return the length of Longest Increasing subsequence ; Set pointers at both ends ; Stores the recent value added to the subsequence ; Stores the length of the subsequence ; Check if both elements can be added to the subsequence ; Check if the element on the left can be added to the subsequence only ; Check if the element on the right can be added to the subsequence only ; If none of the values can be added to the subsequence ; Driver Code ; Length of array
import java . util . * ; class GFG { static int longestSequence ( int n , int arr [ ] ) { int l = 0 , r = n - 1 ; int prev = Integer . MIN_VALUE ; int ans = 0 ; while ( l <= r ) { if ( arr [ l ] > prev && arr [ r ] > prev ) { if ( arr [ l ] < arr [ r ] ) { ans += 1 ; prev = arr [ l ] ; l += 1 ; } else { ans += 1 ; prev = arr [ r ] ; r -= 1 ; } } else if ( arr [ l ] > prev ) { ans += 1 ; prev = arr [ l ] ; l += 1 ; } else if ( arr [ r ] > prev ) { ans += 1 ; prev = arr [ r ] ; r -= 1 ; } else { break ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 1 , 4 , 2 } ; int n = arr . length ; System . out . print ( longestSequence ( n , arr ) ) ; } }
Total number of cells covered in a matrix after D days | Java implementation to find the total number of cells covered in a matrix after D days ; Function to return the total infected cells after d days ; Top extension ; Bottom extension ; Left extension ; Right extension ; Calculating the cells in each quadrilateral ; Sum all of them to get total cells in each quadrilateral ; Add the singleblocks along the lines of top , down , left , right ; Return the ans ; Driver code ; Dimensions of cell ; Starting coordinates ; Number of days ; Function call
import java . util . * ; class GFG { static int solve ( int n , int m , int x , int y , int d ) { int top = Math . min ( d , x - 1 ) ; int down = Math . min ( d , n - x ) ; int left = Math . min ( d , y - 1 ) ; int right = Math . min ( d , m - y ) ; int quad1 = top * left ; int quad2 = left * down ; int quad3 = down * right ; int quad4 = right * top ; int totalsq = quad1 + quad2 + quad3 + quad4 ; int singleBlocks = top + down + left + right + 1 ; return totalsq + singleBlocks ; } public static void main ( String [ ] args ) { int n = 10 , m = 10 ; int x = 7 , y = 8 ; int d = 4 ; d -- ; System . out . println ( solve ( n , m , x , y , d ) ) ; } }
Smallest subsequence with sum of absolute difference of consecutive elements maximized | Java program to find smallest subsequence with sum of absolute difference of consecutive elements maximized ; Function to print the smallest subsequence and its sum ; Final subsequence ; First element is a default endpoint ; Iterating through the array ; Check for monotonically increasing endpoint ; Check for monotonically decreasing endpoint ; Last element is a default endpoint ; Length of final subsequence ; Print the subsequence ; Driver code
import java . util . * ; class GFG { static void getSubsequence ( int [ ] arr , int n ) { Vector < Integer > req = new Vector < Integer > ( ) ; req . add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] && arr [ i ] > arr [ i - 1 ] ) req . add ( arr [ i ] ) ; else if ( arr [ i ] < arr [ i + 1 ] && arr [ i ] < arr [ i - 1 ] ) req . add ( arr [ i ] ) ; } req . add ( arr [ n - 1 ] ) ; System . out . print ( req . size ( ) + "NEW_LINE"); for ( int x : req ) System . out . print ( x + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 5 , 3 , 6 , 7 , 4 } ; int n = arr . length ; getSubsequence ( arr , n ) ; } }
Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Java implementation to find the minimum number of changes required such that every path from top left to the bottom right are palindromic paths ; Function to find the minimum number of the changes required for the every path to be palindromic ; count variable for maintaining total changes . ; left and right variables for keeping distance values from cell ( 0 , 0 ) and ( N - 1 , M - 1 ) respectively . ; Iterating over the matrix ; Finding minimum number of changes required . ; Minimum no . of changes will be the the minimum no . of different values and we will assume to make them equals to value with maximum frequency element ; Moving ahead with greater distance ; Driver Code ; Function Call
import java . io . * ; import java . util . * ; class GFG { static final int M = 3 ; static final int N = 3 ; static int minchanges ( int [ ] [ ] mat ) { int count = 0 ; int left = 0 , right = N + M - 2 ; while ( left < right ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; int totalsize = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( i + j == left ) { mp . put ( mat [ i ] [ j ] , mp . getOrDefault ( mat [ i ] [ j ] , 0 ) + 1 ) ; totalsize ++ ; } else if ( i + j == right ) { mp . put ( mat [ i ] [ j ] , mp . getOrDefault ( mat [ i ] [ j ] , 0 ) + 1 ) ; totalsize ++ ; } } } int changes = 0 ; for ( Map . Entry < Integer , Integer > itr : mp . entrySet ( ) ) changes = Math . max ( changes , itr . getValue ( ) ) ; count += totalsize - changes ; left ++ ; right -- ; } return count ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 4 , 1 } , { 2 , 5 , 3 } , { 1 , 3 , 1 } } ; System . out . println ( minchanges ( mat ) ) ; } }
Count of longest possible subarrays with sum not divisible by K | Java program for the above problem ; Function to find the count of longest subarrays with sum not divisible by K ; Sum of all elements in an array ; If overall sum is not divisible then return 1 , as only one subarray of size n is possible ; Index of the first number not divisible by K ; Index of the last number not divisible by K ; Subarray doesn 't exist ; Sum of the window ; Calculate the sum of rest of the windows of size len ; Driver Code
import java . util . * ; class GFG { static int CountLongestSubarrays ( int arr [ ] , int n , int k ) { int i , s = 0 ; for ( i = 0 ; i < n ; ++ i ) { s += arr [ i ] ; } if ( ( s % k ) != 0 ) { return 1 ; } else { int ini = 0 ; while ( ini < n && arr [ ini ] % k == 0 ) { ++ ini ; } int fin = n - 1 ; while ( fin >= 0 && arr [ fin ] % k == 0 ) { -- fin ; } int len , sum = 0 , count = 0 ; if ( ini == n ) { return - 1 ; } else { len = Math . max ( n - 1 - ini , fin ) ; } for ( i = 0 ; i < len ; i ++ ) { sum += arr [ i ] ; } if ( sum % k != 0 ) { count ++ ; } for ( i = len ; i < n ; i ++ ) { sum = sum + arr [ i ] ; sum = sum - arr [ i - len ] ; if ( sum % k != 0 ) { count ++ ; } } return count ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 2 , 2 , 3 } ; int n = arr . length ; int k = 3 ; System . out . print ( CountLongestSubarrays ( arr , n , k ) ) ; } }
Count of triplets in a given Array having GCD K | Java program to count the number of triplets in the array with GCD equal to K ; Frequency array ; mul [ i ] stores the count of multiples of i ; cnt [ i ] stores the count of triplets with gcd = i ; Return nC3 ; Function to count and return the number of triplets in the array with GCD equal to K ; Store frequency of array elements ; Store the multiples of i present in the array ; Count triplets with gcd equal to any multiple of i ; Remove all triplets which have gcd equal to a multiple of i ; Driver code
class GFG { static int MAXN = 1000001 ; static int freq [ ] = new int [ MAXN ] ; static int mul [ ] = new int [ MAXN ] ; static int cnt [ ] = new int [ MAXN ] ; static int nC3 ( int n ) { if ( n < 3 ) return 0 ; return ( n * ( n - 1 ) * ( n - 2 ) ) / 6 ; } static void count_triplet ( int [ ] arr , int N , int K ) { int i , j ; for ( i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( i = 1 ; i <= 1000000 ; i ++ ) { for ( j = i ; j <= 1000000 ; j += i ) { mul [ i ] += freq [ j ] ; } cnt [ i ] = nC3 ( mul [ i ] ) ; } for ( i = 1000000 ; i >= 1 ; i -- ) { for ( j = 2 * i ; j <= 1000000 ; j += i ) { cnt [ i ] -= cnt [ j ] ; } } System . out . print ( " Number ▁ of ▁ triplets ▁ " + " with ▁ GCD ▁ " + K ) ; System . out . print ( " ▁ are ▁ " + cnt [ K ] ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 7 , 12 , 6 , 15 , 9 } ; int N = 6 , K = 3 ; count_triplet ( arr , N , K ) ; } }
Check if a subsequence of length K with odd sum exists | Java program to check if a subsequence of length K with odd sum exists in the given array ; Function to check if any required subsequence exists or not ; Store count of odd and even elements in the array ; Calculate the count of odd and even elements ; If no odd elements exists or no even elements exists when K is even ; Subsequence is not possible ; Possible otherwise ; Driver Code
class GFG { static boolean isSubseqPossible ( int [ ] arr , int N , int K ) { int i ; int odd = 0 , even = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd ++ ; else even ++ ; } if ( odd == 0 || ( even == 0 && K % 2 == 0 ) ) return false ; return true ; } public static void main ( String args [ ] ) { int [ ] arr = { 2 , 3 , 5 , 7 , 4 } ; int N = arr . length ; int K = 3 ; System . out . print ( isSubseqPossible ( arr , N , K ) ? " Yes " : " No " ) ; } }
Path traversed using exactly M coins in K jumps | Java program to print the path using exactly K jumps and M coins ; Function that print the path using exactly K jumps and M coins ; If no path exists ; It decides which box to be jump ; It decides whether to jump on left side or to jump on right side ; Print the path ; Driver Code ; Function Call
import java . io . * ; class GFG { static void print_path ( int N , int jump , int coin ) { if ( jump > coin || jump * ( N - 1 ) < coin ) { System . out . println ( " - 1" ) ; } else { int pos = 1 ; while ( jump > 0 ) { int tmp = Math . min ( N - 1 , coin - ( jump - 1 ) ) ; if ( pos + tmp <= N ) { pos += tmp ; } else { pos -= tmp ; } System . out . print ( pos + " ▁ " ) ; ; coin -= tmp ; jump -= 1 ; } } } public static void main ( String [ ] args ) { int N = 5 , K = 4 , M = 12 ; print_path ( N , K , M ) ; } }
Minimum salary hike for each employee such that no employee feels unfair | Java program for the above approach ; Function that print minimum hike ; Insert INF at begin and end of array ; To store hike of each employee ; for Type 1 employee ; For Type 2 employee ; For Type 3 employee ; For Type 4 employee ; Print the min hike for each employee ; Driver Code ; Given array of rating of employees ; Function Call
import java . util . * ; class GFG { static final int INF = 10000009 ; static void findMinHike ( Vector < Integer > arr , int n ) { arr . add ( 0 , INF ) ; arr . add ( INF ) ; int [ ] hike = new int [ n + 2 ] ; for ( int i = 1 ; i <= n ; i ++ ) { if ( arr . get ( i - 1 ) >= arr . get ( i ) && arr . get ( i ) <= arr . get ( i + 1 ) ) { hike [ i ] = 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr . get ( i - 1 ) < arr . get ( i ) && arr . get ( i ) <= arr . get ( i + 1 ) ) { hike [ i ] = hike [ i - 1 ] + 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr . get ( i - 1 ) >= arr . get ( i ) && arr . get ( i ) > arr . get ( i + 1 ) ) { hike [ i ] = hike [ i + 1 ] + 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr . get ( i - 1 ) < arr . get ( i ) && arr . get ( i ) > arr . get ( i + 1 ) ) { hike [ i ] = Math . max ( hike [ i - 1 ] , hike [ i + 1 ] ) + 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { System . out . print ( hike [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { Vector < Integer > arr = new Vector < > ( ) ; arr . add ( 5 ) ; arr . add ( 3 ) ; arr . add ( 4 ) ; arr . add ( 2 ) ; arr . add ( 1 ) ; arr . add ( 6 ) ; findMinHike ( arr , arr . size ( ) ) ; } }
Minimize the cost to make all the adjacent elements distinct in an Array | Java program to find the minimum cost required to make all adjacent elements distinct ; Function that prints minimum cost required ; Dp - table ; Base case Not increasing the first element ; Increasing the first element by 1 ; Increasing the first element by 2 ; Condition if current element is not equal to previous non - increased element ; Condition if current element is not equal to previous element after being increased by 1 ; Condition if current element is not equal to previous element after being increased by 2 ; Take the minimum from all cases ; Finding the minimum cost ; Printing the minimum cost required to make all adjacent elements distinct ; Driver Code
import java . util . * ; class GFG { static void minimumCost ( int arr [ ] , int cost [ ] , int N ) { int [ ] [ ] dp = new int [ N ] [ 3 ] ; dp [ 0 ] [ 0 ] = 0 ; dp [ 0 ] [ 1 ] = cost [ 0 ] ; dp [ 0 ] [ 2 ] = cost [ 0 ] * 2 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { int minimum = ( int ) 1e6 ; if ( j + arr [ i ] != arr [ i - 1 ] ) minimum = Math . min ( minimum , dp [ i - 1 ] [ 0 ] ) ; if ( j + arr [ i ] != arr [ i - 1 ] + 1 ) minimum = Math . min ( minimum , dp [ i - 1 ] [ 1 ] ) ; if ( j + arr [ i ] != arr [ i - 1 ] + 2 ) minimum = Math . min ( minimum , dp [ i - 1 ] [ 2 ] ) ; dp [ i ] [ j ] = j * cost [ i ] + minimum ; } } int ans = ( int ) 1e6 ; for ( int i = 0 ; i < 3 ; i ++ ) ans = Math . min ( ans , dp [ N - 1 ] [ i ] ) ; System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 2 , 3 , 4 } ; int cost [ ] = { 3 , 2 , 5 , 4 , 2 , 1 } ; int N = arr . length ; minimumCost ( arr , cost , N ) ; } }
Subarray with largest sum after excluding its maximum element | Java implementation to find the maximum sum subarray such by excluding the maximum element from the subarray ; Function to find the maximum sum subarray by excluding the maximum element from the array ; Loop to store all the positive elements in the map ; Loop to iterating over the map and considering as the maximum element of the current including subarray ; Make the current element maximum ; Iterate through array and apply kadane 's algorithm ; Condition if current element is greater than mx then make the element - infinity ; Store the indices in some variable ; Driver code ; Function call
import java . util . * ; class GFG { static void maximumSumSubarray ( int arr [ ] , int n ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= 0 ) mp . put ( arr [ i ] , 1 ) ; } int first = 0 ; int last = 0 ; int ans = 0 ; int INF = ( int ) 1e6 ; for ( Map . Entry < Integer , Integer > i : mp . entrySet ( ) ) { int mx = i . getKey ( ) ; int curr = 0 ; int curr_start = - 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( curr == 0 ) curr_start = j ; int val = arr [ j ] > mx ? - INF : arr [ j ] ; curr += val ; if ( curr < 0 ) curr = 0 ; if ( curr > ans ) { ans = curr ; first = curr_start ; last = j ; } } } System . out . print ( ( first + 1 ) + " ▁ " + ( last + 1 ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , - 2 , 10 , - 1 , 4 } ; int size = arr . length ; maximumSumSubarray ( arr , size ) ; } }
Extended Knapsack Problem | Java code for the extended Knapsack Approach ; To store the dp values ; for each element given ; For each possible weight value ; For each case where the total elements are less than the constraint ; To ensure that we dont go out of the array ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int [ ] [ ] [ ] dp = new int [ 100 ] [ 100 ] [ 100 ] ; static int maxProfit ( int profit [ ] , int weight [ ] , int n , int max_W , int max_E ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= max_W ; j ++ ) { for ( int k = 1 ; k <= max_E ; k ++ ) { if ( j >= weight [ i - 1 ] ) { dp [ i ] [ j ] [ k ] = Math . max ( dp [ i - 1 ] [ j ] [ k ] , dp [ i - 1 ] [ j - weight [ i - 1 ] ] [ k - 1 ] + profit [ i - 1 ] ) ; } else { dp [ i ] [ j ] [ k ] = dp [ i - 1 ] [ j ] [ k ] ; } } } } return dp [ n ] [ max_W ] [ max_E ] ; } public static void main ( String [ ] args ) { int n = 5 ; int profit [ ] = { 2 , 7 , 1 , 5 , 3 } ; int weight [ ] = { 2 , 5 , 2 , 3 , 4 } ; int max_weight = 8 ; int max_element = 2 ; System . out . println ( maxProfit ( profit , weight , n , max_weight , max_element ) ) ; } }
Minimum insertions to make XOR of an Array equal to half of its sum | Java program to make XOR of all array elements equal to half of its sum by minimum insertions ; Function to make XOR of the array equal to half of its sum ; Calculate the sum and Xor of all the elements ; If the required condition satisfies already , return the original array ; If Xor is already zero , Insert sum ; Otherwise , insert xr and insert sum + xr ; Driver code
import java . util . * ; class GFG { static int make_xor_half ( ArrayList < Integer > arr ) { int sum = 0 , xr = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { int a = arr . get ( i ) ; sum += a ; xr ^= a ; } if ( 2 * xr == sum ) return - 1 ; if ( xr == 0 ) { arr . add ( sum ) ; return 1 ; } arr . add ( xr ) ; arr . add ( sum + xr ) ; return 2 ; } public static void main ( String [ ] args ) { int N = 7 ; ArrayList < Integer > nums = new ArrayList < Integer > ( Arrays . asList ( 3 , 4 , 7 , 1 , 2 , 5 , 6 ) ) ; int count = make_xor_half ( nums ) ; if ( count == - 1 ) System . out . print ( - 1 + "NEW_LINE"); else if ( count == 1 ) System . out . print ( nums . get ( N ) + "NEW_LINE"); else System . out . print ( nums . get ( N ) + " ▁ " + nums . get ( N + 1 ) + "NEW_LINE"); } }