Maximum sum of two elements whose digit sum is equal

Given an array arr[] of N positive elements, the task is to find a pair in the array such that the digit sum of elements in the pair are equal and their sum is maximum. Print -1 if it is not possible to find the pair else print the maximum sum.

Examples:

Input: arr[] = {55, 23, 32, 46, 88}
Output: 101
(23, 32) with digit sum 5 and (55, 46)
with digit sum 10 are the only pairs
with equal digit sum. The pair with maximum
sum is (55, 46) i.e. 55 + 46 = 101

Input: arr[] = {18, 19, 23, 15}
Output: -1
Since there are no two elements
whose digit sums are equal.

Approach: Traverse through the array and use a unordered_map to store the maximum number present for a particular digit sum. For every element arr[i], find the digit sum of arr[i] and check if there is a value present in the map for this digit sum, if yes then update the answer by ans = max(ans, arr[i] + map[digSum]). Also, update the map if the value of arr[i] is greater than the value present in the map.



Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the
// digit sum of n
long digitSum(long n)
{
    long sum = 0;
    while (n) {
        sum += (n % 10);
        n /= 10;
    }
    return sum;
}
  
// Function to return the maximum
// sum possible for a pair whose
// digit sum is equal
int findSum(vector<long> arr, int n)
{
  
    unordered_map<long, long> mp;
    long ans = -1;
  
    // Traversing through all the
    // elements of the array
    for (long i = 0; i < n; i++) {
  
        // Finding the digit sum of arr[i]
        long dSum = digitSum(arr[i]);
  
        if (mp[dSum] != 0) {
            ans = max(ans, mp[dSum] + arr[i]);
        }
        mp[dSum] = max(mp[dSum], arr[i]);
    }
    return ans;
}
  
// Driver code
int main()
{
    vector<long> arr = { 55, 23, 32, 46, 88 };
    int n = arr.size();
  
    cout << findSum(arr, n);
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
import java.util.*;
  
class GFG 
{
      
    // Function to return the 
    // digit sum of n 
    static int digitSum(int n) 
    
        int sum = 0
        while (n != 0)
        
            sum += (n % 10); 
            n /= 10
        
        return sum; 
    
      
    // Function to return the maximum 
    // sum possible for a pair whose 
    // digit sum is equal 
    static int findSum(int arr[], int n) 
    
      
        HashMap<Integer, Integer> mp = new HashMap<Integer,Integer>(); 
          
        for (int i = 0; i < 1000 ; i++)
        {
            mp.put(i, 0);
        }
          
        int ans = -1
      
        // Traversing through all the 
        // elements of the array 
        for (int i = 0; i < n; i++) 
        
      
            // Finding the digit sum of arr[i] 
            int dSum = digitSum(arr[i]); 
              
            if ((int)mp.get(dSum) != 0)
            
                ans = Math.max(ans, (int)mp.get(dSum) + arr[i]); 
            
            mp.put(dSum,Math.max((int)mp.get(dSum), arr[i])) ;
        
        return ans; 
    
      
    // Driver code 
    public static void main (String[] args) 
    
        int arr[] = { 55, 23, 32, 46, 88 }; 
        int n = arr.length; 
      
        System.out.println(findSum(arr, n));
    
}
  
// This code is contributed by AnkitRai01
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the
# digit sum of n
def digitSum(n):
    sum = 0
    while (n):
        sum += (n % 10)
        n //= 10
    return sum
  
# Function to return the maximum
# sum possible for a pair whose
# digit sum is equal
def findSum(arr, n):
  
    mp = dict()
    ans = -1
  
    # Traversing through all the
    # elements of the array
    for i in range(n):
  
        # Finding the digit sum of arr[i]
        dSum = digitSum(arr[i])
  
        if (dSum in mp):
            ans = max(ans, mp[dSum] + arr[i])
  
        mp[dSum] = mp.get(dSum, 0) + arr[i]
  
    return ans
  
# Driver code
arr = [55, 23, 32, 46, 88 ]
n = len(arr)
  
print(findSum(arr, n))
  
# This code is contributed by mohit kumar 29
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
using System.Collections.Generic;
  
class GFG 
{
      
    // Function to return the 
    // digit sum of n 
    static int digitSum(int n) 
    
        int sum = 0; 
        while (n != 0)
        
            sum += (n % 10); 
            n /= 10; 
        
        return sum; 
    
      
    // Function to return the maximum 
    // sum possible for a pair whose 
    // digit sum is equal 
    static int findSum(int[] arr, int n) 
    
      
        Dictionary<int,int> mp = new Dictionary<int,int>(); 
          
        for (int i = 0; i < 1000 ; i++)
        {
            mp.Add(i, 0);
        }
          
        int ans = -1; 
      
        // Traversing through all the 
        // elements of the array 
        for (int i = 0; i < n; i++) 
        
      
            // Finding the digit sum of arr[i] 
            int dSum = digitSum(arr[i]); 
              
            if ((int)mp[dSum] != 0)
            
                ans = Math.Max(ans, (int)mp[dSum] + arr[i]); 
            
              
            mp[dSum]=Math.Max((int)mp[dSum], arr[i]);
        
        return ans; 
    
      
    // Driver code 
    public static void Main () 
    
        int[] arr = { 55, 23, 32, 46, 88 }; 
        int n = arr.Length; 
      
        Console.Write(findSum(arr, n));
    
}
  
// This code is contributed by chitranayal
chevron_right

Output:
101

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :