Find three numbers in N numbers to minimize the absolute value of their sum-

Source: Internet
Author: User

Original question:

Write a function to find any subset of 3 integers from a set of N integers that have a sum with the smallest absolute value,And analyze the time and space complexity of your function.

The input to this function is a pointer or reference to an array of integers, along with the value of N.
When N> = 3, the output is a 3-element array of integers, such that their sum has the smallest absolute value.
When n <3, the output is null.

For example, given the following input:

{-99,-66, 0, 2, 3}, your function shocould return {0, 2, 3 }.

 

Meaning:

Given the unordered array s Containing N numbers (may contain negative numbers, 0, positive numbers ). Calculate the numbers A, B, and C to minimize the absolute values of sum.
Example: S = {-9, 0, 6}, A =-9, B = 3, C = 6, min = 0

 

 

========================================================== ======================================

 

 

Method 1,The brute-force search method, which contains three numbers in triplicate loops and the time complexity is O (n ^ 3). The Code is as follows:

 

 // The time complexity is O (n ^ 3) <br/> // x, y, in Z, three selected numbers are stored in sequence as the subscript in the original array <br/> void brutaldeal (int A [], int N) <br/>{< br/> int x, y, z; <br/> int min = 1000000; <br/> for (INT I = 0; I <N; I ++) <br/>{< br/> for (Int J = I + 1; j <n; j ++) <br/> for (int K = J + 1; k <n; k ++) <br/> If (ABS (A [I] + A [J] + A [k]) <min) <br/>{< br/> X = I; <br/> Y = J; <br/> Z = K; <br/> min = ABS (A [I] + A [J] + A [k]); <br/>}< br/> cout <Endl <"min = A [" <x <"] + A [" <Y <"] + A [" <z <"] =" <A [x] <"+" <A [y] <"+" <<A [Z] <"=" <min <Endl; <br/>}

 

Method 2,After sorting array A, enumerate two of them and search for another number in two parts. O (N ^ 2 * logn)

 

 

Method 3,After sorting array A, enumerate the number of cur (A [I]), and use the two-way pointer head and tail to update from both ends of the array (remove cur ). Update the head or tail based on the positive and negative numbers of a [head] + A [tail] + cur. If it is negative, head ++; otherwise, tail --. Once the sum is 0, exit. At the same time, Min is updated based on the sum. The last min is the request. Use three variables X, Y, and Z to record the subscript of any optimal solution in the sorted array. The worst time complexity of this method is O (n ^ 2 ). First write a fast sorting algorithm, so the code is as follows:

 

// Sort the elements from a [s] to a [T] Quickly <br/> // call format: quicksort (A, 0, n-1). A is an array, n is the array length <br/> void quicksort (int A [], int S, int t) <br/>{< br/> int I = s, j = T; <br/> int TMP; <br/> If (S <t) <br/> {<br/> TMP = A [s]; // TMP temporary Start Element <br/> while (I! = J) <br/>{< br/> while (j> I & A [J]> = TMP) // find the first number smaller than TMP from right to left <br/> j --; <br/> A [I] = A [J]; // move a [J] to the position of a [I] <br/> while (I <J & A [I] <= TMP) // find the first number larger than TMP from left to right <br/> I ++; <br/> A [J] = A [I]; // move a [I] to the position of a [J] <br/>}< br/> A [I] = TMP; // move the starting element TMP to the location of a [I] <br/> quicksort (A, S, I-1); // I is the subscript of the split point, A [I] is a partition element <br/> quicksort (A, I + 1, t ); <br/>}</P> <p> void sortdeal (int A [], int N) <br/>{< br/> // sort the array first <br/> quicksort (A, 0, n-1); <br/> for (INT I = 0; I <n; I ++) <br/> cout <A [I] <""; </P> <p> // start selection <br/> int min = 1000000; <br/> int cur; <br/> int head, tail; <br/> int x, y, z; <br/> for (INT I = 0; I <n; I ++) <br/>{< br/> head = 0; <br/> tail = n-1; </P> <p> cur = A [I]; <br/> while (Head <tail) <br/> {<br/> If (Head = I) <br/> head ++; // remove cur (A [I]) <br/> If (tail = I) <br/> tail --; // remove cur (A [I]) <br/> int now = cur + A [head] + A [tail]; <br/> If (now = 0 | ABS (now) <min) <br/>{< br/> min = ABS (now); <br/> X = I; <br/> Y = head; <br/> Z = tail; <br/> If (now = 0) <br/> break; <br/>}< br/> else if (now <0) <br/> head ++; <br/> else <br/> tail --; <br/>}< br/> If (min = 0) <br/> break; <br/>}< br/> cout <Endl <"min = A [" <x <"] + A [" <Y <"] + A ["<z <"] = "<A [x] <" + "<A [y] <" + "<A [Z] <"=" <min <Endl; <br/>}< br/>

 

 

 

 

 

The main functions of the preceding method are as follows:

 

 

# Include <iostream> <br/> # include <time. h> <br/> using namespace STD; </P> <p> int main () <br/> {<br/> int N; <br/> cout <"input N:"; <br/> CIN> N; <br/> int * ap = new int [N]; <br/> srand (unsigned) Time (null )); </P> <p> // randomly generate elements in the array AP [N] <br/> for (INT I = 0; I <n; I ++) <br/>{< br/> aP [I] = rand (); <br/> If (RAND () % 2) <br/> aP [I] * =-1; <br/> cout <AP [I] <""; <br/>}</P> <p> brutaldeal (AP, n); <br/> sortdeal (AP, n); </P> <p> return 0; <br/>}

 

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.