原文:
给定不同整数的数组arr[]n。任务是求两个数组整数的和 a[i] a[j] ,出现次数最多。如果有多个答案,请全部打印出来。 举例:
输入: arr[] = {1,8,3,11,4,9,2,7} 输出: 10 12 11 10,12 和 11 之和出现 3 次 7 4 = 11,8 3 = 11,9 2 = 11 1 9 = 10,8 2 = 10,7 3 = 10 1 11 9 3 = 12 输入: arr[] = {3,1,7,11,9,2,12} 输出: 12 14 10 13
方法:可以按照以下步骤解决问题:
- 迭代每对元素。
- 使用散列表计算每个和对出现的次数。
- 最后,遍历散列表,找到出现次数最多的和对。
以下是上述方法的实现:
c
// c implementation of the approach
#include
using namespace std;
// function to find the sum pairs
// that occur the most
void findsumpairs(int a[], int n)
{
// hash-table
unordered_map mpp;
for (int i = 0; i < n - 1; i ) {
for (int j = i 1; j < n; j ) {
// keep a count of sum pairs
mpp[a[i] a[j]] ;
}
}
// variables to store
// maximum occurrence
int occur = 0;
// iterate in the hash table
for (auto it : mpp) {
if (it.second > occur) {
occur = it.second;
}
}
// print all sum pair which occur
// maximum number of times
for (auto it : mpp) {
if (it.second == occur)
cout << it.first << endl;
}
}
// driver code
int main()
{
int a[] = { 1, 8, 3, 11, 4, 9, 2, 7 };
int n = sizeof(a) / sizeof(a[0]);
findsumpairs(a, n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java implementation of above approach
import java.util.*;
class gfg
{
// function to find the sum pairs
// that occur the most
static void findsumpairs(int a[], int n)
{
// hash-table
map mpp = new hashmap<>();
for (int i = 0; i < n - 1; i )
{
for (int j = i 1; j < n; j )
{
// keep a count of sum pairs
mpp.put(a[i] a[j],mpp.get(a[i] a[j])==null?1:mpp.get(a[i] a[j]) 1);
}
}
// variables to store
// maximum occurrence
int occur = 0;
// iterate in the hash table
for (map.entry entry : mpp.entryset())
{
if (entry.getvalue() > occur)
{
occur = entry.getvalue();
}
}
// print all sum pair which occur
// maximum number of times
for (map.entry entry : mpp.entryset())
{
if (entry.getvalue() == occur)
system.out.println(entry.getkey());
}
}
// driver code
public static void main(string args[])
{
int a[] = { 1, 8, 3, 11, 4, 9, 2, 7 };
int n = a.length;
findsumpairs(a, n);
}
}
/* this code is contributed by princiraj1992 */
python 3
# python 3 implementation of the approach
# function to find the sum pairs
# that occur the most
def findsumpairs(a, n):
# hash-table
mpp = {i:0 for i in range(21)}
for i in range(n - 1):
for j in range(i 1, n, 1):
# keep a count of sum pairs
mpp[a[i] a[j]] = 1
# variables to store
# maximum occurrence
occur = 0
# iterate in the hash table
for key, value in mpp.items():
if (value > occur):
occur = value
# print all sum pair which occur
# maximum number of times
for key, value in mpp.items():
if (value == occur):
print(key)
# driver code
if __name__ == '__main__':
a = [1, 8, 3, 11, 4, 9, 2, 7]
n = len(a)
findsumpairs(a, n)
# this code is contributed by
# surendra_gangwar
c
// c# implementation of above approach
using system;
using system.collections.generic;
class gfg
{
// function to find the sum pairs
// that occur the most
static void findsumpairs(int []a, int n)
{
// hash-table
dictionary mpp = new dictionary();
for (int i = 0; i < n - 1; i )
{
for (int j = i 1; j < n; j )
{
// keep a count of sum pairs
if(mpp.containskey(a[i] a[j]))
{
var val = mpp[a[i] a[j]];
mpp.remove(a[i] a[j]);
mpp.add(a[i] a[j], val 1);
}
else
{
mpp.add(a[i] a[j], 1);
}
}
}
// variables to store
// maximum occurrence
int occur = 0;
// iterate in the hash table
foreach(keyvaluepair entry in mpp)
{
if (entry.value > occur)
{
occur = entry.value;
}
}
// print all sum pair which occur
// maximum number of times
foreach(keyvaluepair entry in mpp)
{
if (entry.value == occur)
console.writeline(entry.key);
}
}
// driver code
public static void main(string []args)
{
int []a = { 1, 8, 3, 11, 4, 9, 2, 7 };
int n = a.length;
findsumpairs(a, n);
}
}
// this code is contributed by 29ajaykumar
java 描述语言
output:
10
12
11
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处