简体   繁体   English

在数组中搜索值的总和

[英]Searching an array for sum of values

I have a system that generates values in a text file which contains values as below 我有一个在包含以下值的文本文件中生成值的系统

Line 1 : Total value possible 第1行:可能的总价值

Line 2 : No of elements in the array 第2行:数组中的元素数

Line 3(extra lines if required) : The numbers themselves 第3行(如果需要,可以添加额外的行):数字本身

I am now thinking of an approach where I can subtract the total value from the first integer in the array and then searching the array for the remainder and then doing the same until the pair is found. 我现在正在考虑一种方法,我可以从数组中的第一个整数减去总值,然后在数组中搜索剩余的数,然后执行相同的操作直到找到该对。

The other approach is to add the two integers in the array on a permutation and combination basis and finding the pair. 另一种方法是在排列和组合的基础上将两个整数相加并找到对。

As per my analysis the first solution is better since it cuts down on the number of iterations.Is my analysis correct here and is there any other better approach? 根据我的分析,第一个解决方案更好,因为它减少了迭代次数。我的分析在这里正确吗,还有其他更好的方法吗?

Edit : I'll give a sample here to make it more clear Line 1 : 200 Line 2=10 Line 3 : 10 20 80 78 19 25 198 120 12 65 编辑:我将在此处提供一个示例,以使其更清晰第1行:200第2行= 10第3行:10 20 80 78 19 25 198 120 12 65

Now the valid pair here is 80,120 since it sums up to 200 (represented in line one as Total Value possible in the input file) and their positions in the array would be 3,8.So find to this pair I listed out my approach where I take the first element and I subtract it with the Total value possible and searching the other element through basic search algorithms. 现在这里的有效对为80,120,因为它的总数为200(在第一行中表示为输入文件中可能的总值),并且它们在数组中的位置将为3,8。所以找到这个对,我列出了我的方法我选择第一个元素,然后将其与可能的总值相减,然后通过基本搜索算法搜索另一个元素。

Using the example here I first take 10 and subtract it with 200 which gives 190,then I search for 190,if it is found then the pair is found otherwise continue the same process. 在这里使用示例,我首先取10并减去200,得到190,然后搜索190,如果找到了,则找到该对,否则继续相同的过程。

Your problem is vague, but if you are looking for a pair in the array that is summed to a certain number, it can be done in O(n) on average using hash tables. 您的问题含糊不清,但是如果您要在数组中寻找总和达到一定数量的对,则可以使用哈希表平均在O(n)完成。

Iterate the array, and for each element: 迭代数组,并对每个元素进行迭代:
(1) Check if it is in the table. (1)检查是否在表格中。 If it is - stop and return there is such a pair. 如果是的话-停下来再返回,会有这样的一对。
(2) Else: insert num-element to the hash table. (2)其他:在哈希表中插入num-element

If your iteration terminated without finding a match - there is no such pair. 如果您的迭代在没有找到匹配项的情况下终止-没有这样的配对。

pseudo code: 伪代码:

checkIfPairExists(arr,num):
   set <- new empty hash set
   for each element in arr:
        if set.contains(element):
            return true
         else:
            set.add(num-element)
    return false

The general problem of "is there a subset that sums to a certain number" is NP-Hard , and is known as the subset-sum problem , so there is no known polynomial solution to it. “是否存在一个总和等于一定数量的子集”的一般问题是NP-Hard ,被称为子集和问题 ,因此没有已知的多项式解。

If you're trying to find a pair (2) numbers which sum to a third number, in general you'll have something like: 如果您要查找两个数字加起来等于第三个数字,通常您会得到以下信息:

for(i=0;i<N;i++)
   for(j=i+1;j<N;j++)
      if(numbers[i]+numbers[j]==result)
         The answer is <i,j>
         end

which is O(n^2). 就是O(n ^ 2) However, it is possible to do better. 但是,可以做得更好。

If the list of numbers is sorted (which takes O(n log n) time) then you can try: 如果数字列表已排序(需要O(n log n)时间),则可以尝试:

for(i=0;i<N;i++)
   binary_search 'numbers[i+1:N]' for result-numbers[i]
   if search succeeds:
      The answer is <i, search_result_index>
      end

That is you can step through each number and then do a binary search on the remaining list for its companion number. 也就是说,您可以单步浏览每个数字,然后在其余列表上进行二进制搜索以找到其伴随数字。 This takes O(n log n) time. 这需要O(n log n)时间。 You may need to implement the search function above yourself as built-in functions may just walk down the list in O(n) time leading to an O(n^2) result. 您可能需要在自己之上实现search功能,因为内置功能可能会在O(n)时间中沿列表走去,从而导致O(n ^ 2)结果。

For both methods, you'll want to check to for the special case that the current number is equal to your result. 对于这两种方法,您都需要检查一下特殊情况是否当前数等于您的结果。

Both algorithms use no more space than is taken by the array itself. 两种算法使用的空间都不比数组本身占用的空间更多。

Apologies for the coding style, I'm not terribly familiar with Java and it's the ideas here which are important. 对编码风格表示歉意,我对Java并不是很熟悉,这里的想法很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM