Simple Java Solution✅|| Beginner Friendly🔥|| Well Explained🚀
IntuitionThe problem requires us to find two numbers in the given array such that they have the same sum of digits and their sum is maximized. The key observati
# Intuition
The problem requires us to find two numbers in the given array such that they have the same sum of digits and their sum is maximized. The key observation is that if two numbers share the same sum of digits, they form a valid pair. Hence, we need to efficiently group numbers by their digit sum and find the largest two numbers in each group.

---


# Approach
1. Compute the sum of digits:

    - For each number in nums, compute the sum of its digits using a helper function sumCalculator(num).
    
2. Group numbers by digit sum:

    - Use a HashMap<Integer, List<Integer>>, where the key is the sum of digits, and the value is a list of numbers that share that sum.
    
3. Find the maximum sum of a valid pair:

    - Iterate through each group of numbers with the same digit sum.
    - If a group contains at least two numbers, find the largest two numbers and compute their sum.
    - Keep track of the maximum sum encountered.
    
4. Return the result:

    - If no valid pair is found, return -1. Otherwise, return the maximum sum.

---


# Complexity
- Time complexity: O(n)

- Space complexity: O(n)

---


# Code
```java []
class Solution {
    public int maximumSum(int[] nums) {
        Map<Integer, List<Integer>> map = new HashMap<>();
        for(int i=0; i<nums.length; i++) {
            int sum = sumCalculator(nums[i]);
            // System.out.println(sum);
            if(!map.containsKey(sum)) {
                List<Integer> list = new ArrayList<>();
                list.add(nums[i]);
                map.put(sum, list);
            } else {
                List<Integer> list = map.get(sum);
                list.add(nums[i]);
                map.put(sum, list);
            }
        }

        Set<Integer> set = map.keySet();
        int max = -1;
        for(int i: set) {
            List<Integer> list = map.get(i);
            // System.out.println(list);
            if(list.size()>1) {
                Collections.sort(list);
                int val = list.get(list.size()-1) + list.get(list.size()-2);
                max = Math.max(max, val);
            }
            
        }
        return max;
    }

    public int sumCalculator(int num) {
        int sum = 0;
        while(num > 0) {
            int rem = num%10;
            sum += rem;
            num = num/10;
        }
        return sum;
    }
}
```
['Array', 'Hash Table', 'Sorting', 'Java']