An array of two numbers and N to find the subscript for these two numbers

Source: Internet
Author: User

Analysis, two-digit and N. So are the two numbers unique? is the output subscript the first pair to appear?

1, we assume that these two numbers are unique.

And is unique, the larger the number, the smaller the other number. Thinking of the size relationship, we thought of the sort. So first sort:

int array[]={1, 2, 7, 9, 13, 57, 36, 26, 55, 11, 9, 6, 3, 89, 36, 75, 36, 76, 95, 98, 101, 320, 520, 85, 36, 62, 49, 96, 1};

Because you want to return the original subscript, you need to record

int Array1[]=array.clone ();//This record is the most perfect, but the query is inconvenient
Map<integer,integer> hs=new hashmap<integer,integer> ();//This method can only find the original subscript but cannot determine the order, the query is convenient

Arrays.sort (array); Average Time complexity O (NLOGN)

Then the smallest plus the largest, if greater than N, proved too big, just try the second largest number. If less than n proves that the small is too small, try the second small number.
The code is as follows:

while ((addans=array[i]+array[j]-n)!=0&&i<j) {
if (addans>0) {
j--;
}else{
i++;
}
}
if (i==j) {
System.out.println ("no results");
Return
}

Such array[i] and array[j] are the two numbers that are asked.

by Hs.get (Array[i]) and Hs.get (Array[j]), the original subscript output of these two numbers can be obtained. But because of the lack of sequencing, these two subscripts may not be the first to appear.
The first set of numbers can be found by iterating through the array.

Full code:

int array[]={1, 2, 7, 9, 13, 57, 36, 26, 55, 11, 9, 6, 3, 89, 36, 75, 36, 76, 95, 98, 101, 320, 520, 85, 36, 62, 49, 96, 1};
int Array1[]=array.clone ();
Map<integer,integer> hs=new hashmap<integer,integer> ();
for (int i = 0; i < Array.Length; i++) {
Hs.put (Array[i], i);
}

Arrays.sort (array);
for (int i = 0; i < Array.Length; i++) {
System.out.print (array[i]+ ",");
}
System.out.println ();

for (int i = 0; i < Array.Length; i++) {
System.out.print (array1[i]+ ",");
}
System.out.println ();


int N = 35,s1=-1,s2=-1,i=0,j=array.length-1;
int Addans;
Comparison, if addans>0 represents time complexity O (n)
while ((addans=array[i]+array[j]-n)!=0&&i<j) {
if (addans>0) {
j--;
}else{
i++;
}
}
if (i==j) {
System.out.println ("no results");
Return
}
Find one set of subscripts
SYSTEM.OUT.PRINTLN ("Any one:" +hs.get (Array[i]) + ":" + array[i] + "+" + hs.get (Array[j]) + ":" + array[j]);
Find the first set of subscript time complexity O (n)
for (int k=0;k<array1.length-1;k++) {
if (array1[k]==array[i]&&s1<0) {
S1=k;
}
if (array1[k]==array[j]&&s2<0) {
S2=k;
}
if (s1>=0&&s2>=0)
{
Break
}
}
System.out.println ("First one:" +s1 + ":" + array[i] + "+" + s2 + ":" + array[j]);

An array of two numbers and N to find the subscript for these two numbers

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.