Maximum difference between number Pairs

Source: Internet
Author: User

Maximum difference between number Pairs
In the array, subtract the number on the right of the number to get a number pair difference, and calculate the maximum value of the difference of all number pairs. For example, in the array {2, 4, 1, 16, 7, 5, 11, 9}, the maximum value of the number pair is 11, which is the result of 16 minus 5. In the above example, to increase the value of the difference between the number pair, the smaller the value of the subtraction, the larger the value to be slowed down (in the subtraction formula, the number before the subtraction is the number to be dropped, the number after the minus sign is a subtraction). According to the definition of the difference between the number pair, we can find the maximum value of the sub-array on the left of the element marked as I in array a and the minimum value of the sub-array on the right, the difference between the two values is the maximum difference between the number pairs of Elements marked as I in array, we can calculate the difference between the maximum and minimum values of the left and right sub-arrays separated by each subscript in the array, and find the maximum value, that is, the maximum value of the number pair difference. The code implementation is as follows: copy code 1 int maxDiff (int * arr, int n) 2 {3 int nMax = arr [0], nMin = 0; 4 int maxDiff = 0; 5 for (int I = 1; I! = N; I ++) 6 {7 // calculate the maximum value of 8 nMax = max (nMax, arr [I]) in the left subarray labeled as I in the array; 9 10 // calculate the minimum value 11 nMin = arr [I]; 12 for (int j = n-2; j> I; j --) 13 nMin = min (nMin, arr [j]); 14 15 if (nMax-nMin> maxDiff) 16 {17 maxDiff = nMax-nMin; 18} 19} 20 21 return maxDiff; 22} copy the code from the implementation. We can see that this method uses nested loops, and the complexity is O (n ^ 2 ), let's look at more efficient algorithms. Now we know that the subtrahend are the maximum values in the left sub-array of I. In this way, we can traverse array a sequentially and find the maximum value of the left sub-array of the element whose current subscript is I, use this maximum value to subtract the current a [I] element, record the difference value of the current number pair, and compare it with the difference value of the previous number pair to determine whether to update the value. The code is implemented as follows: copy the Code 1 int maxDiff (int * arr, int n) 2 {3 int nMax = arr [0]; // The maximum value in the current array is 4 int nMaxDiff = 0; // The difference between the current maximum number pair is 5 int nCurDiff = 0; // the difference between the maximum value and the element whose array subscript is I is 6 for (int I = 1; I! = N; I ++) 7 {8 // update array Max 9 if (nMax <arr [I-1]) 10 nMax = arr [I-1]; 11 12 nCurDiff = nMax-arr [I]; 13 // update the maximum deviation between the number pairs ending with I in the array. 14 if (nCurDiff> nMaxDiff) 15 nMaxDiff = nCurDiff; 16} 17 18 return nMaxDiff; 19}

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.