Design an algorithm to find all combinations of two elements in an array that are equal to a specified number

Source: Internet
Author: User
Tags int size

Find out if any two numbers in the array are added equal to a K value (assuming that there are two numbers in the array), which is an algorithm question previously asked on 36 Krypton two sides.

Idea 1: Exhaustive method, two-tier for loop

Idea 2: You can use the hash table to store the elements in the array, so we get a number, to judge that Sum-val is not in the array, if in the array, then found a pair of two tuples, their sum of sums, the disadvantage of the algorithm is that need to use a hash table, increase the complexity of space.

Idea 3: The same is based on lookup, we can first sort the array, then after a number, in the array with a binary lookup, to find out if there is a sum-val, if it exists, then found a pair of two-tuple, and their sum, compared to the above method, although not to implement a hash table, There is no need for too much space, but a lot of time. Sort needs O (NLOGN), binary lookup needs (LOGN), lookup n times, so the time complexity is O (NLOGN).

Idea 4: This method is based on the 2nd train of thought, but optimized, in time complexity and space complexity is a compromise, but the algorithm is simple and intuitive, easy to understand. First, sort the array, then use two pointers to the array, one to scan backwards, one to scan from the back, and fist + last < sum to move fist forward if fist + last > Sum, then last move backwards.

Idea 4

public static void Main (String args[]) {
        int[] data = {1, 5, 9,-1, 4, 6,-2, 3,-8};
        Arrays.sort (data);
        PRINTPAIRSUMS1 (data, data.length, 8);
    }

    private static void Printpairsums (int data[], int size, int sum) {
        int-i = 0;
        int last = size-1;
        int s = 0;
        while (a < last) {
            s = Data[first] + data[last];
            if (s = = sum) {
                System.out.println ("-------"  + Data[first] + "+" + data[last] + "=" + sum);
                first++;
                last--;
            } else if (s < sum) {
                first++;
            } else {
                last--}
        }}
    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.