codeparrot/xlcost-text-to-code huggingface dataset:
Maximize sum of count of distinct prime factors of K array elements | Java program for the above approach ; Function to find the maximum sum of count of distinct prime factors of K array elements ; Stores the count of distinct primes ; Stores 1 and 0 at prime and non - prime indices respectively ; Initialize the count of factors to 0 ; Sieve of Eratosthenes ; Count of factors of a prime number is 1 ; Increment CountDistinct of all multiples of i ; Mark its multiples non - prime ; Stores the maximum sum of distinct prime factors of K array elements ; Stores the count of all distinct prime factors ; Traverse the array to find count of all array elements ; Maximum sum of K prime factors of array elements ; Check for the largest prime factor ; Increment sum ; Decrement its count and K ; Print the maximum sum ; Driver code ; Given array ; Size of the array ; Given value of K
import java . io . * ; class GFG { public static int MAX = 1000000 ; static void maxSumOfDistinctPrimeFactors ( int [ ] arr , int N , int K ) { int [ ] CountDistinct = new int [ MAX + 1 ] ; boolean [ ] prime = new boolean [ MAX + 1 ] ; for ( int i = 0 ; i <= MAX ; i ++ ) { CountDistinct [ i ] = 0 ; prime [ i ] = true ; } for ( int i = 2 ; i <= MAX ; i ++ ) { if ( prime [ i ] == true ) { CountDistinct [ i ] = 1 ; for ( int j = i * 2 ; j <= MAX ; j += i ) { CountDistinct [ j ] ++ ; prime [ j ] = false ; } } } int sum = 0 ; int [ ] PrimeFactor = new int [ 20 ] ; for ( int i = 0 ; i < N ; i ++ ) { PrimeFactor [ CountDistinct [ arr [ i ] ] ] ++ ; } for ( int i = 19 ; i >= 1 ; i -- ) { while ( PrimeFactor [ i ] > 0 ) { sum += i ; PrimeFactor [ i ] -- ; K -- ; if ( K == 0 ) break ; } if ( K == 0 ) break ; } System . out . print ( sum ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 6 , 9 , 12 } ; int N = arr . length ; int K = 2 ; maxSumOfDistinctPrimeFactors ( arr , N , K ) ; } }
Minimum replacements such that no palindromic substring of length exceeding 1 is present in the given string | Java Program to implement the above approach ; Function to count the changes required such that no palindromic subString of length exceeding 1 is present in the String ; Base Case ; Stores the count ; Iterate over the String ; Palindromic SubString of Length 2 ; Replace the next character ; Increment changes ; Palindromic SubString of Length 3 ; Replace the next character ; Increment changes ; Driver Code
import java . util . * ; class GFG { static int maxChange ( char [ ] str ) { if ( str . length <= 1 ) { return 0 ; } int minChanges = 0 ; for ( int i = 0 ; i < str . length - 1 ; i ++ ) { if ( str [ i ] == str [ i + 1 ] ) { str [ i + 1 ] = ' N ' ; minChanges += 1 ; } else if ( i > 0 && str [ i - 1 ] == str [ i + 1 ] ) { str [ i + 1 ] = ' N ' ; minChanges += 1 ; } } return minChanges ; } public static void main ( String [ ] args ) { String str = " bbbbbbb " ; System . out . print ( maxChange ( str . toCharArray ( ) ) ) ; } }
Smallest positive integer K such that all array elements can be made equal by incrementing or decrementing by at most K | Java program for the above approach ; Function to find smallest integer K such that incrementing or decrementing each element by K at most once makes all elements equal ; Store distinct array elements ; Traverse the array , A [ ] ; Count elements into the set ; If M is greater than 3 ; If M is equal to 3 ; Stores the first smallest element ; Stores the second smallest element ; Stores the largest element ; IF difference between B_2 and B_1 is equal to B_3 and B_2 ; If M is equal to 2 ; Stores the smallest element ; Stores the largest element ; If difference is an even ; If M is equal to 1 ; Driver code ; Given array ; Given size ; Print the required smallest integer
import java . io . * ; import java . util . * ; class GFG { static void findMinKToMakeAllEqual ( int N , int A [ ] ) { Set < Integer > B = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { B . add ( A [ i ] ) ; } ArrayList < Integer > b = new ArrayList < Integer > ( B ) ; int M = b . size ( ) ; int i = 0 ; if ( M > 3 ) { System . out . print ( " - 1" ) ; } else if ( M == 3 ) { int B_1 = b . get ( i ++ ) ; int B_2 = b . get ( i ++ ) ; int B_3 = b . get ( i ++ ) ; if ( B_2 - B_1 == B_3 - B_2 ) { System . out . print ( B_2 - B_1 ) ; } else { System . out . print ( " - 1" ) ; } } else if ( M == 2 ) { int B_1 = b . get ( i ++ ) ; int B_2 = b . get ( i ++ ) ; if ( ( B_2 - B_1 ) % 2 == 0 ) { System . out . print ( ( B_2 - B_1 ) / 2 ) ; } else { System . out . print ( B_2 - B_1 ) ; } } else { System . out . print ( 0 ) ; } } public static void main ( String [ ] args ) { int A [ ] = { 1 , 3 , 5 , 1 } ; int N = A . length ; findMinKToMakeAllEqual ( N , A ) ; } }
Maximum number of times a given string needs to be concatenated to form a substring of another string | Java program for the above approach ; Function to find lps [ ] for given pattern pat [ 0. . M - 1 ] ; Length of the previous longest prefix suffix ; lps [ 0 ] is always 0 ; Iterate string to calculate lps [ i ] ; If the current character of the pattern matches ; Otherwise ; Otherwise ; Function to implement KMP algorithm ; Stores the longest prefix and suffix values for pattern ; Preprocess the pattern and find the lps [ ] array ; Index for txt [ ] ; Index for pat [ ] ; If pattern is found return 1 ; Mismatch after j matches ; Don 't match lps[0, lps[j - 1]]  characters they will  match anyway ; Return 0 if the pattern is not found ; Function to find the maximum value K for which string S2 concatenated K times is a substring of string S1 ; Store the required maximum number ; Create a temporary string to store string word ; Store the maximum number of times string S2 can occur in string S1 ; Traverse in range [ 0 , numWords - 1 ] ; If curWord is found in sequence ; Concatenate word to curWord ; Increment resCount by 1 ; Otherwise break the loop ; Print the answer ; Driver Code ; Function Call
class GFG { static void computeLPSArray ( String pat , int M , int [ ] lps ) { int len = 0 ; lps [ 0 ] = 0 ; int i = 1 ; while ( i < M ) { if ( pat . charAt ( i ) == pat . charAt ( len ) ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; } else { lps [ i ] = 0 ; i ++ ; } } } } static int KMPSearch ( String pat , String txt ) { int M = pat . length ( ) ; int N = txt . length ( ) ; int lps [ ] = new int [ M ] ; computeLPSArray ( pat , M , lps ) ; int i = 0 ; int j = 0 ; while ( i < N ) { if ( pat . charAt ( j ) == txt . charAt ( i ) ) { j ++ ; i ++ ; } if ( j == M ) { return 1 ; else if ( i < N && pat . charAt ( j ) != txt . charAt ( i ) ) { if ( j != 0 ) j = lps [ j - 1 ] ; else i = i + 1 ; } } return 0 ; } static void maxRepeating ( String seq , String word ) { int resCount = 0 ; String curWord = word ; int numWords = seq . length ( ) / word . length ( ) ; for ( int i = 0 ; i < numWords ; i ++ ) { if ( KMPSearch ( curWord , seq ) == 1 ) { curWord += word ; resCount ++ ; } else break ; } System . out . print ( resCount ) ; } public static void main ( String [ ] args ) { String S1 = " ababc " , S2 = " ab " ; maxRepeating ( S1 , S2 ) ; } }
Count all possible strings that can be generated by placing spaces | Java program to implement the above approach ; Function to count the number of strings that can be generated by placing spaces between pair of adjacent characters ; Count of positions for spaces ; Count of possible strings ; Driver Code
import java . io . * ; class GFG { static long countNumberOfStrings ( String s ) { int n = s . length ( ) - 1 ; long count = ( long ) ( Math . pow ( 2 , n ) ) ; return count ; } public static void main ( String [ ] args ) { String S = " ABCD " ; System . out . println ( countNumberOfStrings ( S ) ) ; } }
Minimum time required to reach a given score | Java program for the above approach ; Function to calculate minimum time required to achieve given score target ; Store the frequency of elements ; Traverse the array p [ ] ; Update the frequency ; Stores the minimim time required ; Store the current score at any time instant t ; Iterate until sum is at least equal to target ; Increment time with every iteration ; Traverse the map ; Increment the points ; Print the time required ; Driver Code ; Function Call
import java . util . * ; import java . io . * ; class GFG { static void findMinimumTime ( int [ ] p , int n , int target ) { HashMap < Integer , Integer > um = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! um . containsKey ( p [ i ] ) ) um . put ( p [ i ] , 1 ) ; else um . put ( p [ i ] , um . get ( p [ i ] ) + 1 ) ; } int time = 0 ; int sum = 0 ; while ( sum < target ) { sum = 0 ; time ++ ; for ( Map . Entry < Integer , Integer > it : um . entrySet ( ) ) { sum += it . getValue ( ) * ( time / it . getKey ( ) ) ; } } System . out . println ( time ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 3 , 3 , 4 } ; int N = arr . length ; int target = 10 ; findMinimumTime ( arr , N , target ) ; } }
Construct MEX array from the given array | Java program for the above approach ; Function to construct array B [ ] that stores MEX of array A [ ] excluding A [ i ] ; Stores elements present in arr [ ] ; Mark all values 1 , if present ; Initialize variable to store MEX ; Find MEX of arr [ ] ; Stores MEX for all indices ; Traverse the given array ; Update MEX ; MEX default ; Print the array B ; Driver Code ; Given array arr [ ] ; Size of array ; Function call
import java . util . * ; class GFG { static int MAXN = 100001 ; static void constructMEX ( int arr [ ] , int N ) { int hash [ ] = new int [ MAXN ] ; for ( int i = 0 ; i < N ; i ++ ) { hash [ i ] = 0 ; } for ( int i = 0 ; i < N ; i ++ ) { hash [ arr [ i ] ] = 1 ; } int MexOfArr = 0 ; for ( int i = 1 ; i < MAXN ; i ++ ) { if ( hash [ i ] == 0 ) { MexOfArr = i ; break ; } } int B [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < MexOfArr ) B [ i ] = arr [ i ] ; else B [ i ] = MexOfArr ; } for ( int i = 0 ; i < N ; i ++ ) System . out . print ( B [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 5 , 3 } ; int N = arr . length ; constructMEX ( arr , N ) ; } }
Minimize remaining array element by repeatedly replacing pairs by half of one more than their sum | Java program to implement the above approach ; Utility pair ; Function to print the smallest element left in the array and the pairs by given operation ; Stores array elements and return the minimum element of arr [ ] in O ( 1 ) ; Stores all the pairs that can be selected by the given operations ; Traverse the array arr [ ] ; Traverse pq while count of elements left in pq greater than 1 ; Stores top element of pq ; Pop top element of pq ; Stores top element of pq ; Pop top element of pq ; Insert ( X + Y + 1 ) / 2 in pq ; Insert the pair ( X , Y ) in pairsArr [ ] ; Print the element left in pq by performing the given operations ; Stores count of elements in pairsArr [ ] ; Print all the pairs that can be selected in given operations ; If i is the first index of pairsArr [ ] ; Print current pairs of pairsArr [ ] ; If i is not the last index of pairsArr [ ] ; If i is the last index of pairsArr [ ] ; Driver Code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void smallestNumberLeftInPQ ( int arr [ ] , int N ) { PriorityQueue < Integer > pq = new PriorityQueue < > ( ( x , y ) -> Integer . compare ( y , x ) ) ; Vector < pair > pairsArr = new Vector < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { pq . add ( arr [ i ] ) ; } while ( pq . size ( ) > 1 ) { int X = pq . peek ( ) ; pq . remove ( ) ; int Y = pq . peek ( ) ; pq . remove ( ) ; pq . add ( ( X + Y + 1 ) / 2 ) ; pairsArr . add ( new pair ( X , Y ) ) ; } System . out . print ( " { " + pq . peek ( ) + " } , ▁ " ) ; int sz = pairsArr . size ( ) ; for ( int i = 0 ; i < sz ; i ++ ) { if ( i == 0 ) { System . out . print ( " { ▁ " ) ; } System . out . print ( " ( " + pairsArr . get ( i ) . first + " , ▁ " + pairsArr . get ( i ) . second + " ) " ) ; if ( i != sz - 1 ) { System . out . print ( " , ▁ " ) ; } if ( i == sz - 1 ) { System . out . print ( " ▁ } " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 } ; int N = arr . length ; smallestNumberLeftInPQ ( arr , N ) ; } }
Move weighting scale alternate under given constraints | Java program to print weights for alternating the weighting scale ; DFS method to traverse among states of weighting scales ; If we reach to more than required steps , return true ; Try all possible weights and choose one which returns 1 afterwards ; * Try this weight only if it is * greater than current residue * and not same as previous chosen weight ; assign this weight to array and recur for next state ; if any weight is not possible , return false ; method prints weights for alternating scale and if not possible prints ' not ▁ possible ' ; call dfs with current residue as 0 and current steps as 0 ; Driver Code
class GFG { static boolean dfs ( int residue , int curStep , int [ ] wt , int [ ] arr , int N , int steps ) { if ( curStep >= steps ) return true ; for ( int i = 0 ; i < N ; i ++ ) { if ( curStep - 1 < 0 || ( arr [ i ] > residue && arr [ i ] != wt [ curStep - 1 ] ) ) { wt [ curStep ] = arr [ i ] ; if ( dfs ( arr [ i ] - residue , curStep + 1 , wt , arr , N , steps ) ) return true ; } } return false ; } static void printWeightOnScale ( int [ ] arr , int N , int steps ) { int [ ] wt = new int [ steps ] ; if ( dfs ( 0 , 0 , wt , arr , N , steps ) ) { for ( int i = 0 ; i < steps ; i ++ ) System . out . print ( wt [ i ] + " ▁ " ) ; System . out . println ( ) ; } else System . out . println ( " Not ▁ Possible " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 6 } ; int N = arr . length ; int steps = 10 ; printWeightOnScale ( arr , N , steps ) ; } }
Minimum removals required such that sum of remaining array modulo M is X | Java program for the above approach ; Function to find the minimum elements having sum x ; Initialize dp table ; Pre - compute subproblems ; If mod is smaller than element ; Minimum elements with sum j upto index i ; Return answer ; Function to find minimum number of removals to make sum % M in remaining array is equal to X ; Sum of all elements ; Sum to be removed ; Print answer ; Driver Code ; Given array ; Given size ; Given mod and x ; Function call
import java . util . * ; class GFG { static int findSum ( int [ ] S , int n , int x ) { int [ ] [ ] table = new int [ n + 1 ] [ x + 1 ] ; for ( int i = 1 ; i <= x ; i ++ ) { table [ 0 ] [ i ] = Integer . MAX_VALUE - 1 ; } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= x ; j ++ ) { if ( S [ i - 1 ] > j ) { table [ i ] [ j ] = table [ i - 1 ] [ j ] ; } else { table [ i ] [ j ] = Math . min ( table [ i - 1 ] [ j ] , table [ i ] [ j - S [ i - 1 ] ] + 1 ) ; } } } return ( table [ n ] [ x ] > n ) ? - 1 : table [ n ] [ x ] ; } static void minRemovals ( int [ ] arr , int n , int m , int x ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } int requied_Sum = 0 ; if ( sum % m < x ) requied_Sum = m + sum % m - x ; else requied_Sum = sum % m - x ; System . out . print ( findSum ( arr , n , requied_Sum ) ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 , 2 } ; int n = arr . length ; int m = 4 , x = 2 ; minRemovals ( arr , n , m , x % m ) ; } }
Count minimum character replacements required such that given string satisfies the given conditions | Java program for the above approach ; Function that finds the minimum count of steps required to make the string special ; Stores the frequency of the left & right half of string ; Find frequency of left half ; Find frequency of left half ; Make all characters equal to character c ; Case 1 : For s [ i ] < s [ j ] ; Subtract all the characters on left side that are <= d ; Adding all characters on the right side that same as d ; Find minimum value of count ; Similarly for Case 2 : s [ i ] > s [ j ] ; Return the minimum changes ; Driver Code ; Given string S ; Function Call
import java . io . * ; import java . util . * ; class Main { public static int minChange ( String s , int n ) { int L [ ] = new int [ 26 ] ; int R [ ] = new int [ 26 ] ; for ( int i = 0 ; i < n / 2 ; i ++ ) { char ch = s . charAt ( i ) ; L [ ch - ' a ' ] ++ ; } for ( int i = n / 2 ; i < n ; i ++ ) { char ch = s . charAt ( i ) ; R [ ch - ' a ' ] ++ ; } int count = n ; for ( char ch = ' a ' ; ch <= ' z ' ; ch ++ ) { count = Math . min ( count , n - L [ ch - ' a ' ] - R [ ch - ' a ' ] ) ; } int change = n / 2 ; for ( int d = 0 ; d + 1 < 26 ; d ++ ) { change -= L [ d ] ; change += R [ d ] ; count = Math . min ( count , change ) ; } change = n / 2 ; for ( int d = 0 ; d + 1 < 26 ; d ++ ) { change -= R [ d ] ; change += L [ d ] ; count = Math . min ( change , count ) ; } return count ; } public static void main ( String [ ] args ) { String S = " aababc " ; int N = S . length ( ) ; System . out . println ( minChange ( S , N ) ) ; } }
Rearrange string to obtain Longest Palindromic Substring | Java program to implement the above approach ; Function to rearrange the string to get the longest palindromic substring ; Stores the length of str ; Store the count of occurrence of each character ; Traverse the string , str ; Count occurrence of each character ; Store the left half of the longest palindromic substring ; Store the right half of the longest palindromic substring ; Traverse the array , hash [ ] ; Append half of the characters to res1 ; Append half of the characters to res2 ; reverse string res2 to make res1 + res2 palindrome ; Store the remaining characters ; Check If any odd character appended to the middle of the resultant string or not ; Append all the character which occurs odd number of times ; If count of occurrence of characters is odd ; Driver code
import java . util . * ; class GFG { static String longestPalinSub ( String str ) { int N = str . length ( ) ; int [ ] hash = new int [ 256 ] ; for ( int i = 0 ; i < N ; i ++ ) { hash [ str . charAt ( i ) ] ++ ; } StringBuilder res1 = new StringBuilder ( ) ; StringBuilder res2 = new StringBuilder ( ) ; for ( int i = 0 ; i < 256 ; i ++ ) { for ( int j = 0 ; j < hash [ i ] / 2 ; j ++ ) { res1 . append ( ( char ) i ) ; } for ( int j = ( hash [ i ] + 1 ) / 2 ; j < hash [ i ] ; j ++ ) { res2 . append ( ( char ) i ) ; } } StringBuilder tmp = res2 . reverse ( ) ; StringBuilder res3 = new StringBuilder ( ) ; boolean f = false ; for ( int i = 0 ; i < 256 ; i ++ ) { if ( hash [ i ] % 2 == 1 ) { if ( ! f ) { res1 . append ( ( char ) i ) ; f = true ; } else { res3 . append ( ( char ) i ) ; } } } return ( res1 . toString ( ) + tmp . toString ( ) + res3 . toString ( ) ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( longestPalinSub ( str ) ) ; } }
Maximize subsequences having array elements not exceeding length of the subsequence | Java program for the above approach ; Function to calculate the number of subsequences that can be formed ; Stores the number of subsequences ; Iterate over the map ; Count the number of subsequences that can be formed from x . first ; Number of occurrences of x . first which are left ; Return the number of subsequences ; Function to create the maximum count of subsequences that can be formed ; Stores the frequency of arr [ ] ; Update the frequency ; Print the number of subsequences ; Driver code ; Given array arr [ ] ; Function call
import java . util . * ; class GFG { public static int No_Of_subsequences ( HashMap < Integer , Integer > mp ) { int count = 0 ; int left = 0 ; for ( Map . Entry < Integer , Integer > x : mp . entrySet ( ) ) { mp . replace ( x . getKey ( ) , x . getValue ( ) + left ) ; count += ( x . getValue ( ) / x . getKey ( ) ) ; left = x . getValue ( ) % x . getKey ( ) ; } return count ; } public static void maximumsubsequences ( int [ ] arr , int n ) { HashMap < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . replace ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } System . out . println ( No_Of_subsequences ( mp ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 1 } ; int N = arr . length ; maximumsubsequences ( arr , N ) ; } }
Check if concatenation of any permutation of given list of arrays generates the given array | Java program for the above approach ; Function to check if it is possible to obtain array by concatenating the arrays in list pieces [ ] ; Stores the index of element in the given array arr [ ] ; Traverse over the list pieces ; If item size is 1 and exists in map ; If item contains > 1 element then check order of element ; If end of the array ; Check the order of elements ; If order is same as the array elements ; Increment idx ; If order breaks ; Otherwise ; Return false if the first element doesn 't exist in m ; Return true ; Driver Code ; Given target list ; Given array of list ; Function Call
import java . util . * ; class GFG { static boolean check ( List < Integer > arr , ArrayList < List < Integer > > pieces ) { Map < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) m . put ( arr . get ( i ) , i ) ; for ( int i = 0 ; i < pieces . size ( ) ; i ++ ) { if ( pieces . get ( i ) . size ( ) == 1 && m . containsKey ( pieces . get ( i ) . get ( 0 ) ) ) { continue ; } else if ( pieces . get ( i ) . size ( ) > 1 && m . containsKey ( pieces . get ( i ) . get ( 0 ) ) ) { int idx = m . get ( pieces . get ( i ) . get ( 0 ) ) ; idx ++ ; if ( idx >= arr . size ( ) ) return false ; for ( int j = 1 ; j < pieces . get ( i ) . size ( ) ; j ++ ) { if ( arr . get ( idx ) . equals ( pieces . get ( i ) . get ( j ) ) ) { idx ++ ; if ( idx >= arr . size ( ) && j < pieces . get ( i ) . size ( ) - 1 ) return false ; } else { return false ; } } } else { return false ; } } return true ; } public static void main ( String [ ] args ) { List < Integer > arr = Arrays . asList ( 1 , 2 , 4 , 3 ) ; ArrayList < List < Integer > > pieces = new ArrayList < > ( ) ; pieces . add ( Arrays . asList ( 1 ) ) ; pieces . add ( Arrays . asList ( 4 , 3 ) ) ; pieces . add ( Arrays . asList ( 2 ) ) ; if ( check ( arr , pieces ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Modify given array to make sum of odd and even indexed elements same | Java program for the above approach ; Function to modify array to make sum of odd and even indexed elements equal ; Stores the count of 0 s , 1 s ; Stores sum of odd and even indexed elements respectively ; Count 0 s ; Count 1 s ; Calculate odd_sum and even_sum ; If both are equal ; Print the original array ; Otherwise ; Print all the 0 s ; For checking even or odd ; Update total count of 1 s ; Print all 1 s ; Driver Code ; Given array arr [ ] ; Function Call
class GFG { static void makeArraySumEqual ( int a [ ] , int N ) { int count_0 = 0 , count_1 = 0 ; int odd_sum = 0 , even_sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( a [ i ] == 0 ) count_0 ++ ; else count_1 ++ ; if ( ( i + 1 ) % 2 == 0 ) even_sum += a [ i ] ; else if ( ( i + 1 ) % 2 > 0 ) odd_sum += a [ i ] ; } if ( odd_sum == even_sum ) { for ( int i = 0 ; i < N ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } else { if ( count_0 >= N / 2 ) { for ( int i = 0 ; i < count_0 ; i ++ ) System . out . print ( "0 ▁ " ) ; } else { int is_Odd = count_1 % 2 ; count_1 -= is_Odd ; for ( int i = 0 ; i < count_1 ; i ++ ) System . out . print ( "1 ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 0 } ; int N = arr . length ; makeArraySumEqual ( arr , N ) ; } }