codeparrot/xlcost-text-to-code huggingface dataset:
Minimize swaps required to make all prime | Java program for the above approach ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to count minimum number of swaps required ; To count the minimum number of swaps required to convert the array into perfectly prime ; To count total number of prime indexes in the array ; To count the total number of prime numbers in the array ; Check whether index is prime or not ; Element is not prime ; If the total number of prime numbers is greater than or equal to the total number of prime indices , then it is possible to convert the array into perfectly prime ; Driver Code ; Pre - calculate prime [ ]
import java . io . * ; class GFG { static boolean prime [ ] = new boolean [ ( int ) 1e4 + 2 ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < ( int ) 1e4 + 2 ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p < ( int ) 1e4 + 2 ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i < ( int ) 1e4 + 2 ; i += p ) prime [ i ] = false ; } } } static int countMin ( int [ ] arr , int n ) { int cMinSwaps = 0 ; int cPrimeIndices = 0 ; int cPrimeNos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ i + 1 ] ) { cPrimeIndices ++ ; if ( prime [ arr [ i ] ] == false ) cMinSwaps ++ ; else cPrimeNos ++ ; } else if ( prime [ arr [ i ] ] ) { cPrimeNos ++ ; } } if ( cPrimeNos >= cPrimeIndices ) return cMinSwaps ; else return - 1 ; } public static void main ( String [ ] args ) { SieveOfEratosthenes ( ) ; int n = 5 ; int arr [ ] = { 2 , 7 , 8 , 5 , 13 } ; System . out . println ( countMin ( arr , n ) ) ; } }
Find the size of Largest Subset with positive Bitwise AND | Java program for the above approach ; Function to find the largest possible subset having Bitwise AND positive ; Stores the number of set bits at each bit position ; Traverse the given array arr [ ] ; Current bit position ; Loop till array element becomes zero ; If the last bit is set ; Increment frequency ; Divide array element by 2 ; Decrease the bit position ; Size of the largest possible subset ; Driver code
import java . io . * ; class GFG { static void largestSubset ( int a [ ] , int N ) { int bit [ ] = new int [ 32 ] ; for ( int i = 0 ; i < N ; i ++ ) { int x = 31 ; while ( a [ i ] > 0 ) { if ( ( int ) ( a [ i ] & 1 ) == ( int ) 1 ) { bit [ x ] ++ ; } a [ i ] = a [ i ] >> 1 ; x -- ; } } int max = Integer . MIN_VALUE ; for ( int i = 0 ; i < 32 ; i ++ ) { max = Math . max ( max , bit [ i ] ) ; } System . out . println ( max ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 13 , 8 , 2 , 3 } ; int N = arr . length ; largestSubset ( arr , N ) ; } }
Maximum element in connected component of given node for Q queries | Java program for the above approach ; Function to perform the find operation to find the parent of a disjoint set ; FUnction to perform union operation of disjoint set union ; If the rank are the same ; Update the maximum value ; Function to find the maximum element of the set which belongs to the element queries [ i ] ; Stores the parent elements of the sets ; Stores the rank of the sets ; Stores the maxValue of the sets ; Update parent [ i ] and maxValue [ i ] to i ; Add arr [ i ] [ 0 ] and arr [ i ] [ 1 ] elements to the same set ; Find the parent element of the element queries [ i ] ; Print the maximum value of the set which belongs to the element P ; Driver Code
import java . util . * ; class GFG { static int Find ( int [ ] parent , int a ) { return parent [ a ] = ( parent [ a ] == a ) ? a : ( Find ( parent , parent [ a ] ) ) ; } static void Union ( int [ ] parent , int [ ] rank , int [ ] maxValue , int a , int b ) { a = Find ( parent , a ) ; b = Find ( parent , b ) ; if ( a == b ) return ; if ( rank [ a ] == rank [ b ] ) { rank [ a ] ++ ; } if ( rank [ a ] < rank [ b ] ) { int temp = a ; a = b ; b = temp ; } parent [ b ] = a ; maxValue [ a ] = Math . max ( maxValue [ a ] , maxValue [ b ] ) ; } static void findMaxValueOfSet ( int [ ] [ ] arr , int [ ] queries , int R , int N , int M ) { int [ ] parent = new int [ R + 1 ] ; int [ ] rank = new int [ R + 1 ] ; int [ ] maxValue = new int [ R + 1 ] ; for ( int i = 1 ; i < R + 1 ; i ++ ) { parent [ i ] = maxValue [ i ] = i ; } for ( int i = 0 ; i < N ; i ++ ) { Union ( parent , rank , maxValue , arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } for ( int i = 0 ; i < M ; i ++ ) { int P = Find ( parent , queries [ i ] ) ; System . out . print ( maxValue [ P ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int R = 5 ; int [ ] [ ] arr = { { 1 , 2 } , { 2 , 3 } , { 4 , 5 } } ; int [ ] queries = { 2 , 4 , 1 , 3 } ; int N = arr . length ; int M = queries . length ; findMaxValueOfSet ( arr , queries , R , N , M ) ; } }
Maximum sum of segments among all segments formed in array after Q queries | Java program for the above approach ; Stores the maximum integer of the sets for each query ; Function to perform the find operation of disjoint set union ; Function to perform the Union operation of disjoint set union ; Find the parent of a and b ; Update the parent ; Update the sum of set a ; Function to find the maximum element from the sets after each operation ; Stores the parent elements of the sets ; Stores the rank of the sets ; Stores the sum of the sets ; Stores the maximum element for each query ; Initially set is empty ; Update the sum as the current element ; After the last query set will be empty and sum will be 0 ; Check if the current element is not in any set then make parent as current element of the queries ; Check left side of the queries [ i ] is not added in any set ; Add the queries [ i ] and the queries [ i ] - 1 in one set ; Check right side of the queries [ i ] is not added in any set ; Add queries [ i ] and the queries [ i ] + 1 in one set ; Update the maxAns ; Push maxAns to the currMax ; Print currMax values in the reverse order ; Driver Code
import java . util . * ; class GFG { static int maxAns = Integer . MIN_VALUE ; static int Find ( int [ ] parent , int a ) { return parent [ a ] = ( parent [ a ] == a ) ? a : ( Find ( parent , parent [ a ] ) ) ; } static void Union ( int [ ] parent , int [ ] rank , int [ ] setSum , int a , int b ) { a = Find ( parent , a ) ; b = Find ( parent , b ) ; if ( a == b ) return ; if ( rank [ a ] > rank [ b ] ) rank [ a ] ++ ; if ( rank [ b ] > rank [ a ] ) { int x = a ; a = b ; b = x ; } parent [ b ] = a ; setSum [ a ] += setSum [ b ] ; } static void maxValues ( int [ ] arr , int [ ] queries , int N ) { int [ ] parent = new int [ N + 1 ] ; int [ ] rank = new int [ N + 1 ] ; int [ ] setSum = new int [ N + 1 ] ; Vector < Integer > currMax = new Vector < Integer > ( ) ; for ( int i = 1 ; i < N + 1 ; i ++ ) { parent [ i ] = - 1 ; setSum [ i ] = arr [ i - 1 ] ; } currMax . add ( 0 ) ; for ( int i = N - 1 ; i > 0 ; i -- ) { if ( parent [ queries [ i ] ] == - 1 ) { parent [ queries [ i ] ] = queries [ i ] ; } if ( queries [ i ] - 1 >= 0 && parent [ queries [ i ] - 1 ] != - 1 ) { Union ( parent , rank , setSum , queries [ i ] , queries [ i ] - 1 ) ; } if ( queries [ i ] + 1 <= N && parent [ queries [ i ] + 1 ] != - 1 ) { Union ( parent , rank , setSum , queries [ i ] , queries [ i ] + 1 ) ; } maxAns = Math . max ( setSum [ queries [ i ] ] , maxAns ) ; currMax . add ( maxAns ) ; } for ( int i = currMax . size ( ) - 1 ; i >= 0 ; i -- ) { System . out . print ( currMax . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 2 , 5 } ; int [ ] queries = { 3 , 4 , 1 , 2 } ; int N = arr . length ; maxValues ( arr , queries , N ) ; } }
ZigZag Level Order Traversal of an N | Java program for the above approach ; Class containing left and right child of current node and key value ; Function to create a new node ; Function to perform zig zag traversal of the given tree ; Stores the vectors containing nodes in each level of tree respectively ; Create a queue for BFS ; Enqueue Root of the tree ; Standard Level Order Traversal code using queue ; Stores the element in the current level ; Iterate over all nodes of the current level ; Insert all children of the current node into the queue ; Insert curLevel into result ; Loop to Print the ZigZag Level order Traversal of the given tree ; If i + 1 is even reverse the order of nodes in the current level ; Print the node of ith level ; Function Call
import java . util . * ; public class Main { static class Node { public int val ; public Vector < Node > child ; public Node ( int key ) { val = key ; child = new Vector < Node > ( ) ; } } static Node newNode ( int key ) { Node temp = new Node ( key ) ; return temp ; } static void zigzagLevelOrder ( Node root ) { if ( root == null ) return ; Vector < Vector < Integer > > result = new Vector < Vector < Integer > > ( ) ; Vector < Node > q = new Vector < Node > ( ) ; q . add ( root ) ; while ( q . size ( ) > 0 ) { int size = q . size ( ) ; Vector < Integer > curLevel = new Vector < Integer > ( ) ; for ( int i = 0 ; i < size ; i ++ ) { Node node = q . get ( 0 ) ; q . remove ( 0 ) ; curLevel . add ( node . val ) ; for ( int j = 0 ; j < ( node . child ) . size ( ) ; j ++ ) q . add ( node . child . get ( j ) ) ; } result . add ( curLevel ) ; } for ( int i = 0 ; i < result . size ( ) ; i ++ ) { if ( ( i + 1 ) % 2 == 0 ) { Collections . reverse ( result . get ( i ) ) ; } for ( int j = 0 ; j < result . get ( i ) . size ( ) ; j ++ ) System . out . print ( result . get ( i ) . get ( j ) + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; ( root . child ) . add ( newNode ( 2 ) ) ; ( root . child ) . add ( newNode ( 3 ) ) ; ( root . child . get ( 0 ) . child ) . add ( newNode ( 4 ) ) ; ( root . child . get ( 0 ) . child ) . add ( newNode ( 5 ) ) ; ( root . child . get ( 1 ) . child ) . add ( newNode ( 6 ) ) ; ( root . child . get ( 1 ) ) . child . add ( newNode ( 7 ) ) ; ( root . child . get ( 1 ) . child ) . add ( newNode ( 8 ) ) ; zigzagLevelOrder ( root ) ; } }
Check if possible to make Array sum equal to Array product by replacing exactly one element | Java program for the above approach ; Function to check if it is possible to form an array whose sum and the product is the same or not ; Find the sum of the array initialize sum ; Iterate through all elements and add them to sum ; Find the product of the array ; Check a complete integer y for every x ; If got such y ; If no such y exist ; Driver Code
import java . util . * ; class GFG { static int canPossibleReplacement ( int N , int [ ] arr ) { int S = 0 ; int i ; for ( i = 0 ; i < arr . length ; i ++ ) S += arr [ i ] ; int P = 1 ; for ( i = 0 ; i < arr . length ; i ++ ) { P *= i ; } for ( int x : arr ) { int y = ( S - x ) / ( P / x - 1 ) ; if ( ( S - x + y ) == ( P * y ) / x ) return 1 ; } return 0 ; } public static void main ( String [ ] args ) { int N = 3 ; int arr [ ] = { 1 , 3 , 4 } ; if ( canPossibleReplacement ( N , arr ) == 1 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Check if all the digits of the given number are same | Java program for the above approach ; Function to check if all the digits in the number N is the same or not ; Get the length of N ; Form the number M of the type K * 111. . . where K is the rightmost digit of N ; Check if the numbers are equal ; Otherwise ; Driver Code
import java . io . * ; class GFG { public static String checkSameDigits ( int N ) { int length = ( ( int ) Math . log10 ( N ) ) + 1 ; int M = ( ( int ) Math . pow ( 10 , length ) - 1 ) / ( 10 - 1 ) ; M *= N % 10 ; if ( M == N ) return " Yes " ; return " No " ; } public static void main ( String args [ ] ) throws IOException { int N = 222 ; System . out . println ( checkSameDigits ( N ) ) ; } }
Maximize sum of path from the Root to a Leaf node in N | Java program for the above approach ; Stores the maximum sum of a path ; Structure of a node in the tree ; Utility function to create a new node in the tree ; Recursive function to calculate the maximum sum in a path using DFS ; If current node is a leaf node ; Traversing all children of the current node ; Recursive call for all the children nodes ; Given Generic Tree ; Function Call
import java . util . * ; public class Main { static int maxSumPath = 0 ; static class Node { public int val ; public Vector < Node > child ; public Node ( int key ) { val = key ; child = new Vector < Node > ( ) ; } } static Node newNode ( int key ) { Node temp = new Node ( key ) ; return temp ; } static void DFS ( Node root , int sum ) { if ( root . child . size ( ) == 0 ) { maxSumPath = Math . max ( maxSumPath , sum ) ; return ; } for ( int i = 0 ; i < root . child . size ( ) ; i ++ ) { DFS ( root . child . get ( i ) , sum + root . child . get ( i ) . val ) ; } } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; ( root . child ) . add ( newNode ( 2 ) ) ; ( root . child ) . add ( newNode ( 3 ) ) ; ( root . child . get ( 0 ) . child ) . add ( newNode ( 4 ) ) ; ( root . child . get ( 1 ) . child ) . add ( newNode ( 6 ) ) ; ( root . child . get ( 0 ) . child ) . add ( newNode ( 5 ) ) ; ( root . child . get ( 1 ) ) . child . add ( newNode ( 7 ) ) ; ( root . child . get ( 1 ) . child ) . add ( newNode ( 8 ) ) ; DFS ( root , root . val ) ; System . out . print ( maxSumPath ) ; } }
Count of even sum triplets in the array for Q range queries | package whatever do not write package name here ; Function to count number of triplets with even sum in range l , r for each query ; Initialization of array ; Initialization of variables ; Traversing array ; If element is odd ; If element is even ; Storing count of even and odd till each i ; Traversing each query ; Count of odd numbers in l to r ; Count of even numbers in l to r ; Finding the ans ; Printing the ans ; Driver Code ; Given Input ; Function Call
import java . io . * ; class GFG { static void countTriplets ( int size , int queries , int [ ] arr , int [ ] [ ] Q ) { int [ ] arr_even = new int [ size + 1 ] ; int [ ] arr_odd = new int [ size + 1 ] ; int even = 0 ; int odd = 0 ; arr_even [ 0 ] = 0 ; arr_odd [ 0 ] = 0 ; for ( int i = 0 ; i < size ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { odd ++ ; } else { even ++ ; } arr_even [ i + 1 ] = even ; arr_odd [ i + 1 ] = odd ; } for ( int i = 0 ; i < queries ; i ++ ) { int l = Q [ i ] [ 0 ] , r = Q [ i ] [ 1 ] ; odd = arr_odd [ r ] - arr_odd [ l - 1 ] ; even = arr_even [ r ] - arr_even [ l - 1 ] ; int ans = ( even * ( even - 1 ) * ( even - 2 ) ) / 6 + ( odd * ( odd - 1 ) / 2 ) * even ; System . out . print ( ans + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 6 , Q = 2 ; int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int [ ] [ ] queries = { { 1 , 3 } , { 2 , 5 } } ; countTriplets ( N , Q , arr , queries ) ; } }
Maximize the rightmost element of an array in k operations in Linear Time | Java program for the above approach ; Function to calculate maximum value of Rightmost element ; Calculating maximum value of Rightmost element ; Checking if arr [ i ] is operationable ; Performing operation of i - th element ; Decreasing the value of k by 1 ; returning the rightmost element ; Driver Code ; Given Input ; Function Call
import java . io . * ; class GFG { static int maxRightmostElement ( int N , int k , int p , int arr [ ] ) { while ( k > 0 ) { for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] >= p ) { arr [ i ] = arr [ i ] - p ; arr [ i + 1 ] = arr [ i + 1 ] + p ; break ; } } k -- ; } return arr [ N - 1 ] ; } public static void main ( String [ ] args ) { int N = 4 , k = 5 , p = 2 ; int arr [ ] = { 3 , 8 , 1 , 4 } ; System . out . println ( maxRightmostElement ( N , k , p , arr ) ) ; } }
Mean of minimum of all possible K | Java program for the above approach ; Function to find the value of nCr ; Base Case ; Find nCr recursively ; Function to find the expected minimum values of all the subsets of size K ; Find the factorials that will be used later ; Find the factorials ; Total number of subsets ; Stores the sum of minimum over all possible subsets ; Iterate over all possible minimum ; Find the mean over all subsets ; Driver Code
import java . util . * ; class GFG { static int nCr ( int n , int r , int f [ ] ) { if ( n < r ) { return 0 ; } return f [ n ] / ( f [ r ] * f [ n - r ] ) ; } static int findMean ( int N , int X ) { int [ ] f = new int [ N + 1 ] ; f [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { f [ i ] = f [ i - 1 ] * i ; } int total = nCr ( N , X , f ) ; int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { count += nCr ( N - i , X - 1 , f ) * i ; } double E_X = ( double ) ( count ) / ( double ) ( total ) ; System . out . print ( String . format ( " % .6f " , E_X ) ) ; return 0 ; } public static void main ( String [ ] args ) { int N = 3 , X = 2 ; findMean ( N , X ) ; } }
Maximize count of odd | Java implementation for the above approach ; To find maximum number of pairs in array with conversion of at most one element ; Initialize count of even elements ; Initialize count of odd elements ; If current number is even then increment x by 1 ; If current number is odd then increment y by 1 ; Initialize the answer by min ( x , y ) ; If difference in count of odd and even is more than 2 than increment answer ; Return final answer ; Driver code ; Given array
import java . io . * ; class GFG { static int maximumNumberofpairs ( int n , int arr [ ] ) { int x = 0 ; int y = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { x ++ ; } else { y ++ ; } } int answer = Math . min ( x , y ) ; if ( Math . abs ( x - y ) >= 2 ) { answer ++ ; } return answer ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 6 , 5 , 10 , 12 } ; int n = arr . length ; System . out . println ( maximumNumberofpairs ( n , arr ) ) ; } }
Sum of product of all unordered pairs in given range with update queries | Java Program for the above approach ; Function to calculate the Pairwise Product Sum in range from L to R ; Loop to iterate over all possible pairs from L to R ; Print answer ; Function to update the Array element at index P to X ; Update the value at Pth index in the array ; Function to solve Q queries ; If Query is of type 1 ; If Query is of type 2 ; Driver Code
import java . util . * ; class GFG { static void pairwiseProductSum ( int a [ ] , int l , int r ) { int sum = 0 ; for ( int j = l - 1 ; j <= r - 1 ; j ++ ) { for ( int k = j + 1 ; k <= r - 1 ; k ++ ) { sum += ( a [ j ] * a [ k ] ) ; } } System . out . print ( sum + "NEW_LINE"); } static void updateArray ( int [ ] a , int p , int x ) { a [ p - 1 ] = x ; } static void solveQueries ( int [ ] a , int n , int Q , int query [ ] [ ] ) { for ( int i = 0 ; i < Q ; i ++ ) { if ( query [ i ] [ 0 ] == 1 ) pairwiseProductSum ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; else updateArray ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; } } public static void main ( String [ ] args ) { int A [ ] = { 5 , 7 , 2 , 3 , 1 } ; int N = A . length ; int Q = 3 ; int query [ ] [ ] = { { 1 , 1 , 3 } , { 2 , 2 , 5 } , { 1 , 2 , 5 } } ; solveQueries ( A , N , Q , query ) ; } }
Find two numbers from their sum and OR | Java code for above approach ; Function to find the two integers from the given sum and Bitwise OR value ; Check if Z is non negative ; Iterate through all the bits ; Find the kth bit of A & B ; Find the kth bit of A | B ; If bit1 = 1 and bit2 = 0 , then there will be no possible pairs ; Print the possible pairs ; Driver Code
import java . util . * ; class GFG { static int MaxBit = 32 ; static void possiblePair ( int X , int Y ) { int Z = Y - X ; if ( Z < 0 ) { System . out . print ( " - 1" ) ; } for ( int k = 0 ; k < MaxBit ; k ++ ) { int bit1 = ( Z >> k ) & 1 ; int bit2 = ( Z >> k ) & 1 ; if ( bit1 != 0 && bit2 == 0 ) { System . out . print ( " - 1" ) ; } } System . out . print ( Z + " ▁ " + X ) ; } public static void main ( String [ ] args ) { int X = 7 , Y = 11 ; possiblePair ( X , Y ) ; } }
Maximum frequencies in each M | Java program for the above approach ; Function to find the frequency of the most common element in each M length subarrays ; Stores frequency of array element ; Stores the maximum frequency ; Iterate for the first sub - array and store the maximum ; Print the maximum frequency for the first subarray ; Iterate over the range [ M , N ] ; Subtract the A [ i - M ] and add the A [ i ] in the map ; Find the maximum frequency ; Print the maximum frequency for the current subarray ; Driver Code
import java . util . * ; class GFG { static void maxFrequencySubarrayUtil ( int [ ] A , int N , int M ) { int i = 0 ; HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; int val = 0 ; for ( ; i < M ; i ++ ) { if ( m . containsKey ( A [ i ] ) ) { m . put ( A [ i ] , m . get ( A [ i ] ) + 1 ) ; } else { m . put ( A [ i ] , 1 ) ; } val = Math . max ( val , m . get ( A [ i ] ) ) ; } System . out . print ( val + " ▁ " ) ; for ( i = M ; i < N ; i ++ ) { if ( m . containsKey ( i - M ) ) { m . put ( i - M , m . get ( i - M ) - 1 ) ; } if ( m . containsKey ( A [ i ] ) ) { m . put ( A [ i ] , m . get ( A [ i ] ) + 1 ) ; } else { m . put ( A [ i ] , 1 ) ; } val = 0 ; for ( Map . Entry < Integer , Integer > x : m . entrySet ( ) ) { val = Math . max ( val , x . getValue ( ) ) ; } System . out . print ( val + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] A = { 1 , 1 , 2 , 2 , 3 , 5 } ; int N = A . length ; int M = 4 ; maxFrequencySubarrayUtil ( A , N , M ) ; } }
Maximum number of pairs of distinct array elements possible by including each element in only one pair | package whatever do not write package name here ; Function to count the maximum number of pairs having different element from the given array ; Stores the frequency of array element ; Stores maximum count of pairs ; Increasing the frequency of every element ; Stores the frequencies of array element from highest to lowest ; Pushing the frequencies to the priority queue ; Iterate until size of PQ > 1 ; Stores the top two element ; Form the pair between the top two pairs ; Decrement the frequencies ; Insert updated frequencies if it is greater than 0 ; Return the total count of resultant pairs ; Driver Code
import java . io . * ; import java . util . * ; class GFG { public static int maximumPairs ( int [ ] a , int n ) { HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int c = a [ i ] ; if ( freq . containsKey ( c ) ) { freq . put ( c , freq . get ( c ) + 1 ) ; } else { freq . put ( c , 1 ) ; } } PriorityQueue < Integer > pq = new PriorityQueue < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . containsKey ( a [ i ] ) ) { pq . add ( freq . get ( a [ i ] ) ) ; freq . remove ( a [ i ] ) ; } } while ( pq . size ( ) > 1 ) { int freq1 = pq . poll ( ) ; int freq2 = pq . poll ( ) ; count ++ ; freq1 -- ; freq2 -- ; if ( freq1 > 0 ) pq . add ( freq1 ) ; if ( freq2 > 0 ) pq . add ( freq2 ) ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 4 , 1 , 4 , 3 } ; int N = 6 ; System . out . println ( maximumPairs ( arr , N ) ) ; } }
Generate an N | Java program for above approach ; Function to print target array ; Sort the given array ; Seeking for index of elements with minimum diff . ; Seeking for index ; To store target array ; Copying element ; Copying remaining element ; Printing target array ; Driver code ; Given Input ; Function Call
import java . io . * ; import java . util . * ; class GFG { public static void printArr ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int minDifference = 1000000007 ; int minIndex = - 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( minDifference > Math . abs ( arr [ i ] - arr [ i - 1 ] ) ) { minDifference = Math . abs ( arr [ i ] - arr [ i - 1 ] ) ; minIndex = i - 1 ; } } int Arr [ ] = new int [ n ] ; Arr [ 0 ] = arr [ minIndex ] ; Arr [ n - 1 ] = arr [ minIndex + 1 ] ; int pos = 1 ; for ( int i = minIndex + 2 ; i < n ; i ++ ) { Arr [ pos ++ ] = arr [ i ] ; } for ( int i = 0 ; i < minIndex ; i ++ ) { Arr [ pos ++ ] = arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( Arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 8 ; int arr [ ] = { 4 , 6 , 2 , 6 , 8 , 2 , 6 , 4 } ; printArr ( arr , N ) ; } }