Kembalikan jumlah maksimum dua angka yang digitnya menambah jumlah yang sama
// 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 +"\n");
}
// 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
Arb9i