// Java program for the above approach
import java.util.*;
class GFG{
// Function to find sum of digits
static int digitSum(long n)
{
int sum = 0;
while (n > 0)
{
sum += (n % 10);
n /= 10;
}
return sum;
}
// Function to find maximum sum pair
// having the same sum of digits
static void findMax(int []arr, int n)
{
// Map to store the sum of digits
// in a number as the key and
// the maximum number having
// that sum of digits as the value
HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
int ans = -1, pairi = 0, pairj = 0;
for (int i = 0; i < n; i++) {
// Store the current sum of digits
// of the number in temp
int temp = digitSum(arr[i]);
// If temp is already present
// in the map then update
// ans if the sum is greater
// than the existing value
if (mp.containsKey(temp)) {
if (arr[i] + mp.get(temp) > ans) {
pairi = arr[i];
pairj = mp.get(temp);
ans = pairi + pairj;
}
mp.put(temp, Math.max(arr[i], mp.get(temp)));
}
else
// Change the value in the map
mp.put(temp, arr[i]);
}
System.out.print(pairi+ " " + pairj
+ " " + ans +"
");
}
// Driver Code Starts.
public static void main(String[] args)
{
int []arr = { 55, 23, 32, 46, 88 };
int n = arr.length;
findMax(arr, n);
}
}
// This code is contributed by shikhasingrajput
public static void findMaximum(Integer[] input)
{
// base case
if (input.length <= 1) {
return;
}
// sort the array in descending order
Arrays.sort(input, Comparator.reverseOrder());
// fill `x` with digits at the odd indices of the sorted array
int x = 0;
for (int i = 0; i < input.length; i = i + 2) {
x = x * 10 + input[i];
}
// fill `y` with digits at the even indices of the sorted array
int y = 0;
for (int i = 1; i < input.length; i = i + 2) {
y = y * 10 + input[i];
}
// print `x` and `y`
System.out.println("The two numbers with maximum sum are "
+ x + " and " + y);
}