0

I have the following function:

function myFunction(array, sum){
     for(var i = 0; i < array.length; i++){
          var firstValue = array[i];
              for(var x = i + 1; x < array.length; x++){
                    var secondValue = array[x];
                    if((firstValue + secondValue) == sum){
                        return i + ": " + x; 
                    }
              }
     }
     return "No two array values match the sum";
}

The above takes two parameters; first an array and the second a sum. It finds the first two numbers in the array that when added sums to the second parameter. The array indexes of the two numbers that sum to the second parameter is then returned. Right now, the above function solves the problem in n^2 time. Is there a way to solve the same problem in kn time?

I wrote the function in JavaScript, but this can apply to all modern languages.

1
  • 1
    What is k? If you want sum of only two elements, then it is a two sum problem that can be implemented in linear time for a sorted array. Commented Jan 22, 2014 at 5:55

3 Answers 3

1

It is 2-sum problem. Generally there are 2 efficient algorithms for this problem.

1) Hash method, you create a hash table for each value and for each value v, just look up sum-v.

function myFunction(array, sum){
 hash h;
 for(var i = 0; i < array.length; i++){
      if (h.find(sum-array[i])) return "found";
      h.insert(array[i]);
 }
 return "No two array values match the sum";

}

The time and space complexity are both O(N).

2) Another method is sort the array first and find with 2 index.

function myFunction(array, sum){
     sort(arrary);
     var i = 0, j = array.length-1;
     while (i < j) {
       if (array[i] + array[j] == sum) return "Found";
       else if (array[i] + array[j] > sum) --j;
       else ++i;
     }
     return "No two array values match the sum";
   }

It cost O(NlnN) time and O(1) space.

To return the 2-index of the value, you should store the index in the hash table and the array you sort.

Sign up to request clarification or add additional context in comments.

Comments

1

For this problem, there are two approaches, which are all based on the property of a sorted array.

First step, sort the array, which require O(nlogn) time complexity.

Second step:

  • Approach A: iterating through each value array[i] in the sorted array, use binary search to find in this array value : sum - array[i]. Time complexity for this step is O(nlogn) (or even O(n) if we limit the range for searching is starting from i onward)

  • Approach B: maintaining two pointers, start points at the beginning of the array ( the smallest value) and end points at the end of the array ( the largest value). So we will increase the value of start one by one until start is no longer less than end. For each value where start points to, we compare the value sum - array[start] with array[end]. If value array[end] > sum - array[start], we decrease end, else if array[end] < sum - array[start], return null. Time complexity for this step is O(n).

In total, both approaches give a O(nlogn) time complexity.

Comments

0

I'm not sure what you mean by k, but if k stands for the sum, and the array contains only non negative integers, then you can do it in O(k*n) time. This can be useful when k << n, and it has the benefits of O(1) additional memory, and no modification of the original array.

The idea is to scan the array k+1 times, using i = 0..k. On each scan, the values i and k-i are searched. If both are found, return their positions. If all k+1 scans complete with no match, then the sum can't be achieved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.