Algorithm--The maximum difference between two adjacent numbers

Source: Internet
Author: User

Maximum difference between two adjacent numbers Code Implementation
1  PackageCom.hzf.sort;2 3 Importorg.junit.Test;4 5 /**6 * There is a shape array A, please design an algorithm with the complexity of O (n), calculate the maximum difference between two adjacent numbers after sorting. 7  * 8 * Given the size n of an int array A and a, return the maximum difference. The array element is guaranteed to be more than 1. 9  * Ten * Test Sample: [1,2,5,4,6],5 One * return: 2 A  *  -  * @authorHzf -  * the  */ -  Public classGap { -      Public intMaxgap (int[] A,intN) { -         /** + * The first step is to find out the difference between the maximum and minimum values of this group of numbers - * Step Two: Open the right bucket according to the difference (the number of buckets is +1, which is guaranteed to have an empty bucket in the middle of the bucket) + * Step three: Put each number in the corresponding bucket, there will be empty barrels in the case A * Fourth Step: simply loop through the bucket, compare the difference between the smallest value in a bucket and the maximum in the previous bucket, and find the maximum difference at          */ -          -         //The first step is to find out the difference between the maximum and minimum values for this group of numbers -         intMin = a[0]; -         intmax = A[0]; -          for(inti=0; i<a.length; i++){ in             if(A[i] < min) min =A[i]; -             if(A[i] > max) max =A[i]; to         } +          -         //Step Two: Open the right bucket according to the difference (the number of buckets is +1, the number of buckets is greater than the length of the array, there are empty buckets in the middle) the         //The third step: Put each number in the corresponding bucket, there will be empty barrels in the case *         Boolean[] Hasnum =New Boolean[A.length+1];//determine if there is a number in the bucket $         int[] Maxnumbucket =New int[A.length+1];//record the maximum value of the current bucketPanax Notoginseng         int[] Minnumbucket =New int[A.length+1];//record the minimum value of the current bucket -          for(inti=0; i<a.length; i++){ the             intBucketnum = Bucket (A[i], a.length, Min, max);//in which bucket the current number should be placed +Maxnumbucket[bucketnum] = Hasnum[bucketnum]?Math.max (Maxnumbucket[bucketnum], a[i]): A[i]; AMinnumbucket[bucketnum] = Hasnum[bucketnum]?math.min (Minnumbucket[bucketnum], a[i]): A[i]; theHasnum[bucketnum] =true; +         } -          $         //The fourth step: simply loop through the bucket, compare the difference between the smallest value in a bucket and the maximum in the previous bucket, and find the maximum difference $         intresult = 0;//The final result -         intLeftbucketmax = maxnumbucket[0];//No. 0 Barrels must have a value . -         intRightbucketmin = 0; the         intStartbucket = 1;//start the traversal from bucket 1th . -         Wuyi          for(intI=startbucket; i<a.length+1; i++){ the             if(!hasnum[i])Continue; -Rightbucketmin =Minnumbucket[i]; Wu             if(Rightbucketmin-leftbucketmax >result) -result = Rightbucketmin-Leftbucketmax; AboutLeftbucketmax =Maxnumbucket[i]; $         } -          -         returnresult; -     } A      Public intBucketLongNumLongArrlength,LongMinLongMax) {//prevents multiplication of two int numbers, out of bounds +         /** the * 3,7,12,6,4 - * Minimum 3, maximum 12, each bucket range is (12-3)/5 $          *  the * Assuming the current value is 6, it should be placed in (int) ((6-3)/(9/5)) barrels the          */ the         return(int) ((Num-min)/((max-min) *1.0/arrlength));  the     } - @Test in      Public voidTest () { the         int[] arr =New int[]{3,7,19,6,4}; theSystem.out.println (Maxgap (arr,5)); About     } the}
View Code

Test results

Algorithm--The maximum difference between two adjacent 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.