The lowest absolute value of 22 difference in an array of integers

Source: Internet
Author: User

Topic 1:

There is an array of integers that asks for the lowest absolute value of the difference of 22, remembering that the minimum value can be obtained without requiring that the two number be required.

Topic 2: Request the absolute value of the smallest continuous subsequence and, in other words, the absolute minimum of the sum of successive sub-sequences

For question 1:

method "1": the Way of violence. Traverse all two numbers for the difference and record the minimum value. Complexity of the algorithm O (n2)

method "2": two number to the absolute minimum of the difference, it is necessary to need two numbers of similar size. So there is the idea: first sort the array, then iterate over, subtract the adjacent number, record the minimum number of absolute value.

method "3": Transform the current issue:

Set this array of integers to be a1,a2,..., an
Construct Array b= (B1,B2,..., bn-1)
B1 = A1-a2,
B2 = A2-a3,
B3 = A3-a4,
...
Bn-1 = an-1-an

In the original array, the difference between any two integers Ai-aj (1<=i,j<=n) can be expressed as
Continuous summation of the first and second j-1 elements in B

For example b2+b3+b4 = (A2-A3) + (A3-A4) + (A4-A5) = A2-a5

O (n) after constructing the B sequence

Using similar "maximal sub-segments and" algorithms to find the minimum absolute number of sub-segments and "See this problem to know the second problem of the method, to find the sum of the continuous sub-sequence of array b absolute minimum value This is the conversion between two topics

(but this method is problematic, but the idea of transformation is very good)

Method 4: Traverse through the data, find the maximum max and Min min, then divide the entire data, step= (max-min)/n. Then, traversing the n buckets, the maximum value of the adjacent element must be the difference between the maximum value in a bucket I and the minimum value in the bucket (i+1). Specifically how to prove that you can think about it.

(

If the maximum spacing of the
adjacent element is not the difference between the maximum value in a bucket I and the minimum value in the bucket (i+1), that is, the maximum spacing of two elements in the same bucket, that is, the maximum spacing is less than step, so min+n*step<maxd. So contradictory. So the biggest element is definitely in a different bucket.

)

The entire algorithm has a time complexity of O (n), and the spatial complexity is O (n)

The lowest absolute value of 22 difference in an array of integers

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.