Greatest number that can be formed from a pair in a given Array | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; string getNumber ( int a , int b ) { string X = to_string ( a ) ; string Y = to_string ( b ) ; string XY = X + Y ; string YX = Y + X ; return XY > YX ? XY : YX ; } void printMaxPair ( int arr [ ] , int n ) { int largest = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { int number = stoi ( getNumber ( arr [ i ] , arr [ j ] ) ) ; largest = max ( largest , number ) ; } cout << largest ; } int main ( ) { int a [ ] = { 23 , 14 , 16 , 25 , 3 , 9 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; printMaxPair ( a , n ) ; return 0 ; }