Greatest number that can be formed from a pair in a given Array | Java implementation to find the greatest number from the given pairs of the array ; Function to find the greatest number formed from the pairs ; First append Y at the end of X ; Then append X at the end of Y ; Now see which of the two formed numbers is greater than other ; Function to find pairs from array ; Iterate through all pairs ; Driver code
import java . util . * ; class GFG { static String getNumber ( int a , int b ) { String X = Integer . toString ( a ) ; String Y = Integer . toString ( b ) ; String XY = X + Y ; String YX = Y + X ; return XY . compareTo ( YX ) > 0 ? XY : YX ; } static void printMaxPair ( int arr [ ] , int n ) { int largest = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { int number = Integer . parseInt ( getNumber ( arr [ i ] , arr [ j ] ) ) ; largest = Math . max ( largest , number ) ; } System . out . println ( largest ) ; } public static void main ( String [ ] args ) { int a [ ] = { 23 , 14 , 16 , 25 , 3 , 9 } ; int n = a . length ; printMaxPair ( a , n ) ; } }