找到具有相同数字和的最大和对
给定一个具有N个整数的数组arr ,任务是找到一个具有最大和且具有相同数字和的对。打印该对的总和(如果存在)。否则,打印-1 。
例子:
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
方法:按照以下步骤解决此问题:
- 创建一个映射,例如mp以将数字中的数字总和作为键存储,并将该数字总和作为值的最大数字存储。
- 现在创建一个全局变量ans来存储这个问题的答案。
- 现在开始遍历数组,并且在每次迭代中:
- 检查其数字的总和是否已经存在于地图中。如果是,则将 ans 更改为ans的最大值和两个数之和。
- 如果地图中不存在数字的总和。为它创建一个新键并存储它的值。否则,如果它大于现有数字,则更新地图中的数字。
- 返回ans作为这个问题的答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find sum of digits
int digitSum(long n)
{
long sum = 0;
while (n) {
sum += (n % 10);
n /= 10;
}
return sum;
}
// Function to find maximum sum pair
// having the same sum of digits
void findMax(vector 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
unordered_map mp;
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[temp] != 0) {
if (arr[i] + mp[temp] > ans) {
pairi = arr[i];
pairj = mp[temp];
ans = pairi + pairj;
}
}
// Change the value in the map
mp[temp] = max(arr[i], mp[temp]);
}
cout << pairi << " " << pairj
<< " " << ans << endl;
}
// Driver Code Starts.
int main()
{
vector arr = { 55, 23, 32, 46, 88 };
int n = arr.size();
findMax(arr, n);
return 0;
}
Java
// 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 mp = new HashMap();
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
Python3
# Python Program to implement
# the above approach
# Function to find sum of digits
def digitSum(n):
sum = 0
while (n):
sum += (n % 10)
n = n // 10
return sum
# Function to find maximum sum pair
# having the same sum of digits
def findMax(arr, 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
mp = {}
ans = -1
pairi = 0
pairj = 0
for i in range(n):
# Store the current sum of digits
# of the number in temp
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 (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
# Change the value in the map
mp[temp] = max(arr[i], mp[temp])
print(f"{pairi} {pairj} {ans}")
# Driver Code Starts.
arr = [55, 23, 32, 46, 88]
n = len(arr)
findMax(arr, n)
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find sum of digits
static int digitSum(long n)
{
int sum = 0;
while (n > 0)
{
sum += (int)(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
Dictionary mp = new Dictionary();
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[temp] > ans) {
pairi = arr[i];
pairj = mp[temp];
ans = pairi + pairj;
}
mp[temp] = Math.Max(arr[i], mp[temp]);
}
else
// Change the value in the map
mp[temp] = arr[i];
}
Console.WriteLine(pairi+ " " + pairj + " " + ans );
}
// Driver Code Starts.
public static void Main()
{
int []arr = { 55, 23, 32, 46, 88 };
int n = arr.Length;
findMax(arr, n);
}
}
// This code is contributed by Saurabh Jaiswal
Javascript
输出
46 55 101
时间复杂度: O(N)
辅助空间: O(N)