简体   繁体   中英

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

Line 2 : No of elements in the array

Line 3(extra lines if required) : The numbers themselves

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

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.

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.

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.

Iterate the array, and for each element:
(1) Check if it is in the table. If it is - stop and return there is such a pair.
(2) Else: insert num-element to the hash table.

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.

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). 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:

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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