codeparrot/xlcost-text-to-code huggingface dataset:
Count number of triplets in an array having sum in the range [ a , b ] | Java program to count triplets with sum that lies in given range [ a , b ] . ; Function to find count of triplets having sum less than or equal to val . ; sort the input array . ; Initialize result ; to store sum ; Fix the first element ; Initialize other two elements as corner elements of subarray arr [ j + 1. . k ] ; Use Meet in the Middle concept . ; If sum of current triplet is greater , then to reduce it decrease k . ; If sum is less than or equal to given value , then add possible triplets ( k - j ) to result . ; Function to return count of triplets having sum in range [ a , b ] . ; to store count of triplets . ; Find count of triplets having sum less than or equal to b and subtract count of triplets having sum less than or equal to a - 1. ; Driver Code
import java . util . * ; class GFG { public static int countTripletsLessThan ( int [ ] arr , int n , int val ) { Arrays . sort ( arr ) ; int ans = 0 ; int j , k ; int sum ; for ( int i = 0 ; i < n - 2 ; i ++ ) { j = i + 1 ; k = n - 1 ; while ( j != k ) { sum = arr [ i ] + arr [ j ] + arr [ k ] ; if ( sum > val ) k -- ; else { ans += ( k - j ) ; j ++ ; } } } return ans ; } public static int countTriplets ( int arr [ ] , int n , int a , int b ) { int res ; res = countTripletsLessThan ( arr , n , b ) - countTripletsLessThan ( arr , n , a - 1 ) ; return res ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 5 , 3 , 8 , 4 , 1 , 9 } ; int n = arr . length ; int a = 8 , b = 16 ; System . out . println ( " " + countTriplets ( arr , n , a , b ) ) ; } }
Multiset Equivalence Problem | Java program to check if two given multisets are equivalent ; Create two unordered maps m1 and m2 and insert values of both vectors . ; Now we check if both unordered_maps are same of not . ; Driver code
import java . util . * ; class GFG { static boolean areSame ( int [ ] a , int [ ] b ) { if ( a . length != b . length ) return false ; HashMap < Integer , Integer > m1 , m2 ; m1 = new HashMap < Integer , Integer > ( ) ; m2 = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( m1 . containsKey ( a [ i ] ) ) { m1 . put ( a [ i ] , m1 . get ( a [ i ] ) + 1 ) ; } else { m1 . put ( a [ i ] , 1 ) ; } if ( m2 . containsKey ( b [ i ] ) ) { m2 . put ( b [ i ] , m2 . get ( b [ i ] ) + 1 ) ; } else { m2 . put ( b [ i ] , 1 ) ; } } for ( Map . Entry < Integer , Integer > x : m1 . entrySet ( ) ) { if ( ! m2 . containsKey ( x . getKey ( ) ) || m2 . get ( x . getKey ( ) ) != x . getValue ( ) ) return false ; } return true ; } public static void main ( String args [ ] ) { int [ ] a = { 7 , 7 , 5 } ; int [ ] b = { 7 , 7 , 5 } ; if ( areSame ( a , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Sum of Areas of Rectangles possible for an array | Java code to find sum of all area rectangle possible ; Function to find area of rectangles ; sorting the array in descending order ; store the final sum of all the rectangles area possible ; temporary variable to store the length of rectangle ; Selecting the length of rectangle so that difference between any two number is 1 only . Here length is selected so flag is set ; flag is set means we have got length of rectangle ; length is set to a [ i + 1 ] so that if a [ i ] a [ i + 1 ] is less than by 1 then also we have the correct choice for length ; incrementing the counter one time more as we have considered a [ i + 1 ] element also so . ; Selecting the width of rectangle so that difference between any two number is 1 only . Here width is selected so now flag is again unset for next rectangle ; area is calculated for rectangle ; flag is set false for another rectangle which we can get from elements in array ; incrementing the counter one time more as we have considered a [ i + 1 ] element also so . ; Driver code
import java . io . * ; import java . util . Arrays ; import java . util . * ; class GFG { static int MaxTotalRectangleArea ( Integer [ ] a , int n ) { Arrays . sort ( a , Collections . reverseOrder ( ) ) ; int sum = 0 ; boolean flag = false ; int len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] == a [ i + 1 ] a [ i ] - a [ i + 1 ] == 1 ) && ! flag ) { flag = true ; len = a [ i + 1 ] ; i ++ ; } else if ( ( a [ i ] == a [ i + 1 ] a [ i ] - a [ i + 1 ] == 1 ) && ( flag ) ) { sum = sum + a [ i + 1 ] * len ; flag = false ; i ++ ; } } return sum ; } public static void main ( String args [ ] ) { Integer [ ] a = { 10 , 10 , 10 , 10 , 11 , 10 , 11 , 10 , 9 , 9 , 8 , 8 } ; int n = a . length ; System . out . print ( MaxTotalRectangleArea ( a , n ) ) ; } }
Insertion sort to sort even and odd positioned elements in different orders | Java program to sort even positioned elements in ascending order and odd positioned elements in descending order . ; Function to calculate the given problem . ; checking for odd positioned . ; Inserting even positioned elements in ascending order . ; sorting the even positioned . ; Inserting odd positioned elements in descending order . ; A utility function to print an array of size n ; Driver program to test insertion sort
class GFG { static void evenOddInsertionSort ( int arr [ ] , int n ) { for ( int i = 2 ; i < n ; i ++ ) { int j = i - 2 ; int temp = arr [ i ] ; if ( ( ( i + 1 ) & 1 ) == 1 ) { while ( j >= 0 && temp >= arr [ j ] ) { arr [ j + 2 ] = arr [ j ] ; j -= 2 ; } arr [ j + 2 ] = temp ; } else { while ( j >= 0 && temp <= arr [ j ] ) { arr [ j + 2 ] = arr [ j ] ; j -= 2 ; } arr [ j + 2 ] = temp ; } } } static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . printf ( " % d ▁ " , arr [ i ] ) ; } System . out . printf ( "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 11 , 13 , 5 , 6 } ; int n = arr . length ; evenOddInsertionSort ( arr , n ) ; printArray ( arr , n ) ; } }
Stable sort for descending order | Bubble sort implementation to sort elements in descending order . ; Sorts a [ ] in descending order using bubble sort . ; Driver code
import java . io . * ; import java . util . * ; class GFG { static void print ( ArrayList < Integer > a , int n ) { for ( int i = 0 ; i <= n ; i ++ ) System . out . print ( a . get ( i ) + " ▁ " ) ; System . out . println ( ) ; } static void sort ( ArrayList < Integer > a , int n ) { for ( int i = n ; i >= 0 ; i -- ) for ( int j = n ; j > n - i ; j -- ) if ( a . get ( j ) > a . get ( j - 1 ) ) { int tempswap = a . get ( j ) ; a . remove ( j ) ; a . add ( j , a . get ( j - 1 ) ) ; a . remove ( j - 1 ) ; a . add ( j - 1 , tempswap ) ; } print ( a , n ) ; } public static void main ( String [ ] args ) { int n = 6 ; ArrayList < Integer > a = new ArrayList < Integer > ( ) ; a . add ( 2 ) ; a . add ( 4 ) ; a . add ( 3 ) ; a . add ( 2 ) ; a . add ( 4 ) ; a . add ( 5 ) ; a . add ( 3 ) ; sort ( a , n ) ; } }
Subtraction in the Array | Java implementation of the approach ; Function to perform the given operation on arr [ ] ; Skip elements which are 0 ; Pick smallest non - zero element ; If all the elements of arr [ ] are 0 ; Driver code
import java . util . * ; class GFG { static void operations ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int i = 0 , sum = 0 ; while ( k -- > 0 ) { while ( i < n && arr [ i ] - sum == 0 ) i ++ ; if ( i < n && arr [ i ] - sum > 0 ) { System . out . print ( arr [ i ] - sum + " ▁ " ) ; sum = arr [ i ] ; } else System . out . println ( "0" ) ; } } public static void main ( String args [ ] ) { int k = 5 ; int arr [ ] = { 3 , 6 , 4 , 2 } ; int n = arr . length ; operations ( arr , n , k ) ; } }
Sort an array of 0 s , 1 s and 2 s ( Simple Counting ) | Simple Java program to sort an array of 0 s 1 s and 2 s . ; Variables to maintain the count of 0 ' s , ▁ ▁ 1' s and 2 's in the array ; Putting the 0 's in the  array in starting. ; Putting the 1 ' s ▁ in ▁ the ▁ ▁ array ▁ after ▁ the ▁ 0' s . ; Putting the 2 ' s ▁ in ▁ the ▁ ▁ array ▁ after ▁ the ▁ 1' s ; Prints the array ; Driver function
import java . util . * ; import java . lang . * ; public class GfG { public static void sort012 ( int arr [ ] , int n ) { int count0 = 0 , count1 = 0 ; int count2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) count0 ++ ; if ( arr [ i ] == 1 ) count1 ++ ; if ( arr [ i ] == 2 ) count2 ++ ; } for ( int i = 0 ; i < count0 ; i ++ ) arr [ i ] = 0 ; for ( int i = count0 ; i < ( count0 + count1 ) ; i ++ ) arr [ i ] = 1 ; for ( int i = ( count0 + count1 ) ; i < n ; i ++ ) arr [ i ] = 2 ; printArray ( arr , n ) ; } public static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 } ; int n = 12 ; sort012 ( arr , n ) ; } }
Alternate Lower Upper String Sort | Java program for unusual string sorting ; Function for alternate sorting of string ; Count occurrences of individual lower case and upper case characters ; Traverse through count arrays and one by one pick characters . Below loop takes O ( n ) time considering the MAX is constant . ; Driver function
import java . util . * ; import java . lang . * ; public class GfG { private final static int MAX = 100 ; public static String alternateSort ( String s1 ) { int n = s1 . length ( ) ; char [ ] s = s1 . toCharArray ( ) ; int [ ] lCount = new int [ MAX ] ; int [ ] uCount = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( Character . isUpperCase ( s [ i ] ) ) uCount [ s [ i ] - ' A ' ] ++ ; else lCount [ s [ i ] - ' a ' ] ++ ; } int i = 0 , j = 0 , k = 0 ; while ( k < n ) { while ( i < MAX && uCount [ i ] == 0 ) i ++ ; if ( i < MAX ) { s [ k ++ ] = ( char ) ( ' A ' + i ) ; uCount [ i ] -- ; } while ( j < MAX && lCount [ j ] == 0 ) j ++ ; if ( j < MAX ) { s [ k ++ ] = ( char ) ( ' a ' + j ) ; lCount [ j ] -- ; } } return ( new String ( s ) ) ; } public static void main ( String argc [ ] ) { String str = " bAwutndekWEdkd " ; System . out . println ( alternateSort ( str ) ) ; } }
Job Selection Problem | Java implementation of the above approach ; Create a min - heap ( priority queue ) ; Add all goods to the Queue ; Pop Goods from Queue as long as it is not empty ; Print the good ; Add the Queue to the vector so that total voulme can be calculated ; Calclulating volume of goods left when all are produced . Move from right to left of sequence multiplying each volume by increasing powers of 1 - P starting from 0 ; Print result ; Driver code ; For implementation simplicity days are numbered from 1 to N . Hence 1 based indexing is used ; 10 % loss per day
import java . util . * ; class GFG { static void optimum_sequence_jobs ( int [ ] V , double P ) { int j = 1 , N = V . length - 1 ; double result = 0 ; PriorityQueue < Integer > Queue = new PriorityQueue < > ( ) ; for ( int i = 1 ; i <= N ; i ++ ) Queue . add ( V [ i ] ) ; while ( ! Queue . isEmpty ( ) ) { System . out . print ( Queue . peek ( ) + " ▁ " ) ; V [ j ++ ] = Queue . peek ( ) ; Queue . remove ( ) ; } for ( int i = N ; i >= 1 ; i -- ) result += Math . pow ( ( 1 - P ) , N - i ) * V [ i ] ; System . out . printf ( " % .2f ", result ); } public static void main ( String [ ] args ) { int [ ] V = { - 1 , 3 , 5 , 4 , 1 , 2 , 7 , 6 , 8 , 9 , 10 } ; double P = 0.10 ; optimum_sequence_jobs ( V , P ) ; } }
Sum of Manhattan distances between all pairs of points | Java Program to find sum of Manhattan distance between all the pairs of given points ; Return the sum of distance between all the pair of points . ; for each point , finding distance to rest of the point ; Driven Program
import java . io . * ; class GFG { static int distancesum ( int x [ ] , int y [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) sum += ( Math . abs ( x [ i ] - x [ j ] ) + Math . abs ( y [ i ] - y [ j ] ) ) ; return sum ; } public static void main ( String [ ] args ) { int x [ ] = { - 1 , 1 , 3 , 2 } ; int y [ ] = { 5 , 6 , 5 , 3 } ; int n = x . length ; System . out . println ( distancesum ( x , y , n ) ) ; } }
Sum of Manhattan distances between all pairs of points | Java Program to find sum of Manhattan distances between all the pairs of given points ; Return the sum of distance of one axis . ; sorting the array . ; for each point , finding the distance . ; Driven Program
import java . io . * ; import java . util . * ; class GFG { static int distancesum ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int res = 0 , sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { res += ( arr [ i ] * i - sum ) ; sum += arr [ i ] ; } return res ; } static int totaldistancesum ( int x [ ] , int y [ ] , int n ) { return distancesum ( x , n ) + distancesum ( y , n ) ; } public static void main ( String [ ] args ) { int x [ ] = { - 1 , 1 , 3 , 2 } ; int y [ ] = { 5 , 6 , 5 , 3 } ; int n = x . length ; System . out . println ( totaldistancesum ( x , y , n ) ) ; } }
Sort an array with swapping only with a special element is allowed | Java program to sort an array by moving one space around . ; n is total number of elements . index is index of 999 or space . k is number of elements yet to be sorted . ; print the sorted array when loop reaches the base case ; else if k > 0 and space is at 0 th index swap each number with space and store index at second last index ; if space is neither start of array nor last element of array and element before it greater than / the element next to space ; first swap space and element next to space in case of { 3 , 999 , 2 } make it { 3 , 2 , 999 } ; than swap space and greater element convert { 3 , 2 , 999 } to { 999 , 2 , 3 } ; Wrapper over sortRec . ; Find index of space ( or 999 ) ; Invalid input ; Driver code
class GFG { static void sortRec ( int arr [ ] , int index , int k , int n ) { if ( k == 0 ) { for ( int i = 1 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( 999 ) ; return ; } else if ( k > 0 && index == 0 ) { index = n - 2 ; for ( int i = 1 ; i <= index ; i ++ ) { arr [ i - 1 ] = arr [ i ] ; } arr [ index ] = 999 ; } if ( index - 1 >= 0 && index + 1 < n && arr [ index - 1 ] > arr [ index + 1 ] ) { swap ( arr , index , index + 1 ) ; swap ( arr , index - 1 , index + 1 ) ; } else swap ( arr , index , index - 1 ) ; sortRec ( arr , index - 1 , k - 1 , n ) ; } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } static void sort ( int arr [ ] , int n ) { int index = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 999 ) { index = i ; break ; } } if ( index == - 1 ) return ; sortRec ( arr , index , n , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 999 , 1 } ; int n = arr . length ; sort ( arr , n ) ; } }
Find array with k number of merge sort calls | Java program to find an array that can be sorted with k merge sort calls . ; We make two recursive calls , so reduce k by 2. ; Create an array with values in [ 1 , n ] ; calling unsort function ; Driver code
class GFG { static void unsort ( int l , int r , int a [ ] , int k ) { if ( k < 1 l + 1 == r ) return ; k -= 2 ; int mid = ( l + r ) / 2 ; int temp = a [ mid - 1 ] ; a [ mid - 1 ] = a [ mid ] ; a [ mid ] = temp ; unsort ( l , mid , a , k ) ; unsort ( mid , r , a , k ) ; } static void arrayWithKCalls ( int n , int k ) { if ( k % 2 == 0 ) { System . out . print ( " NO ▁ SOLUTION " ) ; return ; } int a [ ] = new int [ n + 1 ] ; a [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) a [ i ] = i + 1 ; k -- ; unsort ( 0 , n , a , k ) ; for ( int i = 0 ; i < n ; ++ i ) System . out . print ( a [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 10 , k = 17 ; arrayWithKCalls ( n , k ) ; } }
Program to sort string in descending order | Java program to sort a string of characters in descending order ; function to print string in sorted order ; Hash array to keep count of characters . Initially count of all charters is initialized to zero . ; Traverse string and increment count of characters ' a ' - ' a ' will be 0 , ' b ' - ' a ' will be 1 , ; so for location of character in count array we wil do str [ i ] - ' a ' . ; Traverse the hash array and print characters ; Driver code
class GFG { static int MAX_CHAR = 26 ; static void sortString ( String str ) { int charCount [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { charCount [ str . charAt ( i ) - ' a ' ] ++ ; } for ( int i = MAX_CHAR - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < charCount [ i ] ; j ++ ) { System . out . print ( ( char ) ( ' a ' + i ) ) ; } } } public static void main ( String [ ] args ) { String s = " alkasingh " ; sortString ( s ) ; } }
Median after K additional integers | Java program to find median of an array when we are allowed to add additional K integers to it . ; Find median of array after adding k elements ; sorting the array in increasing order . ; printing the median of array . Since n + K is always odd and K < n , so median of array always lies in the range of n . ; Driver code
import java . util . Arrays ; class GFG { static void printMedian ( int arr [ ] , int n , int K ) { Arrays . sort ( arr ) ; System . out . print ( arr [ ( n + K ) / 2 ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 2 , 8 } ; int k = 3 ; int n = arr . length ; printMedian ( arr , n , k ) ; } }
Dual pivot Quicksort | Java program to implement dual pivot QuickSort ; piv [ ] stores left pivot and right pivot . piv [ 0 ] means left pivot and piv [ 1 ] means right pivot ; p is the left pivot , and q is the right pivot . ; If elements are less than the left pivot ; If elements are greater than or equal to the right pivot ; Bring pivots to their appropriate positions . ; Returning the indices of the pivots because we cannot return two elements from a function , we do that using an array . ; Driver code
class GFG { static void swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } static void dualPivotQuickSort ( int [ ] arr , int low , int high ) { if ( low < high ) { int [ ] piv ; piv = partition ( arr , low , high ) ; dualPivotQuickSort ( arr , low , piv [ 0 ] - 1 ) ; dualPivotQuickSort ( arr , piv [ 0 ] + 1 , piv [ 1 ] - 1 ) ; dualPivotQuickSort ( arr , piv [ 1 ] + 1 , high ) ; } } static int [ ] partition ( int [ ] arr , int low , int high ) { if ( arr [ low ] > arr [ high ] ) swap ( arr , low , high ) ; int j = low + 1 ; int g = high - 1 , k = low + 1 , p = arr [ low ] , q = arr [ high ] ; while ( k <= g ) { if ( arr [ k ] < p ) { swap ( arr , k , j ) ; j ++ ; } else if ( arr [ k ] >= q ) { while ( arr [ g ] > q && k < g ) g -- ; swap ( arr , k , g ) ; g -- ; if ( arr [ k ] < p ) { swap ( arr , k , j ) ; j ++ ; } } k ++ ; } j -- ; g ++ ; swap ( arr , low , j ) ; swap ( arr , high , g ) ; return new int [ ] { j , g } ; } public static void main ( String [ ] args ) { int [ ] arr = { 24 , 8 , 42 , 75 , 29 , 77 , 38 , 57 } ; dualPivotQuickSort ( arr , 0 , 7 ) ; System . out . print ( " Sorted ▁ array : ▁ " ) ; for ( int i = 0 ; i < 8 ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } }
A sorting algorithm that slightly improves on selection sort | Java program to implement min max selection sort . ; shifting the min . ; Shifting the max . The equal condition happens if we shifted the max to arr [ min_i ] in the previous swap . ; Driver code
class GFG { static void minMaxSelectionSort ( int [ ] arr , int n ) { for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { int min = arr [ i ] , max = arr [ i ] ; int min_i = i , max_i = i ; for ( int k = i ; k <= j ; k ++ ) { if ( arr [ k ] > max ) { max = arr [ k ] ; max_i = k ; } else if ( arr [ k ] < min ) { min = arr [ k ] ; min_i = k ; } } swap ( arr , i , min_i ) ; if ( arr [ min_i ] == max ) swap ( arr , j , min_i ) ; else swap ( arr , j , max_i ) ; } } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void main ( String [ ] args ) { int arr [ ] = { 23 , 78 , 45 , 8 , 32 , 56 , 1 } ; int n = arr . length ; minMaxSelectionSort ( arr , n ) ; System . out . printf ( "Sorted array:NEW_LINE"); for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } }
Efficiently merging two sorted arrays with O ( 1 ) extra space |  ; Find maximum element of both array ; Increment one two avoid collision of 0 and maximum element of array in modulo operation ; Recover back original element to compare ; Update element by adding multiplication with new number ; Update element by adding multiplication with new number ; Process those elements which are left in array a ; Process those elements which are left in array a ; Finally update elements by dividing with maximum element ; Finally update elements by dividing with maximum element ; Driver code ; Length of a ; length of b ; Function Call
import java . io . * ; class GFG { static void mergeArray ( int a [ ] , int b [ ] , int n , int m ) { int mx = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mx = Math . max ( mx , a [ i ] ) ; } for ( int i = 0 ; i < m ; i ++ ) { mx = Math . max ( mx , b [ i ] ) ; } mx ++ ; int i = 0 , j = 0 , k = 0 ; while ( i < n && j < m && k < ( n + m ) ) { int e1 = a [ i ] % mx ; int e2 = b [ j ] % mx ; if ( e1 <= e2 ) { if ( k < n ) a [ k ] += ( e1 * mx ) ; else b [ k - n ] += ( e1 * mx ) ; i ++ ; k ++ ; } else { if ( k < n ) a [ k ] += ( e2 * mx ) ; else b [ k - n ] += ( e2 * mx ) ; j ++ ; k ++ ; } } while ( i < n ) { int el = a [ i ] % mx ; if ( k < n ) a [ k ] += ( el * mx ) ; else b [ k - n ] += ( el * mx ) ; i ++ ; k ++ ; } while ( j < m ) { int el = b [ j ] % mx ; if ( k < n ) b [ k ] += ( el * mx ) ; else b [ k - n ] += ( el * mx ) ; j ++ ; k ++ ; } for ( int in = 0 ; in < n ; in ++ ) a [ in ] = a [ in ] / mx ; for ( int in = 0 ; in < m ; in ++ ) b [ in ] = b [ in ] / mx ; return ; } public static void main ( String [ ] args ) { int a [ ] = { 3 , 5 , 6 , 8 , 12 } ; int b [ ] = { 1 , 4 , 9 , 13 } ; int n = a . length ; int m = b . length ; mergeArray ( a , b , n , m ) ; System . out . print ( " First ▁ array ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; System . out . println ( ) ; System . out . print ( " Second ▁ array ▁ : ▁ " ) ; for ( int i = 0 ; i < m ; i ++ ) System . out . print ( b [ i ] + " ▁ " ) ; System . out . println ( ) ; } }
Sort an array according to absolute difference with a given value " using ▁ constant ▁ extra ▁ space " | Java program to sort an array based on absolute difference with a given value x . ; Below lines are similar to insertion sort ; Insert arr [ i ] at correct place ; Function to print the array ; Driver code
class GFG { static void arrange ( int arr [ ] , int n , int x ) { for ( int i = 1 ; i < n ; i ++ ) { int diff = Math . abs ( arr [ i ] - x ) ; int j = i - 1 ; if ( Math . abs ( arr [ j ] - x ) > diff ) { int temp = arr [ i ] ; while ( j >= 0 && Math . abs ( arr [ j ] - x ) > diff ) { arr [ j + 1 ] = arr [ j ] ; j -- ; } arr [ j + 1 ] = temp ; } } } static void print ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 5 , 3 , 9 , 2 } ; int n = arr . length ; int x = 7 ; arrange ( arr , n , x ) ; print ( arr , n ) ; } }
Check if a grid can become row | Java program to check if we can make a grid of character sorted using adjacent swaps . ; v [ ] is vector of strings . len is length of strings in every row . ; Driver code ; int len = 5 ; Length of strings
import java . util . Arrays ; class GfG { static boolean check ( String [ ] v , int len ) { int n = v . length ; char [ ] tempArray ; for ( int i = 0 ; i < n ; i ++ ) { tempArray = v [ i ] . toCharArray ( ) ; Arrays . sort ( tempArray ) ; v [ i ] = new String ( tempArray ) ; } for ( int i = 0 ; i < len - 1 ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( v [ i ] . charAt ( j ) > v [ i + 1 ] . charAt ( j ) ) return false ; return true ; } public static void main ( String [ ] args ) { String [ ] v = { " ebcda " , " ihgfj " , " klmno " , " pqrst " , " yvwxu " } ; if ( check ( v , len ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }