codeparrot/xlcost-text-to-code huggingface dataset:
Maximum water that can be stored between two buildings | Java implementation of the approach ; Return the maximum water that can be stored ; To store the maximum water so far ; Both the pointers are pointing at the first and the last buildings respectively ; While the water can be stored between the currently chosen buildings ; Update maximum water so far and increment i ; Update maximum water so far and decrement j ; Any of the pointers can be updated ( or both ) ; Driver code
import java . util . Arrays ; class GFG { static int maxWater ( int height [ ] , int n ) { int max = 0 ; int i = 0 , j = n - 1 ; while ( i < j ) { if ( height [ i ] < height [ j ] ) { max = Math . max ( max , ( j - i - 1 ) * height [ i ] ) ; i ++ ; } else if ( height [ j ] < height [ i ] ) { max = Math . max ( max , ( j - i - 1 ) * height [ j ] ) ; j -- ; } else { max = Math . max ( max , ( j - i - 1 ) * height [ i ] ) ; i ++ ; j -- ; } } return max ; } public static void main ( String [ ] args ) { int height [ ] = { 2 , 1 , 3 , 4 , 6 , 5 } ; int n = height . length ; System . out . print ( maxWater ( height , n ) ) ; } }
Count the maximum number of elements that can be selected from the array | Java implementation of the approach ; Function to return the maximum count of selection possible from the given array following the given process ; Initialize result ; Sorting the array ; Initialize the select variable ; Loop through array ; If selection is possible ; Increment result ; Increment selection variable ; Driver Code
import java . util . * ; class GFG { static int maxSelectionCount ( int a [ ] , int n ) { int res = 0 ; Arrays . sort ( a ) ; int select = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= select ) { res ++ ; select ++ ; } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 1 , 3 , 5 , 1 , 4 } ; int N = arr . length ; System . out . println ( maxSelectionCount ( arr , N ) ) ; } }
Print combinations of distinct numbers which add up to give sum N | Java implementation of the approach ; arr [ ] to store all the distinct elements index - next location in array num - given number reducedNum - reduced number ; Set to store all the distinct elements ; Base condition ; Iterate over all the elements and store it into the set ; Calculate the sum of all the elements of the set ; Compare whether the sum is equal to n or not , if it is equal to n print the numbers ; Find previous number stored in the array ; Store all the numbers recursively into the arr [ ] ; Function to find all the distinct combinations of n ; Driver code
import java . util . * ; class GFG { static void findCombinationsUtil ( int arr [ ] , int index , int n , int red_num ) { HashSet < Integer > s = new HashSet < > ( ) ; int sum = 0 ; if ( red_num < 0 ) { return ; } if ( red_num == 0 ) { for ( int i = 0 ; i < index ; i ++ ) { s . add ( arr [ i ] ) ; } for ( Integer itr : s ) { sum = sum + itr ; } if ( sum == n ) { for ( Integer i : s ) { System . out . print ( i + " ▁ " ) ; } System . out . println ( ) ; return ; } } int prev = ( index == 0 ) ? 1 : arr [ index - 1 ] ; for ( int k = prev ; k <= n ; k ++ ) { if ( index < n ) { arr [ index ] = k ; findCombinationsUtil ( arr , index + 1 , n , red_num - k ) ; } } } static void findCombinations ( int n ) { int [ ] a = new int [ n ] ; findCombinationsUtil ( a , 0 , n , n ) ; } public static void main ( String arr [ ] ) { int n = 7 ; findCombinations ( n ) ; } }
Check whether an array can be made strictly increasing by modifying atmost one element | Java implementation of the approach ; Function that returns true if arr [ ] can be made strictly increasing after modifying at most one element ; To store the number of modifications required to make the array strictly increasing ; Check whether the first element needs to be modify or not ; Loop from 2 nd element to the 2 nd last element ; Check whether arr [ i ] needs to be modified ; Modifying arr [ i ] ; Check if arr [ i ] is equal to any of arr [ i - 1 ] or arr [ i + 1 ] ; Check whether the last element needs to be modify or not ; If more than 1 modification is required ; Driver code
class GFG { static boolean check ( int arr [ ] , int n ) { int modify = 0 ; if ( arr [ 0 ] > arr [ 1 ] ) { arr [ 0 ] = arr [ 1 ] / 2 ; modify ++ ; } for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( ( arr [ i - 1 ] < arr [ i ] && arr [ i + 1 ] < arr [ i ] ) || ( arr [ i - 1 ] > arr [ i ] && arr [ i + 1 ] > arr [ i ] ) ) { arr [ i ] = ( arr [ i - 1 ] + arr [ i + 1 ] ) / 2 ; if ( arr [ i ] == arr [ i - 1 ] arr [ i ] == arr [ i + 1 ] ) return false ; modify ++ ; } } if ( arr [ n - 1 ] < arr [ n - 2 ] ) modify ++ ; if ( modify > 1 ) return false ; return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 8 , 6 , 9 , 12 } ; int n = arr . length ; if ( check ( arr , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Check if the given matrix is increasing row and column wise | Java implementation of the approach ; Function that returns true if the matrix is strictly increasing ; Check if the matrix is strictly increasing ; Out of bound condition ; Out of bound condition ; Driver code
import java . io . * ; class GFG { static int N = 2 ; static int M = 2 ; static boolean isMatrixInc ( int a [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( i - 1 >= 0 ) { if ( a [ i ] [ j ] <= a [ i - 1 ] [ j ] ) return false ; } if ( j - 1 >= 0 ) { if ( a [ i ] [ j ] <= a [ i ] [ j - 1 ] ) return false ; } } } return true ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 2 , 10 } , { 11 , 20 } } ; if ( isMatrixInc ( a ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Maximize the size of array by deleting exactly k sub | Java implementation of the approach ; Sieve of Eratosthenes ; Function to return the size of the maximized array ; Insert the indices of composite numbers ; Compute the number of prime between two consecutive composite ; Sort the diff vector ; Compute the prefix sum of diff vector ; Impossible case ; Delete sub - arrays of length 1 ; Find the number of primes to be deleted when deleting the sub - arrays ; Driver code
import java . util . * ; class GFG { static int N = 10000005 ; static int [ ] prime = new int [ N ] ; static void seive ( ) { for ( int i = 2 ; i < N ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = i + i ; j < N ; j += i ) { prime [ j ] = 1 ; } } } prime [ 1 ] = 1 ; } static int maxSizeArr ( int arr [ ] , int n , int k ) { ArrayList < Integer > v = new ArrayList < Integer > ( ) ; ArrayList < Integer > diff = new ArrayList < Integer > ( ) ; int num = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] == 1 ) { v . add ( i ) ; } } num = 0 ; for ( int i = 1 ; i < v . size ( ) ; i ++ ) { diff . add ( v . get ( i ) - v . get ( i - 1 ) - 1 ) ; } Collections . sort ( diff ) ; for ( int i = 1 ; i < diff . size ( ) ; i ++ ) { diff . set ( i , diff . get ( i ) + diff . get ( i - 1 ) ) ; } if ( k > n || ( k == 0 && v . size ( ) > 0 ) ) { return - 1 ; } else if ( v . size ( ) <= k ) { return ( n - k ) ; } else if ( v . size ( ) > k ) { int tt = v . size ( ) - k ; int sum = 0 ; sum += diff . get ( tt - 1 ) ; int res = n - ( v . size ( ) + sum ) ; return res ; } return 1 ; } public static void main ( String [ ] args ) { seive ( ) ; int [ ] arr = { 2 , 4 , 2 , 2 , 4 , 2 , 4 , 2 } ; int n = arr . length ; int k = 2 ; System . out . println ( maxSizeArr ( arr , n , k ) ) ; } }
Count number of subsets whose median is also present in the same subset | Java implementation of the approach ; Function to return the factorial of a number ; Function to return the value of nCr ; Function to return a raised to the power n with complexity O ( log ( n ) ) ; Function to return the number of sub - sets whose median is also present in the set ; Number of odd length sub - sets ; Sort the array ; Checking each element for leftmost middle element while they are equal ; Calculate the number of elements in right of rightmost middle element ; Calculate the number of elements in left of leftmost middle element ; Add selected even length subsets to the answer ; Driver code
import java . util . * ; class GFG { static long mod = 1000000007 ; static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } static long powmod ( long a , long n ) { if ( n == 0 ) return 1 ; long pt = powmod ( a , n / 2 ) ; pt = ( pt * pt ) % mod ; if ( n % 2 == 1 ) return ( pt * a ) % mod ; else return pt ; } static long CountSubset ( int [ ] arr , int n ) { long ans = powmod ( 2 , n - 1 ) ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; ++ i ) { int j = i + 1 ; while ( j < n && arr [ j ] == arr [ i ] ) { int r = n - 1 - j ; int l = i ; ans = ( ans + nCr ( l + r , l ) ) % mod ; j ++ ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 2 } ; int n = arr . length ; System . out . println ( CountSubset ( arr , n ) ) ; } }
Count number of subsets whose median is also present in the same subset | Java implementation of the above approach ; Function to store pascal triangle in 2 - d array ; Function to return a raised to the power n with complexity O ( log ( n ) ) ; Function to return the number of sub - sets whose median is also present in the set ; Number of odd length sub - sets ; Sort the array ; Checking each element for leftmost middle element while they are equal ; Calculate the number of elements in right of rightmost middle element ; Calculate the number of elements in left of leftmost middle element ; Add selected even length subsets to the answer ; Driver code
import java . util . Arrays ; class GFG { static long mod = 1000000007 ; static long [ ] [ ] arr = new long [ 1001 ] [ 1001 ] ; static void Preprocess ( ) { arr [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= 1000 ; ++ i ) { arr [ i ] [ 0 ] = 1 ; for ( int j = 1 ; j < i ; ++ j ) { arr [ i ] [ j ] = ( arr [ i - 1 ] [ j - 1 ] + arr [ i - 1 ] [ j ] ) % mod ; } arr [ i ] [ i ] = 1 ; } } static long powmod ( long a , long n ) { if ( n == 0 ) { return 1 ; } long pt = powmod ( a , n / 2 ) ; pt = ( pt * pt ) % mod ; if ( n % 2 == 1 ) { return ( pt * a ) % mod ; } else { return pt ; } } static long CountSubset ( int [ ] val , int n ) { long ans = powmod ( 2 , n - 1 ) ; Arrays . sort ( val ) ; for ( int i = 0 ; i < n ; ++ i ) { int j = i + 1 ; while ( j < n && val [ j ] == val [ i ] ) { int r = n - 1 - j ; int l = i ; ans = ( ans + arr [ l + r ] [ l ] ) % mod ; j ++ ; } } return ans ; } public static void main ( String [ ] args ) { Preprocess ( ) ; int val [ ] = { 2 , 3 , 2 } ; int n = val . length ; System . out . println ( CountSubset ( val , n ) ) ; } }
Reorder the position of the words in alphabetical order | Java implementation of the approach ; Function to print the ordering of words ; Creating list of words and assigning them index numbers ; Sort the list of words lexicographically ; Print the ordering ; Driver Code
import java . util . * ; class GFG { static void reArrange ( String words [ ] , int n ) { HashMap < String , Integer > freq = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { freq . put ( words [ i ] , ( i + 1 ) ) ; } Arrays . sort ( words ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( freq . get ( words [ i ] ) + " ▁ " ) ; } public static void main ( String [ ] args ) { String words [ ] = { " live " , " place " , " travel " , " word " , " sky " } ; int n = words . length ; reArrange ( words , n ) ; } }
Sum of elements in 1 st array such that number of elements less than or equal to them in 2 nd array is maximum | Java implementation of the approach ; Function to return the required sum ; Creating hash array initially filled with zero ; Calculate the frequency of elements of arr2 [ ] ; Running sum of hash array such that hash [ i ] will give count of elements less than or equal to i in arr2 [ ] ; To store the maximum value of the number of elements in arr2 [ ] which are smaller than or equal to some element of arr1 [ ] ; Calculate the sum of elements from arr1 [ ] corresponding to maximum frequency ; Return the required sum ; Driver code
class GFG { static int MAX = 100000 ; static int findSumofEle ( int arr1 [ ] , int m , int arr2 [ ] , int n ) { int hash [ ] = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { hash [ arr2 [ i ] ] ++ ; } for ( int i = 1 ; i < MAX ; i ++ ) { hash [ i ] = hash [ i ] + hash [ i - 1 ] ; } int maximumFreq = 0 ; for ( int i = 0 ; i < m ; i ++ ) { maximumFreq = Math . max ( maximumFreq , hash [ arr1 [ i ] ] ) ; } int sumOfElements = 0 ; for ( int i = 0 ; i < m ; i ++ ) { sumOfElements += ( maximumFreq == hash [ arr1 [ i ] ] ) ? arr1 [ i ] : 0 ; } return sumOfElements ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 2 , 5 , 6 , 8 } ; int arr2 [ ] = { 4 , 10 } ; int m = arr1 . length ; int n = arr2 . length ; System . out . println ( findSumofEle ( arr1 , m , arr2 , n ) ) ; } }
Split the array elements into strictly increasing and decreasing sequence | Java program to implement the above approach ; Function to print both the arrays ; Store both arrays ; Used for hashing ; Iterate for every element ; Increase the count ; If first occurrence ; If second occurrence ; If occurs more than 2 times ; Sort in increasing order ; Print the increasing array ; Sort ; Print the decreasing array ; Driver code
import java . util . * ; class GFG { static void PrintBothArrays ( int a [ ] , int n ) { Vector < Integer > v1 = new Vector < Integer > ( ) , v2 = new Vector < Integer > ( ) ; HashMap < Integer , Integer > mpp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mpp . put ( a [ i ] , ( mpp . get ( a [ i ] ) == null ? 0 : mpp . get ( a [ i ] ) ) + 1 ) ; if ( mpp . get ( a [ i ] ) == 1 ) v1 . add ( a [ i ] ) ; else if ( mpp . get ( a [ i ] ) == 2 ) v2 . add ( a [ i ] ) ; else { System . out . println ( " Not ▁ possible " ) ; return ; } } Collections . sort ( v1 ) ; System . out . println ( " Strictly ▁ increasing ▁ array ▁ is : " ) ; for ( int i = 0 ; i < v1 . size ( ) ; i ++ ) System . out . print ( v1 . get ( i ) + " ▁ " ) ; Collections . sort ( v2 ) ; Collections . reverse ( v2 ) ; System . out . println ( " Strictly decreasing array is : "); for ( int i = 0 ; i < v2 . size ( ) ; i ++ ) System . out . print ( v2 . get ( i ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int a [ ] = { 7 , 2 , 7 , 3 , 3 , 1 , 4 } ; int n = a . length ; PrintBothArrays ( a , n ) ; } }
Iterative selection sort for linked list |  ; Traverse the List ; Traverse the unsorted sublist ; Swap Data
void selectionSort ( node head ) { node temp = head ; while ( temp ) { node min = temp ; node r = temp . next ; while ( r ) { if ( min . data > r . data ) min = r ; r = r . next ; } int x = temp . data ; temp . data = min . data ; min . data = x ; temp = temp . next ; } }
Sort ugly numbers in an array at their relative positions | Java implementation of the approach ; Function that returns true if n is an ugly number ; While divisible by 2 , keep dividing ; While divisible by 3 , keep dividing ; While divisible by 5 , keep dividing ; n must be 1 if it was ugly ; Function to sort ugly numbers in their relative positions ; To store the ugly numbers from the array ; If current element is an ugly number ; Add it to the ArrayList and set arr [ i ] to - 1 ; Sort the ugly numbers ; Position of an ugly number ; Driver code
import java . util . ArrayList ; import java . util . Collections ; class GFG { static boolean isUgly ( int n ) { while ( n % 2 == 0 ) n = n / 2 ; while ( n % 3 == 0 ) n = n / 3 ; while ( n % 5 == 0 ) n = n / 5 ; if ( n == 1 ) return true ; return false ; } static void sortUglyNumbers ( int arr [ ] , int n ) { ArrayList < Integer > list = new ArrayList < > ( ) ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( isUgly ( arr [ i ] ) ) { list . add ( arr [ i ] ) ; arr [ i ] = - 1 ; } } Collections . sort ( list ) ; int j = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == - 1 ) System . out . print ( list . get ( j ++ ) + " ▁ " ) ; else System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 7 , 12 , 10 } ; int n = arr . length ; sortUglyNumbers ( arr , n ) ; } }
Sort elements of the array that occurs in between multiples of K | Java implementation of the approach ; Utility function to print the contents of an array ; Function to sort elements in between multiples of k ; To store the index of previous multiple of k ; If it is not the first multiple of k ; Sort the elements in between the previous and the current multiple of k ; Update previous to be current ; Print the updated array ; Driver code
import java . util . Arrays ; class GFG { static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void sortArr ( int arr [ ] , int n , int k ) { int prev = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { if ( prev != - 1 ) Arrays . sort ( arr , prev + 1 , i ) ; prev = i ; } } printArr ( arr , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 13 , 3 , 7 , 8 , 21 , 13 , 12 } ; int n = arr . length ; int k = 2 ; sortArr ( arr , n , k ) ; } }
Find A and B from list of divisors | Java implementation of the approach ; Function to print A and B all of whose divisors are present in the given array ; Sort the array ; A is the largest element from the array ; Iterate from the second largest element ; If current element is not a divisor of A then it must be B ; If current element occurs more than once ; Print A and B ; Driver code
import java . util . * ; class GfG { static void printNumbers ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int A = arr [ n - 1 ] , B = - 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( A % arr [ i ] != 0 ) { B = arr [ i ] ; break ; } if ( i - 1 >= 0 && arr [ i ] == arr [ i - 1 ] ) { B = arr [ i ] ; break ; } } System . out . print ( " A ▁ = ▁ " + A + " , ▁ B ▁ = ▁ " + B ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 8 , 16 , 1 , 2 , 4 } ; int n = arr . length ; printNumbers ( arr , n ) ; } }
Find the modified array after performing k operations of given type | Java implementation of the approach ; Utility function to print the contents of an array ; Function to remove the minimum value of the array from every element of the array ; Get the minimum value from the array ; Remove the minimum value from every element of the array ; Function to remove every element of the array from the maximum value of the array ; Get the maximum value from the array ; Remove every element of the array from the maximum value of the array ; Function to print the modified array after k operations ; If k is odd then remove the minimum element of the array from every element of the array ; Else remove every element of the array from the maximum value from the array ; Print the modified array ; Driver code
class GFG { static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void removeMin ( int arr [ ] , int n ) { int i , minVal = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) minVal = Math . min ( minVal , arr [ i ] ) ; for ( i = 0 ; i < n ; i ++ ) arr [ i ] = arr [ i ] - minVal ; } static void removeFromMax ( int arr [ ] , int n ) { int i , maxVal = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) maxVal = Math . max ( maxVal , arr [ i ] ) ; for ( i = 0 ; i < n ; i ++ ) arr [ i ] = maxVal - arr [ i ] ; } static void modifyArray ( int arr [ ] , int n , int k ) { if ( k % 2 == 0 ) removeMin ( arr , n ) ; else removeFromMax ( arr , n ) ; printArray ( arr , n ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 4 , 8 , 12 , 16 } ; int n = arr . length ; int k = 2 ; modifyArray ( arr , n , k ) ; } }
Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k | Java implementation of the approach ; Function to return the maximum product value ; To store the product ; Sort the array ; Efficiently finding product including every element once ; Storing values in hash map ; Including the greater repeating values so that product can be maximized ; Driver code
import java . util . * ; class GFG { static long maxProd ( int arr [ ] , int n , int k ) { long product = 1 ; HashMap < Integer , Integer > s = new HashMap < Integer , Integer > ( ) ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . containsKey ( arr [ i ] ) == false ) { product = product * arr [ i ] ; s . put ( arr [ i ] , 1 ) ; } else s . put ( arr [ i ] , s . get ( arr [ i ] ) + 1 ) ; } for ( int j = n - 1 ; j >= 0 && k > 0 ; j -- ) { if ( ( k > ( s . get ( arr [ j ] ) - 1 ) ) && ( ( s . get ( arr [ j ] ) - 1 ) > 0 ) ) { product *= Math . pow ( arr [ j ] , s . get ( arr [ j ] ) - 1 ) ; k = k - s . get ( arr [ j ] ) + 1 ; s . put ( arr [ j ] , 0 ) ; } if ( k <= ( s . get ( arr [ j ] ) - 1 ) && ( ( s . get ( arr [ j ] ) - 1 ) > 0 ) ) { product *= Math . pow ( arr [ j ] , k ) ; break ; } } return product ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 7 , 8 , 2 , 5 , 6 , 8 } ; int n = arr . length ; int k = 2 ; System . out . println ( maxProd ( arr , n , k ) ) ; } }
Minimize the sum of the squares of the sum of elements of each group the array is divided into | Java implementation of the approach ; Function to return the minimized sum ; Sort the array to pair the elements ; Variable to hold the answer ; Pair smallest with largest , second smallest with second largest , and so on ; Driver code
import java . util . * ; class GFG { static int findAnswer ( int n , int [ ] arr ) { Arrays . sort ( arr ) ; int sum = 0 ; for ( int i = 0 ; i < n / 2 ; ++ i ) { sum += ( arr [ i ] + arr [ n - i - 1 ] ) * ( arr [ i ] + arr [ n - i - 1 ] ) ; } return sum ; } public static void main ( String [ ] args ) { int [ ] arr = { 53 , 28 , 143 , 5 } ; int n = arr . length ; System . out . println ( findAnswer ( n , arr ) ) ; } }
Merge K sorted arrays | Set 3 ( Using Divide and Conquer Approach ) | Java program to merge K sorted arrays ; Function to perform merge operation ; To store the starting point of left and right array ; to store the size of left and right array ; array to temporarily store left and right array ; storing data in left array ; storing data in right array ; to store the current index of temporary left and right array ; to store the current index for output array ; two pointer merge for two sorted arrays ; Code to drive merge - sort and create recursion tree ; base step to initialize the output array before performing merge operation ; to sort left half ; to sort right half ; merge the left and right half ; Driver Code ; input 2D - array ; Number of arrays ; Output array ; Print merged array
import java . util . * ; class GFG { static int n = 4 ; static void merge ( int l , int r , int [ ] output ) { int l_in = l * n , r_in = ( ( l + r ) / 2 + 1 ) * n ; int l_c = ( ( l + r ) / 2 - l + 1 ) * n ; int r_c = ( r - ( l + r ) / 2 ) * n ; int l_arr [ ] = new int [ l_c ] , r_arr [ ] = new int [ r_c ] ; for ( int i = 0 ; i < l_c ; i ++ ) l_arr [ i ] = output [ l_in + i ] ; for ( int i = 0 ; i < r_c ; i ++ ) r_arr [ i ] = output [ r_in + i ] ; int l_curr = 0 , r_curr = 0 ; int in = l_in ; while ( l_curr + r_curr < l_c + r_c ) { if ( r_curr == r_c || ( l_curr != l_c && l_arr [ l_curr ] < r_arr [ r_curr ] ) ) { output [ in ] = l_arr [ l_curr ] ; l_curr ++ ; in ++ ; } else { output [ in ] = r_arr [ r_curr ] ; r_curr ++ ; in ++ ; } } } static void divide ( int l , int r , int [ ] output , int arr [ ] [ ] ) { if ( l == r ) { for ( int i = 0 ; i < n ; i ++ ) output [ l * n + i ] = arr [ l ] [ i ] ; return ; } divide ( l , ( l + r ) / 2 , output , arr ) ; divide ( ( l + r ) / 2 + 1 , r , output , arr ) ; merge ( l , r , output ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 5 , 7 , 15 , 18 } , { 1 , 8 , 9 , 17 } , { 1 , 4 , 7 , 7 } } ; int k = arr . length ; int [ ] output = new int [ n * k ] ; divide ( 0 , k - 1 , output , arr ) ; for ( int i = 0 ; i < n * k ; i ++ ) System . out . print ( output [ i ] + " ▁ " ) ; } }