Enter an incrementing sorted array and a number s, look for two numbers in the array, and yes their sum is exactly s, if there are many pairs of numbers and equals s, the output of the two numbers is the smallest.

Source: Internet
Author: User

Enter an incrementing sorted array and a number s, look for two numbers in the array, and yes their sum is exactly s, if there are many pairs of numbers and equals s, the output of the two numbers is the smallest.

Two kinds of problem solving step 1: Consider the case with the smallest product:

Public arraylist<integer> findnumberswithsum (int [] array,int sum) {Arraylist<arraylist<integer&gt        ;> lists = new arraylist<> ();            for (int i=0;i<array.length;i++) {arraylist<integer> list = new arraylist<> (); for (int j=i+1;j<array.length;j++) {if (Array[i] + array[j] = = sum) {list.add (Array[i])                    ;                    List.add (Array[j]);                List.add (array[i] * array[j]);        }} if (!list.isempty ()) Lists.add (list);        } if (Lists.size () ==0) return new arraylist<integer> ();        if (Lists.size () ==1) return Lists.get (0); Collections.sort (lists, new comparator<arraylist<integer>> () {@Override public int com                              Pare (arraylist<integer> O1, arraylist<integer> O2) {return O1.get (2). CompareTo (O2.get (2));   }     });        arraylist<integer> result = new arraylist<> ();        arraylist<integer> temp = lists.get (0);        for (int i=0;i<temp.size () -1;i++) {Result.add (Temp.get (i));    } return result; }

2: Order increment according to the elements of the array, regardless of product

Public arraylist<integer> findnumberswithsum (int [] array,int sum) {        arraylist<integer> list = new Arraylist<integer> ();        if (array = = NULL | | Array.Length < 2) {            return list;        }        int i=0,j=array.length-1;        while (i<j) {            if (array[i]+array[j]==sum) {            list.add (array[i]);            List.add (Array[j]);                return list;           } else if (array[i]+array[j]>sum) {                j--;            } else{                i++;            }                     }        return list;    }

Enter an incrementing sorted array and a number s, look for two numbers in the array, and yes their sum is exactly s, if there are many pairs of numbers and equals s, the output of the two numbers is the smallest.

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.