codeparrot/xlcost-text-to-code huggingface dataset:
Middle of three using minimum comparisons | java program to find middle of three distinct numbers ; Function to find the middle of three number ; x is positive if a is greater than b . x is negative if b is greater than a . ; Similar to x ; Similar to x and y . ; Checking if b is middle ( x and y both are positive ) ; Checking if c is middle ( x and z both are positive ) ; driver code
import java . util . * ; class Middle { public static int middleOfThree ( int a , int b , int c ) { int x = a - b ; int y = b - c ; int z = a - c ; if ( x * y > 0 ) return b ; else if ( x * z > 0 ) return c ; else return a ; } public static void main ( String [ ] args ) { int a = 20 , b = 30 , c = 40 ; System . out . println ( middleOfThree ( a , b , c ) ) ; } }
Find four missing numbers in an array containing elements from 1 to N | Java program to find missing 4 elements in an array of size N where elements are in range from 1 to N + 4. ; Finds missing 4 numbers in O ( N ) time and O ( 1 ) auxiliary space . ; To keep track of 4 possible numbers greater than length of input array In Java , helper is automatically initialized as 0. ; Traverse the input array and mark visited elements either by marking them as negative in arr [ ] or in helper [ ] . ; If element is smaller than or equal to length , mark its presence in arr [ ] ; Mark presence in helper [ ] ; Print all those elements whose presence is not marked . ; Driver code
class Missing4 { public static void missing4 ( int [ ] arr ) { int [ ] helper = new int [ 4 ] ; for ( int i = 0 ; i < arr . length ; i ++ ) { int temp = Math . abs ( arr [ i ] ) ; if ( temp <= arr . length ) arr [ temp - 1 ] *= ( - 1 ) ; else if ( temp > arr . length ) { if ( temp % arr . length != 0 ) helper [ temp % arr . length - 1 ] = - 1 ; else helper [ ( temp % arr . length ) + arr . length - 1 ] = - 1 ; } } for ( int i = 0 ; i < arr . length ; i ++ ) if ( arr [ i ] > 0 ) System . out . print ( i + 1 + " ▁ " ) ; for ( int i = 0 ; i < helper . length ; i ++ ) if ( helper [ i ] >= 0 ) System . out . print ( arr . length + i + 1 + " ▁ " ) ; return ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 7 , 3 , 12 , 5 , 10 , 8 , 4 , 9 } ; missing4 ( arr ) ; } }
Permutation present at the middle of lexicographic ordering of permutations of at most length N made up integers up to K | Java program for the above approach ; Function that finds the middle the lexicographical smallest sequence ; If K is even ; First element is K / 2 ; Remaining elements of the sequence are all integer K ; Stores the sequence when K is odd ; Iterate over the range [ 0 , N / 2 ] ; Check if the sequence ends with in 1 or not ; Remove the sequence ending in 1 ; If it doesn 't end in 1 ; Decrement by 1 ; Insert K to the sequence till its size is N ; Print the sequence stored in the vector ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void lexiMiddleSmallest ( int K , int N ) { if ( K % 2 == 0 ) { System . out . print ( K / 2 + " ▁ " ) ; for ( int i = 0 ; i < N - 1 ; ++ i ) { System . out . print ( K + " ▁ " ) ; } System . out . println ( ) ; return ; } ArrayList < Integer > a = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < N / 2 ; ++ i ) { if ( a . get ( a . size ( ) - 1 ) == 1 ) { a . remove ( a . size ( ) - 1 ) ; } else { int t = a . get ( a . size ( ) - 1 ) - 1 ; a . set ( a . get ( a . size ( ) - 1 ) , t ) ; while ( a . size ( ) < N ) { a . add ( K ) ; } } } for ( int i : a ) { System . out . print ( i + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int K = 2 , N = 4 ; lexiMiddleSmallest ( K , N ) ; } }
Remaining array element after repeated removal of the smallest element from pairs with absolute difference of 2 or 0 | Java program for the above approach ; Function to find the last remaining array element after repeatedly removing the smallest from pairs having absolute difference 2 or 0 ; Sort the given array in ascending order ; Traverse the array ; If difference between adjacent elements is not equal to 0 or 2 ; If operations can be performed ; Driver Code
import java . util . * ; class GFG { static void findLastElement ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int i = 0 ; for ( i = 1 ; i < N ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] != 0 && arr [ i ] - arr [ i - 1 ] != 2 ) { System . out . println ( " - 1" ) ; return ; } } System . out . println ( arr [ N - 1 ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 , 8 , 0 , 8 } ; int N = arr . length ; findLastElement ( arr , N ) ; } }
Maximize count of subsets into which the given array can be split such that it satisfies the given condition | Java program for the above approach ; Function to count maximum subsets into which the given array can be split such that it satisfies the given condition ; Sort the array in decreasing order ; Stores count of subsets possible ; Stores count of elements in current subset ; Traverse the array arr [ ] ; Update size ; If product of the smallest element present in the current subset and size of current subset is >= K ; Update maxSub ; Update size ; Driver Code ; Given array ; Size of the array ; Given value of X
import java . util . * ; class GFG { static void maxDivisions ( Integer arr [ ] , int N , int X ) { Arrays . sort ( arr , Collections . reverseOrder ( ) ) ; int maxSub = 0 ; int size = 0 ; for ( int i = 0 ; i < N ; i ++ ) { size ++ ; if ( arr [ i ] * size >= X ) { maxSub ++ ; size = 0 ; } } System . out . print ( maxSub + "NEW_LINE"); } public static void main ( String [ ] args ) { Integer arr [ ] = { 1 , 3 , 3 , 7 } ; int N = arr . length ; int X = 3 ; maxDivisions ( arr , N , X ) ; } }
Maximize sum of second minimums in all quadruples of a given array | Java program for the above approach ; Function to find maximum possible sum of second minimums in each quadruple ; Sort the array ; Add the second minimum ; Print maximum possible sum ; Driver Code ; Given array ; Size of the array
import java . io . * ; import java . util . * ; class GFG { public static void maxPossibleSum ( int [ ] arr , int N ) { Arrays . sort ( arr ) ; int sum = 0 ; int j = N - 3 ; while ( j >= 0 ) { sum += arr [ j ] ; j -= 3 ; } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 7 , 4 , 5 , 2 , 3 , 1 , 5 , 9 } ; int N = arr . length ; maxPossibleSum ( arr , N ) ; } }
Difference between Insertion sort and Selection sort | Java program for the above approach ; Function to sort an array using insertion sort ; Move elements of arr [ 0. . i - 1 ] , that are greater than key to one position ahead of their current position ; Function to print an array of size N ; Print the array ; Driver code ; Function Call
import java . util . * ; class GFG { static void insertionSort ( int arr [ ] , int n ) { int i , key , j ; for ( i = 1 ; i < n ; i ++ ) { key = arr [ i ] ; j = i - 1 ; while ( j >= 0 && arr [ j ] > key ) { arr [ j + 1 ] = arr [ j ] ; j = j - 1 ; } arr [ j + 1 ] = key ; } } static void printArray ( int arr [ ] , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 11 , 13 , 5 , 6 } ; int N = arr . length ; insertionSort ( arr , N ) ; printArray ( arr , N ) ; } }
Count pairs ( i , j ) from given array such that i K * arr [ j ] | Java program for the above approach ; Function to find the count required pairs ; Stores count of pairs ; Traverse the array ; Check if the condition is satisfied or not ; Driver Code ; Function Call
class GFG { static void getPairs ( int arr [ ] , int N , int K ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] > K * arr [ i + 1 ] ) count ++ ; } } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 2 , 1 } ; int N = arr . length ; int K = 2 ; getPairs ( arr , N , K ) ; } }
Count pairs ( i , j ) from given array such that i K * arr [ j ] | Java program for the above approach ; Function to merge two sorted arrays ; i : index to left subarray ; j : index to right subarray ; Stores count of pairs that satisfy the given condition ; Traverse to check for the valid conditions ; If condition satisfies ; All elements in the right side of the left subarray also satisfies ; Sort the two given arrays and store in the resultant array ; Elements which are left in the left subarray ; Elements which are left in the right subarray ; Return the count obtained ; Function to partition array into two halves ; Same as ( l + r ) / 2 , but avoids overflow for large l and h ; Sort first and second halves ; Call the merging function ; Function to print the count of required pairs using Merge Sort ; Driver code ; Function Call
class GFG { static int merge ( int arr [ ] , int temp [ ] , int l , int m , int r , int K ) { int i = l ; int j = m + 1 ; int cnt = 0 ; for ( i = l ; i <= m ; i ++ ) { boolean found = false ; while ( j <= r ) { if ( arr [ i ] >= K * arr [ j ] ) { found = true ; } else break ; j ++ ; } if ( found == true ) { cnt += j - ( m + 1 ) ; j -- ; } } int k = l ; i = l ; j = m + 1 ; while ( i <= m && j <= r ) { if ( arr [ i ] <= arr [ j ] ) temp [ k ++ ] = arr [ i ++ ] ; else temp [ k ++ ] = arr [ j ++ ] ; } while ( i <= m ) temp [ k ++ ] = arr [ i ++ ] ; while ( j <= r ) temp [ k ++ ] = arr [ j ++ ] ; for ( i = l ; i <= r ; i ++ ) arr [ i ] = temp [ i ] ; return cnt ; } static int mergeSortUtil ( int arr [ ] , int temp [ ] , int l , int r , int K ) { int cnt = 0 ; if ( l < r ) { int m = ( l + r ) / 2 ; cnt += mergeSortUtil ( arr , temp , l , m , K ) ; cnt += mergeSortUtil ( arr , temp , m + 1 , r , K ) ; cnt += merge ( arr , temp , l , m , r , K ) ; } return cnt ; } static void mergeSort ( int arr [ ] , int N , int K ) { int temp [ ] = new int [ N ] ; System . out . print ( mergeSortUtil ( arr , temp , 0 , N - 1 , K ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 2 , 5 } ; int N = arr . length ; int K = 2 ; mergeSort ( arr , N , K ) ; } }
Minimize consecutive removals of elements of the same type to empty given array | Java implementation of the above approach ; Function to count minimum consecutive removals of elements of the same type ; Sort the array ; Stores the maximum element present in the array ; Stores sum of the array ; Calculate sum of the array ; Driver Code ; Function call
import java . util . Arrays ; class GFG { static void minRemovals ( int [ ] A , int N ) { Arrays . sort ( A ) ; int mx = A [ N - 1 ] ; int sum = 1 ; for ( int i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; } if ( sum - mx >= mx ) { System . out . println ( 0 ) ; } else { System . out . println ( 2 * mx - sum ) ; } } public static void main ( String [ ] args ) { int [ ] A = { 3 , 3 , 2 } ; int N = A . length ; minRemovals ( A , N ) ; } }
Rearrange given array such that no array element is same as its index | Java program for the above approach ; Function to rearrange the array a [ ] such that none of the array elements is same as its index ; Sort the array ; Traverse the indices [ 0 , N - 2 ] of the given array ; Check if the current element is equal to its index ; If found to be true , swap current element with the next element ; Check if the last element is same as its index ; If found to be true , swap current element with the previous element ; Print the modified array ; Driver Code ; Function Call
import java . util . * ; class GFG { static void rearrangeArray ( int a [ ] , int n ) { Arrays . sort ( a ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( a [ i ] == i + 1 ) { int temp = a [ i ] ; a [ i ] = a [ i + 1 ] ; a [ i + 1 ] = temp ; } } if ( a [ n - 1 ] == n ) { int temp = a [ n - 1 ] ; a [ n - 1 ] = a [ n - 2 ] ; a [ n - 2 ] = temp ; } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( a [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 5 , 3 , 2 , 4 } ; int N = arr . length ; rearrangeArray ( arr , N ) ; } }
Count minimum number of moves to front or end to sort an array | Java program for the above approach ; Function that counts the minimum moves required to covert arr [ ] to brr [ ] ; Base Case ; If arr [ i ] < arr [ j ] ; Include the current element ; Otherwise , excluding the current element ; Function that counts the minimum moves required to sort the array ; If both the arrays are equal ; No moves required ; Otherwise ; Print minimum operations required ; Driver code
import java . util . * ; import java . io . * ; import java . lang . Math ; class GFG { static int minOperations ( int arr1 [ ] , int arr2 [ ] , int i , int j ) { if ( arr1 . equals ( arr2 ) ) return 0 ; if ( i >= arr1 . length j >= arr2 . length ) return 0 ; if ( arr1 [ i ] < arr2 [ j ] ) return 1 + minOperations ( arr1 , arr2 , i + 1 , j + 1 ) ; return Math . max ( minOperations ( arr1 , arr2 , i , j + 1 ) , minOperations ( arr1 , arr2 , i + 1 , j ) ) ; } static void minOperationsUtil ( int [ ] arr ) { int brr [ ] = new int [ arr . length ] ; for ( int i = 0 ; i < arr . length ; i ++ ) brr [ i ] = arr [ i ] ; Arrays . sort ( brr ) ; if ( arr . equals ( brr ) ) System . out . print ( "0" ) ; else System . out . println ( minOperations ( arr , brr , 0 , 0 ) ) ; } public static void main ( final String [ ] args ) { int arr [ ] = { 4 , 7 , 2 , 3 , 9 } ; minOperationsUtil ( arr ) ; } }
Check if a string can be transformed to another by sorting substrings | Java Program to implement the above approach ; Function to check if str1 can be transformed to t by sorting subStrings ; Occur [ i ] stores the indices of char ( ' a ' + i ) in String s ; idx [ i ] stores the next available index of char ( ' a ' + i ) in occur [ i ] ; If this char is not available anymore ; Conversion not possible ; If one of the smaller characters is available and occurs before ; Conversion not possible ; Print the answer ; Driver Code
import java . util . * ; class GFG { static void canTransform ( String s , String t ) { int n = s . length ( ) ; Vector < Integer > occur [ ] = new Vector [ 26 ] ; for ( int i = 0 ; i < occur . length ; i ++ ) occur [ i ] = new Vector < Integer > ( ) ; for ( int x = 0 ; x < n ; x ++ ) { char ch = ( char ) ( s . charAt ( x ) - ' a ' ) ; occur [ ch ] . add ( x ) ; } int [ ] idx = new int [ 26 ] ; boolean poss = true ; for ( int x = 0 ; x < n ; x ++ ) { char ch = ( char ) ( t . charAt ( x ) - ' a ' ) ; if ( idx [ ch ] >= occur [ ch ] . size ( ) ) { poss = false ; break ; } for ( int small = 0 ; small < ch ; small ++ ) { if ( idx [ small ] < occur [ small ] . size ( ) && occur [ small ] . get ( idx [ small ] ) < occur [ ch ] . get ( idx [ ch ] ) ) { poss = false ; break ; } } idx [ ch ] ++ ; } if ( poss ) { System . out . print ( " Yes " + "NEW_LINE"); } else { System . out . print ( " No " + "NEW_LINE"); } } public static void main ( String [ ] args ) { String s , t ; s = " hdecb " ; t = " cdheb " ; canTransform ( s , t ) ; } }
Check whether two strings can be made equal by reversing substring of equal length from both strings | Java program for the above approach ; Function to count inversion count of the string ; For storing frequency ; Add all the characters which are less than the ith character before i . ; Adding the count to inversion count ; Updating the character in the frequency array ; Function to check whether any of the string have a repeated character ; Function to check whether the string S1 and S2 can be made equal by reversing sub strings of same size in both strings ; Frequency array to check whether both string have same character or not ; Adding the frequency ; ; If the character is not in S1 ; Decrementing the frequency ; If both string doesnot have same characters or not ; Finding inversion count of both strings ; If inversion count is same , or have same parity or if any of the string have a repeated character then the answer is Yes else No ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int inversionCount ( String s ) { int [ ] freq = new int [ 26 ] ; int inv = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int temp = 0 ; for ( int j = 0 ; j < ( int ) ( s . charAt ( i ) - ' a ' ) ; j ++ ) temp += freq [ j ] ; inv += ( i - temp ) ; freq [ s . charAt ( i ) - ' a ' ] ++ ; } return inv ; } static boolean haveRepeated ( String S1 , String S2 ) { int [ ] freq = new int [ 26 ] ; for ( char i : S1 . toCharArray ( ) ) { if ( freq [ i - ' a ' ] > 0 ) return true ; freq [ i - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) freq [ i ] = 0 ; for ( char i : S2 . toCharArray ( ) ) { if ( freq [ i - ' a ' ] > 0 ) return true ; freq [ i - ' a ' ] ++ ; } return false ; } static void checkToMakeEqual ( String S1 , String S2 ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < S1 . length ( ) ; i ++ ) { freq [ S1 . charAt ( i ) - ' a ' ] ++ ; } boolean flag = false ; for ( int i = 0 ; i < S2 . length ( ) ; i ++ ) { if ( freq [ S2 . charAt ( i ) - ' a ' ] == 0 ) { flag = true ; break ; } freq [ S2 . charAt ( i ) - ' a ' ] -- ; } if ( flag == true ) { System . out . println ( " No " ) ; return ; } int invCount1 = inversionCount ( S1 ) ; int invCount2 = inversionCount ( S2 ) ; if ( invCount1 == invCount2 || ( invCount1 & 1 ) == ( invCount2 & 1 ) || haveRepeated ( S1 , S2 ) ) { System . out . println ( " Yes " ) ; } else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String S1 = " abbca " , S2 = " acabb " ; checkToMakeEqual ( S1 , S2 ) ; } }
Sort a Bitonic Array | Java program for the above approach ; Function to Sort a Bitonic array in constant space ; Initialize the value of k ; In each iteration compare elements k distance apart and swap if they are not in order ; k is reduced to half after every iteration ; Print the array elements ; Driver code ; Given array arr [ ] ; Function call
import java . io . * ; class GFG { static void sortArr ( int a [ ] , int n ) { int i , k ; k = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ; k = ( int ) Math . pow ( 2 , k ) ; while ( k > 0 ) { for ( i = 0 ; i + k < n ; i ++ ) if ( a [ i ] > a [ i + k ] ) { int tmp = a [ i ] ; a [ i ] = a [ i + k ] ; a [ i + k ] = tmp ; } k = k / 2 ; } for ( i = 0 ; i < n ; i ++ ) { System . out . print ( a [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 20 , 30 , 40 , 36 , 33 , 25 , 15 , 10 } ; int n = arr . length ; sortArr ( arr , n ) ; } }
Split array into K subsets to maximize their sum of maximums and minimums | Java program to implement the above approach ; Function that prints the maximum sum possible ; Find elements in each group ; Sort all elements in non - descending order ; Add K largest elements ; For sum of minimum elements from each subset ; Printing the maximum sum ; Driver code
import java . util . Arrays ; class GFG { static void maximumSum ( int arr [ ] , int n , int k ) { int elt = n / k ; int sum = 0 ; Arrays . sort ( arr ) ; int count = 0 ; int i = n - 1 ; while ( count < k ) { sum += arr [ i ] ; i -- ; count ++ ; } count = 0 ; i = 0 ; while ( count < k ) { sum += arr [ i ] ; i += elt - 1 ; count ++ ; } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int Arr [ ] = { 1 , 13 , 7 , 17 , 6 , 5 } ; int K = 2 ; int size = Arr . length ; maximumSum ( Arr , size , K ) ; } }
Minimize sum of smallest elements from K subsequences of length L | Java program to find the minimum possible sum of the smallest elements from K subsequences ; Function to find the minimum sum ; Sort the array ; Calculate sum of smallest K elements ; Return the sum ; Driver Code
import java . util . Arrays ; class GFG { static int findMinSum ( int [ ] arr , int K , int L , int size ) { if ( K * L > size ) return - 1 ; int minsum = 0 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < K ; i ++ ) minsum += arr [ i ] ; return minsum ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 15 , 5 , 1 , 35 , 16 , 67 , 10 } ; int K = 3 ; int L = 2 ; int length = arr . length ; System . out . print ( findMinSum ( arr , K , L , length ) ) ; } }
Kth smallest or largest element in unsorted Array | Set 4 | Java program for the above approach ; Function to find the Kth smallest element in Unsorted Array ; Initialize the max Element as 0 ; Iterate arr [ ] and find the maximum element in it ; Frequency array to store the frequencies ; Counter variable ; Counting the frequencies ; Iterate through the freq [ ] ; Check if num is present in the array ; Increment the counter with the frequency of num ; Checking if we have reached the Kth smallest element ; Return the Kth smallest element ; Driver code ; Given array ; Function call
import java . io . * ; class GFG { static int findKthSmallest ( int [ ] arr , int n , int k ) { int max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } int [ ] counter = new int [ max + 1 ] ; int smallest = 0 ; for ( int i = 0 ; i < n ; i ++ ) { counter [ arr [ i ] ] ++ ; } for ( int num = 1 ; num <= max ; num ++ ) { if ( counter [ num ] > 0 ) { smallest += counter [ num ] ; } if ( smallest >= k ) { return num ; } } return - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 7 , 1 , 4 , 4 , 20 , 15 , 8 } ; int N = arr . length ; int K = 5 ; System . out . print ( findKthSmallest ( arr , N , K ) ) ; } }
Generate all numbers up to N in Lexicographical Order | Java Program to implement the above approach ; Function to print all the numbers up to n in lexicographical order ; Driver Program
import java . util . * ; class GFG { static void lexNumbers ( int n ) { Vector < String > s = new Vector < String > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { s . add ( String . valueOf ( i ) ) ; } Collections . sort ( s ) ; Vector < Integer > ans = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) ans . add ( Integer . valueOf ( s . get ( i ) ) ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( ans . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 15 ; lexNumbers ( n ) ; } }
Sort Matrix in alternating ascending and descending order rowwise | Java implementation to print row of matrix in ascending or descending order alternatively ; Iterate matrix rowwise ; Sort even rows in ascending order ; Compare adjacent elements ; Swap adjacent element ; Sort even rows in descending order ; Compare adjacent elements ; Swap adjacent element ; Printing the final Output ; Driver code
class GFG { static int N = 4 ; static void func ( int a [ ] [ ] ) { int i , j , k ; for ( i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { for ( j = 0 ; j < N ; j ++ ) { for ( k = j + 1 ; k < N ; ++ k ) { if ( a [ i ] [ j ] > a [ i ] [ k ] ) { int temp = a [ i ] [ j ] ; a [ i ] [ j ] = a [ i ] [ k ] ; a [ i ] [ k ] = temp ; } } } } else { for ( j = 0 ; j < N ; j ++ ) { for ( k = j + 1 ; k < N ; ++ k ) { if ( a [ i ] [ j ] < a [ i ] [ k ] ) { int temp = a [ i ] [ j ] ; a [ i ] [ j ] = a [ i ] [ k ] ; a [ i ] [ k ] = temp ; } } } } } for ( i = 0 ; i < N ; i ++ ) { for ( j = 0 ; j < N ; j ++ ) { System . out . print ( a [ i ] [ j ] + " ▁ " ) ; } System . out . print ( "NEW_LINE"); } } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 5 , 7 , 3 , 4 } , { 9 , 5 , 8 , 2 } , { 6 , 3 , 8 , 1 } , { 5 , 8 , 9 , 3 } } ; func ( a ) ; } }