How to write an algorithm to check if the sum of any two numbers in an array/list matches a given number?

前端 未结 14 1950
萌比男神i
萌比男神i 2021-01-30 11:29

How can I write an algorithm to check if the sum of any two numbers in an array/list matches a given number with a complexity of nlogn?

相关标签:
14条回答
  • 2021-01-30 11:56

    Let us say that we want to find two numbers in the array A that when added together equal N.

    1. Sort the array.
    2. Find the largest number in the array that is less than N/2. Set the index of that number as lower.
    3. Initialize upper to be lower + 1.
    4. Set sum = A[lower] + A[upper].
    5. If sum == N, done.
    6. If sum < N, increment upper.
    7. If sum > N, decrement lower.
    8. If either lower or upper is outside the array, done without any matches.
    9. Go back to 4.

    The sort can be done in O(n log n). The search is done in linear time.

    0 讨论(0)
  • 2021-01-30 11:57
        public void sumOfTwoQualToTargetSum()
        {
            List<int> list= new List<int>();
            list.Add(1);
            list.Add(3);
            list.Add(5);
            list.Add(7);
            list.Add(9);
    
            int targetsum = 12;
    
            int[] arr = list.ToArray();
    
            for (int i = 0; i < arr.Length; i++)
            {
                for (int j = 0; j < arr.Length; j++)
                {
                    if ((i != j) && ((arr[i] + arr[j]) == targetsum))
                    {
                        Console.Write("i =" + i);
                        Console.WriteLine("j =" + j);
                    }
                }
            }
        }
    
    0 讨论(0)
  • 2021-01-30 11:58

    Use a hash table. Insert every number into your hash table, along with its index. Then, let S be your desired sum. For every number array[i] in your initial array, see if S - array[i] exists in your hash table with an index different than i.

    Average case is O(n), worst case is O(n^2), so use the binary search solution if you're afraid of the worst case.

    0 讨论(0)
  • 2021-01-30 12:01

    Step 1 : Sort the array in O(n logn)

    Step 2 : Find two indices

    0<=i<j<=n in a[0..n] such that a[i]+a[j]==k, where k is given key.

    int i=0,j=n;
    while(i<j) {
       int sum = a[i]+a[j];
       if(sum == k)
            print(i,j)
       else if (sum < k)
            i++;
       else if (sum > k)
            j--;
    }
    
    0 讨论(0)
  • 2021-01-30 12:06

    Depends If you want only one sum O(N) or O(N log N) or all sums O(N^2) or O(N^2 log N). In the latter case better uses an FFT>

    0 讨论(0)
  • 2021-01-30 12:07

    This question is missing some more details into it. Like what is the return value, the limitation on the input. I have seen some questions related to that, which can be this question with extra requirement, to return the actual elements that result in the input

    Here is my version of the solution, it should be O(n).

    import java.util.*;
    
    public class IntegerSum {
    
        private static final int[] NUMBERS = {1,2,3,4,5,6,7,8,9,10};
    
        public static void main(String[] args) {
            int[] result = IntegerSum.isSumExist(7);
            System.out.println(Arrays.toString(result));
        }
    
        /**
         * n = x + y
         * 7 = 1 + 6
         * 7 - 1 =  6
         * 7 - 6 = 1
         * The subtraction of one element in the array should result into the value of the other element if it exist;
         */
        public static int[] isSumExist(int n) {
            // validate the input, based on the question
            // This to return the values that actually result in the sum. which is even more tricky
            int[] output = new int[2];
            Map resultsMap = new HashMap<Integer, Integer>();
            // O(n)
            for (int number : NUMBERS) {
                if ( number > n )
                    throw new IllegalStateException("The number is not in the array.");
                if ( resultsMap.containsKey(number) ) {
                    output[0] = number;
                    output[1] = (Integer) resultsMap.get(number);
                    return output;
                }
                resultsMap.put(n - number, number);
            }
            throw new IllegalStateException("The number is not in the array.");
        }
    }
    
    0 讨论(0)
提交回复
热议问题