Find maximum sum pair with same digit sum
Last Updated :
15 Dec, 2021
Given an array arr having N integers, the task is to find a pair with maximum sum and having the same sum of digits. Print the sum of that pair, if it exists. Otherwise, print -1.
Examples:
Input: arr[]={55, 23, 32, 46, 88}
Output: 46 55 101
Explanation: Pair {55, 46} will give the sum of 55 + 46 = 101
Input: arr[]={18, 19, 23, 15}
Output: -1
Approach: Follow the below steps to solve this problem:
- Create a map, say mp to store the sum of digits in a number as the key and the maximum number having that sum of digits as the value.
- Now create a global variable ans to store the answer to this problem.
- Now start traversing the array, and in each iteration:
- Check if the sum of its digit is already present in the map. If it is, then change ans with the maximum of ans and the sum of the two numbers.
- If the sum of the digits is not present in the map. Create a new key for it and store its value. Otherwise, update the number in the map if it’s greater than the existing number.
- Return ans as the answer to this problem.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int digitSum( long n)
{
long sum = 0;
while (n) {
sum += (n % 10);
n /= 10;
}
return sum;
}
void findMax(vector< int > arr, int n)
{
unordered_map< int , int > mp;
int ans = -1, pairi = 0, pairj = 0;
for ( int i = 0; i < n; i++) {
int temp = digitSum(arr[i]);
if (mp[temp] != 0) {
if (arr[i] + mp[temp] > ans) {
pairi = arr[i];
pairj = mp[temp];
ans = pairi + pairj;
}
}
mp[temp] = max(arr[i], mp[temp]);
}
cout << pairi << " " << pairj
<< " " << ans << endl;
}
int main()
{
vector< int > arr = { 55, 23, 32, 46, 88 };
int n = arr.size();
findMax(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int digitSum( long n)
{
int sum = 0 ;
while (n > 0 )
{
sum += (n % 10 );
n /= 10 ;
}
return sum;
}
static void findMax( int []arr, int n)
{
HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
int ans = - 1 , pairi = 0 , pairj = 0 ;
for ( int i = 0 ; i < n; i++) {
int temp = digitSum(arr[i]);
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
mp.put(temp, arr[i]);
}
System.out.print(pairi+ " " + pairj
+ " " + ans + "\n" );
}
public static void main(String[] args)
{
int []arr = { 55 , 23 , 32 , 46 , 88 };
int n = arr.length;
findMax(arr, n);
}
}
|
Python3
def digitSum(n):
sum = 0
while (n):
sum + = (n % 10 )
n = n / / 10
return sum
def findMax(arr, n):
mp = {}
ans = - 1
pairi = 0
pairj = 0
for i in range (n):
temp = digitSum(arr[i])
if (temp not in mp):
mp[temp] = 0
if (mp[temp] ! = 0 ) :
if (arr[i] + mp[temp] > ans):
pairi = arr[i]
pairj = mp.get(temp)
ans = pairi + pairj
mp[temp] = max (arr[i], mp[temp])
print (f "{pairi} {pairj} {ans}" )
arr = [ 55 , 23 , 32 , 46 , 88 ]
n = len (arr)
findMax(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int digitSum( long n)
{
int sum = 0;
while (n > 0)
{
sum += ( int )(n % 10);
n /= 10;
}
return sum;
}
static void findMax( int []arr, int n)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
int ans = -1, pairi = 0, pairj = 0;
for ( int i = 0; i < n; i++) {
int temp = digitSum(arr[i]);
if (mp.ContainsKey(temp)) {
if (arr[i] + mp[temp] > ans) {
pairi = arr[i];
pairj = mp[temp];
ans = pairi + pairj;
}
mp[temp] = Math.Max(arr[i], mp[temp]);
}
else
mp[temp] = arr[i];
}
Console.WriteLine(pairi+ " " + pairj + " " + ans );
}
public static void Main()
{
int []arr = { 55, 23, 32, 46, 88 };
int n = arr.Length;
findMax(arr, n);
}
}
|
Javascript
<script>
function digitSum(n) {
let sum = 0;
while (n) {
sum += (n % 10);
n = Math.floor(n / 10);
}
return sum;
}
function findMax(arr, n)
{
let mp = new Map();
let ans = -1, pairi = 0, pairj = 0;
for (let i = 0; i < n; i++) {
let temp = digitSum(arr[i]);
if (!mp.has(temp)) {
mp.set(temp, 0);
}
if (mp.get(temp) != 0) {
if (arr[i] + mp.get(temp) > ans) {
pairi = arr[i];
pairj = mp.get(temp);
ans = pairi + pairj;
}
}
mp.set(temp, Math.max(arr[i], mp.get(temp)));
}
document.write(pairi + " " + pairj
+ " " + ans + '<br>' );
}
let arr = [55, 23, 32, 46, 88];
let n = arr.length;
findMax(arr, n);
</script>
|
Time complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...